-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
70 lines (66 loc) · 1.62 KB
/
server.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
require 'gserver'
class NardsServer < GServer
def initialize *args
super
@gamers = Hash.new
@msg = []
@msg[0], @msg[1], @msg[2] = "msg[0]", "hello", "hello"
end
def disconnecting(clientPort)
log("#{self.class.to_s} #{@host}:#{@port} " +
"client:#{clientPort} disconnect")
@gamers.each{|key, value| @gamers.delete(key) if value.peeraddr(true)[1] == clientPort}
log("#{@gamers}")
end
def serve io
unless @gamers.has_value?(io)
if [email protected]_key?("1")
@gamers["1"] = io.clone
next_side = 1
elsif [email protected]_key?("2")
@gamers["2"] = io.clone
next_side = 2
else
next_side = 0
end
end
loop{
cmd, *arg = *io.gets.chomp.split
if arg[0].to_i == 1
to_side = 2
elsif arg[0].to_i == 2
to_side = 1
else
to_side = 0
end
case cmd
when "get_side"
begin
io.puts "#{next_side}"
end
when "move_selected_to_position"
begin
@msg[to_side] = "move_selected_to_position #{arg[1]} #{arg[2]} #{arg[3]}"
io.puts "+OK"
end
when "your_movement"
begin
@msg[to_side] = "your_movement"
io.puts "+OK"
end
when "get_message"
begin
io.puts "#{@msg[arg[0].to_i]}"
@msg[arg[0].to_i] = "hello"
end
when "shutdown"
begin
io.puts "+OK"
io.close
end
end
io.puts "+OK"
}
end
end
NardsServer.new(7000, 'localhost', 2, $stderr, true).start.join