Ok this is the actual inital code... need to convert from mongo to sqlite...
This commit is contained in:
parent
8c856ce465
commit
8fa1664bcb
13 changed files with 1168 additions and 0 deletions
166
active_rpg.rb
Normal file
166
active_rpg.rb
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
load "mongo_connect.rb"
|
||||
load "mongo_document_wrapper.rb"
|
||||
load "entity_factory.rb"
|
||||
|
||||
load "rpg_grid.rb"
|
||||
load "rpg_bets.rb"
|
||||
|
||||
class ActiveRpg
|
||||
@grid = nil
|
||||
@mc = 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)
|
||||
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
|
||||
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 = @mc.collection.find({"type" => :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
|
||||
Loading…
Add table
Add a link
Reference in a new issue