Trying to shoe-horn in sqlite. This needs a lot of work. SQLiteConnect needs to initialize itself... reset tables on command and build new ones. Then start hacking at EntityFactory to get active records back.

This commit is contained in:
Alex Stevenson 2025-07-16 23:13:07 -04:00
parent 8fa1664bcb
commit 5ea17719be
5 changed files with 104 additions and 35 deletions

View file

@ -1,31 +1,23 @@
load "mongo_connect.rb" load "sqlite_connect.rb"
load "mongo_document_wrapper.rb" load "active_sqlite_record.rb"
load "entity_factory.rb" load "entity_factory.rb"
load "rpg_grid.rb" load "rpg_grid.rb"
load "rpg_bets.rb"
class ActiveRpg class ActiveRpg
@grid = nil @grid = nil
@mc = nil @sc = nil
@ef = nil @ef = nil
def initialize(grid_size = 10, db="testdb", coll="entities") def initialize(grid_size = 10, db="testdb")
@mc = MongoConnect.new(db, coll) @sc = SQLiteConnect.new(db)
@ef = EntityFactory.new(@mc) @ef = EntityFactory.new(@sc)
@grid = RpgGrid.new(@mc, grid_size) @grid = RpgGrid.new(@sc, grid_size)
@bets = RpgBets.new(@mc)
end end
# this should only really ever be needed in a simulation run to clear out the DB and reset the indexes # this should only really ever be needed in a simulation run to clear out the DB and reset the indexes
def get_mc() def get_sc()
return @mc return @sc
end
# this is just a placeholder hopefully. need to flesh out how bets are gonna work but gonna
# delegate it to the irc bot for now...
def get_bets()
return @bets
end end
# Create and/or load player. Spawn another entity. Move the player. # Create and/or load player. Spawn another entity. Move the player.
@ -56,7 +48,9 @@ class ActiveRpg
end end
def find_player(player_name) def find_player(player_name)
found_player = @mc.collection.find({"type" => :player, "name" => player_name}).first found_player = @sc.execute("
SELECT * FROM players WHERE player_name = #{player_name}
").first
if found_player == nil then if found_player == nil then
p = @ef.get(:player) p = @ef.get(:player)

77
active_sqlite_record.rb Normal file
View file

@ -0,0 +1,77 @@
class ActiveSqliteRecord
@mc = nil # MongoConnect instance
@doc = nil # MongoDocument instance....
@id = nil # BSON ID for the Doc
def initialize(mc, doc=nil)
@mc = mc
if doc == nil then
create()
else
@id = doc['_id']
@doc = doc
end
end
def create()
@id = BSON::ObjectId.new()
doc = get_default_doc()
doc['_id'] = @id
@doc = doc
@mc.collection.insert_one(@doc)
end
def load()
@doc = @mc.collection().find({"_id" => @id}).first()
end
# save and grab the ID and doc....
def set(params={}, persist=true)
@doc.merge!(params)
if persist == true then
persist()
end
end
def get(field)
return @doc[field]
end
def persist()
@mc.collection().update_one({"_id" => @id}, @doc)
end
def delete()
if @id == nil then
die
end
@mc.collection().delete_one({"_id" => @id})
end
def update(query)
@mc.collection().update_one({"_id" => @id}, query)
end
def get_default_doc()
puts "Override this in ent class"
return {}
end
def self.build_from(mc, doc=nil)
if doc == nil then
puts "wtf whoops??"
else
@mc = mc
@doc = doc
@id = @doc['_id']
end
return self
end
end

0
dbname Normal file
View file

14
sqlite_connect.rb Normal file
View file

@ -0,0 +1,14 @@
#require 'rubygems' # not necessary for Ruby 1.9
require 'sqlite3'
class SQLiteConnect
@db = nil
def initialize(dbname)
@db = SQLite3::Database.open('dbname')
end
def execute(query)
return @db.execute(query)
end
end

View file

@ -16,8 +16,7 @@ $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()
$ar.get_mc().collection.drop() puts $ar.inspect
$ar.get_mc().collection().indexes.create_one(_id: 1)
end end
# generates a line of given length. # generates a line of given length.
@ -25,18 +24,6 @@ def get_line(len)
line = Array.new(1+rand(len), ".").join line = Array.new(1+rand(len), ".").join
end end
# I want to find a way to make it a bit more dynamic in
# adding bets here.... More variations
def place_bet(player, target)
chance = rand(1000)
if chance <= 2 then
amount = 1+rand(10)
puts $bets.place_bet(player, target, amount)
end
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 " + collection_name + ", " + grid_size.to_s + " grid."
@ -45,9 +32,6 @@ puts player_num.to_s + " players, " + turns.to_s + " turns, " + max_damage.to_s
puts "Creating ActiveRpg" puts "Creating ActiveRpg"
$ar = ActiveRpg.new(grid_size, collection_name) $ar = ActiveRpg.new(grid_size, collection_name)
puts "Grabbing bets..."
$bets = $ar.get_bets()
puts "Dropping and building index on the collection" puts "Dropping and building index on the collection"
reset_all() reset_all()