diff --git a/lib/score_bot.rb b/lib/score_bot.rb index cbeda50..deebcca 100644 --- a/lib/score_bot.rb +++ b/lib/score_bot.rb @@ -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' diff --git a/lib/score_bot/game.rb b/lib/score_bot/game.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/score_bot/incoming_message_handler.rb b/lib/score_bot/incoming_message_handler.rb new file mode 100644 index 0000000..9eeb741 --- /dev/null +++ b/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 diff --git a/lib/score_bot/incoming_message_interpreter.rb b/lib/score_bot/incoming_message_interpreter.rb deleted file mode 100644 index e83ad65..0000000 --- a/lib/score_bot/incoming_message_interpreter.rb +++ /dev/null @@ -1,5 +0,0 @@ -module ScoreBot - module IncomingMessageInterpreter - - end -end diff --git a/lib/score_bot/server.rb b/lib/score_bot/server.rb index 9330338..6af8ee2 100644 --- a/lib/score_bot/server.rb +++ b/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