Module: Lich::Common::DB_Store

Defined in:
documented/common/db_store.rb

Class Method Summary collapse

Class Method Details

.get_data(scope = "#{XMLData.game}:#{XMLData.name}", script) ⇒ Hash

Retrieves data from the database for a specific script and scope.

Examples:

Get script data

Lich::Common::DB_Store.get_data("game:character", "script_name")

Parameters:

  • scope (String) (defaults to: "#{XMLData.game}:#{XMLData.name}")

    the scope for the data retrieval, defaults to "#XMLData.game:#XMLData.name"

  • script (String)

    the name of the script to retrieve data for

Returns:

  • (Hash)

    the retrieved data, or an empty hash if no data is found



53
54
55
56
57
# File 'documented/common/db_store.rb', line 53

def self.get_data(scope = "#{XMLData.game}:#{XMLData.name}", script)
  hash = Lich.db.get_first_value('SELECT hash FROM script_auto_settings WHERE script=? AND scope=?;', [script.encode('UTF-8'), scope.encode('UTF-8')])
  return {} unless hash
  Marshal.load(hash)
end

.get_vars(scope = "#{XMLData.game}:#{XMLData.name}") ⇒ Hash

Retrieves user variables from the database for a specific scope.

Examples:

Get user variables

Lich::Common::DB_Store.get_vars("game:character")

Parameters:

  • scope (String) (defaults to: "#{XMLData.game}:#{XMLData.name}")

    the scope for the user variables retrieval, defaults to "#XMLData.game:#XMLData.name"

Returns:

  • (Hash)

    the retrieved user variables, or an empty hash if no data is found



64
65
66
67
68
# File 'documented/common/db_store.rb', line 64

def self.get_vars(scope = "#{XMLData.game}:#{XMLData.name}")
  hash = Lich.db.get_first_value('SELECT hash FROM uservars WHERE scope=?;', scope.encode('UTF-8'))
  return {} unless hash
  Marshal.load(hash)
end

.read(scope = "#{XMLData.game}:#{XMLData.name}", script) ⇒ Hash

Reads data from the database based on the provided scope and script name.

Examples:

Read user variables

Lich::Common::DB_Store.read("game:character", "uservars")

Parameters:

  • scope (String) (defaults to: "#{XMLData.game}:#{XMLData.name}")

    the scope for the data retrieval, defaults to "#XMLData.game:#XMLData.name"

  • script (String)

    the name of the script to read data for

Returns:

  • (Hash)

    the retrieved data, or an empty hash if no data is found



22
23
24
25
26
27
28
29
# File 'documented/common/db_store.rb', line 22

def self.read(scope = "#{XMLData.game}:#{XMLData.name}", script)
  case script
  when 'vars', 'uservars'
    get_vars(scope)
  else
    get_data(scope, script)
  end
end

.save(scope = "#{XMLData.game}:#{XMLData.name}", script, val) ⇒ String

Saves data to the database based on the provided scope, script name, and value.

Examples:

Save user variables

Lich::Common::DB_Store.save("game:character", "uservars", {name: "new_name"})

Parameters:

  • scope (String) (defaults to: "#{XMLData.game}:#{XMLData.name}")

    the scope for the data storage, defaults to "#XMLData.game:#XMLData.name"

  • script (String)

    the name of the script to save data for

  • val (Hash)

    the data to be saved

Returns:

  • (String)

    a message indicating success or error



38
39
40
41
42
43
44
45
# File 'documented/common/db_store.rb', line 38

def self.save(scope = "#{XMLData.game}:#{XMLData.name}", script, val)
  case script
  when 'vars', 'uservars'
    store_vars(scope, val)
  else
    store_data(scope, script, val)
  end
end

.store_data(scope = "#{XMLData.game}:#{XMLData.name}", script, val) ⇒ String

Stores data in the database for a specific script and scope.

Examples:

Store script data

Lich::Common::DB_Store.store_data("game:character", "script_name", {key: "value"})

Parameters:

  • scope (String) (defaults to: "#{XMLData.game}:#{XMLData.name}")

    the scope for the data storage, defaults to "#XMLData.game:#XMLData.name"

  • script (String)

    the name of the script to save data for

  • val (Hash)

    the data to be stored

Returns:

  • (String)

    a message indicating success or error



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'documented/common/db_store.rb', line 77

def self.store_data(scope = "#{XMLData.game}:#{XMLData.name}", script, val)
  blob = SQLite3::Blob.new(Marshal.dump(val))
  return 'Error: No data to store.' unless blob

  Lich.db_mutex.synchronize do
    begin
      Lich.db.execute('INSERT OR REPLACE INTO script_auto_settings(script,scope,hash) VALUES(?,?,?);', [script.encode('UTF-8'), scope.encode('UTF-8'), blob])
    rescue SQLite3::BusyException
      sleep 0.05
      retry
    rescue StandardError
      respond "--- Lich: error: #{$ERROR_INFO}"
      respond $ERROR_INFO.backtrace[0..1]
    end
  end
end

.store_vars(scope = "#{XMLData.game}:#{XMLData.name}", val) ⇒ String

Stores user variables in the database for a specific scope.

Examples:

Store user variables

Lich::Common::DB_Store.store_vars("game:character", {name: "new_name"})

Parameters:

  • scope (String) (defaults to: "#{XMLData.game}:#{XMLData.name}")

    the scope for the user variables storage, defaults to "#XMLData.game:#XMLData.name"

  • val (Hash)

    the user variables to be stored

Returns:

  • (String)

    a message indicating success or error



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'documented/common/db_store.rb', line 100

def self.store_vars(scope = "#{XMLData.game}:#{XMLData.name}", val)
  blob = SQLite3::Blob.new(Marshal.dump(val))
  return 'Error: No data to store.' unless blob

  Lich.db_mutex.synchronize do
    begin
      Lich.db.execute('INSERT OR REPLACE INTO uservars(scope,hash) VALUES(?,?);', [scope.encode('UTF-8'), blob])
    rescue SQLite3::BusyException
      sleep 0.05
      retry
    rescue StandardError
      respond "--- Lich: error: #{$ERROR_INFO}"
      respond $ERROR_INFO.backtrace[0..1]
    end
  end
end