activerpg/rpg_bets.rb

167 lines
5.2 KiB
Ruby

# I am not happy with most of this.
class RpgBets < MongoDocumentWrapper
@bets = []
def initialize(mc)
doc = mc.collection().find({"type" => :bets}).first
if doc == nil then
@mc = mc
self.create()
else
super(mc, doc)
end
@bets = self.get("bets")
end
# This needs so much cleanup
# Call with 2 player names and an amount. Returns a string with the interaction output
def place_bet(player, target, amount)
# NO SELF BETS!
if player == target then
return "No self bets " + player + "!"
end
# so this happened in the sim...
if amount == 0 then
return "Who bets 0?"
end
possible_player = @mc.collection().find({"type" => :player, "name" => player}).first
# This should never really happen, but just in case...
if possible_player == nil then
return "Who the heck are YOU " + player + "??"
end
# Check to see if the target even exists...
possible_target = @mc.collection().find({"type" => :player, "name" => target}).first
if possible_target == nil then
return "Who the heck is " + target + "?"
end
# if we get here we know we have both a valid player and target
p = Player.new(@mc, possible_player)
t = Player.new(@mc, possible_target)
# Make sure betting/calling player has enough xp to place the bet...
if p.get("exp") < amount then
return "Sorry " + p.get("name") + " but you only have " + p.get("exp").to_s + "xp to bet with!"
end
bet = get_bet(p.get('name'), t.get('name'), amount)
response = ""
# Check the exp on the bet and see if the new amount is greater.
# If greater, if it's from the same player who already owns the most
# recent bet, do nothing. Otherwise, replace the old exp with the new one
# and replace the last bet player with the current.
if amount > bet['exp'] then
if bet['last_bet'] == p.get('name') then
response = "You already have the highest bet of " + bet['exp'].to_s + "xp!"
else
bet['exp'] = amount
bet['last_bet'] = player
response = player + " upped the ante to " + amount.to_s + "xp against " + target + "!"
end
# If amount is less than the current bet, don't return immediately but spit out
# a message about how it's not enough
elsif amount < bet['exp'] then
response = player + " be real, yo. Current bet with " + target + " is at " + t.get('exp').to_s + "xp. "
response = response + "Match to fight, or overbid them."
# fight? This should prolly be refactored into some function.... it gets busy...
else
if bet['last_bet'] == player then
response = "You have the highest bet already against " + target + " for "
response = response + bet['exp'].to_s + "xp! They need to call you on it."
else
# this whole block needs cleaning. It's gross atm.
p_attack = 1 + rand(amount)
t_attack = 1 + rand(amount)
response = player + " and " + target + " fight over " + amount.to_s + "xp! "
response = response + "[" + p_attack.to_s + "dmg vs " + t_attack.to_s + "dmg]! "
winner = loser = nil
if p_attack > t_attack then
winner = p
loser = t
elsif t_attack > p_attack then
winner = t
loser = p
# If it's a tie, persist the bet back down with no changes and return
else
@bets.push(bet)
self.set({"bets" => @bets})
response = "They tied! The crowd goes wild in anticipation! The bet remains unresolved!"
return response
end
response = response + winner.get("name") + " wins! They won " + amount.to_s + "xp "
response = response + "from " + loser.get("name") + ", who respectfully hands it over! "
# update the players...
winner.gain_exp(amount)
loser.gain_exp(-amount)
if loser.get("exp") <= 0 then
response = response + "Holy wow! " + loser.get("name") + " went negative! "
response = response + "The Powers That Be resets them to 1xp!"
loser.set_exp(1)
puts response
die
end
return response
end
end
@bets.push(bet)
self.set({"bets" => @bets})
return response
end
def get_player_bets(player)
found_bets = []
@bets.each do |b|
if b['players'].include?(player) then
found_bets.push(b)
end
end
return found_bets
end
def get_bet(player, target, amount)
found_bet = nil
@bets.each do |b|
if b["players"].include?(player) and b["players"].include?(target) then
found_bet = b
end
end
# This feels cludgy but deleting an element from inside an iterator (the .each above)
# is apparently straight up disallowed in ruby. so need to nuke it here. It will either
# resolve and disappear in the place_bet method or be updated and re-inserted there
if found_bet != nil then
@bets.delete_if{ |b| b["players"].include?(player) and b["players"].include?(target) }
return found_bet
end
return {"players" => [player, target], "last_bet" => player, "exp" => amount}
end
def get_default_doc()
return {
"type" => :bets,
"bets" => [],
}
end
end