Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
sejong25
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Tear of Sejong
sejong25
Commits
a8aa3e7f
Commit
a8aa3e7f
authored
Jul 04, 2019
by
18신대성
Committed by
16이지혜
Jul 04, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
게임룸 만들고 접속하게 만듬
parent
dadf43ce
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
11 deletions
+50
-11
main.js
js/main.js
+2
-2
server.js
server.js
+48
-9
No files found.
js/main.js
View file @
a8aa3e7f
...
@@ -48,9 +48,9 @@ function update()
...
@@ -48,9 +48,9 @@ function update()
// client side
// client side
var
socket
=
io
.
connect
();
var
socket
=
io
.
connect
();
socket
.
emit
(
'
idRequest
'
);
socket
.
on
(
'
idSet
'
,
function
(
msg
)
// {str, num}
socket
.
on
(
'
idSet
'
,
function
(
msg
)
// {str, num}
{
{
console
.
log
(
msg
.
str
);
console
.
log
(
msg
.
str
);
this
.
playerNum
=
msg
.
num
;
this
.
playerNum
=
msg
.
num
;
});
});
socket
.
emit
(
'
idRequest
'
);
\ No newline at end of file
\ No newline at end of file
server.js
View file @
a8aa3e7f
...
@@ -18,29 +18,66 @@ server.listen(80, function() {
...
@@ -18,29 +18,66 @@ server.listen(80, function() {
var
GameServer
=
GameServer
||
{};
var
GameServer
=
GameServer
||
{};
GameServer
.
waitingRoom
=
[];
GameServer
.
currentPlayer
=
[];
GameServer
.
playingRoom
=
[];
GameServer
.
getPlayerNumber
=
function
()
GameServer
.
getPlayerNumber
=
function
()
{
{
do
do
{
{
var
num
=
Math
.
floor
(
Math
.
random
()
*
1000
+
1
);
var
num
=
Math
.
floor
(
Math
.
random
()
*
1000
+
1
);
if
(
!
this
.
waitingRoom
.
includes
(
num
))
return
num
;
if
(
!
this
.
currentPlayer
.
includes
(
num
))
return
num
;
}
while
(
true
)
}
while
(
true
)
}
}
GameServer
.
findPlayer
=
function
(
playerId
)
GameServer
.
findPlayer
=
function
(
playerId
)
{
{
var
idx
=
this
.
waitingRoom
.
findIndex
(
function
(
element
)
var
idx
=
this
.
currentPlayer
.
findIndex
(
function
(
element
)
{
{
return
element
.
id
===
socket
;
return
element
.
id
===
socket
;
});
});
if
(
idx
!=
-
1
)
return
this
.
waitingRoom
[
idx
];
if
(
idx
!=
-
1
)
return
this
.
currentPlayer
[
idx
];
else
else
{
{
console
.
log
(
'
[ERR] wrong playerId to find
'
);
console
.
log
(
'
[ERR] wrong playerId to find
'
);
return
null
;
return
null
;
}
}
}
}
GameServer
.
nextRoomNumber
=
0
;
GameServer
.
makeRoom
=
function
()
{
var
roomOption
=
{
roomNum
:
GameServer
.
nextRoomNumber
++
,
maxPlayer
:
25
,
currentPlayer
:
[]
}
this
.
playingRoom
.
push
(
roomOption
);
console
.
log
(
'
new room made, roomCount:
'
+
this
.
playingRoom
.
length
);
return
this
.
playingRoom
.
length
-
1
;
}
GameServer
.
enterRoom
=
function
(
roomIdx
,
playerData
)
{
this
.
playingRoom
[
roomIdx
].
currentPlayer
.
push
(
playerData
);
console
.
log
(
playerData
.
id
+
'
entered to room#
'
+
this
.
playingRoom
[
roomIdx
].
roomNum
);
return
this
.
playingRoom
[
roomIdx
];
}
GameServer
.
enterEmptyRoom
=
function
(
playerData
)
{
var
toEnter
=
-
1
;
for
(
let
i
=
0
;
i
<
this
.
playingRoom
.
length
;
i
++
)
{
if
(
this
.
playingRoom
[
i
].
currentPlayer
.
length
<
this
.
playingRoom
[
i
].
maxPlayer
)
{
toEnter
=
i
;
break
;
}
}
if
(
toEnter
===
-
1
)
{
toEnter
=
this
.
makeRoom
();
}
return
this
.
enterRoom
(
toEnter
,
playerData
);
}
// 클라이언트 요청에 대한 콜백 정의
// 클라이언트 요청에 대한 콜백 정의
io
.
on
(
'
connection
'
,
function
(
socket
)
io
.
on
(
'
connection
'
,
function
(
socket
)
...
@@ -49,28 +86,30 @@ io.on('connection', function(socket)
...
@@ -49,28 +86,30 @@ io.on('connection', function(socket)
var
playerSocket
=
var
playerSocket
=
{
{
id
:
GameServer
.
getPlayerNumber
(),
id
:
GameServer
.
getPlayerNumber
(),
nickname
:
'
게스트
'
,
socketId
:
socket
socketId
:
socket
}
}
GameServer
.
waitingRoom
.
push
(
playerSocket
);
GameServer
.
currentPlayer
.
push
(
playerSocket
);
console
.
log
(
'
client request, id:
'
+
playerSocket
.
id
);
console
.
log
(
'
client request, id:
'
+
playerSocket
.
id
);
socket
.
emit
(
'
idSet
'
,
socket
.
emit
(
'
idSet
'
,
{
{
str
:
'
your number is
'
+
playerSocket
.
id
,
str
:
'
your number is
'
+
playerSocket
.
id
+
'
, your nickname is
'
+
playerSocket
.
nickname
,
num
:
playerSocket
.
id
num
:
playerSocket
.
id
});
});
GameServer
.
enterEmptyRoom
(
playerSocket
);
});
});
socket
.
on
(
'
disconnect
'
,
function
(
reason
)
socket
.
on
(
'
disconnect
'
,
function
(
reason
)
{
{
var
idxToDel
=
GameServer
.
waitingRoom
.
findIndex
(
function
(
element
)
var
idxToDel
=
GameServer
.
currentPlayer
.
findIndex
(
function
(
element
)
{
{
return
element
.
socketId
===
socket
;
return
element
.
socketId
===
socket
;
}
}
);
);
if
(
idxToDel
!=
-
1
)
if
(
idxToDel
!=
-
1
)
{
{
console
.
log
(
'
client disconnected, id:
'
+
GameServer
.
waitingRoom
[
idxToDel
].
id
+
'
, reason:
'
+
reason
);
console
.
log
(
'
client disconnected, id:
'
+
GameServer
.
currentPlayer
[
idxToDel
].
id
+
'
, reason:
'
+
reason
);
GameServer
.
waitingRoom
.
splice
(
idxToDel
,
1
);
GameServer
.
currentPlayer
.
splice
(
idxToDel
,
1
);
}
}
});
});
});
});
\ 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