Hopefully getting the database to init and reset itself (in testground.rb, need to make that more of a active_rpg.rb thing...

This commit is contained in:
Alex Stevenson 2025-07-17 00:44:04 -04:00
parent 3579f47eda
commit d81c397629
7 changed files with 35 additions and 25 deletions

View file

@ -9,14 +9,13 @@ class ActiveRpg
@sc = nil @sc = nil
@ef = nil @ef = nil
def initialize(grid_size = 10, db="testdb") def initialize(grid_size = 10, db="defaultdb")
@sc = SQLiteConnect.new(db) @sc = SQLiteConnect.new(db)
@ef = EntityFactory.new(@sc) @ef = EntityFactory.new(@sc)
@grid = RpgGrid.new(@sc, grid_size) @grid = RpgGrid.new(@sc, grid_size)
end end
# this should only really ever be needed in a simulation run to clear out the DB and reset the indexes def get_sql_connect()
def get_sc()
return @sc return @sc
end end
@ -48,7 +47,7 @@ class ActiveRpg
end end
def find_player(player_name) def find_player(player_name)
found_player = @sc.execute(" found_player = @sc.query("
SELECT * FROM players WHERE player_name = #{player_name} SELECT * FROM players WHERE player_name = #{player_name}
").first ").first

View file

@ -1,16 +1,16 @@
class ActiveSqliteRecord class ActiveSqliteRecord
@mc = nil # MongoConnect instance @sqlite_connect = nil # SQLiteConnect instance
@doc = nil # MongoDocument instance.... @row = nil # SQLite row...
@id = nil # BSON ID for the Doc @id = nil # Row ID for the record
def initialize(mc, doc=nil) def initialize(sc, table, row=nil)
@mc = mc @sqlite_connect = sc
if doc == nil then if row == nil then
create() create(table)
else else
@id = doc['_id'] @id = row['_id']
@doc = doc @row = row
end end
end end

View file

@ -1,8 +1,8 @@
load "mongo_document_wrapper.rb" load "active_sqlite_record.rb"
class Entity < MongoDocumentWrapper class Entity < ActiveSqliteRecord
def initialize(mc, doc=nil) def initialize(sc, row={})
super(mc, doc) super(sc, row)
end end
def resolve(target) def resolve(target)

View file

@ -5,10 +5,15 @@ class SQLiteConnect
@db = nil @db = nil
def initialize(dbname) def initialize(dbname)
@db = SQLite3::Database.open('dbname') @db = SQLite3::Database.new(dbname + ".db")
puts @db
end end
def execute(query) def query(query)
return @db.execute(query) puts "Running query " + query + "..."
r = @db.execute(query)
puts "response...."
puts r.inspect
return r
end end
end end

View file

BIN
testground.db Normal file

Binary file not shown.

View file

@ -5,18 +5,24 @@
load "active_rpg.rb" load "active_rpg.rb"
#configgy bits #configgy bits
collection_name = "testground" db_name = "testground"
grid_size = 25 grid_size = 25
player_num = 20 player_num = 20
turns = 1000 turns = 1000
max_damage = 100 max_damage = 100
$ar = nil # ActigeRpg instance $ar = nil # ActigeRpg instance
$bets = nil
# call this if you want to drop all of the DB. # call this if you want to drop all of the DB.
def reset_all() def reset_all()
puts $ar.inspect puts "Trying to null out the players/entities tables..."
sql = $ar.get_sql_connect
puts sql.inspect
sql.query("DROP TABLE IF EXISTS players;")
sql.query("DROP TABLE IF EXISTS entities;")
sql.query("CREATE TABLE players(id INT PRIMARY KEY, name TEXT NOT NULL UNIQUE);")
sql.query("CREATE TABLE entities(id INT PRIMARY KEY);")
end end
# generates a line of given length. # generates a line of given length.
@ -26,11 +32,11 @@ end
# start of simulator code # start of simulator code
puts "Starting run in collection " + collection_name + ", " + grid_size.to_s + " grid." puts "Starting run in collection " + db_name + ", " + grid_size.to_s + " grid."
puts player_num.to_s + " players, " + turns.to_s + " turns, " + max_damage.to_s + " max damage." puts player_num.to_s + " players, " + turns.to_s + " turns, " + max_damage.to_s + " max damage."
puts "Creating ActiveRpg" puts "Creating ActiveRpg"
$ar = ActiveRpg.new(grid_size, collection_name) $ar = ActiveRpg.new(grid_size, db_name)
puts "Dropping and building index on the collection" puts "Dropping and building index on the collection"
reset_all() reset_all()