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
76
entities/player.rb
Normal file
76
entities/player.rb
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
class Player < Entity
|
||||
def initialize(mc, doc=nil)
|
||||
super(mc, doc)
|
||||
end
|
||||
|
||||
def get_default_doc()
|
||||
q = {
|
||||
'type' => :player,
|
||||
'name' => nil,
|
||||
'location' => nil,
|
||||
'exp' => 0,
|
||||
'last_line' => nil
|
||||
}
|
||||
|
||||
return q
|
||||
end
|
||||
|
||||
def gain_exp(amount)
|
||||
self.set_exp(self.get('exp') + amount)
|
||||
end
|
||||
|
||||
# be careful with this one. very rare use case when you want
|
||||
# to use this directly from a bot or playground
|
||||
def set_exp(amount)
|
||||
self.set({'exp' => amount})
|
||||
end
|
||||
|
||||
# yeah this needs a WHOLE LOT of cleanup and rethinking....
|
||||
def resolve(foe)
|
||||
result = nil
|
||||
|
||||
# call the resolve method for anything that's not a player.
|
||||
if foe.get('type') != :player then
|
||||
result = foe.resolve(self)
|
||||
|
||||
# Handle player v player here.... can't exactly recall resolve without
|
||||
# recursing forever
|
||||
# most of this should probably be somehow abstracted into Entity...
|
||||
# I'm ok with this for now.
|
||||
else
|
||||
max_player_attack = self.get('exp')
|
||||
max_foe_attack = foe.get('exp')
|
||||
|
||||
player_str = self.get('name')
|
||||
player_str = player_str + "[" + max_player_attack.to_s + "str] "
|
||||
|
||||
result = player_str + "felt the need to fight "
|
||||
|
||||
result = result + foe.get('name')
|
||||
result = result + "[" + max_foe_attack.to_s + "str]" + "! "
|
||||
|
||||
player_attack = 1+rand(max_player_attack)
|
||||
foe_attack = 1+rand(foe.get('exp'))
|
||||
|
||||
result = result + "[" + player_attack.to_s + "dmg vs " + foe_attack.to_s + "dmg]! "
|
||||
|
||||
if player_attack > foe_attack then
|
||||
# Max of 25% of foe exp, minimum of 1. Cannot pass a 0 into rand without
|
||||
# getting a float, so this is a bit cludgy. if rand gets a 1 it always returns
|
||||
# 0 so the floor is effectively 1 :/
|
||||
max_xp_gain = (foe.get('exp').to_f * 0.25).to_i + 1
|
||||
xp = 1 + rand(max_xp_gain)
|
||||
|
||||
result = result + foe.get('name') + " lost! "
|
||||
result = result + self.get('name') + " wins and earns " + xp.to_s + "xp!"
|
||||
gain_exp(xp)
|
||||
elsif player_attack < foe_attack then
|
||||
result = result + foe.get("name") + " wins and saunters away"
|
||||
else
|
||||
result = result + "They tied! They shake hands and agree to disagree and part ways."
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue