Toggle navigation
Toggle navigation
This project
Loading...
Sign in
강동현
/
nodejs-game
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
강동현
2021-05-26 21:54:28 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
ab43ac703c3148557cd9becbe796aa56c73cbf22
ab43ac70
1 parent
d628b33f
RoomDescription, RoomInfo 수정
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
40 additions
and
31 deletions
server/message/handler/roomJoinHandler.ts
server/message/types.ts
server/room/Room.ts
server/room/RoomManager.ts
server/room/types.ts
server/message/handler/roomJoinHandler.ts
View file @
ab43ac7
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
RoomManager
}
from
"../../room/RoomManager"
;
import
{
RoomInfo
}
from
"../../room/types"
;
import
{
User
}
from
"../../user/User"
;
import
{
MessageResponse
,
Room
Info
,
Room
JoinMessage
}
from
"../types"
;
import
{
MessageResponse
,
RoomJoinMessage
}
from
"../types"
;
export
function
roomJoinHandler
(
user
:
User
,
...
...
@@ -9,8 +10,8 @@ export function roomJoinHandler(
):
MessageResponse
<
RoomInfo
>
{
const
room
=
RoomManager
.
instance
().
get
(
message
.
uuid
);
if
(
room
!==
undefined
)
{
const
roomInfo
=
room
.
connect
(
user
);
return
{
ok
:
roomInfo
!==
undefined
,
result
:
roomInfo
};
room
.
connect
(
user
);
return
{
ok
:
user
.
room
!==
undefined
,
result
:
user
.
room
?.
getInfo
()
};
}
return
{
ok
:
false
};
}
...
...
server/message/types.ts
View file @
ab43ac7
import
{
RoomD
ata
}
from
"../room/types"
;
import
{
RoomD
escription
}
from
"../room/types"
;
import
{
UserData
}
from
"../user/types"
;
export
interface
Message
{
...
...
@@ -39,11 +39,11 @@ export class LoginMessage implements Message {
*/
export
class
RoomListMessage
implements
Message
{
readonly
type
=
MessageType
.
ROOM_LIST
;
constructor
(
public
rooms
:
RoomD
ata
[])
{}
constructor
(
public
rooms
:
RoomD
escription
[])
{}
}
/**
* 클라 -> 서버 -> RoomInfo
Message
* 클라 -> 서버 -> RoomInfo
* 방에 접속합니다.
*/
export
class
RoomJoinMessage
implements
Message
{
...
...
@@ -93,11 +93,3 @@ export class MessageType {
static
readonly
ROOM_USER_UPDATE
=
"room_user_update"
;
static
readonly
ROOM_CHAT
=
"room_chat"
;
}
/**
* 방의 정보를 담고 있습니다.
* @param userdata 현재 방에 접속 중인 유저 목록입니다.
*/
export
interface
RoomInfo
{
userdata
:
UserData
[];
}
...
...
server/room/Room.ts
View file @
ab43ac7
import
{
Connection
}
from
"../connection/Connection"
;
import
{
v4
as
uuidv4
}
from
"uuid"
;
import
{
RoomD
ata
}
from
"./types"
;
import
{
RoomD
escription
,
RoomInfo
}
from
"./types"
;
import
{
Message
,
RoomChatMessage
,
RoomInfo
,
RoomUserUpdateMessage
,
}
from
"../message/types"
;
import
{
UserData
}
from
"../user/types"
;
...
...
@@ -26,23 +25,15 @@ export class Room {
this
.
maxUsers
=
maxUsers
;
}
public
connect
(
user
:
User
):
RoomInfo
|
undefine
d
{
public
connect
(
user
:
User
):
voi
d
{
if
(
this
.
users
.
includes
(
user
)
||
this
.
users
.
length
>=
this
.
maxUsers
)
{
return
undefined
;
return
;
}
this
.
broadcast
(
new
RoomUserUpdateMessage
(
"added"
,
user
.
getData
()));
this
.
users
.
push
(
user
);
user
.
room
=
this
;
// TODO: 더 나은 관리
var
users
:
UserData
[]
=
[];
this
.
users
.
forEach
((
u
)
=>
{
if
(
user
!==
u
)
{
users
.
push
(
u
.
getData
());
}
});
return
{
userdata
:
users
};
}
public
disconnect
(
user
:
User
):
void
{
...
...
@@ -59,7 +50,7 @@ export class Room {
this
.
broadcast
(
new
RoomChatMessage
(
message
,
user
.
username
),
user
);
}
public
getD
ata
():
RoomData
{
public
getD
escription
():
RoomDescription
{
return
{
uuid
:
this
.
uuid
,
name
:
this
.
name
,
...
...
@@ -68,6 +59,16 @@ export class Room {
};
}
public
getInfo
():
RoomInfo
{
var
users
:
UserData
[]
=
this
.
users
.
map
((
u
)
=>
u
.
getData
());
return
{
uuid
:
this
.
uuid
,
name
:
this
.
name
,
maxUsers
:
this
.
maxUsers
,
users
:
users
,
};
}
public
broadcast
(
message
:
Message
,
except
?:
User
):
void
{
this
.
users
.
forEach
((
u
)
=>
{
if
(
u
!==
except
)
{
...
...
server/room/RoomManager.ts
View file @
ab43ac7
import
{
Connection
}
from
"../connection/Connection"
;
import
{
RoomListMessage
}
from
"../message/types"
;
import
{
Room
}
from
"./Room"
;
import
{
RoomD
ata
}
from
"./types"
;
import
{
RoomD
escription
}
from
"./types"
;
export
class
RoomManager
{
private
static
_instance
:
RoomManager
;
...
...
@@ -36,9 +36,9 @@ export class RoomManager {
}
public
sendList
(
connection
:
Connection
):
void
{
var
roomData
:
RoomD
ata
[]
=
[];
var
roomData
:
RoomD
escription
[]
=
[];
this
.
rooms
.
forEach
((
room
)
=>
{
roomData
.
push
(
room
.
getD
ata
());
roomData
.
push
(
room
.
getD
escription
());
});
connection
.
send
(
new
RoomListMessage
(
roomData
));
...
...
server/room/types.ts
View file @
ab43ac7
export
interface
RoomData
{
import
{
UserData
}
from
"../user/types"
;
/**
* 방 리스트에서 사용됩니다.
*/
export
interface
RoomDescription
{
uuid
:
string
;
name
:
string
;
currentUsers
:
number
;
maxUsers
:
number
;
}
/**
* 방에 접속했을 때 사용됩니다.
*/
export
interface
RoomInfo
{
uuid
:
string
;
name
:
string
;
maxUsers
:
number
;
users
:
UserData
[];
}
...
...
Please
register
or
login
to post a comment