-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from Crazy-Cow/feature/CD-99
feat: 소켓 인게임/아웃게임 Controller 분리
- Loading branch information
Showing
14 changed files
with
269 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Socket } from 'socket.io' | ||
import { EmitEventName } from 'socket/types/emit' | ||
import { OnEventData } from 'socket/types/on' | ||
|
||
export abstract class BaseController { | ||
socket: Socket | ||
constructor({ socket }: { socket: Socket }) { | ||
this.socket = socket | ||
} | ||
|
||
abstract register(): void | ||
abstract disconnect(): void | ||
|
||
getUserId(): string { | ||
return this.socket.id | ||
} | ||
|
||
broadcast(roomId: string, emitMessage: EmitEventName, data: unknown) { | ||
// self | ||
this.socket.emit<EmitEventName>(emitMessage, data) | ||
|
||
// the other | ||
this.socket.to(roomId).emit<EmitEventName>(emitMessage, data) | ||
} | ||
|
||
logger = (msg: string, args?: OnEventData) => { | ||
console.log( | ||
`[${this.socket.id}] ${msg} ${args ? JSON.stringify(args) : ''}` | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import OutgameController from './outgame' | ||
import IngameController from './ingame' | ||
|
||
export { OutgameController, IngameController } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Socket } from 'socket.io' | ||
import { BaseController } from './base' | ||
import { OnEventData, OnEventName } from '../types/on' | ||
import { Room } from '../../service/rooms' | ||
import { CommonMap } from '../../game/maps' | ||
import { Character } from '../../game/objects/player' | ||
|
||
function handleMove(character: Character, data: OnEventData['move']) { | ||
character.shift = data.shift | ||
character.position = data.character.position | ||
character.velocity = data.character.velocity | ||
character.isOnGround = data.character.isOnGround | ||
} | ||
|
||
class IngameController extends BaseController { | ||
socket: Socket | ||
gameMap: CommonMap | ||
|
||
register() { | ||
this.socket.on<OnEventName>('move', this.handleMove) | ||
} | ||
|
||
disconnect() { | ||
this.gameMap?.removeCharacter(this.socket.id) | ||
} | ||
|
||
private handleMove = (data: OnEventData['move']) => { | ||
const character = this.gameMap.findCharacter(this.socket.id) | ||
handleMove(character, data) | ||
} | ||
|
||
handleStartGame = (room: Room) => { | ||
this.gameMap = room.gameMap | ||
this.broadcast(room.roomId, 'game.start', { players: room.players }) | ||
|
||
room.loadGame() | ||
|
||
room.startGameLoop({ | ||
handleGameState: (data) => { | ||
this.broadcast(room.roomId, 'game.state', data) | ||
}, | ||
handleGameOver: () => { | ||
this.broadcast(room.roomId, 'game.over', undefined) | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
export default IngameController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Socket } from 'socket.io' | ||
import { BaseController } from './base' | ||
import { OnEventData, OnEventName } from '../types/on' | ||
import roomService, { Room } from '../../service/rooms' | ||
import userService from '../../service/users' | ||
import { EmitEventData } from '../types/emit' | ||
import IngameController from './ingame' | ||
|
||
function getRoomStateDto(room: Room): EmitEventData['room.changeState'] { | ||
return { | ||
roomId: room.roomId, | ||
playerCnt: room.players.length, | ||
state: room.state, | ||
maxPlayerCnt: room.maxPlayerCnt, | ||
} | ||
} | ||
|
||
// 로비 ~ 대기실 | ||
class OutgameController extends BaseController { | ||
ingameCtrl: IngameController | ||
|
||
constructor(props: { socket: Socket; ingameCtrl: IngameController }) { | ||
super(props) | ||
this.ingameCtrl = props.ingameCtrl | ||
} | ||
|
||
register() { | ||
this.socket.on<OnEventName>('room.enter', this.handleRoomEnter) | ||
this.socket.on<OnEventName>('room.leave', this.handleRoomLeave) | ||
} | ||
|
||
disconnect() { | ||
const userId = this.getUserId() | ||
const room = roomService.leaveRoom(userId) | ||
userService.removeUser(userId) | ||
|
||
if (room) { | ||
// user가 존재하던 room | ||
this.broadcastRoomState(room) | ||
} | ||
} | ||
|
||
private broadcastRoomState = (room: Room) => { | ||
const data = getRoomStateDto(room) | ||
this.broadcast(room.roomId, 'room.changeState', data) | ||
} | ||
|
||
private handleRoomEnter = (args: OnEventData['room.enter']): Room => { | ||
this.logger('room.enter', args) | ||
const userId = this.socket.id | ||
const player = userService.findUserById(userId) | ||
const room = roomService.joinRoom(player) | ||
this.socket.join(room.roomId) | ||
this.broadcastRoomState(room) | ||
|
||
if (room.state === 'playing') { | ||
this.ingameCtrl.handleStartGame(room) | ||
} | ||
|
||
return room | ||
} | ||
|
||
handleRoomLeave = (args: OnEventData['room.leave']) => { | ||
this.logger('room.leave', args) | ||
const userId = this.socket.id | ||
const room = roomService.leaveRoom(userId) | ||
this.broadcastRoomState(room) | ||
} | ||
} | ||
|
||
export default OutgameController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.