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:

data = Lich::Common::DB_Store.get_data("game:name", "script_name")

Parameters:

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

    The scope for the data, defaults to “#XMLData.game:#XMLData.name”

  • script (String)

    The name of the script to retrieve data for

Returns:

  • (Hash)

    The data retrieved from the database, or an empty hash if not found

Raises:

  • (StandardError)

    If there is an error during the database operation



57
58
59
60
61
# File 'documented/common/db_store.rb', line 57

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:

user_vars = Lich::Common::DB_Store.get_vars("game:name")

Parameters:

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

    The scope for the user variables, defaults to “#XMLData.game:#XMLData.name”

Returns:

  • (Hash)

    The user variables retrieved from the database, or an empty hash if not found

Raises:

  • (StandardError)

    If there is an error during the database operation



69
70
71
72
73
# File 'documented/common/db_store.rb', line 69

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:

data = Lich::Common::DB_Store.read("game:name", "vars")

Parameters:

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

    The scope for the data, defaults to “#XMLData.game:#XMLData.name”

  • script (String)

    The name of the script to read data for

Returns:

  • (Hash)

    The data retrieved from the database

Raises:

  • (StandardError)

    If there is an error during the database operation



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

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:

result = Lich::Common::DB_Store.save("game:name", "vars", { key: "value" })

Parameters:

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

    The scope for the data, defaults to “#XMLData.game:#XMLData.name”

  • script (String)

    The name of the script to save data for

  • val (Object)

    The value to be saved in the database

Returns:

  • (String)

    A message indicating success or error

Raises:

  • (StandardError)

    If there is an error during the database operation



41
42
43
44
45
46
47
48
# File 'documented/common/db_store.rb', line 41

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

Note:

This method is synchronized to prevent concurrent access issues.

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

Examples:

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

Parameters:

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

    The scope for the data, defaults to “#XMLData.game:#XMLData.name”

  • script (String)

    The name of the script to save data for

  • val (Object)

    The value to be stored in the database

Returns:

  • (String)

    A message indicating success or error

Raises:

  • (StandardError)

    If there is an error during the database operation



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'documented/common/db_store.rb', line 84

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

Note:

This method is synchronized to prevent concurrent access issues.

Stores user variables in the database for a specific scope.

Examples:

result = Lich::Common::DB_Store.store_vars("game:name", { user_key: "user_value" })

Parameters:

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

    The scope for the user variables, defaults to “#XMLData.game:#XMLData.name”

  • val (Object)

    The user variables to be stored in the database

Returns:

  • (String)

    A message indicating success or error

Raises:

  • (StandardError)

    If there is an error during the database operation



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'documented/common/db_store.rb', line 109

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