-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.rb
95 lines (75 loc) · 2.01 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
$stdout.sync = true
require 'em-websocket'
require 'yajl'
require 'yajl/json_gem'
require File.join(File.dirname(__FILE__), 'app', 'models', 'table')
class PokerConnection
attr_accessor :player, :websocket
def initialize(player, websocket)
@player = player
@websocket = websocket
end
def send_message(msg)
@websocket.send(msg.to_json)
end
end
class PokerRoom
def initialize
@connections = {}
@table = Table.new
end
def start
EM::WebSocket.run(:host => "0.0.0.0", :port => 9293) do |ws|
ws.onopen { |handshake| player_connected!(ws, handshake) }
ws.onclose { player_disconnected!(ws) }
ws.onmessage { |msg| message_received!(ws, msg) }
end
end
def player_connected!(ws, handshake)
player = Player.new(:name => handshake.query["name"])
connection = PokerConnection.new(player, ws)
@connections[ws] = connection
update_poker_room
end
def player_disconnected!(ws)
connection = @connections.delete(ws)
@table.unseat_player(connection.player)
if @table.playing?
@table.abort!
end
update_poker_room
end
def message_received!(ws, json)
msg = JSON.parse(json)
player = @connections[ws].player
@table.handle_command msg["command"], player, msg["data"]
update_poker_room
end
def update_poker_room
@connections.values.each do |connection|
connection.send_message :command => :update_poker_room, :data => as_json(connection.player)
end
broadcast_messages
end
def broadcast_messages
@connections.values.each do |connection|
@table.messages.each do |message|
connection.send_message :command => :broadcast_message, :data => message
end
end
@table.messages.clear
end
def connected_players
@connections.values.map(&:player)
end
def as_json(viewer)
{
:players => connected_players.map { |p| p.as_json },
:table => @table.as_json(viewer),
:current_player => viewer.as_json(viewer)
}
end
end
EM.run do
PokerRoom.new.start
end