Commit 875e8753 by Nick An

mark

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