Commit 875e8753 by Nick An

mark

parent d68283ed
...@@ -5,23 +5,38 @@ ...@@ -5,23 +5,38 @@
payload: { payload: {
nick_name: 'asdfasdfa', nick_name: 'asdfasdfa',
avatar_url: 'https://xxx.com', avatar_url: 'https://xxx.com',
mark: 0,
x: 1, x: 1,
y: 4 y: 4
} }
} }
{
type:'catch',
cid: 'abcdefg',
room_id: 'default_room',
payload: {
nick_name: 'asdfasdfa',
avatar_url: 'https://xxx.com',
role: 'ghost', # runner
mark: 0,
x:1,
y:2
}
}
{ {
type: 'game_ready', type: 'game_ready',
cid: 'abcdefg', cid: 'abcdefg',
room_id: 'default_room', room_id: 'default_room',
payload: { payload: [{
nick_name: 'asdfasdfa', nick_name: 'asdfasdfa',
avatar_url: 'https://xxx.com', avatar_url: 'https://xxx.com',
role: 'ghost', # runner role: 'ghost', # runner
mark: 0,
x: 1, x: 1,
y: 3 y: 3
} }]
} }
{ {
......
...@@ -44,8 +44,8 @@ module CrazyFlirt ...@@ -44,8 +44,8 @@ module CrazyFlirt
end end
def pick_born_point def pick_born_point
x = @rd.rand(0..@x_max) x = @rd.rand(0..@x_max - 1)
y = @rd.rand(0..@y_max) y = @rd.rand(0..@y_max - 1)
[x, y] [x, y]
end end
end end
...@@ -53,7 +53,7 @@ module CrazyFlirt ...@@ -53,7 +53,7 @@ module CrazyFlirt
class Client class Client
attr_reader :key attr_reader :key
attr_accessor :role, :room, :user_info attr_accessor :role, :room, :user_info, :seat_index, :ws
def initialize(ws) def initialize(ws)
@ws = ws @ws = ws
...@@ -63,6 +63,7 @@ module CrazyFlirt ...@@ -63,6 +63,7 @@ module CrazyFlirt
end end
def msg(msg) def msg(msg)
puts "reply #{ user_info[:nick_name]}"
@ws.send(msg.to_json) @ws.send(msg.to_json)
end end
private private
...@@ -82,10 +83,16 @@ module CrazyFlirt ...@@ -82,10 +83,16 @@ module CrazyFlirt
@name = name @name = name
@status = :waiting @status = :waiting
@mutex = Mutex.new @mutex = Mutex.new
@rank = RankRoom.new
end end
def set_client(client) def set_client(client)
return unless @status == :waiting unless @status == :waiting
puts "game is staring now"
puts "will close #{client.key}"
client.ws.close
return
end
@mutex.synchronize do @mutex.synchronize do
@clients << client @clients << client
@clients.size - 1 @clients.size - 1
...@@ -96,8 +103,13 @@ module CrazyFlirt ...@@ -96,8 +103,13 @@ module CrazyFlirt
@status = :start @status = :start
end end
def waiting!
@status = :waiting
end
def leave_client(key) def leave_client(key)
idx = @clients.find_index { |c| c.key == key} idx = @clients.find_index { |c| c.key == key}
return if idx.nil?
@clients.delete_at(idx) @clients.delete_at(idx)
end end
...@@ -137,6 +149,7 @@ module CrazyFlirt ...@@ -137,6 +149,7 @@ module CrazyFlirt
client = nil client = nil
puts 'closed' puts 'closed'
end end
ws.on(:error) do |event| ws.on(:error) do |event|
puts event.code puts event.code
ws.close() ws.close()
...@@ -152,7 +165,7 @@ module CrazyFlirt ...@@ -152,7 +165,7 @@ module CrazyFlirt
end end
def start(msg,cl) def start(msg,cl)
room = @room_pool[msg['rid']] room = @room_pool["default_room"]
room.start! room.start!
picker = RandomPicker.new(room.clients) picker = RandomPicker.new(room.clients)
idx = picker.pick_ghost_index idx = picker.pick_ghost_index
...@@ -173,18 +186,25 @@ module CrazyFlirt ...@@ -173,18 +186,25 @@ module CrazyFlirt
payload:{ payload:{
} }
}) })
room.waiting!
puts ""
puts "room now wating"
end end
@item_thread = Thread.new do @item_thread = Thread.new do
5.times do 5.times do
arr = []
3.times do
x,y = RandomPicker.item_generate x,y = RandomPicker.item_generate
notice_all(room,{ arr << {
type: 'item_refresh',
rid: 'default_room',
payload:{
x:x, x:x,
y:y y:y
} }
end
notice_all(room,{
type: 'item_refresh',
rid: 'default_room',
payload: arr
}) })
sleep(5) sleep(5)
end end
...@@ -193,66 +213,108 @@ module CrazyFlirt ...@@ -193,66 +213,108 @@ module CrazyFlirt
def game_ready_notice(room, ghost, picker) def game_ready_notice(room, ghost, picker)
msg = {
type: 'game_ready',
rid: room.name,
payload: []
}
room.clients.each do |client| room.clients.each do |client|
msg = ready_message(room, client) rd_msg = ready_message(client)
if ghost.key == client.key if ghost.key == client.key
msg[:payload][:role] = 'ghost' rd_msg[:role] = 'ghost'
else else
x, y = picker.pick_born_point x, y = picker.pick_born_point
msg[:payload][:x] = x rd_msg[:x] = x
msg[:payload][:y] = y rd_msg[:y] = y
msg[:payload][:role] = 'runner' rd_msg[:role] = 'runner'
end end
rd_msg[:t_cid] = client.key
notice_all(room, msg) msg[:payload] << rd_msg
end end
notice_all(room, msg)
end end
def notice_all(room, msg) def notice_all(room, msg)
room.clients.each do |client| room.clients.each do |client|
msg[:cid] = client.key
client.msg(msg) client.msg(msg)
end end
end end
def ready_message(room, client) def ready_message(client)
{ message = {}
type: 'game_ready', message[:nick_name] = client.user_info[:nick_name]
cid: client.key, message[:avatar_url] = client.user_info[:avatar_url]
rid: room.name, message[:cid] = client.key
payload: { message
nick_name: client.user_info['nick_name'],
avatar_url: client.user_info['avatar_url'],
}
}
end end
def clicking(msg,cl) def clicking(msg,cl)
room = @room_pool[msg['rid']] room = @room_pool["default_room"]
room.clients.each do |client| room.clients.each do |client|
msg['cid'] = client.key
msg['payload']['t_cid'] = cl.key
client.msg(msg) client.msg(msg)
end end
end end
def moving(msg,cl) def moving(msg, cl)
room = @room_pool[msg['rid']] room = @room_pool["default_room"]
room.clients.each do |client| room.clients.each do |client|
msg['cid'] = client.key
msg['payload']['t_cid'] = cl.key
client.msg(msg) client.msg(msg)
end end
end end
def entry(msg, client) def entry(msg, client)
room = @room_pool[msg['rid']] room = @room_pool["default_room"]
seat_index = room.set_client(client) seat_index = room.set_client(client)
puts seat_index
puts client.key
puts msg['payload']['nick_name']
client.room = room client.room = room
client.user_info = {nick_name: msg['payload']['nick_name'], avatar_url: msg['payload']['avatar_url']} client.user_info = {nick_name: msg['payload']['nick_name'], avatar_url: msg['payload']['avatar_url']}
msg['cid'] = client.key client.seat_index = seat_index
msg['payload']['seat'] = seat_index msg['payload']['seat'] = seat_index
msg['payload']['members'] = room.clients.map do |cl| cl.user_info end msg['payload']['members'] = room.clients.map do |cl| cl.user_info end
room.clients.each do |client| room.clients.each do |client|
msg['cid'] = client.key
msg['payload']['seat'] = client.seat_index
client.msg(msg) client.msg(msg)
end end
end end
class RankRoom
def initialize
@players = {}
end
def insert(key, role)
if role == 'ghost'
@players[key] = 0
else
@players[key] = 3
end
end
def add_mark(key)
@players[key] += 1
end
def less_mark(key)
@players[key] -= 1
end
end
# def create_room(msg) # def create_room(msg)
# client_id = msg['cid'] # client_id = msg['cid']
# key = "room_#{::CrazyFlirt::RandomKeyGenerator.key}" # key = "room_#{::CrazyFlirt::RandomKeyGenerator.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