Module: Lich::Common::DB_Store

Defined in:
lib/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:character', 'settings')

Parameters:

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

    the scope to read from, defaults to “#XMLData.game:#XMLData.name”

  • script (String)

    the script name to read data for

Returns:

  • (Hash)

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



55
56
57
58
59
# File 'lib/common/db_store.rb', line 55

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:character')

Parameters:

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

    the scope to read from, defaults to “#XMLData.game:#XMLData.name”

Returns:

  • (Hash)

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



67
68
69
70
71
# File 'lib/common/db_store.rb', line 67

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.

Examples:

data = Lich::Common::DB_Store.read('game:character', 'vars')

Parameters:

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

    the scope to read from, defaults to “#XMLData.game:#XMLData.name”

  • script (String)

    the script name to read data for, can be ‘vars’, ‘uservars’, or other scripts

Returns:

  • (Hash)

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



20
21
22
23
24
25
26
27
# File 'lib/common/db_store.rb', line 20

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 and script.

Examples:

result = Lich::Common::DB_Store.save('game:character', 'vars', {name: 'Hero'})

Parameters:

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

    the scope to save to, defaults to “#XMLData.game:#XMLData.name”

  • script (String)

    the script name to save data for, can be ‘vars’, ‘uservars’, or other scripts

  • val (Object)

    the value to be stored in the database

Returns:

  • (String)

    a message indicating success or error

Raises:

  • (SQLite3::BusyException)

    if the database is busy

  • (StandardError)

    for any other errors during the database operation



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

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:

result = Lich::Common::DB_Store.store_data('game:character', 'settings', {level: 10})

Parameters:

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

    the scope to save to, defaults to “#XMLData.game:#XMLData.name”

  • script (String)

    the script name to save data for

  • val (Object)

    the value to be stored in the database

Returns:

  • (String)

    a message indicating success or error

Raises:

  • (SQLite3::BusyException)

    if the database is busy

  • (StandardError)

    for any other errors during the database operation



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

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:

result = Lich::Common::DB_Store.store_vars('game:character', {nickname: 'Hero'})

Parameters:

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

    the scope to save to, 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:

  • (SQLite3::BusyException)

    if the database is busy

  • (StandardError)

    for any other errors during the database operation



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/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