diff --git a/apps/server/src/logging/logEvent.ts b/apps/server/src/logging/logEvent.ts index 716193d..7bf8083 100644 --- a/apps/server/src/logging/logEvent.ts +++ b/apps/server/src/logging/logEvent.ts @@ -2,17 +2,19 @@ * logEvent * 共通ログ出力で利用するイベントログ関数を提供する */ -import type { LogScope } from "./logEvents"; +import type { LogPayloadByScope, LogScope } from "./logEvents"; +import type { LogRequiredFieldsByScope } from "./logEvents"; type LogEventPayload = { - event: string; - result: string; socketId?: string; roomId?: string; [key: string]: unknown; }; /** スコープ名とイベント情報を標準出力へ記録する */ -export const logEvent = (scope: LogScope, payload: LogEventPayload) => { +export const logEvent = ( + scope: TScope, + payload: LogEventPayload & LogPayloadByScope[TScope] & LogRequiredFieldsByScope[TScope] +) => { console.log(`[${scope}]`, payload); }; diff --git a/apps/server/src/logging/logEvents.ts b/apps/server/src/logging/logEvents.ts index 3712dea..c14dd1c 100644 --- a/apps/server/src/logging/logEvents.ts +++ b/apps/server/src/logging/logEvents.ts @@ -20,6 +20,186 @@ /** ログ出力で利用するスコープ名型 */ export type LogScope = (typeof logScopes)[keyof typeof logScopes]; +type ValueOf = T[keyof T]; + +/** Networkスコープで利用可能なイベント名型 */ +export type NetworkLogEvent = ValueOf; + +/** Networkスコープで利用可能な結果値型 */ +export type NetworkLogResult = + | "connected" + | "disconnected" + | "rejected_room_full" + | "rejected_duplicate" + | "ignored_invalid_payload"; + +/** GameUseCaseスコープで利用可能なイベント名型 */ +export type GameUseCaseLogEvent = + | "start-game" + | "ready-for-game" + | "game-start" + | "game-end" + | "disconnect"; + +/** GameUseCaseスコープで利用可能な結果値型 */ +export type GameUseCaseLogResult = + | "ignored_no_room" + | "ignored_already_playing" + | "ignored_room_not_found" + | "ignored_missing_room" + | "accepted" + | "received" + | "emitted" + | "player_removed"; + +/** RoomUseCaseスコープで利用可能なイベント名型 */ +export type RoomUseCaseLogEvent = + | "join-room" + | "disconnect" + | "room-update"; + +/** RoomUseCaseスコープで利用可能な結果値型 */ +export type RoomUseCaseLogResult = + | "received" + | "rejected" + | "processed" + | "emitted"; + +/** GameLoopスコープで利用可能なイベント名型 */ +export type GameLoopLogEvent = "GAME_LOOP"; + +/** GameLoopスコープで利用可能な結果値型 */ +export type GameLoopLogResult = + | "started" + | "stopped"; + +/** GameRoomSessionスコープで利用可能なイベント名型 */ +export type GameRoomSessionLogEvent = "move"; + +/** GameRoomSessionスコープで利用可能な結果値型 */ +export type GameRoomSessionLogResult = + | "ignored_player_not_found" + | "ignored_invalid_payload"; + +/** GamePlayerOperationServiceスコープで利用可能なイベント名型 */ +export type GamePlayerOperationServiceLogEvent = + | "PLAYER_MOVE" + | "PLAYER_REMOVE"; + +/** GamePlayerOperationServiceスコープで利用可能な結果値型 */ +export type GamePlayerOperationServiceLogResult = + | "ignored_player_not_in_session" + | "session_disposed_empty_room"; + +/** GameSessionLifecycleServiceスコープで利用可能なイベント名型 */ +export type GameSessionLifecycleServiceLogEvent = "SESSION_START"; + +/** GameSessionLifecycleServiceスコープで利用可能な結果値型 */ +export type GameSessionLifecycleServiceLogResult = + | "ignored_already_running" + | "started"; + +/** RoomJoinServiceスコープで利用可能なイベント名型 */ +export type RoomJoinServiceLogEvent = + | "ROOM_CREATE" + | "PLAYER_JOIN"; + +/** RoomJoinServiceスコープで利用可能な結果値型 */ +export type RoomJoinServiceLogResult = + | "created" + | "ignored_duplicate" + | "ignored_room_full" + | "joined"; + +/** RoomExitServiceスコープで利用可能なイベント名型 */ +export type RoomExitServiceLogEvent = + | "PLAYER_LEAVE" + | "ROOM_DELETE" + | "OWNER_TRANSFER"; + +/** RoomExitServiceスコープで利用可能な結果値型 */ +export type RoomExitServiceLogResult = + | "removed" + | "deleted" + | "transferred"; + +/** スコープごとの event/result 型契約 */ +export type LogPayloadByScope = { + [logScopes.NETWORK]: { + event: NetworkLogEvent; + result: NetworkLogResult; + }; + [logScopes.GAME_USE_CASE]: { + event: GameUseCaseLogEvent; + result: GameUseCaseLogResult; + }; + [logScopes.ROOM_USE_CASE]: { + event: RoomUseCaseLogEvent; + result: RoomUseCaseLogResult; + }; + [logScopes.GAME_LOOP]: { + event: GameLoopLogEvent; + result: GameLoopLogResult; + }; + [logScopes.GAME_ROOM_SESSION]: { + event: GameRoomSessionLogEvent; + result: GameRoomSessionLogResult; + }; + [logScopes.GAME_PLAYER_OPERATION_SERVICE]: { + event: GamePlayerOperationServiceLogEvent; + result: GamePlayerOperationServiceLogResult; + }; + [logScopes.GAME_SESSION_LIFECYCLE_SERVICE]: { + event: GameSessionLifecycleServiceLogEvent; + result: GameSessionLifecycleServiceLogResult; + }; + [logScopes.ROOM_JOIN_SERVICE]: { + event: RoomJoinServiceLogEvent; + result: RoomJoinServiceLogResult; + }; + [logScopes.ROOM_EXIT_SERVICE]: { + event: RoomExitServiceLogEvent; + result: RoomExitServiceLogResult; + }; +}; + +/** スコープごとの必須追加フィールド型契約 */ +export type LogRequiredFieldsByScope = { + [logScopes.NETWORK]: { + socketId: string; + }; + [logScopes.GAME_USE_CASE]: { + socketId?: string; + roomId?: string; + }; + [logScopes.ROOM_USE_CASE]: { + socketId: string; + roomId?: string; + }; + [logScopes.GAME_LOOP]: { + roomId: string; + }; + [logScopes.GAME_ROOM_SESSION]: { + roomId: string; + socketId: string; + }; + [logScopes.GAME_PLAYER_OPERATION_SERVICE]: { + socketId: string; + roomId?: string; + }; + [logScopes.GAME_SESSION_LIFECYCLE_SERVICE]: { + roomId: string; + }; + [logScopes.ROOM_JOIN_SERVICE]: { + roomId: string; + socketId: string; + }; + [logScopes.ROOM_EXIT_SERVICE]: { + roomId: string; + socketId: string; + }; +}; + /** GameUseCaseログで利用するイベント名定数 */ export const gameUseCaseLogEvents = { START_GAME: protocol.SocketEvents.START_GAME,