Commit 153545ae by Shawn Wang

Merge branch 'develop' of cd.i.strikingly.com:shawn.wang/crazyflirt into develop

parents 10ba22f7 1eebe1f6
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
cid: 'abcdefg', cid: 'abcdefg',
room_id: 'asdfadfaf', room_id: 'asdfadfaf',
payload: { payload: {
open_id: 'asdfasdfa', nick_name: 'asdfasdfa',
picture: 'https://xxx.com', avatar_url: 'https://xxx.com',
x: 1, x: 1,
y: 4 y: 4
} }
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
cid: 'abcdefg', cid: 'abcdefg',
room_id: 'asdfadfaf', room_id: 'asdfadfaf',
payload: { payload: {
open_id: 'asdfasdfa', nick_name: 'asdfasdfa',
picture: 'https://xxx.com', avatar_url: 'https://xxx.com',
role: 'ghost', # runner role: 'ghost', # runner
x: 1, x: 1,
y: 3 y: 3
......
...@@ -16,7 +16,7 @@ module CrazyFlirt ...@@ -16,7 +16,7 @@ module CrazyFlirt
5.times do 5.times do
random_key += words[random.rand(0..size)] random_key += words[random.rand(0..size)]
end end
Time.now.to_s.to_i + random_key Time.now.to_s.to_i.to_s + random_key
end end
end end
...@@ -66,7 +66,7 @@ module CrazyFlirt ...@@ -66,7 +66,7 @@ module CrazyFlirt
class GameRoom class GameRoom
attr_reader :clients attr_reader :clients, :name
def initialize(name) def initialize(name)
@clients = [] @clients = []
...@@ -75,8 +75,9 @@ module CrazyFlirt ...@@ -75,8 +75,9 @@ module CrazyFlirt
end end
def set_client(client) def set_client(client)
return unless @status == :waiting
@clients << client @clients << client
client.size - 1 @clients.size - 1
end end
def start! def start!
...@@ -105,12 +106,11 @@ module CrazyFlirt ...@@ -105,12 +106,11 @@ module CrazyFlirt
def call(env) def call(env)
return unless @faye.websocket?(env) return unless @faye.websocket?(env)
ws = @faye.new(env) ws = @faye.new(env)
client = ::CrazyFlirt::Client.new(ws) client = ::CrazyFlirt::Client.new(ws)
@socket_pool[client.key] = client @socket_pool[client.key] = client
ws.on :message do |event| ws.on :message do |event|
message = JSON.parse(event.data) message = JSON.parse(event.data)
seq = message[:type] seq = message['type']
command(seq, message, client) command(seq, message, client)
end end
...@@ -125,33 +125,56 @@ module CrazyFlirt ...@@ -125,33 +125,56 @@ module CrazyFlirt
private private
def commend(command, *args) def command(command, *args)
send(command, *args) send(command, *args)
end end
def start(msg) def start(msg,cl)
room = @room_pool[msg['room_id']] room = @room_pool[msg['rid']]
room.start!
picker = RandomPicker.new(room.clients) picker = RandomPicker.new(room.clients)
idx = picker.pick_ghost_index idx = picker.pick_ghost_index
ghost = room.clients[idx] ghost = room.clients[idx]
room.clients.each do |client|
msg = {
type: 'game_ready',
cid: client.id,
room_id: room.name,
payload: {
open_id: client.open_id,
picture: client.picture,
}
}
if ghost.key == client.key
msg[:payload][:role] = 'ghost'
client.msg(msg)
else
x,y = picker.pick_born_point
msg[:payload][:x] = x
msg[:payload][:y] = y
msg[:payload][:role] = 'runner'
client.msg(msg)
end
end
end end
def clicking(msg) def clicking(msg,cl)
room = @room_pool[msg['room_id']] room = @room_pool[msg['rid']]
room.clients.each do |client| room.clients.each do |client|
client.msg(msg) client.msg(msg)
end end
end end
def moving(msg) def moving(msg,cl)
room = @room_pool[msg['room_id']] room = @room_pool[msg['rid']]
room.clients.each do |client| room.clients.each do |client|
client.msg(msg) client.msg(msg)
end end
end end
def entry(msg, client) def entry(msg, client)
room = @room_pool[msg['room_id']] room = @room_pool[msg['rid']]
seat_index = room.set_client(client) seat_index = room.set_client(client)
client.room = room client.room = room
msg['cid'] = client.key msg['cid'] = client.key
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment