-
-
Save notblizzard/4dae9559d0ffebeb2b24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Battle | |
attr_accessor :ws, :room, :moves_power, :you, :team, :opp, :strongest, :magaed | |
def initialize(tier, challenged) | |
ws = ws | |
@tier = tier | |
@pkmn_team = Array.new | |
@moves = Array.new | |
@you = Hash.new | |
@opp = Hash.new | |
@team = false | |
@moves_power = [] | |
@p1 = true | |
@choice_move = '' | |
@challenged = challenged | |
@greeted = false | |
end | |
def run(ws, data, room) | |
if @challenged | |
@p1 = true | |
else | |
@p1 = false | |
end | |
d = data | |
m = data.split('|') | |
puts "m1 is #{m[1]}" | |
case m[1] | |
when 'request' | |
data = JSON.parse(m[2]) | |
if m[2].split(':')[0].include? "active" | |
data['active'][0]['moves'].each_with_index do |_, i| | |
@moves << { | |
:name => data['active'][0]['moves'][i]['id'], | |
:type => JSON.parse(Faraday.get(MOVES_URL).body.downcase)[data['active'][0]['moves'][i]['id'].downcase.gsub('-','')]['type'].downcase, | |
:power => JSON.parse(Faraday.get(MOVES_URL).body.downcase)[data['active'][0]['moves'][i]['id'].downcase.gsub('-','')]['basepower'], | |
:priority => JSON.parse(Faraday.get(MOVES_URL).body.downcase)[data['active'][0]['moves'][i]['id'].downcase.gsub('-','')]['priority'] | |
} | |
end | |
end | |
if @greeted == false | |
ws.send("#{room}|glhf") | |
@greeted = true | |
end | |
if @tier == 'cc1v1' or @tier == 'ou' | |
ws.send("#{room}|/team #{rand(1...7)}") | |
end | |
@you[:hp] = 100 | |
@opp[:hp] = 100 | |
if m[2].include? 'side' | |
if @current_team == false | |
for x in 0..5 do | |
unless @tier == 'cc1v1' | |
@pkmn_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 => JSON.parse(Faraday.get(POKEDEX_URL).body.downcase)[data['side']['pokemon'][x]['details'].split(',')[0].downcase.gsub(/[^A-z0-9]/,'')]['types'].map(&:downcase) | |
} | |
puts "team member is #{@pkmn_team[x][:name]}" | |
else | |
@you[:item] = data['side']['pokemon'][0]['item'] | |
@you[:moves] = data['side']['pokemon'][0]['moves'] | |
@you[:mega] = data['side']['pokemon'][0]['canMegaEvo'] | |
@you[:name] = data['side']['pokemon'][0]['details'].split(',')[0].gsub(/[^A-z0-9]/,'') | |
@you[:type] = JSON.parse(Faraday.get(POKEDEX_URL).body.downcase)[@you[:name].downcase]["types"] | |
end | |
end | |
end | |
@team = true | |
end | |
when 'win' | |
when 'lose' | |
when 'tie' | |
ws.send("#{room}|good game") | |
ws.send("#{room}|/part") | |
when 'faint' | |
if @tier == 'cc1v1' | |
ws.send("#{room}|good game") | |
ws.send("#{room}|/part") | |
else | |
pkmn = m[2].split(': ')[1] | |
if @p1 | |
if m[2].include? "p1a: " | |
@pkmn_team.find{|x| x[:nick] == pkmn}[:fainted] = true | |
@you[:hp] = 0 | |
@moves_power = [] | |
move = self.decide | |
ws.send("#{room}|#{move}") | |
end | |
end | |
end | |
when 'player' | |
if @p1 | |
if (d.include? "p2a: ") | |
@you[:name] = d.split("#{@me}a: ")[1].split('|')[0].downcase.gsub(/[^A-z0-9]/, '') | |
@opp[:name] = d.split("#{@op}a: ")[1].split('|')[0].downcase.gsub(/[^A-z0-9]/, '') | |
@you[:item] = @pkmn_team.find{|x| x[:name] == @you[:name]}[:item] | |
@you[:type] = JSON.parse(RestClient.get(POKEDEX_URL))[@you[:name]]['types'].map(&:downcase) | |
@opp[:type] = JSON.parse(RestClient.get(POKEDEX_URL))[@opp[:name]]['types'].map(&:downcase) | |
@you[:speed] = JSON.parse(Faraday.get(POKEDEX_URL).body)[@you[:name]]['baseStats']['spe'] | |
@opp[:speed] = JSON.parse(Faraday.get(POKEDEX_URL).body)[@opp[:name]]['baseStats']['spe'] | |
move = self.decide | |
ws.send("#{room}|#{move}") | |
end | |
end | |
when '-damage' | |
if @p1 | |
if m[2].include? "p1a" | |
if m[3].include? 'fnt' | |
@you[:hp] = 0 | |
else | |
@you[:hp] = Rational(m[3].split('/')[0]) / m[3].split('/')[1].to_f | |
if m[3].include? " " | |
@you[:hp] = Rational(m[3].split('/')[0]) / m[3].split('/')[1].split(' ')[0].to_f | |
end | |
end | |
end | |
else | |
if m[3].include? 'fnt' | |
@opp[:hp] = 0 | |
else | |
@opp[:hp] = Rational(m[3].split('/')[0]) / m[3].to_f | |
if m[3].include? " " | |
@opp[:hp] = Rational(m[3].split('/')[0]) / m[3].split('/')[1].split(' ')[0].to_f | |
end | |
end | |
end | |
when 'turn' | |
move = self.decide | |
unless @tier == 'cc1v1' | |
if @pkmn_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}") | |
when 'switch' | |
if @p1 | |
if m[2].include? 'p1a' | |
if @tier != 'cc1v1' | |
@you[:name] = m[3].split(',')[0].downcase.gsub(/[^A-z0-9]/,'') | |
@you[:type] = JSON.parse(RestClient.get(POKEDEX_URL))[@you[:name]]['types'].map(&:downcase) | |
@you[:item] = @pkmn_team.find{|x| x[:name].downcase.gsub(/[^A-z0-9]/,'') == @you[:name]}[:item] | |
@you[:speed] = JSON.parse(Faraday.get(POKEDEX_URL).body.downcase)[@you[:name]]['basestats']['spe'] | |
end | |
else | |
@opp[:name] = m[3].split(',')[0].downcase.gsub(/[^A-z0-9]/,'') | |
@opp[:type] = JSON.parse(Faraday.get(POKEDEX_URL).body.downcase)[@opp[:name]]['types'] | |
@opp[:speed] = JSON.parse(Faraday.get(POKEDEX_URL).body.downcase)[@opp[:name]]['basestats']['spe'] | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment