Skip to content

Instantly share code, notes, and snippets.

@notblizzard
Created June 21, 2015 01:50
Show Gist options
  • Save notblizzard/2d26618fe6c34b1ffd50 to your computer and use it in GitHub Desktop.
Save notblizzard/2d26618fe6c34b1ffd50 to your computer and use it in GitHub Desktop.
require 'rest-client'
class Battle
attr_accessor :ws, :room, :moves_power
@current_team = false
@moves = []
@you = {
:hp => 100
}
@opp = {
:hp => 100
}
@team = Array.new
def initialize(ws)
@ws = ws
end
def run
@ws.on :message do |event|
event.data.gsub!(/(^>|\n)/, '')
event.data.split('\n').each do |m|
d = m
m = m.split('|')
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(RestClient.get(MOVES_URL).downcase)[data['active'][0]['moves'][i]['id'].downcase.gsub('-','')]['type'],
:power => JSON.parse(RestClient.get(MOVES_URL).downcase)[data['active'][0]['moves'][i]['id'].downcase.gsub('-','')]['basepower'],
:priority => JSON.parse(RestClient.get(MOVES_URL).downcase)[data['active'][0]['moves'][i]['id'].downcase.gsub('-','')]['priority']
}
end
end
if m[2].include? 'forceSwitch'
if m[2].include? "0 fnt"
@team.delete_if {|p| p[:name] == m[2].split('p1: ')[1].split('"')[0]}
end
@ws.send("#{@room}|/switch #{@team.sample[:name]}")
end
if m[2].include? 'side' and @current_team == false
for x in 0..5 do
pkmn = data['side']['pokemon'][x]['ident'].split('p1: ')[1].downcase
puts "pkmn is #{pkmn}"
@team << {
:name => data['side']['pokemon'][x]['ident'].gsub(/p1: /, ''),
:moves => data['side']['pokemon'][x]['moves']
}
end
@current_team = true
end
when 'player'
@room = m[0].gsub(/[\n>]/, '')
if (d.include? 'p2a: ')
@you[:name] = d.split('p1a: ')[1].split('|')[0].downcase.gsub(/[^A-z0-9]/, '')
@opp[:name] = d.split('p2a: ')[1].split('|')[0].downcase.gsub(/[^A-z0-9]/, '')
@you[:type] = JSON.parse(RestClient.get(POKEDEX_URL))[@you[:name]]['types']
@opp[:type] = JSON.parse(RestClient.get(POKEDEX_URL))[@opp[:name]]['types']
if @tier == 'cc1v1'
@ws.send("#{@room}|/team #{rand(1..7)}")
elsif @tier == 'randombattle'
move = self.decide
@ws.send("#{@room}|/move #{move}")
end
end
when 'title'
@room = m[0].gsub(/\n>/,'')
@ws.send("#{@room}|Good luck, have fun.")
when ''
if d.include? '/'
#puts d.downcase.gsub('-','')[/(\s|)#{$battles[@battleroom[/(\d+)/]][:name]}\|(\d*)\/(\d*)/i]#.gsub(/[^0-9\/]/,'')#.split('|')[1]
begin
@opp[:hp] = d.gsub('-','')[/(\s|)(#{@opp[:name]})\|(\d*)\/(\d*)/i].gsub(/[^0-9\/]/,'')
rescue
end
end
if d.include? "drag"
@opp[:name] = d.split('drag|')[1].split('|')[0].split('p2a: ')[1]
end
if m[2] == 'start' and @you[:name].nil?
@you[:name] = m[4].split('p1a: ')[1].downcase
@opp[:name] = m[8].split('p2a: ')[1].downcase.gsub(/[^A-z0-9]/,'')
end
if m[2] == 'switch'
if (d.include?'p2a')
@opp[:name] = d.split('p2a: ')[1].split('|')[0].downcase
@opp[:type] = JSON.parse(RestClient.get(POKEDEX_URL))[@opp[:name]]['types']
else
@you[:name] = d.split('p1a: ')[1].split('|')[0].downcase
@you[:type] = JSON.parse(RestClient.get(POKEDEX_URL))[@you[:name]]['types']
end
end
if d.match(/(\Wwin|\Wlose)/)
@ws.send("#{@room}|good game.")
@ws.send("#{@room}|/part")
end
if d.include? 'faint'
if d.split('faint|')[1].split(':')[0].include? 'p1a'
fainted_pokemon = d.split('faint|p1a: ')[1].split('|')[0].downcase
@team.delete_if {|x| x[:name] == fainted_pokemon }
index = @team.index(@team.sample)
@ws.send("#{@battleroom}|/switch #{index.to_i+1}")
@you[:hp] = 100
else
fainted_pokemon = d.split('faint|p2a: ')[1].split('|')[0].downcase
end
else
move = self.decide
@ws.send("#{@room}|/move #{move}")
end
end
end
end
end
def decide
opponent_weaknesses = Array.new
opponent_resistances = Array.new
opponent_immunities = Array.new
@opp[:type].each do |p|
opponent_weaknesses << JSON.parse(RestClient.get(WEAKNESS_URL))[p.downcase].to_a
opponent_resistances << JSON.parse(RestClient.get(RESISTANCE_URL))[p.downcase].to_a
opponent_immunities << JSON.parse(RestClient.get(IMMUNITIES_URL))[p.downcase].to_a
end
opponent_abilities = JSON.parse(RestClient.get(POKEDEX_URL))[opponent[:name].downcase]['abilities'].values.flatten.map(&:downcase)
opponent_weaknesses = opponent_weaknesses.flatten.map(&:downcase).uniq
opponent_resistances = opponent_resistances.flatten.map(&:downcase).uniq
opponent_immunities = opponent_immunities.flatten.map(&:downcase).uniq
weaknesses = 0
resistances = 0
if opponent[:hp].to_i < 0.15
unless moves.find {|x| x[:priority] == 1}.nil?
return moves.find {|x| x[:priority] == 1}[:name]
end
end
@moves.each_with_index do |move, i|
mod = 1
counts = Hash.new 0
if you_type.include? move[:type]
# STAB
mod+=0.5
end
opponent_weaknesses.each {|x| counts[x] += 1}
if counts[move[:type]] == 2
mod+=3
elsif counts[move[:type]] == 1
mod+=1
end
counts = Hash.new 0
opponent_resistances.each {|x| counts[x] += 1}
if counts[move[:type]] == 2
mod-=1.5
elsif counts[move[:type]] == 1
mod-=0.5
end
if opponent_immunities.include? move[:type]
mod=0
end
@moves_power << {:power => move[:power] * mod, :name => move[:name]}
if move[:type] == 'dragon' and opponent_type.include? 'fairy'
return decide(moves - moves[i], you, opponent)
end
# Allow Sigilyph to set up.
if @you[:name].downcase == 'sigilyph'
return ['cosmicpower', 'storedpower'].sample
end
end
return @moves_power.max_by {|x| x[:power] }[:name].downcase
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment