-
-
Save Corgan/ee8c06ce857fe51e8ebb 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
require 'eventmachine' | |
require 'stringio' | |
require 'bindata' | |
require 'timeout' | |
$instance = 0 | |
$time = 45 | |
$user_id = "" | |
$user_hash = "" | |
module ArenaClient | |
MAGIC = "\xc7" | |
MESSAGE_PLAYSERVERCONNECT = 1 | |
MESSAGE_CHALLENGE = 2 | |
MESSAGE_PLAYERCHAT = 3 | |
MESSAGE_MATCHRESULTS = 4 | |
MESSAGE_USERINFO = 5 | |
MESSAGE_USERMOVE = 6 | |
MESSAGE_ADMINACTION = 7 | |
MESSAGE_SPAWNBUSH = 8 | |
MESSAGE_USEPOWERUP = 9 | |
MESSAGE_GETDEFINITIONS = 10 | |
MESSAGE_WHACKBUSH = 11 | |
MESSAGE_ONLINEFRIENDS = 12 | |
MESSAGE_STATUS = 13 | |
MESSAGE_MATCHHANDSHAKE = 14 | |
MESSAGE_USERSINZONE = 15 | |
MESSAGE_ACTIVEBATTLES = 16 | |
MESSAGE_FRIENDSTATUS = 17 | |
MESSAGE_WATCHMATCH = 18 | |
MESSAGE_LEAVEMATCH = 19 | |
MESSAGE_SERVERSTATUS = 20 | |
MESSAGE_ARENARANK = 21 | |
MESSAGE_SETTITLE = 22 | |
MESSAGE_PING = 23 | |
def post_init | |
packet = PlayServerConnect.new() | |
packet.id = $user_id | |
packet.zhash = $user_hash | |
sendMessage(MESSAGE_PLAYSERVERCONNECT, packet) | |
end | |
def sendMessage(type, data) | |
header = Header.new() | |
header.magic = MAGIC | |
header.type = type | |
header.sender_id = 0 | |
header.payload_length = data.num_bytes | |
puts "Sending: #{data.inspect}" | |
send_data(header.to_binary_s + data.to_binary_s) | |
end | |
def receive_data(data) | |
data = StringIO.new(data) | |
header = Header.read(data.read(13)) | |
packet = nil | |
case header.type.to_i | |
when MESSAGE_PLAYSERVERCONNECT | |
packet = PlayServerConnect.read(data) | |
puts "Getting: #{packet}" if !packet.nil? | |
challenge = Challenge.new() | |
challenge.id = $user_id | |
sendMessage(MESSAGE_CHALLENGE, challenge) | |
$time = 45 | |
when MESSAGE_CHALLENGE | |
packet = Challenge.read(data) | |
puts "Getting: #{packet}" if !packet.nil? | |
when MESSAGE_PLAYERCHAT | |
when MESSAGE_MATCHRESULTS | |
packet = MatchResults.read(data) | |
puts "Getting: #{packet}" if !packet.nil? | |
EventMachine::stop_event_loop | |
when MESSAGE_USERINFO | |
when MESSAGE_USERMOVE | |
packet = UserMove.read(data) | |
puts "Getting: #{packet}" if !packet.nil? | |
when MESSAGE_ADMINACTION | |
when MESSAGE_SPAWNBUSH | |
packet = SpawnBush.read(data) | |
puts "Getting: #{packet}" if !packet.nil? | |
whack = WhackBush.new() | |
whack.targets = [packet.bushes.first.target] | |
whack.latency = rand(100) | |
whack.seconds_left = ($time -= 1) | |
move = UserMove.new() | |
move.id = $user_id | |
move.fromX = rand(100) | |
move.fromY = rand(100) | |
move.toX = rand(100) | |
move.toY = rand(100) | |
move.latency = rand(100) | |
move.is_only_move = rand(2) | |
sendMessage(MESSAGE_USERMOVE, move) | |
sleep rand(3) | |
sendMessage(MESSAGE_WHACKBUSH, whack) | |
when MESSAGE_USEPOWERUP | |
when MESSAGE_GETDEFINITIONS | |
when MESSAGE_WHACKBUSH | |
packet = WhackBush.read(data) | |
packet = nil if packet.id == $user_id | |
puts "Getting: #{packet}" if !packet.nil? | |
when MESSAGE_ONLINEFRIENDS | |
when MESSAGE_STATUS | |
when MESSAGE_MATCHHANDSHAKE | |
packet = MatchHandShake.read(data) | |
puts "Getting: #{packet}" if !packet.nil? | |
$instance = packet.instance_id | |
shakeAndBake = MatchHandShake.new() | |
shakeAndBake.instance_id = packet.instance_id | |
shakeAndBake.i_am_ready = 1 | |
sendMessage(MESSAGE_MATCHHANDSHAKE, shakeAndBake) | |
when MESSAGE_USERSINZONE | |
when MESSAGE_ACTIVEBATTLES | |
when MESSAGE_FRIENDSTATUS | |
when MESSAGE_WATCHMATCH | |
when MESSAGE_LEAVEMATCH | |
when MESSAGE_SERVERSTATUS | |
when MESSAGE_ARENARANK | |
when MESSAGE_SETTITLE | |
when MESSAGE_PING | |
end | |
end | |
def unbind | |
puts "A connection has terminated" | |
end | |
class Header < BinData::Record | |
endian :big | |
string :magic, :read_length => 1 | |
int32 :type | |
int32 :sender_id | |
int32 :payload_length | |
end | |
class PlayServerConnect < BinData::Record | |
endian :big | |
int32 :id_len, :value => lambda { id.length } | |
string :id, :read_length => :id_len | |
int32 :zhash_len, :value => lambda { zhash.length } | |
string :zhash, :read_length => :zhash_len | |
int32 :success | |
end | |
class Challenge < BinData::Record | |
endian :big | |
int32 :id_len, :value => lambda { id.length } | |
string :id, :read_length => :id_len | |
int32 :challengeid_len, :value => lambda { challengeid.length } | |
string :challengeid, :read_length => :challengeid_len | |
int32 :is_complete | |
int32 :is_cancelled | |
int32 :success | |
end | |
class MatchHandShake < BinData::Record | |
endian :big | |
int32 :instance_id | |
int32 :zone_id | |
int32 :num_users, :value => lambda { users.length } | |
array :users, :initial_length => :num_users do | |
int32 :user_length, :value => lambda { user.length } | |
string :user, :read_length => :user_length | |
int32 :user_ready | |
int32 :user_team | |
int32 :user_x | |
int32 :user_y | |
end | |
int32 :time_to_match | |
int32 :i_am_ready | |
int32 :match_length | |
int32 :match_is_cancelled | |
end | |
class SpawnBush < BinData::Record | |
endian :big | |
int32 :num_bushes, :value => lambda { bushes.length } | |
array :bushes, :initial_length => :num_bushes do | |
int32 :target | |
int32 :type | |
end | |
end | |
class WhackBush < BinData::Record | |
endian :big | |
int32 :id_len, :value => lambda { id.length } | |
string :id, :read_length => :id_len | |
int32 :num_targets, :value => lambda { targets.length } | |
array :targets, :initial_length => :num_targets do | |
int32 :target | |
end | |
int32 :num_scores, :value => lambda { scores.length } | |
array :scores, :initial_length => :num_scores do | |
int32 :score | |
int32 :prize | |
end | |
int32 :success | |
int32 :whack_type | |
int32 :latency | |
int32 :seconds_left | |
end | |
class UserMove < BinData::Record | |
endian :big | |
int32 :id_len, :value => lambda { id.length } | |
string :id, :read_length => :id_len | |
int32 :fromX | |
int32 :fromY | |
int32 :toX | |
int32 :toY | |
int32 :latency | |
int32 :is_only_move | |
end | |
class MatchResults < BinData::Record | |
endian :big | |
int32 :num_teams, :value => lambda { teams.length } | |
array :teams, :initial_length => :num_teams do | |
int32 :num_players, :value => lambda { players.length } | |
array :players, :initial_length => :num_players do | |
int32 :id_len, :value => lambda { id.length } | |
string :id, :read_length => :id_len | |
int32 :user_score | |
int32 :user_whacked | |
int32 :points_awarded | |
int32 :tokens_awarded | |
int32 :match_result | |
end | |
end | |
end | |
class LeaveMatch < BinData::Record | |
endian :big | |
int32 :id_len, :value => lambda { id.length } | |
string :id, :read_length => :id_len | |
int32 :instance_id | |
int32 :success | |
end | |
end | |
while true do | |
begin | |
Timeout::timeout(90) do | |
EventMachine::run { | |
EventMachine::connect "ec2-50-18-3-43.us-west-1.compute.amazonaws.com", 9896, ArenaClient | |
} | |
end | |
rescue Timeout::Error | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment