This repository has been archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
77 lines (63 loc) · 1.68 KB
/
game.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literals:true
require 'pp'
require 'socket'
require 'thread'
require 'json'
require_relative 'lib/core_ext'
require_relative 'lib/map'
require_relative 'lib/unit'
module Game
class Match
attr_reader :map
def initialize
@my_units = {}
@map = Map.new
end
def process_updates_from_server(msgs)
msgs.each do |msg|
@map.update_tiles(msg['tile_updates'])
# TODO, parse Turn 0 Infos (like Gametime, UnitStats, etc)
msg['unit_updates'].each do |uu|
@my_units[uu['id']] ||= Unit.from_type(@map, uu)
@my_units[uu['id']].update(uu)
end
end
{ commands: @my_units.values.map(&:act!).compact }
end
end
def self.start(port)
server = TCPServer.new port
Thread.abort_on_exception = true
loop do
puts "waiting for connection on #{port}"
Thread.new(server.accept) do |server_connection|
msg_queue = Queue.new
start_listener(server_connection, msg_queue)
run!(Match.new, server_connection, msg_queue)
server_connection.close
end
end
end
def self.start_listener(server_connection, msg_queue)
Thread.new do
begin
while (msg = server_connection.gets)
msg_queue.push JSON.parse(msg)
end
end
end
end
def self.run!(game, server_connection, msg_queue)
loop do
msg = msg_queue.pop
msgs = [msg]
until msg_queue.empty?
puts '!!! missed turn!'
msgs << msg_queue.pop
end
commands = game.process_updates_from_server(msgs)
server_connection.puts(commands.to_json)
end
end
end
Game.start(ARGV[0] || 9090) if $PROGRAM_NAME == __FILE__