Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
crazyflirt
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shawn Wang
crazyflirt
Commits
c04468c7
Commit
c04468c7
authored
May 05, 2018
by
Nick An
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
item
parent
c6a417b5
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
91 additions
and
21 deletions
+91
-21
doc.rb
be/doc.rb
+12
-2
index.rb
be/index.rb
+79
-19
No files found.
be/doc.rb
View file @
c04468c7
...
@@ -14,7 +14,7 @@
...
@@ -14,7 +14,7 @@
{
{
type:
'game_ready'
,
type:
'game_ready'
,
cid:
'abcdefg'
,
cid:
'abcdefg'
,
room_id:
'
asdfadfaf
'
,
room_id:
'
default_room
'
,
payload:
{
payload:
{
nick_name:
'asdfasdfa'
,
nick_name:
'asdfasdfa'
,
avatar_url:
'https://xxx.com'
,
avatar_url:
'https://xxx.com'
,
...
@@ -26,7 +26,16 @@
...
@@ -26,7 +26,16 @@
{
{
type:
'game_end'
,
type:
'game_end'
,
room_id:
'asdfadfaf'
,
room_id:
'default_room'
,
payload
:{
}
}
{
type:
'item_refresh'
,
room_id:
'default_room'
,
payload
:{
payload
:{
x
:
1
,
y
:
2
}
}
}
}
\ No newline at end of file
be/index.rb
View file @
c04468c7
...
@@ -21,8 +21,16 @@ module CrazyFlirt
...
@@ -21,8 +21,16 @@ module CrazyFlirt
end
end
class
RandomPicker
class
RandomPicker
def
self
.
item_generate
rd
=
Random
.
new
x
=
@rd
.
rand
(
0
..
@x_max
)
y
=
@rd
.
rand
(
0
..
@y_max
)
[
x
,
y
]
end
def
initialize
(
clients
)
def
initialize
(
clients
)
@client
=
clients
@client
=
clients
@x_max
=
14
@x_max
=
14
...
@@ -45,12 +53,13 @@ module CrazyFlirt
...
@@ -45,12 +53,13 @@ module CrazyFlirt
class
Client
class
Client
attr_reader
:key
attr_reader
:key
attr_accessor
:role
,
:room
attr_accessor
:role
,
:room
,
:user_info
def
initialize
(
ws
)
def
initialize
(
ws
)
@ws
=
ws
@ws
=
ws
@key
=
generate_key
@key
=
generate_key
@role
=
nil
@role
=
nil
@room
=
nil
end
end
def
msg
(
msg
)
def
msg
(
msg
)
...
@@ -111,11 +120,15 @@ module CrazyFlirt
...
@@ -111,11 +120,15 @@ module CrazyFlirt
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'
]
puts
'seq is nil'
if
seq
.
nil?
next
if
seq
.
nil?
command
(
seq
,
message
,
client
)
command
(
seq
,
message
,
client
)
end
end
ws
.
on
:close
do
|
event
|
ws
.
on
:close
do
|
event
|
puts
"leave key
#{
client
.
key
}
"
@socket_pool
.
delete
(
client
.
key
)
@socket_pool
.
delete
(
client
.
key
)
p
client
.
room
client
.
room
.
leave_client
(
client
.
key
)
client
.
room
.
leave_client
(
client
.
key
)
client
=
nil
client
=
nil
puts
'closed'
puts
'closed'
...
@@ -135,28 +148,73 @@ module CrazyFlirt
...
@@ -135,28 +148,73 @@ module CrazyFlirt
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
]
game_ready_notice
(
room
,
ghost
,
picker
)
timer
(
room
)
end
room
.
clients
.
each
do
|
client
|
msg
=
{
def
timer
(
room
)
type:
'game_ready'
,
@thread
=
Thread
.
new
do
cid:
client
.
id
,
puts
'start'
sleep
(
30
)
puts
'notice'
notice_all
(
room
,
{
type:
'game_end'
,
room_id:
room
.
name
,
room_id:
room
.
name
,
payload:
{
payload
:{
open_id:
client
.
open_id
,
picture:
client
.
picture
,
}
}
})
end
@item_thread
=
Thread
.
new
do
5
.
times
do
x
,
y
=
RandomPicker
.
item_generate
notice_all
(
room
,{
type:
'item_refresh'
,
room_id:
'default_room'
,
payload
:{
x
:x
,
y
:y
}
}
})
sleep
(
5
)
end
end
end
def
game_ready_notice
(
room
,
ghost
,
picker
)
room
.
clients
.
each
do
|
client
|
msg
=
ready_message
(
room
,
client
)
if
ghost
.
key
==
client
.
key
if
ghost
.
key
==
client
.
key
msg
[
:payload
][
:role
]
=
'ghost'
msg
[
:payload
][
:role
]
=
'ghost'
client
.
msg
(
msg
)
else
else
x
,
y
=
picker
.
pick_born_point
x
,
y
=
picker
.
pick_born_point
msg
[
:payload
][
:x
]
=
x
msg
[
:payload
][
:x
]
=
x
msg
[
:payload
][
:y
]
=
y
msg
[
:payload
][
:y
]
=
y
msg
[
:payload
][
:role
]
=
'runner'
msg
[
:payload
][
:role
]
=
'runner'
end
notice_all
(
room
,
msg
)
end
end
def
notice_all
(
room
,
msg
)
room
.
clients
.
each
do
|
client
|
client
.
msg
(
msg
)
client
.
msg
(
msg
)
end
end
end
end
def
ready_message
(
room
,
client
)
{
type:
'game_ready'
,
cid:
client
.
id
,
room_id:
room
.
name
,
payload:
{
open_id:
client
.
open_id
,
picture:
client
.
picture
,
}
}
end
end
def
clicking
(
msg
,
cl
)
def
clicking
(
msg
,
cl
)
...
@@ -177,22 +235,24 @@ module CrazyFlirt
...
@@ -177,22 +235,24 @@ module CrazyFlirt
room
=
@room_pool
[
msg
[
'rid'
]]
room
=
@room_pool
[
msg
[
'rid'
]]
seat_index
=
room
.
set_client
(
client
)
seat_index
=
room
.
set_client
(
client
)
client
.
room
=
room
client
.
room
=
room
client
.
user_info
=
{
nick_name:
msg
[
'payload'
][
'nick_name'
],
avatar_url:
msg
[
'payload'
][
'avatar_url'
]}
msg
[
'cid'
]
=
client
.
key
msg
[
'cid'
]
=
client
.
key
msg
[
'payload'
][
'seat'
]
=
seat_index
msg
[
'payload'
][
'seat'
]
=
seat_index
msg
[
'payload'
][
'members'
]
=
room
.
clients
.
map
do
|
cl
|
cl
.
user_info
end
room
.
clients
.
each
do
|
client
|
room
.
clients
.
each
do
|
client
|
client
.
msg
(
msg
)
client
.
msg
(
msg
)
end
end
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}"
room
=
GameRoom
.
new
(
key
)
#
room = GameRoom.new(key)
@room_pool
[
key
]
=
room
#
@room_pool[key] = room
client
=
@socket_pool
[
client_id
]
#
client = @socket_pool[client_id]
room
.
set_client
(
client
)
#
room.set_client(client)
client
.
msg
(
room_id:
key
)
#
client.msg(room_id: key)
end
#
end
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment