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
84143c7e
Commit
84143c7e
authored
May 05, 2018
by
Nick An
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mass init
parent
29b570c6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
160 additions
and
0 deletions
+160
-0
index.rb
be/index.rb
+150
-0
webserver.rb
be/webserver.rb
+10
-0
No files found.
be/index.rb
0 → 100644
View file @
84143c7e
require
'faye/websocket'
require
'rack'
require
'thin'
require
'json'
module
CrazyFlirt
class
RandomKeyGenerator
WORDS
=
%w(a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 0)
SIZE
=
WORDS
.
size
def
self
.
key
words
=
::
CrazyFlirt
::
RandomKeyGenerator
::
WORDS
size
=
::
CrazyFlirt
::
RandomKeyGenerator
::
SIZE
-
1
random_key
=
''
random
=
Random
.
new
5
.
times
do
random_key
+=
words
[
random
.
rand
(
0
..
size
)]
end
Time
.
now
.
to_s
.
to_i
+
random_key
end
end
class
RandomRole
def
initialize
@roles
=
%i(ghost runner runner runner)
end
def
pick_role
size
=
@roles
.
size
-
1
index
=
Random
.
new
.
rand
(
0
..
size
)
role
=
@roles
[
index
]
@roles
.
delete_at
(
index
)
role
end
end
class
Client
attr_reader
:key
attr_accessor
:role
def
initialize
(
ws
)
@ws
=
ws
@key
=
generate_key
@role
=
nil
end
def
msg
(
msg
)
@ws
.
send
(
msg
.
to_json
)
end
private
def
generate_key
CrazyFlirt
::
RandomKeyGenerator
.
key
end
end
class
GameRoom
attr_reader
:clients
def
initialize
(
name
)
@clients
=
{}
@name
=
name
@status
=
:waiting
end
def
set_client
(
client
)
@clients
[
client
.
key
]
=
client
end
def
start!
@status
=
:start
end
def
leave_client
(
key
)
@clients
.
delete
(
key
)
end
end
class
SocketEngine
def
initialize
Faye
::
WebSocket
.
load_adapter
(
'thin'
)
@faye
=
Faye
::
WebSocket
@socket_pool
=
{}
@room_pool
=
{}
end
def
call
(
env
)
return
unless
@faye
.
websocket?
(
env
)
ws
=
@faye
.
new
(
env
)
ws
.
on
:message
do
|
event
|
ws
.
send
(
event
.
data
)
message
=
JSON
.
parse
(
event
.
data
)
seq
=
message
[
:seq
]
command
(
seq
,
message
)
end
ws
.
on
:close
do
|
event
|
puts
'closed'
end
ws
.
rack_response
end
private
def
commend
(
command
,
*
args
)
send
(
command
,
*
args
)
end
def
clicking
room
=
@room_pool
[
msg
[
'room_id'
]]
room
.
clients
.
each
do
|
client
|
next
if
client
.
key
==
msg
client
.
msg
(
msg
)
end
end
def
moving
(
msg
)
room
=
@room_pool
[
msg
[
'room_id'
]]
room
.
clients
.
each
do
|
client
|
next
if
client
.
key
==
msg
client
.
msg
(
msg
)
end
end
def
create_room
(
msg
)
client_id
=
msg
[
'cid'
]
key
=
"room_
#{
::
CrazyFlirt
::
RandomKeyGenerator
.
key
}
"
room
=
GameRoom
.
new
(
key
)
@room_pool
[
key
]
=
room
client
=
@socket_pool
[
client_id
]
room
.
set_client
(
client
)
client
.
msg
(
room_id:
key
)
end
end
end
Rack
::
Handler
.
get
(
'thin'
).
run
(
CrazyFlirt
::
SocketEngine
.
new
,
Host
:
'192.168.50.118'
,
Port
:
80
)
\ No newline at end of file
be/webserver.rb
0 → 100644
View file @
84143c7e
require
'sinatra'
set
:bind
,
'192.168.50.118'
set
:port
,
1234
get
'/hello'
do
'hello'
end
\ No newline at end of file
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