Skip to content

Commit

Permalink
initial setup on incoming_message_handler (renamed from interpreter) …
Browse files Browse the repository at this point in the history
…for handling data
  • Loading branch information
Marc Bowes committed Dec 25, 2008
1 parent 4b5dfa7 commit 375e8ea
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/score_bot.rb
Expand Up @@ -4,5 +4,6 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

require 'score_bot/incoming_message_interpreter'
require 'game'
require 'score_bot/incoming_message_handler'
require 'score_bot/server'
Empty file added lib/score_bot/game.rb
Empty file.
78 changes: 78 additions & 0 deletions lib/score_bot/incoming_message_handler.rb
@@ -0,0 +1,78 @@
module IncomingMessageHandler
def handle_incoming_line(game, incoming)
case incoming.split.first.downcase
# 1. description
# 2. example
when 'ability'
# ability used with no target
# ABILITY Equinox 66:1093684036
player, flag, id = extract_ability_information(incoming)
game.ability_used_no_target(player, flag, id)
when 'chat'
# player chat
# CHATCMD Equinox -wtf
# ...
when 'create'
# new game created
# CREATE [30]testdota test
game.name = extract_name(incoming)
when 'ended'
# game is over
# ENDED
game.finish!
when 'host'
# set game host
# HOST Equinox
game.host = extract_host(incoming)
when 'pause'
# game is paused
# PAUSE Equinox
game.pause!
when 'plist'
# incoming list of players
# PLIST 1.Equinox
game.players = extract_players(incoming)
when 'start'
# signify game started
# START
game.start!
when 'unpause'
# game is unpaused
# UNPAUSE Equinox
game.unpause!
else
# unknown message
end
end

protected

def extract_ability_information(incoming)
# ABILITY Equinox 66:1093684036
ability, name, flag_and_id = incoming.split
flag, id = flag_and_id.split(':')
return name, flag.to_i, id.to_i
end

def extract_host(incoming)
# HOST Equinox
incoming.split.last
end

def extract_name(incoming)
# CREATE [30]testdota test
incoming.sub 'CREATE ', ''
end

def extract_players(incoming)
# PLIST 1.Equinox
slots_and_names = incoming.split
slots_and_names.delete_at(0)
players = {}
slots_and_names.each do |sn|
slot, name = sn.split('.')
players.merge!({ slot.to_i => name })
end
return players
end
end
5 changes: 0 additions & 5 deletions lib/score_bot/incoming_message_interpreter.rb

This file was deleted.

15 changes: 10 additions & 5 deletions lib/score_bot/server.rb
@@ -1,17 +1,22 @@
require 'gserver'

module ScoreBot
include IncomingMessageInterpreter

module ScoreBot
class Server < GServer
include IncomingMessageHandler

def initialize(port=6110, host="0.0.0.0", *args)
super(port, host, *args)
end

def serve(io)
def serve(io)
game = Game.new
loop do
if IO.select([io], nil, nil, 2)
puts "recv: #{io.gets.chomp}"
begin
handle_incoming_line(game, io.gets.chomp)
rescue Exception => e
puts e
end
end
break if io.closed?
end
Expand Down

0 comments on commit 375e8ea

Please sign in to comment.