From d81c397629addaf84d68386454d49d046cd211eb Mon Sep 17 00:00:00 2001 From: Alex Stevenson Date: Thu, 17 Jul 2025 00:44:04 -0400 Subject: [PATCH] Hopefully getting the database to init and reset itself (in testground.rb, need to make that more of a active_rpg.rb thing... --- active_rpg.rb | 7 +++---- active_sqlite_record.rb | 18 +++++++++--------- entities/entity.rb | 8 ++++---- sqlite_connect.rb | 11 ++++++++--- dbname => testground | 0 testground.db | Bin 0 -> 24576 bytes testground.rb | 16 +++++++++++----- 7 files changed, 35 insertions(+), 25 deletions(-) rename dbname => testground (100%) create mode 100644 testground.db diff --git a/active_rpg.rb b/active_rpg.rb index 85fed38..4c86b37 100644 --- a/active_rpg.rb +++ b/active_rpg.rb @@ -9,14 +9,13 @@ class ActiveRpg @sc = nil @ef = nil - def initialize(grid_size = 10, db="testdb") + def initialize(grid_size = 10, db="defaultdb") @sc = SQLiteConnect.new(db) @ef = EntityFactory.new(@sc) @grid = RpgGrid.new(@sc, grid_size) end - # this should only really ever be needed in a simulation run to clear out the DB and reset the indexes - def get_sc() + def get_sql_connect() return @sc end @@ -48,7 +47,7 @@ class ActiveRpg end def find_player(player_name) - found_player = @sc.execute(" + found_player = @sc.query(" SELECT * FROM players WHERE player_name = #{player_name} ").first diff --git a/active_sqlite_record.rb b/active_sqlite_record.rb index 50dbee3..eff131f 100644 --- a/active_sqlite_record.rb +++ b/active_sqlite_record.rb @@ -1,16 +1,16 @@ class ActiveSqliteRecord - @mc = nil # MongoConnect instance - @doc = nil # MongoDocument instance.... - @id = nil # BSON ID for the Doc + @sqlite_connect = nil # SQLiteConnect instance + @row = nil # SQLite row... + @id = nil # Row ID for the record - def initialize(mc, doc=nil) - @mc = mc + def initialize(sc, table, row=nil) + @sqlite_connect = sc - if doc == nil then - create() + if row == nil then + create(table) else - @id = doc['_id'] - @doc = doc + @id = row['_id'] + @row = row end end diff --git a/entities/entity.rb b/entities/entity.rb index b80cfed..5f62a61 100644 --- a/entities/entity.rb +++ b/entities/entity.rb @@ -1,8 +1,8 @@ -load "mongo_document_wrapper.rb" +load "active_sqlite_record.rb" -class Entity < MongoDocumentWrapper - def initialize(mc, doc=nil) - super(mc, doc) +class Entity < ActiveSqliteRecord + def initialize(sc, row={}) + super(sc, row) end def resolve(target) diff --git a/sqlite_connect.rb b/sqlite_connect.rb index 6637f2b..c1b5425 100644 --- a/sqlite_connect.rb +++ b/sqlite_connect.rb @@ -5,10 +5,15 @@ class SQLiteConnect @db = nil def initialize(dbname) - @db = SQLite3::Database.open('dbname') + @db = SQLite3::Database.new(dbname + ".db") + puts @db end - def execute(query) - return @db.execute(query) + def query(query) + puts "Running query " + query + "..." + r = @db.execute(query) + puts "response...." + puts r.inspect + return r end end diff --git a/dbname b/testground similarity index 100% rename from dbname rename to testground diff --git a/testground.db b/testground.db new file mode 100644 index 0000000000000000000000000000000000000000..267d32506fcdeaee05ba92fdfeb239e4a42f7319 GIT binary patch literal 24576 zcmeI(O-sWt7{Kw=ZS@7V4m|WQ201!#%Jk;RI&zrS>1K!SVyUb`VK2H>(4$|&kF%TK z$fIp(LBWFO@((0=64IvmC6^p}Gq8iumye6(%nRkQs0&RKr&0*w`T8)c(T#yhz009ILKmY** z5I_I{1Q0-=r~*6j!npqzb$eMC0tg_000IagfB*srAb~ literal 0 HcmV?d00001 diff --git a/testground.rb b/testground.rb index e633eee..0289589 100644 --- a/testground.rb +++ b/testground.rb @@ -5,18 +5,24 @@ load "active_rpg.rb" #configgy bits -collection_name = "testground" +db_name = "testground" grid_size = 25 player_num = 20 turns = 1000 max_damage = 100 $ar = nil # ActigeRpg instance -$bets = nil # call this if you want to drop all of the DB. 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 # generates a line of given length. @@ -26,11 +32,11 @@ end # 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 "Creating ActiveRpg" -$ar = ActiveRpg.new(grid_size, collection_name) +$ar = ActiveRpg.new(grid_size, db_name) puts "Dropping and building index on the collection" reset_all()