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:
parent
3579f47eda
commit
d81c397629
7 changed files with 35 additions and 25 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
BIN
testground.db
Normal file
BIN
testground.db
Normal file
Binary file not shown.
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue