Skip to content

Instantly share code, notes, and snippets.

@notblizzard
Created June 26, 2015 07:25
Show Gist options
  • Save notblizzard/7673e1cad47e85bf0813 to your computer and use it in GitHub Desktop.
Save notblizzard/7673e1cad47e85bf0813 to your computer and use it in GitHub Desktop.
class BattleHandler
attr_accessor :ws, :bot, :opponent, :team, :data
def intialize(ws, bot, opponent, data)
@ws = ws
@messages = data.split('|')
end
def request_helper
def win_lose_tie_helper(room)
ws.send("#{room}|good game")
ws.send("#{room}|/part")
end
def faint_helper(room, team)
pkmn = @messages[2].split(': ')[1]
if player_one
if @data.include? "p1a: "
team.find{|x| x[:nick] == pkmn}[:fainted] = true
@bot[:hp] = 0
move = BattleHelpers.decide(moves, bot, opponent)
ws.send("#{room}|#{move}")
end
end
end
def get_bot_player(data, team, p1_split)
you = {}
you[:name] = data.split("#{p1_split}a: ")[1].split('|')[0].downcase.gsub(/[^A-z0-9]/, '')
you[:item] = team.find{|x| x[:name] == you[:name]}[:item]
you[:type] = POKEDEX[you[:name]]['types'].map(&:downcase)
you[:speed] = POKEDEX[you[:name]]['baseStats']['spe']
return you
end
def get_opponent_player(data, p2_split)
opponent = {}
opponent[:name] = data.split("#{p2_split}a: ")[1].split('|')[0].downcase.gsub(/[^A-z0-9]/, '')
opponent[:type] = POKEDEX[opponent[:name]]['types'].map(&:downcase)
opponent[:speed] = POKEDEX[opponent[:name]]['baseStats']['spe']
return opponent
end
def get_bot_switch(data, team)
you = {}
you[:name] = data.split(',')[0].downcase.gsub(/[^A-z0-9]/,'')
you[:type] = POKEDEX[you[:name]]['types'].map(&:downcase)
you[:item] = team.find{|x| x[:name] == you[:name]}[:item]
you[:speed] = POKEDEX[you[:name]]['basestats']['spe']
return you
end
def get_opponent_switch(data)
opponent = {}
opponent[:name] = data.split(',')[0].downcase.gsub(/[^A-z0-9]/,'')
opponent[:type] = POKEDEX[opponent[:name]]['types']
opponent[:speed] = POKEDEX[opponent[:name]]['baseStats']['spe']
return opponent
end
def get_hp(data)
hp = Rational(data.split('/')[0]) / data.split('/')[1].to_f
if data.include? " "
hp = Rational(data.split('/')[0]) / datasplit('/')[1].split(' ')[0].to_f
end
return hp
end
def get_bot_request(data)
data = JSON.parse(data)
you = {}
you[:item] = data['side']['pokemon'][0]['item']
you[:mega] = data['side']['pokemon'][0]['canMegaEvo']
you[:name] = data['side']['pokemon'][0]['details'].split(',')[0].gsub(/[^A-z0-9]/,'')
you[:type] = POKEDEX[you[:name].downcase]["types"]
you[:moves] = data['side']['pokemon'][0]['moves']
return you
end
def mega_or_not(tier, team, you, ws)
unless tier == 'cc1v1'
if team.find{|x| x[:name].downcase == you[:name]}[:mega] == true
if megaed == false
ws.send("#{room}|#{move} mega")
magaed = true
end
else
ws.send("#{room}|#{move}")
end
else
if you[:mega] == true
ws.send("#{room}|#{move} mega")
else
ws.send("#{room}|#{move}")
end
end
ws.send("#{room}|#{move}")
end
end
def switch_helper
if player_one
if data[2].include? 'p1a'
unless tier == 'cc1v1'
bot = get_bot_switch(data[3], team)
end
else
opponent = get_opponent_switch(data[3])
end
else
if data[2].include? 'p2a'
unless tier == 'cc1v1'
bot = get_bot_switch(data[3], team)
end
else
opponent = get_opponent_switch(data[3])
end
end
end
def player_helper(bot, opponent, data, player_one, ws, moves)
if player_one and data.include? "p2a: "
bot = get_bot_player(d, team, 'p1')
opponent = get_opponent_player(d, 'p2')
move = decide(moves, bot, opponent)
ws.send("#{room}|#{move}")
end
end
def get_team_helper(data)
team = []
data = JSON.parse(data)
for x in 0..5 do
team << {
:nick => data['side']['pokemon'][x]['ident'].split(': ')[1].downcase,
:name => data['side']['pokemon'][x]['details'].split(',')[0].gsub(/[^A-z0-9]/,'').downcase,
:moves => data['side']['pokemon'][x]['moves'].to_a.map(&:downcase),
:item => data['side']['pokemon'][x]['item'].downcase,
:ability => data['side']['pokemon'][x]['baseAbility'].downcase,
:mega => data['side']['pokemon'][x]['canMegaEvo'],
:speed => data['side']['pokemon'][x]['stats']['spe'],
:fainted => false,
# For some reason we have to do this the hard way.
:type => POKEDEX[data['side']['pokemon'][x]['details'].split(',')[0].downcase.gsub(/[^A-z0-9]/,'')]['types'].map(&:downcase)
}
end
team
end
def moves_helper(data)
data = JSON.parse(data)
moves = []
data['active'][0]['moves'].each_with_index do |_, i|
moves << {
:name => data['active'][0]['moves'][i]['id'],
:type => MOVES[data['active'][0]['moves'][i]['id'].downcase.gsub('-','')]['type'].downcase,
:power => MOVES[data['active'][0]['moves'][i]['id'].downcase.gsub('-','')]['basepower'],
:priority => MOVES[data['active'][0]['moves'][i]['id'].downcase.gsub('-','')]['priority']
}
end
return moves
end
ws.send("#{room}|good luck have fun.")
ws.send("#{room}|/team #{rand(1...7)}")if tier == 'cc1v1' or tier == 'ou'
if data.include? 'side'
m = data.split('|')
if have_team == false
have_team = true
else
bot = get_bot_request(m[2])
end
end
end
def damage_helper(data_1, data_2, player_one, bot, opponent)
if player_one
if data_1.include? "p1a"
if data_2.include? 'fnt'
bot[:hp] = 0
else
bot[:hp] = get_hp(data_2)
end
end
else
if data_2.include? 'fnt'
opponent[:hp] = 0
else
opponent[:hp] = get_hp(data_2)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment