From 5ea17719bec181eb9451c7722e096262a2272d92 Mon Sep 17 00:00:00 2001 From: Alex Stevenson Date: Wed, 16 Jul 2025 23:13:07 -0400 Subject: [PATCH] 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. --- active_rpg.rb | 30 +++++++--------- active_sqlite_record.rb | 77 +++++++++++++++++++++++++++++++++++++++++ dbname | 0 sqlite_connect.rb | 14 ++++++++ testground.rb | 18 +--------- 5 files changed, 104 insertions(+), 35 deletions(-) create mode 100644 active_sqlite_record.rb create mode 100644 dbname create mode 100644 sqlite_connect.rb diff --git a/active_rpg.rb b/active_rpg.rb index 985cc51..85fed38 100644 --- a/active_rpg.rb +++ b/active_rpg.rb @@ -1,31 +1,23 @@ -load "mongo_connect.rb" -load "mongo_document_wrapper.rb" +load "sqlite_connect.rb" +load "active_sqlite_record.rb" load "entity_factory.rb" load "rpg_grid.rb" -load "rpg_bets.rb" class ActiveRpg @grid = nil - @mc = nil + @sc = nil @ef = nil - def initialize(grid_size = 10, db="testdb", coll="entities") - @mc = MongoConnect.new(db, coll) - @ef = EntityFactory.new(@mc) - @grid = RpgGrid.new(@mc, grid_size) - @bets = RpgBets.new(@mc) + def initialize(grid_size = 10, db="testdb") + @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_mc() - return @mc - 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 + def get_sc() + return @sc end # Create and/or load player. Spawn another entity. Move the player. @@ -56,7 +48,9 @@ class ActiveRpg end 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 p = @ef.get(:player) diff --git a/active_sqlite_record.rb b/active_sqlite_record.rb new file mode 100644 index 0000000..50dbee3 --- /dev/null +++ b/active_sqlite_record.rb @@ -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 diff --git a/dbname b/dbname new file mode 100644 index 0000000..e69de29 diff --git a/sqlite_connect.rb b/sqlite_connect.rb new file mode 100644 index 0000000..6637f2b --- /dev/null +++ b/sqlite_connect.rb @@ -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 diff --git a/testground.rb b/testground.rb index e06648c..e633eee 100644 --- a/testground.rb +++ b/testground.rb @@ -16,8 +16,7 @@ $bets = nil # call this if you want to drop all of the DB. def reset_all() - $ar.get_mc().collection.drop() - $ar.get_mc().collection().indexes.create_one(_id: 1) + puts $ar.inspect end # generates a line of given length. @@ -25,18 +24,6 @@ def get_line(len) line = Array.new(1+rand(len), ".").join 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 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" $ar = ActiveRpg.new(grid_size, collection_name) -puts "Grabbing bets..." -$bets = $ar.get_bets() - puts "Dropping and building index on the collection" reset_all()