159 lines
3.3 KiB
Ruby
159 lines
3.3 KiB
Ruby
load "sqlite_connect.rb"
|
|
load "active_sqlite_record.rb"
|
|
load "entity_factory.rb"
|
|
|
|
load "rpg_grid.rb"
|
|
|
|
class ActiveRpg
|
|
@grid = nil
|
|
@sc = nil
|
|
@ef = nil
|
|
|
|
def initialize(grid_size = 10, db="defaultdb")
|
|
@sc = SQLiteConnect.new(db)
|
|
@ef = EntityFactory.new(@sc)
|
|
@grid = RpgGrid.new(@sc, grid_size)
|
|
end
|
|
|
|
def get_sql_connect()
|
|
return @sc
|
|
end
|
|
|
|
# Create and/or load player. Spawn another entity. Move the player.
|
|
def take_turn(player_name, line)
|
|
p = find_player(player_name)
|
|
|
|
new_loc = @grid.get_move_location(p.get("location"))
|
|
|
|
# Update the player doc with new location/exp/line
|
|
p.set({
|
|
'exp' => p.get('exp').to_i + 1,
|
|
'location' => new_loc,
|
|
'last_line' => line,
|
|
})
|
|
|
|
# Spawn a new Monster
|
|
spawn_monster(player_name, line)
|
|
|
|
# Spawn a potential event
|
|
spawn_event()
|
|
|
|
# Resolve interaction from player movement and grab a string and return it
|
|
res = resolve_conflict(p, new_loc)
|
|
|
|
update_user_boss(player_name, line)
|
|
|
|
return res
|
|
end
|
|
|
|
def find_player(player_name)
|
|
found_player = @sc.query("
|
|
SELECT * FROM players WHERE player_name = #{player_name}
|
|
").first
|
|
|
|
if found_player == nil then
|
|
p = @ef.get(:player)
|
|
|
|
p.set({
|
|
"name" => player_name,
|
|
"exp" => 1,
|
|
"location" => @grid.find_location()
|
|
})
|
|
|
|
return p
|
|
else
|
|
return @ef.build_instance(found_player)
|
|
end
|
|
end
|
|
|
|
def update_user_boss(player_name, line)
|
|
found_boss = @mc.collection.find({"type" => :boss, "spawned_by" => player_name}).first
|
|
|
|
if found_boss != nil then
|
|
#level them up by line length...
|
|
|
|
b = @ef.build_instance(found_boss)
|
|
b.set({"exp" => b.get("exp") + line.length})
|
|
else
|
|
if rand(1000) > 900 then
|
|
spawn_boss(player_name, line)
|
|
end
|
|
end
|
|
end
|
|
|
|
def spawn_boss(player_name, line)
|
|
b = @ef.get(:boss)
|
|
b.set({
|
|
"spawned_by" => player_name,
|
|
"location" => @grid.find_location(),
|
|
"exp" => line.length,
|
|
"last_line" => line,
|
|
})
|
|
end
|
|
|
|
def spawn_monster(player_name, line)
|
|
m = @ef.get(:monster)
|
|
m.set({
|
|
"spawned_by" => player_name,
|
|
"location" => @grid.find_location(),
|
|
"line" => line,
|
|
"exp" => line.length,
|
|
})
|
|
end
|
|
|
|
def spawn_event()
|
|
if rand(1000) <= 10 then
|
|
e = @ef.get(:random_event)
|
|
e.set({
|
|
"location" => @grid.find_location(),
|
|
})
|
|
end
|
|
end
|
|
|
|
def resolve_conflict(player, loc)
|
|
ents = find_at_location(loc)
|
|
|
|
if ents.count > 1 then
|
|
battle_ents = []
|
|
|
|
ents.each do |e|
|
|
if e.get("name") != player.get("name") then
|
|
battle_ents.push e
|
|
end
|
|
end
|
|
|
|
results = []
|
|
|
|
# A bit clunky but basically resolve any conflicts with other objects
|
|
# and nuke any of them that aren't other players
|
|
battle_ents.each do |b|
|
|
res = player.resolve(b)
|
|
|
|
if res != nil or res != "" then
|
|
results.push(res)
|
|
end
|
|
|
|
if b.get("type") != :player then
|
|
b.delete()
|
|
else
|
|
new_loc = @grid.find_location()
|
|
b.set({"location" => new_loc})
|
|
end
|
|
end
|
|
|
|
return results.join("; ")
|
|
end
|
|
end
|
|
|
|
def find_at_location(loc)
|
|
ents = @mc.collection.find({"location" => loc})
|
|
|
|
wrapper_docs = []
|
|
|
|
ents.each do |e|
|
|
wrapper_docs.push(@ef.build_instance(e))
|
|
end
|
|
|
|
return wrapper_docs
|
|
end
|
|
end
|