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
2e7e59be
Commit
2e7e59be
authored
May 05, 2018
by
Nick An
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add entry and start
parent
8a3ba213
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
25 deletions
+57
-25
doc.rb
be/doc.rb
+8
-0
index.rb
be/index.rb
+49
-25
No files found.
be/doc.rb
View file @
2e7e59be
...
@@ -23,3 +23,10 @@
...
@@ -23,3 +23,10 @@
y:
3
y:
3
}
}
}
}
{
type:
'game_end'
,
room_id:
'asdfadfaf'
,
payload
:{
}
}
\ No newline at end of file
be/index.rb
View file @
2e7e59be
...
@@ -21,25 +21,31 @@ module CrazyFlirt
...
@@ -21,25 +21,31 @@ module CrazyFlirt
end
end
class
Random
Role
class
Random
Picker
def
initialize
def
initialize
(
clients
)
@roles
=
%i(ghost runner runner runner)
@client
=
clients
@x_max
=
14
@y_max
=
10
@rd
=
Random
.
new
end
def
pick_ghost_index
size
=
@client
.
size
-
1
@rd
.
rand
(
0
..
size
)
end
end
def
pick_role
def
pick_born_point
size
=
@roles
.
size
-
1
x
=
@rd
.
rand
(
0
..
@x_max
)
index
=
Random
.
new
.
rand
(
0
..
size
)
y
=
@rd
.
rand
(
0
..
@y_max
)
role
=
@roles
[
index
]
[
x
,
y
]
@roles
.
delete_at
(
index
)
role
end
end
end
end
class
Client
class
Client
attr_reader
:key
attr_reader
:key
attr_accessor
:role
attr_accessor
:role
,
:room
def
initialize
(
ws
)
def
initialize
(
ws
)
@ws
=
ws
@ws
=
ws
...
@@ -47,7 +53,6 @@ module CrazyFlirt
...
@@ -47,7 +53,6 @@ module CrazyFlirt
@role
=
nil
@role
=
nil
end
end
def
msg
(
msg
)
def
msg
(
msg
)
@ws
.
send
(
msg
.
to_json
)
@ws
.
send
(
msg
.
to_json
)
end
end
...
@@ -64,13 +69,14 @@ module CrazyFlirt
...
@@ -64,13 +69,14 @@ module CrazyFlirt
attr_reader
:clients
attr_reader
:clients
def
initialize
(
name
)
def
initialize
(
name
)
@clients
=
{}
@clients
=
[]
@name
=
name
@name
=
name
@status
=
:waiting
@status
=
:waiting
end
end
def
set_client
(
client
)
def
set_client
(
client
)
@clients
[
client
.
key
]
=
client
@clients
<<
client
client
.
size
-
1
end
end
def
start!
def
start!
...
@@ -78,7 +84,8 @@ module CrazyFlirt
...
@@ -78,7 +84,8 @@ module CrazyFlirt
end
end
def
leave_client
(
key
)
def
leave_client
(
key
)
@clients
.
delete
(
key
)
idx
=
@clients
.
find_index
{
|
c
|
c
.
key
==
key
}
@clients
.
delete_at
(
idx
)
end
end
end
end
...
@@ -87,31 +94,32 @@ module CrazyFlirt
...
@@ -87,31 +94,32 @@ module CrazyFlirt
def
initialize
def
initialize
Faye
::
WebSocket
.
load_adapter
(
'thin'
)
Faye
::
WebSocket
.
load_adapter
(
'thin'
)
@default_room
=
"default_room"
@faye
=
Faye
::
WebSocket
@faye
=
Faye
::
WebSocket
@socket_pool
=
{}
@socket_pool
=
{}
@room_pool
=
{}
@room_pool
=
{}
@room_pool
[
@default_room
]
=
GameRoom
.
new
(
@default_room
)
end
end
def
call
(
env
)
def
call
(
env
)
return
unless
@faye
.
websocket?
(
env
)
return
unless
@faye
.
websocket?
(
env
)
default_room
=
"default_room"
ws
=
@faye
.
new
(
env
)
ws
=
@faye
.
new
(
env
)
client
=
::
CrazyFlirt
::
Client
.
new
(
ws
)
@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
[
:
seq
]
seq
=
message
[
:
type
]
command
(
seq
,
message
)
command
(
seq
,
message
,
client
)
end
end
ws
.
on
:close
do
|
event
|
ws
.
on
:close
do
|
event
|
@socket_pool
.
delete
(
client
.
key
)
client
.
room
.
leave_client
(
client
.
key
)
client
=
nil
puts
'closed'
puts
'closed'
end
end
ws
.
rack_response
ws
.
rack_response
end
end
...
@@ -121,10 +129,16 @@ module CrazyFlirt
...
@@ -121,10 +129,16 @@ module CrazyFlirt
send
(
command
,
*
args
)
send
(
command
,
*
args
)
end
end
def
clicking
def
start
(
msg
)
room
=
@room_pool
[
msg
[
'room_id'
]]
picker
=
RandomPicker
.
new
(
room
.
clients
)
idx
=
picker
.
pick_ghost_index
ghost
=
room
.
clients
[
idx
]
end
def
clicking
(
msg
)
room
=
@room_pool
[
msg
[
'room_id'
]]
room
=
@room_pool
[
msg
[
'room_id'
]]
room
.
clients
.
each
do
|
client
|
room
.
clients
.
each
do
|
client
|
next
if
client
.
key
==
msg
client
.
msg
(
msg
)
client
.
msg
(
msg
)
end
end
end
end
...
@@ -132,7 +146,17 @@ module CrazyFlirt
...
@@ -132,7 +146,17 @@ module CrazyFlirt
def
moving
(
msg
)
def
moving
(
msg
)
room
=
@room_pool
[
msg
[
'room_id'
]]
room
=
@room_pool
[
msg
[
'room_id'
]]
room
.
clients
.
each
do
|
client
|
room
.
clients
.
each
do
|
client
|
next
if
client
.
key
==
msg
client
.
msg
(
msg
)
end
end
def
entry
(
msg
,
client
)
room
=
@room_pool
[
msg
[
'room_id'
]]
seat_index
=
room
.
set_client
(
client
)
client
.
room
=
room
msg
[
'cid'
]
=
client
.
key
msg
[
'payload'
][
'seat'
]
=
seat_index
room
.
clients
.
each
do
|
client
|
client
.
msg
(
msg
)
client
.
msg
(
msg
)
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