diff --git a/webclient/src/api/ModeratorService.tsx b/webclient/src/api/ModeratorService.tsx index 0adb00669..28668c2e5 100644 --- a/webclient/src/api/ModeratorService.tsx +++ b/webclient/src/api/ModeratorService.tsx @@ -1,4 +1,5 @@ import { ModeratorCommands } from 'websocket'; +import { LogFilters } from 'types'; export default class ModeratorService { static banFromServer(minutes: number, userName?: string, address?: string, reason?: string, @@ -18,7 +19,7 @@ export default class ModeratorService { ModeratorCommands.getWarnList(modName, userName, userClientid); } - static viewLogHistory(filters): void { + static viewLogHistory(filters: LogFilters): void { ModeratorCommands.viewLogHistory(filters); } diff --git a/webclient/src/containers/Logs/Logs.tsx b/webclient/src/containers/Logs/Logs.tsx index 906d283bf..a34a7b008 100644 --- a/webclient/src/containers/Logs/Logs.tsx +++ b/webclient/src/containers/Logs/Logs.tsx @@ -7,6 +7,7 @@ import { ModeratorService } from 'api'; import { AuthGuard, ModGuard } from 'components'; import { SearchForm } from 'forms'; import { ServerDispatch, ServerSelectors, ServerStateLogs } from 'store'; +import { LogFilters } from 'types'; import LogResults from './LogResults'; import './Logs.css'; @@ -24,7 +25,7 @@ class Logs extends Component { ServerDispatch.clearLogs(); } - onSubmit(fields) { + onSubmit(fields: LogFilters) { const trimmedFields: any = this.trimFields(fields); const { userName, ipAddress, gameName, gameId, message, logLocation } = trimmedFields; diff --git a/webclient/src/types/index.ts b/webclient/src/types/index.ts index 24b3c27a4..8a6147b83 100644 --- a/webclient/src/types/index.ts +++ b/webclient/src/types/index.ts @@ -12,3 +12,5 @@ export * from './forms'; export * from './message'; export * from './settings'; export * from './languages'; +export * from './logs'; +export * from './session'; diff --git a/webclient/src/types/logs.ts b/webclient/src/types/logs.ts new file mode 100644 index 000000000..4c2f0364e --- /dev/null +++ b/webclient/src/types/logs.ts @@ -0,0 +1,8 @@ +export interface LogFilters { + userName: string; + ipAddress: string; + gameName: string; + gameId: string; + message: string; + logLocation: string; +} diff --git a/webclient/src/types/session.ts b/webclient/src/types/session.ts new file mode 100644 index 000000000..1b49616c5 --- /dev/null +++ b/webclient/src/types/session.ts @@ -0,0 +1,7 @@ +export enum NotificationType { + UNKNOWN = 0, + PROMOTED = 1, + WARNING = 2, + IDLEWARNING = 3, + CUSTOM = 4, +}; diff --git a/webclient/src/websocket/commands/admin/adjustMod.ts b/webclient/src/websocket/commands/admin/adjustMod.ts index fed82eeaa..5e8a83153 100644 --- a/webclient/src/websocket/commands/admin/adjustMod.ts +++ b/webclient/src/websocket/commands/admin/adjustMod.ts @@ -3,10 +3,7 @@ import { AdminPersistence } from '../../persistence'; export function adjustMod(userName: string, shouldBeMod?: boolean, shouldBeJudge?: boolean): void { const command = webClient.protobuf.controller.Command_AdjustMod.create({ userName, shouldBeMod, shouldBeJudge }); - - const sc = webClient.protobuf.controller.AdminCommand.create({ - '.Command_AdjustMod.ext': command - }); + const sc = webClient.protobuf.controller.AdminCommand.create({ '.Command_AdjustMod.ext': command }); webClient.protobuf.sendAdminCommand(sc, (raw) => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/admin/reloadConfig.ts b/webclient/src/websocket/commands/admin/reloadConfig.ts index 6dbae9712..16d2a574b 100644 --- a/webclient/src/websocket/commands/admin/reloadConfig.ts +++ b/webclient/src/websocket/commands/admin/reloadConfig.ts @@ -3,10 +3,7 @@ import { AdminPersistence } from '../../persistence'; export function reloadConfig(): void { const command = webClient.protobuf.controller.Command_ReloadConfig.create(); - - const sc = webClient.protobuf.controller.AdminCommand.create({ - '.Command_ReloadConfig.ext': command - }); + const sc = webClient.protobuf.controller.AdminCommand.create({ '.Command_ReloadConfig.ext': command }); webClient.protobuf.sendAdminCommand(sc, (raw) => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/admin/shutdownServer.ts b/webclient/src/websocket/commands/admin/shutdownServer.ts index 090a25187..0624e823d 100644 --- a/webclient/src/websocket/commands/admin/shutdownServer.ts +++ b/webclient/src/websocket/commands/admin/shutdownServer.ts @@ -3,10 +3,7 @@ import { AdminPersistence } from '../../persistence'; export function shutdownServer(reason: string, minutes: number): void { const command = webClient.protobuf.controller.Command_ShutdownServer.create({ reason, minutes }); - - const sc = webClient.protobuf.controller.AdminCommand.create({ - '.Command_ShutdownServer.ext': command - }); + const sc = webClient.protobuf.controller.AdminCommand.create({ '.Command_ShutdownServer.ext': command }); webClient.protobuf.sendAdminCommand(sc, (raw) => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/admin/updateServerMessage.ts b/webclient/src/websocket/commands/admin/updateServerMessage.ts index 00cea46d2..cb025f5a9 100644 --- a/webclient/src/websocket/commands/admin/updateServerMessage.ts +++ b/webclient/src/websocket/commands/admin/updateServerMessage.ts @@ -3,10 +3,7 @@ import { AdminPersistence } from '../../persistence'; export function updateServerMessage(): void { const command = webClient.protobuf.controller.Command_UpdateServerMessage.create(); - - const sc = webClient.protobuf.controller.AdminCommand.create({ - '.Command_UpdateServerMessage.ext': command - }); + const sc = webClient.protobuf.controller.AdminCommand.create({ '.Command_UpdateServerMessage.ext': command }); webClient.protobuf.sendAdminCommand(sc, (raw) => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/moderator/banFromServer.ts b/webclient/src/websocket/commands/moderator/banFromServer.ts index fc19cdcca..028eb4606 100644 --- a/webclient/src/websocket/commands/moderator/banFromServer.ts +++ b/webclient/src/websocket/commands/moderator/banFromServer.ts @@ -6,10 +6,7 @@ export function banFromServer(minutes: number, userName?: string, address?: stri const command = webClient.protobuf.controller.Command_BanFromServer.create({ minutes, userName, address, reason, visibleReason, clientid, removeMessages }); - - const sc = webClient.protobuf.controller.ModeratorCommand.create({ - '.Command_BanFromServer.ext': command - }); + const sc = webClient.protobuf.controller.ModeratorCommand.create({ '.Command_BanFromServer.ext': command }); webClient.protobuf.sendModeratorCommand(sc, (raw) => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/moderator/getBanHistory.ts b/webclient/src/websocket/commands/moderator/getBanHistory.ts index 9446a1f56..a75fe51e6 100644 --- a/webclient/src/websocket/commands/moderator/getBanHistory.ts +++ b/webclient/src/websocket/commands/moderator/getBanHistory.ts @@ -3,10 +3,7 @@ import { ModeratorPersistence } from '../../persistence'; export function getBanHistory(userName: string): void { const command = webClient.protobuf.controller.Command_GetBanHistory.create({ userName }); - - const sc = webClient.protobuf.controller.ModeratorCommand.create({ - '.Command_GetBanHistory.ext': command - }); + const sc = webClient.protobuf.controller.ModeratorCommand.create({ '.Command_GetBanHistory.ext': command }); webClient.protobuf.sendModeratorCommand(sc, (raw) => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/moderator/getWarnHistory.ts b/webclient/src/websocket/commands/moderator/getWarnHistory.ts index be6d7fb3c..0a0c4d549 100644 --- a/webclient/src/websocket/commands/moderator/getWarnHistory.ts +++ b/webclient/src/websocket/commands/moderator/getWarnHistory.ts @@ -3,10 +3,7 @@ import { ModeratorPersistence } from '../../persistence'; export function getWarnHistory(userName: string): void { const command = webClient.protobuf.controller.Command_GetWarnHistory.create({ userName }); - - const sc = webClient.protobuf.controller.ModeratorCommand.create({ - '.Command_GetWarnHistory.ext': command - }); + const sc = webClient.protobuf.controller.ModeratorCommand.create({ '.Command_GetWarnHistory.ext': command }); webClient.protobuf.sendModeratorCommand(sc, (raw) => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/moderator/getWarnList.ts b/webclient/src/websocket/commands/moderator/getWarnList.ts index 5d7fc775c..83be78c89 100644 --- a/webclient/src/websocket/commands/moderator/getWarnList.ts +++ b/webclient/src/websocket/commands/moderator/getWarnList.ts @@ -3,10 +3,7 @@ import { ModeratorPersistence } from '../../persistence'; export function getWarnList(modName: string, userName: string, userClientid: string): void { const command = webClient.protobuf.controller.Command_GetWarnList.create({ modName, userName, userClientid }); - - const sc = webClient.protobuf.controller.ModeratorCommand.create({ - '.Command_GetWarnList.ext': command - }); + const sc = webClient.protobuf.controller.ModeratorCommand.create({ '.Command_GetWarnList.ext': command }); webClient.protobuf.sendModeratorCommand(sc, (raw) => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/moderator/viewLogHistory.ts b/webclient/src/websocket/commands/moderator/viewLogHistory.ts index cc4924a6d..29e3c4bd6 100644 --- a/webclient/src/websocket/commands/moderator/viewLogHistory.ts +++ b/webclient/src/websocket/commands/moderator/viewLogHistory.ts @@ -1,12 +1,10 @@ import webClient from '../../WebClient'; import { ModeratorPersistence } from '../../persistence'; +import { LogFilters } from 'types'; -export function viewLogHistory(filters): void { +export function viewLogHistory(filters: LogFilters): void { const command = webClient.protobuf.controller.Command_ViewLogHistory.create(filters); - - const sc = webClient.protobuf.controller.ModeratorCommand.create({ - '.Command_ViewLogHistory.ext': command - }); + const sc = webClient.protobuf.controller.ModeratorCommand.create({ '.Command_ViewLogHistory.ext': command }); webClient.protobuf.sendModeratorCommand(sc, (raw) => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/moderator/warnUser.ts b/webclient/src/websocket/commands/moderator/warnUser.ts index d69193e6e..c9c3a2155 100644 --- a/webclient/src/websocket/commands/moderator/warnUser.ts +++ b/webclient/src/websocket/commands/moderator/warnUser.ts @@ -3,10 +3,7 @@ import { ModeratorPersistence } from '../../persistence'; export function warnUser(userName: string, reason: string, clientid?: string, removeMessage?: boolean): void { const command = webClient.protobuf.controller.Command_BanFromServer.create({ userName, reason, clientid, removeMessage }); - - const sc = webClient.protobuf.controller.ModeratorCommand.create({ - '.Command_WarnUser.ext': command - }); + const sc = webClient.protobuf.controller.ModeratorCommand.create({ '.Command_WarnUser.ext': command }); webClient.protobuf.sendModeratorCommand(sc, (raw) => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/room/RoomCommands.spec.ts b/webclient/src/websocket/commands/room/RoomCommands.spec.ts deleted file mode 100644 index 603f450d6..000000000 --- a/webclient/src/websocket/commands/room/RoomCommands.spec.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { RoomPersistence } from '../../persistence'; -import webClient from '../../WebClient'; - -import { leaveRoom, roomSay } from './'; - -describe.skip('RoomCommands', () => { - const roomId = 1; - let sendRoomCommandSpy; - - beforeEach(() => { - sendRoomCommandSpy = jest.spyOn(webClient.protobuf, 'sendRoomCommand').mockImplementation(() => {}); - - webClient.protobuf.controller.RoomCommand = { create: args => args }; - }); - - afterEach(() => { - jest.restoreAllMocks(); - }); - - describe('roomSay', () => { - beforeEach(() => { - webClient.protobuf.controller.Command_RoomSay = { create: args => args }; - }); - - it('should call protobuf controller methods and sendCommand', () => { - const message = ' message '; - - roomSay(roomId, message); - - expect(webClient.protobuf.sendRoomCommand).toHaveBeenCalled(); - expect(webClient.protobuf.sendRoomCommand).toHaveBeenCalledWith(roomId, { - '.Command_RoomSay.ext': { message: message.trim() } - }); - }); - - it('should not call sendRoomCommand if trimmed message is empty', () => { - const message = ' '; - - roomSay(roomId, message); - - expect(webClient.protobuf.sendRoomCommand).not.toHaveBeenCalled(); - }); - }); - - describe('leaveRoom', () => { - beforeEach(() => { - webClient.protobuf.controller.Command_LeaveRoom = { create: () => ({}) }; - }); - - it('should call protobuf controller methods and sendCommand', () => { - leaveRoom(roomId); - - expect(webClient.protobuf.sendRoomCommand).toHaveBeenCalled(); - expect(webClient.protobuf.sendRoomCommand).toHaveBeenCalledWith( - roomId, - { '.Command_LeaveRoom.ext': {} }, - expect.any(Function) - ); - }); - - it('should call RoomPersistence.leaveRoom if RespOk', () => { - const RespOk = 'ok'; - webClient.protobuf.controller.Response = { ResponseCode: { RespOk } }; - sendRoomCommandSpy.mockImplementation((_, __, callback) => { - callback({ responseCode: RespOk }) - }); - - jest.spyOn(RoomPersistence, 'leaveRoom').mockImplementation(() => {}); - - leaveRoom(roomId); - - expect(RoomPersistence.leaveRoom).toHaveBeenCalledWith(roomId); - }); - }); -}); diff --git a/webclient/src/websocket/commands/room/leaveRoom.ts b/webclient/src/websocket/commands/room/leaveRoom.ts index 3d2f9c288..477ee39a0 100644 --- a/webclient/src/websocket/commands/room/leaveRoom.ts +++ b/webclient/src/websocket/commands/room/leaveRoom.ts @@ -2,11 +2,8 @@ import { RoomPersistence } from '../../persistence'; import webClient from '../../WebClient'; export function leaveRoom(roomId: number): void { - const CmdLeaveRoom = webClient.protobuf.controller.Command_LeaveRoom.create(); - - const rc = webClient.protobuf.controller.RoomCommand.create({ - '.Command_LeaveRoom.ext': CmdLeaveRoom - }); + const command = webClient.protobuf.controller.Command_LeaveRoom.create(); + const rc = webClient.protobuf.controller.RoomCommand.create({ '.Command_LeaveRoom.ext': command }); webClient.protobuf.sendRoomCommand(roomId, rc, (raw) => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/room/roomSay.ts b/webclient/src/websocket/commands/room/roomSay.ts index 7d3995be8..c06abd3ab 100644 --- a/webclient/src/websocket/commands/room/roomSay.ts +++ b/webclient/src/websocket/commands/room/roomSay.ts @@ -7,13 +7,8 @@ export function roomSay(roomId: number, message: string): void { return; } - const CmdRoomSay = webClient.protobuf.controller.Command_RoomSay.create({ - 'message': trimmed - }); - - const rc = webClient.protobuf.controller.RoomCommand.create({ - '.Command_RoomSay.ext': CmdRoomSay - }); + const command = webClient.protobuf.controller.Command_RoomSay.create({ 'message': trimmed }); + const rc = webClient.protobuf.controller.RoomCommand.create({ '.Command_RoomSay.ext': command }); webClient.protobuf.sendRoomCommand(roomId, rc); } diff --git a/webclient/src/websocket/commands/session/accountEdit.ts b/webclient/src/websocket/commands/session/accountEdit.ts index 582a31b4a..f9210cb9c 100644 --- a/webclient/src/websocket/commands/session/accountEdit.ts +++ b/webclient/src/websocket/commands/session/accountEdit.ts @@ -3,10 +3,7 @@ import { SessionPersistence } from '../../persistence'; export function accountEdit(passwordCheck: string, realName?: string, email?: string, country?: string): void { const command = webClient.protobuf.controller.Command_AccountEdit.create({ passwordCheck, realName, email, country }); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_AccountEdit.ext': command - }); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_AccountEdit.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/session/accountImage.ts b/webclient/src/websocket/commands/session/accountImage.ts index 243c43ccc..4add7db13 100644 --- a/webclient/src/websocket/commands/session/accountImage.ts +++ b/webclient/src/websocket/commands/session/accountImage.ts @@ -1,14 +1,9 @@ import webClient from '../../WebClient'; import { SessionPersistence } from '../../persistence'; -import { common } from 'protobufjs'; -import IBytesValue = common.IBytesValue; -export function accountImage(image: IBytesValue): void { +export function accountImage(image: unknown): void { const command = webClient.protobuf.controller.Command_AccountImage.create({ image }); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_AccountImage.ext': command - }); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_AccountImage.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/session/accountPassword.ts b/webclient/src/websocket/commands/session/accountPassword.ts index c07e830cb..0b5ef0111 100644 --- a/webclient/src/websocket/commands/session/accountPassword.ts +++ b/webclient/src/websocket/commands/session/accountPassword.ts @@ -3,10 +3,7 @@ import { SessionPersistence } from '../../persistence'; export function accountPassword(oldPassword: string, newPassword: string, hashedNewPassword: string): void { const command = webClient.protobuf.controller.Command_AccountPassword.create({ oldPassword, newPassword, hashedNewPassword }); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_AccountPassword.ext': command - }); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_AccountPassword.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/session/activate.ts b/webclient/src/websocket/commands/session/activate.ts index 1f45369f5..74e41afeb 100644 --- a/webclient/src/websocket/commands/session/activate.ts +++ b/webclient/src/websocket/commands/session/activate.ts @@ -15,11 +15,8 @@ export function activate(options: WebSocketConnectOptions, passwordSalt?: string token, }; - const CmdActivate = webClient.protobuf.controller.Command_Activate.create(accountActivationConfig); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_Activate.ext': CmdActivate - }); + const command = webClient.protobuf.controller.Command_Activate.create(accountActivationConfig); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_Activate.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { if (raw.responseCode === webClient.protobuf.controller.Response.ResponseCode.RespActivationAccepted) { diff --git a/webclient/src/websocket/commands/session/addToList.ts b/webclient/src/websocket/commands/session/addToList.ts index 522b5a8c8..6696d39d4 100644 --- a/webclient/src/websocket/commands/session/addToList.ts +++ b/webclient/src/websocket/commands/session/addToList.ts @@ -1,4 +1,5 @@ import webClient from '../../WebClient'; +import { SessionPersistence } from '../../persistence'; export function addToBuddyList(userName: string): void { addToList('buddy', userName); @@ -9,13 +10,16 @@ export function addToIgnoreList(userName: string): void { } export function addToList(list: string, userName: string): void { - const CmdAddToList = webClient.protobuf.controller.Command_AddToList.create({ list, userName }); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_AddToList.ext': CmdAddToList - }); + const command = webClient.protobuf.controller.Command_AddToList.create({ list, userName }); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_AddToList.ext': command }); webClient.protobuf.sendSessionCommand(sc, ({ responseCode }) => { - // @TODO: filter responseCode, pop snackbar for error + switch (responseCode) { + case webClient.protobuf.controller.Response.ResponseCode.RespOk: + SessionPersistence.addToList(list, userName); + break; + default: + console.error('Failed to add to list', responseCode); + } }); } diff --git a/webclient/src/websocket/commands/session/forgotPasswordChallenge.ts b/webclient/src/websocket/commands/session/forgotPasswordChallenge.ts index 40c248f35..f6399e6a7 100644 --- a/webclient/src/websocket/commands/session/forgotPasswordChallenge.ts +++ b/webclient/src/websocket/commands/session/forgotPasswordChallenge.ts @@ -14,11 +14,8 @@ export function forgotPasswordChallenge(options: WebSocketConnectOptions): void email, }; - const CmdForgotPasswordChallenge = webClient.protobuf.controller.Command_ForgotPasswordChallenge.create(forgotPasswordChallengeConfig); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_ForgotPasswordChallenge.ext': CmdForgotPasswordChallenge - }); + const command = webClient.protobuf.controller.Command_ForgotPasswordChallenge.create(forgotPasswordChallengeConfig); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_ForgotPasswordChallenge.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { if (raw.responseCode === webClient.protobuf.controller.Response.ResponseCode.RespOk) { diff --git a/webclient/src/websocket/commands/session/forgotPasswordRequest.ts b/webclient/src/websocket/commands/session/forgotPasswordRequest.ts index 5e5cc7d5e..3e858ad41 100644 --- a/webclient/src/websocket/commands/session/forgotPasswordRequest.ts +++ b/webclient/src/websocket/commands/session/forgotPasswordRequest.ts @@ -22,11 +22,8 @@ export function forgotPasswordRequest(options: WebSocketConnectOptions, password forgotPasswordResetConfig.newPassword = newPassword; } - const CmdForgotPasswordReset = webClient.protobuf.controller.Command_ForgotPasswordReset.create(forgotPasswordResetConfig); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_ForgotPasswordReset.ext': CmdForgotPasswordReset - }); + const command = webClient.protobuf.controller.Command_ForgotPasswordReset.create(forgotPasswordResetConfig); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_ForgotPasswordReset.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { if (raw.responseCode === webClient.protobuf.controller.Response.ResponseCode.RespOk) { diff --git a/webclient/src/websocket/commands/session/getGamesOfUser.ts b/webclient/src/websocket/commands/session/getGamesOfUser.ts index 88f73a0f7..507ba3e20 100644 --- a/webclient/src/websocket/commands/session/getGamesOfUser.ts +++ b/webclient/src/websocket/commands/session/getGamesOfUser.ts @@ -3,10 +3,7 @@ import { SessionPersistence } from '../../persistence'; export function getGamesOfUser(userName: string): void { const command = webClient.protobuf.controller.Command_GetGamesOfUser.create({ userName }); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_GetGamesOfUser.ext': command - }); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_GetGamesOfUser.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/session/getUserInfo.ts b/webclient/src/websocket/commands/session/getUserInfo.ts index 33da8da4b..709cc0baa 100644 --- a/webclient/src/websocket/commands/session/getUserInfo.ts +++ b/webclient/src/websocket/commands/session/getUserInfo.ts @@ -3,10 +3,7 @@ import { SessionPersistence } from '../../persistence'; export function getUserInfo(userName: string): void { const command = webClient.protobuf.controller.Command_GetUserInfo.create({ userName }); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_GetUserInfo.ext': command - }); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_GetUserInfo.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/session/joinRoom.ts b/webclient/src/websocket/commands/session/joinRoom.ts index 83fe136cb..9f4699708 100644 --- a/webclient/src/websocket/commands/session/joinRoom.ts +++ b/webclient/src/websocket/commands/session/joinRoom.ts @@ -2,16 +2,13 @@ import webClient from '../../WebClient'; import { RoomPersistence } from '../../persistence'; export function joinRoom(roomId: number): void { - const CmdJoinRoom = webClient.protobuf.controller.Command_JoinRoom.create({ roomId }); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_JoinRoom.ext': CmdJoinRoom - }); + const command = webClient.protobuf.controller.Command_JoinRoom.create({ roomId }); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_JoinRoom.ext': command }); webClient.protobuf.sendSessionCommand(sc, (raw) => { const { responseCode } = raw; - let error; + let error: string; switch (responseCode) { case webClient.protobuf.controller.Response.ResponseCode.RespOk: diff --git a/webclient/src/websocket/commands/session/listRooms.ts b/webclient/src/websocket/commands/session/listRooms.ts index 29d75dbb4..b1e31621f 100644 --- a/webclient/src/websocket/commands/session/listRooms.ts +++ b/webclient/src/websocket/commands/session/listRooms.ts @@ -1,11 +1,8 @@ import webClient from '../../WebClient'; export function listRooms(): void { - const CmdListRooms = webClient.protobuf.controller.Command_ListRooms.create(); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_ListRooms.ext': CmdListRooms - }); + const command = webClient.protobuf.controller.Command_ListRooms.create(); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_ListRooms.ext': command }); webClient.protobuf.sendSessionCommand(sc); } diff --git a/webclient/src/websocket/commands/session/listUsers.ts b/webclient/src/websocket/commands/session/listUsers.ts index 4a149a653..027a251d4 100644 --- a/webclient/src/websocket/commands/session/listUsers.ts +++ b/webclient/src/websocket/commands/session/listUsers.ts @@ -2,11 +2,8 @@ import webClient from '../../WebClient'; import { SessionPersistence } from '../../persistence'; export function listUsers(): void { - const CmdListUsers = webClient.protobuf.controller.Command_ListUsers.create(); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_ListUsers.ext': CmdListUsers - }); + const command = webClient.protobuf.controller.Command_ListUsers.create(); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_ListUsers.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/session/login.ts b/webclient/src/websocket/commands/session/login.ts index 4797114bc..db24b4f46 100644 --- a/webclient/src/websocket/commands/session/login.ts +++ b/webclient/src/websocket/commands/session/login.ts @@ -25,13 +25,10 @@ export function login(options: WebSocketConnectOptions, passwordSalt?: string): loginConfig.password = password; } - const CmdLogin = webClient.protobuf.controller.Command_Login.create(loginConfig); + const command = webClient.protobuf.controller.Command_Login.create(loginConfig); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_Login.ext': command }); - const command = webClient.protobuf.controller.SessionCommand.create({ - '.Command_Login.ext': CmdLogin - }); - - webClient.protobuf.sendSessionCommand(command, raw => { + webClient.protobuf.sendSessionCommand(sc, raw => { const resp = raw['.Response_Login.ext']; if (raw.responseCode === webClient.protobuf.controller.Response.ResponseCode.RespOk) { diff --git a/webclient/src/websocket/commands/session/message.ts b/webclient/src/websocket/commands/session/message.ts index 35dda60d2..99b615562 100644 --- a/webclient/src/websocket/commands/session/message.ts +++ b/webclient/src/websocket/commands/session/message.ts @@ -3,10 +3,7 @@ import { SessionPersistence } from '../../persistence'; export function message(userName: string, message: string): void { const command = webClient.protobuf.controller.Command_Message.create({ userName, message }); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_Message.ext': command - }); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_Message.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { const { responseCode } = raw; diff --git a/webclient/src/websocket/commands/session/ping.ts b/webclient/src/websocket/commands/session/ping.ts index 642f45f62..795e0f71f 100644 --- a/webclient/src/websocket/commands/session/ping.ts +++ b/webclient/src/websocket/commands/session/ping.ts @@ -1,12 +1,8 @@ import webClient from '../../WebClient'; -import { SessionPersistence } from '../../persistence'; export function ping(pingReceived: Function): void { const command = webClient.protobuf.controller.Command_Ping.create(); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_Ping.ext': command - }); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_Ping.ext': command }); webClient.protobuf.sendSessionCommand(sc, pingReceived); } diff --git a/webclient/src/websocket/commands/session/register.ts b/webclient/src/websocket/commands/session/register.ts index f1d0a2f5d..45b2fa977 100644 --- a/webclient/src/websocket/commands/session/register.ts +++ b/webclient/src/websocket/commands/session/register.ts @@ -25,11 +25,8 @@ export function register(options: WebSocketConnectOptions, passwordSalt?: string registerConfig.password = password; } - const CmdRegister = webClient.protobuf.controller.Command_Register.create(registerConfig); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_Register.ext': CmdRegister - }); + const command = webClient.protobuf.controller.Command_Register.create(registerConfig); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_Register.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { if (raw.responseCode === webClient.protobuf.controller.Response.ResponseCode.RespRegistrationAccepted) { @@ -46,7 +43,6 @@ export function register(options: WebSocketConnectOptions, passwordSalt?: string SessionPersistence.registrationUserNameError('Username is taken'); break; case webClient.protobuf.controller.Response.ResponseCode.RespUsernameInvalid: - console.error('ResponseCode.RespUsernameInvalid', raw.reasonStr); SessionPersistence.registrationUserNameError('Invalid username'); break; case webClient.protobuf.controller.Response.ResponseCode.RespPasswordTooShort: @@ -65,7 +61,7 @@ export function register(options: WebSocketConnectOptions, passwordSalt?: string SessionPersistence.registrationFailed('Registration is currently disabled'); break; case webClient.protobuf.controller.Response.ResponseCode.RespUserIsBanned: - SessionPersistence.registrationFailed(NormalizeService.normalizeBannedUserError(raw.reasonStr, raw.endTime)); + SessionPersistence.registrationFailed(raw.reasonStr, raw.endTime); break; case webClient.protobuf.controller.Response.ResponseCode.RespRegistrationFailed: default: diff --git a/webclient/src/websocket/commands/session/removeFromList.ts b/webclient/src/websocket/commands/session/removeFromList.ts index 25974036c..222d9c57d 100644 --- a/webclient/src/websocket/commands/session/removeFromList.ts +++ b/webclient/src/websocket/commands/session/removeFromList.ts @@ -1,4 +1,5 @@ import webClient from '../../WebClient'; +import { SessionPersistence } from '../../persistence'; export function removeFromBuddyList(userName: string): void { removeFromList('buddy', userName); @@ -9,13 +10,16 @@ export function removeFromIgnoreList(userName: string): void { } export function removeFromList(list: string, userName: string): void { - const CmdRemoveFromList = webClient.protobuf.controller.Command_RemoveFromList.create({ list, userName }); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_RemoveFromList.ext': CmdRemoveFromList - }); + const command = webClient.protobuf.controller.Command_RemoveFromList.create({ list, userName }); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_RemoveFromList.ext': command }); webClient.protobuf.sendSessionCommand(sc, ({ responseCode }) => { - // @TODO: filter responseCode, pop snackbar for error + switch (responseCode) { + case webClient.protobuf.controller.Response.ResponseCode.RespOk: + SessionPersistence.removeFromList(list, userName); + break; + default: + console.error('Failed to remove from list', responseCode); + } }); } diff --git a/webclient/src/websocket/commands/session/requestPasswordSalt.ts b/webclient/src/websocket/commands/session/requestPasswordSalt.ts index 667190bda..21ea27485 100644 --- a/webclient/src/websocket/commands/session/requestPasswordSalt.ts +++ b/webclient/src/websocket/commands/session/requestPasswordSalt.ts @@ -20,11 +20,8 @@ export function requestPasswordSalt(options: WebSocketConnectOptions): void { userName, }; - const CmdRequestPasswordSalt = webClient.protobuf.controller.Command_RequestPasswordSalt.create(registerConfig); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_RequestPasswordSalt.ext': CmdRequestPasswordSalt - }); + const command = webClient.protobuf.controller.Command_RequestPasswordSalt.create(registerConfig); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_RequestPasswordSalt.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { switch (raw.responseCode) { diff --git a/webclient/src/websocket/commands/session/resetPasswordRequest.ts b/webclient/src/websocket/commands/session/resetPasswordRequest.ts index 32fcde7ab..800e8cc0d 100644 --- a/webclient/src/websocket/commands/session/resetPasswordRequest.ts +++ b/webclient/src/websocket/commands/session/resetPasswordRequest.ts @@ -14,11 +14,8 @@ export function resetPasswordRequest(options: WebSocketConnectOptions): void { userName, }; - const CmdForgotPasswordRequest = webClient.protobuf.controller.Command_ForgotPasswordRequest.create(forgotPasswordConfig); - - const sc = webClient.protobuf.controller.SessionCommand.create({ - '.Command_ForgotPasswordRequest.ext': CmdForgotPasswordRequest - }); + const command = webClient.protobuf.controller.Command_ForgotPasswordRequest.create(forgotPasswordConfig); + const sc = webClient.protobuf.controller.SessionCommand.create({ '.Command_ForgotPasswordRequest.ext': command }); webClient.protobuf.sendSessionCommand(sc, raw => { if (raw.responseCode === webClient.protobuf.controller.Response.ResponseCode.RespOk) { diff --git a/webclient/src/websocket/events/room/RoomEvents.spec.ts b/webclient/src/websocket/events/room/RoomEvents.spec.ts deleted file mode 100644 index 2b1ca3add..000000000 --- a/webclient/src/websocket/events/room/RoomEvents.spec.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { Message } from 'types'; - -import { RoomPersistence } from '../../persistence'; - -import { - RoomEvent, - JoinRoomData, - LeaveRoomData, - ListGamesData, -} from './interfaces'; - -import { RoomEvents } from '.'; - -describe.skip('RoomEvents', () => { - it('.Event_JoinRoom.ext should call RoomPersistence.userJoined', () => { - jest.spyOn(RoomPersistence, 'userJoined').mockImplementation(() => {}); - const data: JoinRoomData = { userInfo: {} as any }; - const event: RoomEvent = { roomEvent: { roomId: 1 } }; - - RoomEvents['.Event_JoinRoom.ext'](data, event); - - expect(RoomPersistence.userJoined).toHaveBeenCalledWith( - event.roomEvent.roomId, - data.userInfo - ); - }); - - it('.Event_LeaveRoom.ext should call RoomPersistence.userLeft', () => { - jest.spyOn(RoomPersistence, 'userLeft').mockImplementation(() => {}); - const data: LeaveRoomData = { name: '' }; - const event: RoomEvent = { roomEvent: { roomId: 1 } }; - - RoomEvents['.Event_LeaveRoom.ext'](data, event); - - expect(RoomPersistence.userLeft).toHaveBeenCalledWith( - event.roomEvent.roomId, - data.name - ); - }); - - it('.Event_ListGames.ext should call RoomPersistence.updateGames', () => { - jest.spyOn(RoomPersistence, 'updateGames').mockImplementation(() => {}); - const data: ListGamesData = { gameList: [] }; - const event: RoomEvent = { roomEvent: { roomId: 1 } }; - - RoomEvents['.Event_ListGames.ext'](data, event); - - expect(RoomPersistence.updateGames).toHaveBeenCalledWith( - event.roomEvent.roomId, - data.gameList - ); - }); - - it('.Event_RoomSay.ext should call RoomPersistence.addMessage', () => { - jest.spyOn(RoomPersistence, 'addMessage').mockImplementation(() => {}); - const data: Message = {} as any; - const event: RoomEvent = { roomEvent: { roomId: 1 } }; - - RoomEvents['.Event_RoomSay.ext'](data, event); - - expect(RoomPersistence.addMessage).toHaveBeenCalledWith( - event.roomEvent.roomId, - data - ); - }); -}); diff --git a/webclient/src/websocket/events/room/index.ts b/webclient/src/websocket/events/room/index.ts index 4f9543f19..5b571d388 100644 --- a/webclient/src/websocket/events/room/index.ts +++ b/webclient/src/websocket/events/room/index.ts @@ -7,9 +7,9 @@ import { roomSay } from './roomSay'; import { removeMessages } from './removeMessages'; export const RoomEvents: ProtobufEvents = { + '.Event_JoinRoom.ext': joinRoom, '.Event_LeaveRoom.ext': leaveRoom, '.Event_ListGames.ext': listGames, '.Event_RemoveMessages.ext': removeMessages, '.Event_RoomSay.ext': roomSay, - '.Event_JoinRoom.ext': joinRoom, }; diff --git a/webclient/src/websocket/events/room/interfaces.ts b/webclient/src/websocket/events/room/interfaces.ts index 245f44306..b3f922141 100644 --- a/webclient/src/websocket/events/room/interfaces.ts +++ b/webclient/src/websocket/events/room/interfaces.ts @@ -1,11 +1,5 @@ import { Game, User } from 'types'; -export interface RoomEvent { - roomEvent: { - roomId: number; - } -} - export interface JoinRoomData { userInfo: User; } @@ -22,3 +16,9 @@ export interface RemoveMessagesData { name: string; amount: number; } + +export interface RoomEvent { + roomEvent: { + roomId: number; + } +} diff --git a/webclient/src/websocket/events/room/joinRoom.ts b/webclient/src/websocket/events/room/joinRoom.ts index ba703e114..b1a5f6606 100644 --- a/webclient/src/websocket/events/room/joinRoom.ts +++ b/webclient/src/websocket/events/room/joinRoom.ts @@ -1,8 +1,6 @@ import { RoomPersistence } from '../../persistence'; import { JoinRoomData, RoomEvent } from './interfaces'; -export function joinRoom({ userInfo }: JoinRoomData, { roomEvent }: RoomEvent) { - const { roomId } = roomEvent; - +export function joinRoom({ userInfo }: JoinRoomData, { roomEvent: { roomId } }: RoomEvent): void { RoomPersistence.userJoined(roomId, userInfo); } diff --git a/webclient/src/websocket/events/room/leaveRoom.ts b/webclient/src/websocket/events/room/leaveRoom.ts index e94555164..6d45197fc 100644 --- a/webclient/src/websocket/events/room/leaveRoom.ts +++ b/webclient/src/websocket/events/room/leaveRoom.ts @@ -1,7 +1,6 @@ import { RoomPersistence } from '../../persistence'; import { LeaveRoomData, RoomEvent } from './interfaces'; -export function leaveRoom({ name }: LeaveRoomData, { roomEvent }: RoomEvent) { - const { roomId } = roomEvent; +export function leaveRoom({ name }: LeaveRoomData, { roomEvent: { roomId } }: RoomEvent): void { RoomPersistence.userLeft(roomId, name); } diff --git a/webclient/src/websocket/events/room/listGames.ts b/webclient/src/websocket/events/room/listGames.ts index df7e7913e..d460a5336 100644 --- a/webclient/src/websocket/events/room/listGames.ts +++ b/webclient/src/websocket/events/room/listGames.ts @@ -1,7 +1,6 @@ import { RoomPersistence } from '../../persistence'; import { ListGamesData, RoomEvent } from './interfaces'; -export function listGames({ gameList }: ListGamesData, { roomEvent }: RoomEvent) { - const { roomId } = roomEvent; +export function listGames({ gameList }: ListGamesData, { roomEvent: { roomId } }: RoomEvent): void { RoomPersistence.updateGames(roomId, gameList); } diff --git a/webclient/src/websocket/events/room/removeMessages.ts b/webclient/src/websocket/events/room/removeMessages.ts index 57fa8b1f6..4fe01cb6f 100644 --- a/webclient/src/websocket/events/room/removeMessages.ts +++ b/webclient/src/websocket/events/room/removeMessages.ts @@ -1,6 +1,6 @@ import { RoomPersistence } from '../../persistence'; import { RemoveMessagesData, RoomEvent } from './interfaces'; -export function removeMessages({ name, amount }: RemoveMessagesData, { roomEvent: { roomId } }: RoomEvent) { +export function removeMessages({ name, amount }: RemoveMessagesData, { roomEvent: { roomId } }: RoomEvent): void { RoomPersistence.removeMessages(roomId, name, amount); } diff --git a/webclient/src/websocket/events/room/roomSay.ts b/webclient/src/websocket/events/room/roomSay.ts index 657676d18..5a96198ea 100644 --- a/webclient/src/websocket/events/room/roomSay.ts +++ b/webclient/src/websocket/events/room/roomSay.ts @@ -3,7 +3,6 @@ import { Message } from 'types'; import { RoomPersistence } from '../../persistence'; import { RoomEvent } from './interfaces'; -export function roomSay(message: Message, { roomEvent }: RoomEvent) { - const { roomId } = roomEvent; +export function roomSay(message: Message, { roomEvent: { roomId } }: RoomEvent): void { RoomPersistence.addMessage(roomId, message); } diff --git a/webclient/src/websocket/events/session/SessionEvents.spec.ts b/webclient/src/websocket/events/session/SessionEvents.spec.ts deleted file mode 100644 index 37c1d8c9e..000000000 --- a/webclient/src/websocket/events/session/SessionEvents.spec.ts +++ /dev/null @@ -1,393 +0,0 @@ -import { StatusEnum, WebSocketConnectReason } from 'types'; - -import { SessionCommands } from '../../commands'; -import { RoomPersistence, SessionPersistence } from '../../persistence'; -import webClient from '../../WebClient'; - -import { - AddToListData, - ConnectionClosedData, - ListRoomsData, - RemoveFromListData, - ServerIdentificationData, - ServerMessageData, - UserJoinedData, - UserLeftData, -} from './interfaces'; - -import { SessionEvents } from '.'; - -describe.skip('SessionEvents', () => { - const roomId = 1; - - beforeEach(() => { - jest.spyOn(SessionCommands, 'updateStatus').mockImplementation(() => {}); - }); - - describe('.Event_AddToList.ext', () => { - it('should call SessionPersistence.addToBuddyList if buddy listName', () => { - jest.spyOn(SessionPersistence, 'addToBuddyList').mockImplementation(() => {}); - const data: AddToListData = { listName: 'buddy', userInfo: {} as any }; - - SessionEvents['.Event_AddToList.ext'](data); - - expect(SessionPersistence.addToBuddyList).toHaveBeenCalledWith( - data.userInfo - ); - }); - - it('should call SessionPersistence.addToIgnoreList if ignore listName', () => { - jest.spyOn(SessionPersistence, 'addToIgnoreList').mockImplementation(() => {}); - const data: AddToListData = { listName: 'ignore', userInfo: {} as any }; - - SessionEvents['.Event_AddToList.ext'](data); - - expect(SessionPersistence.addToIgnoreList).toHaveBeenCalledWith( - data.userInfo - ); - }); - - it('should call console.log if unknown listName', () => { - jest.spyOn(console, 'log').mockImplementation(() => {}); - const data: AddToListData = { listName: 'unknown', userInfo: {} as any }; - - SessionEvents['.Event_AddToList.ext'](data); - - expect(console.log).toHaveBeenCalledWith( - `Attempted to add to unknown list: ${data.listName}` - ); - }); - }); - - describe('.Event_ConnectionClosed.ext', () => { - describe('with reasonStr', () => { - it('should call SessionCommands.updateStatus', () => { - const data: ConnectionClosedData = { endTime: 0, reason: 0, reasonStr: 'reasonStr' }; - - SessionEvents['.Event_ConnectionClosed.ext'](data); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - data.reasonStr - ); - }); - }); - - describe('without reasonStr', () => { - beforeEach(() => { - webClient.protobuf.controller.Event_ConnectionClosed = { CloseReason: {} }; - }); - - describe('USER_LIMIT_REACHED', () => { - it('should call SessionCommands.updateStatus', () => { - const USER_LIMIT_REACHED = 1; - webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.USER_LIMIT_REACHED = USER_LIMIT_REACHED; - const data: ConnectionClosedData = { endTime: 0, reason: USER_LIMIT_REACHED, reasonStr: null }; - - SessionEvents['.Event_ConnectionClosed.ext'](data); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - 'The server has reached its maximum user capacity' - ); - }); - }); - - describe('TOO_MANY_CONNECTIONS', () => { - it('should call SessionCommands.updateStatus', () => { - const TOO_MANY_CONNECTIONS = 1; - webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.TOO_MANY_CONNECTIONS = TOO_MANY_CONNECTIONS; - const data: ConnectionClosedData = { endTime: 0, reason: TOO_MANY_CONNECTIONS, reasonStr: null }; - - SessionEvents['.Event_ConnectionClosed.ext'](data); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - 'There are too many concurrent connections from your address' - ); - }); - }); - - describe('BANNED', () => { - it('should call SessionCommands.updateStatus', () => { - const BANNED = 1; - webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.BANNED = BANNED; - const data: ConnectionClosedData = { endTime: 0, reason: BANNED, reasonStr: null }; - - SessionEvents['.Event_ConnectionClosed.ext'](data); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - 'You are banned' - ); - }); - }); - - describe('DEMOTED', () => { - it('should call SessionCommands.updateStatus', () => { - const DEMOTED = 1; - webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.DEMOTED = DEMOTED; - const data: ConnectionClosedData = { endTime: 0, reason: DEMOTED, reasonStr: null }; - - SessionEvents['.Event_ConnectionClosed.ext'](data); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - 'You were demoted' - ); - }); - }); - - describe('SERVER_SHUTDOWN', () => { - it('should call SessionCommands.updateStatus', () => { - const SERVER_SHUTDOWN = 1; - webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.SERVER_SHUTDOWN = SERVER_SHUTDOWN; - const data: ConnectionClosedData = { endTime: 0, reason: SERVER_SHUTDOWN, reasonStr: null }; - - SessionEvents['.Event_ConnectionClosed.ext'](data); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - 'Scheduled server shutdown' - ); - }); - }); - - describe('USERNAMEINVALID', () => { - it('should call SessionCommands.updateStatus', () => { - const USERNAMEINVALID = 1; - webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.USERNAMEINVALID = USERNAMEINVALID; - const data: ConnectionClosedData = { endTime: 0, reason: USERNAMEINVALID, reasonStr: null }; - - SessionEvents['.Event_ConnectionClosed.ext'](data); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - 'Invalid username' - ); - }); - }); - - describe('LOGGEDINELSEWERE', () => { - it('should call SessionCommands.updateStatus', () => { - const LOGGEDINELSEWERE = 1; - webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.LOGGEDINELSEWERE = LOGGEDINELSEWERE; - const data: ConnectionClosedData = { endTime: 0, reason: LOGGEDINELSEWERE, reasonStr: null }; - - SessionEvents['.Event_ConnectionClosed.ext'](data); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - 'You have been logged out due to logging in at another location' - ); - }); - }); - - describe('OTHER', () => { - it('should call SessionCommands.updateStatus', () => { - const OTHER = 1; - webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.OTHER = OTHER; - const data: ConnectionClosedData = { endTime: 0, reason: OTHER, reasonStr: null }; - - SessionEvents['.Event_ConnectionClosed.ext'](data); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - 'Unknown reason' - ); - }); - }); - - describe('UNKNOWN', () => { - it('should call SessionCommands.updateStatus', () => { - const UNKNOWN = 1; - webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.UNKNOWN = UNKNOWN; - const data: ConnectionClosedData = { endTime: 0, reason: UNKNOWN, reasonStr: null }; - - SessionEvents['.Event_ConnectionClosed.ext'](data); - - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - 'Unknown reason' - ); - }); - }); - }); - }); - - describe('.Event_ListRooms.ext', () => { - beforeEach(() => { - webClient.clientOptions.autojoinrooms = false; - jest.spyOn(RoomPersistence, 'updateRooms').mockImplementation(() => {}); - }); - - it('should call RoomPersistence.updateRooms', () => { - const data: ListRoomsData = { roomList: [{ roomId, autoJoin: false } as any] }; - - SessionEvents['.Event_ListRooms.ext'](data); - - expect(RoomPersistence.updateRooms).toHaveBeenCalledWith(data.roomList); - }); - - it('should call SessionCommands.joinRoom if webClient and room is configured for autojoin', () => { - webClient.clientOptions.autojoinrooms = true; - jest.spyOn(SessionCommands, 'joinRoom').mockImplementation(() => {}); - const data: ListRoomsData = { roomList: [{ roomId, autoJoin: true } as any, { roomId: 2, autoJoin: false } as any] }; - - SessionEvents['.Event_ListRooms.ext'](data); - - expect(SessionCommands.joinRoom).toHaveBeenCalledTimes(1); - expect(SessionCommands.joinRoom).toHaveBeenCalledWith(data.roomList[0].roomId); - }); - }); - - describe('.Event_RemoveFromList.ext', () => { - it('should call SessionPersistence.removeFromBuddyList if buddy listName', () => { - jest.spyOn(SessionPersistence, 'removeFromBuddyList').mockImplementation(() => {}); - const data: RemoveFromListData = { listName: 'buddy', userName: '' }; - - SessionEvents['.Event_RemoveFromList.ext'](data); - - expect(SessionPersistence.removeFromBuddyList).toHaveBeenCalledWith( - data.userName - ); - }); - - it('should call SessionPersistence.removeFromIgnoreList if ignore listName', () => { - jest.spyOn(SessionPersistence, 'removeFromIgnoreList').mockImplementation(() => {}); - const data: RemoveFromListData = { listName: 'ignore', userName: '' }; - - SessionEvents['.Event_RemoveFromList.ext'](data); - - expect(SessionPersistence.removeFromIgnoreList).toHaveBeenCalledWith( - data.userName - ); - }); - - it('should call console.log if unknown listName', () => { - jest.spyOn(console, 'log').mockImplementation(() => {}); - const data: RemoveFromListData = { listName: 'unknown', userName: '' }; - - SessionEvents['.Event_RemoveFromList.ext'](data); - - expect(console.log).toHaveBeenCalledWith( - `Attempted to remove from unknown list: ${data.listName}` - ); - }); - }); - - describe('.Event_ServerIdentification.ext', () => { - let data: ServerIdentificationData; - let event; - - beforeEach(() => { - webClient.protocolVersion = 0; - event = SessionEvents['.Event_ServerIdentification.ext']; - data = { - serverName: 'serverName', - serverVersion: 'serverVersion', - protocolVersion: 0, - serverOptions: 0 - }; - - jest.spyOn(SessionPersistence, 'updateInfo').mockImplementation(() => {}); - webClient.protobuf.controller.Event_ServerIdentification = { ServerOptions: { SupportsPasswordHash: 1 } }; - webClient.options = {}; - }); - - it('update status/info and login', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - - webClient.options.reason = WebSocketConnectReason.LOGIN; - - event(data); - - expect(SessionPersistence.updateInfo).toHaveBeenCalledWith(data.serverName, data.serverVersion); - expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.LOGGING_IN, expect.any(String)); - expect(SessionCommands.login).toHaveBeenCalled(); - }); - - it('should update stat/info and register', () => { - jest.spyOn(SessionCommands, 'register').mockImplementation(() => {}); - - webClient.options.reason = WebSocketConnectReason.REGISTER; - - event(data); - - expect(SessionPersistence.updateInfo).toHaveBeenCalledWith(data.serverName, data.serverVersion); - expect(SessionCommands.register).toHaveBeenCalled(); - }); - - it('should update stat/info and activate account', () => { - jest.spyOn(SessionCommands, 'activate').mockImplementation(() => {}); - - webClient.options.reason = WebSocketConnectReason.ACTIVATE_ACCOUNT; - - event(data); - - expect(SessionPersistence.updateInfo).toHaveBeenCalledWith(data.serverName, data.serverVersion); - expect(SessionCommands.activate).toHaveBeenCalled(); - }); - - it('should disconnect if protocolVersion mismatched', () => { - jest.spyOn(SessionCommands, 'login').mockImplementation(() => {}); - jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {}); - - webClient.protocolVersion = 0; - const data: ServerIdentificationData = { - serverName: '', - serverVersion: '', - protocolVersion: 1, - serverOptions: 0 - }; - - event(data); - - expect(SessionCommands.disconnect).toHaveBeenCalled(); - expect(SessionCommands.updateStatus).toHaveBeenCalledWith( - StatusEnum.DISCONNECTED, - `Protocol version mismatch: ${data.protocolVersion}` - ); - expect(SessionCommands.login).not.toHaveBeenCalled(); - }); - }); - - describe('.Event_ServerMessage.ext', () => { - it('should call SessionPersistence.serverMessage', () => { - jest.spyOn(SessionPersistence, 'serverMessage').mockImplementation(() => {}); - const data: ServerMessageData = { message: 'message' }; - - SessionEvents['.Event_ServerMessage.ext'](data); - - expect(SessionPersistence.serverMessage).toHaveBeenCalledWith( - data.message - ); - }); - }); - - describe('.Event_UserJoined.ext', () => { - it('should call SessionPersistence.userJoined', () => { - jest.spyOn(SessionPersistence, 'userJoined').mockImplementation(() => {}); - const data: UserJoinedData = { userInfo: {} as any }; - - SessionEvents['.Event_UserJoined.ext'](data); - - expect(SessionPersistence.userJoined).toHaveBeenCalledWith( - data.userInfo - ); - }); - }); - - describe('.Event_UserLeft.ext', () => { - it('should call SessionPersistence.userLeft', () => { - jest.spyOn(SessionPersistence, 'userLeft').mockImplementation(() => {}); - const data: UserLeftData = { name: '' }; - - SessionEvents['.Event_UserLeft.ext'](data); - - expect(SessionPersistence.userLeft).toHaveBeenCalledWith( - data.name - ); - }); - }); -}); diff --git a/webclient/src/websocket/events/session/addToList.ts b/webclient/src/websocket/events/session/addToList.ts index 7c5199e81..08b19b45d 100644 --- a/webclient/src/websocket/events/session/addToList.ts +++ b/webclient/src/websocket/events/session/addToList.ts @@ -1,7 +1,7 @@ import { SessionPersistence } from '../../persistence'; import { AddToListData } from './interfaces'; -export function addToList({ listName, userInfo }: AddToListData) { +export function addToList({ listName, userInfo }: AddToListData): void { switch (listName) { case 'buddy': { SessionPersistence.addToBuddyList(userInfo); diff --git a/webclient/src/websocket/events/session/connectionClosed.ts b/webclient/src/websocket/events/session/connectionClosed.ts index f92fa52c1..9c39db940 100644 --- a/webclient/src/websocket/events/session/connectionClosed.ts +++ b/webclient/src/websocket/events/session/connectionClosed.ts @@ -3,8 +3,8 @@ import webClient from '../../WebClient'; import { updateStatus } from '../../commands/session'; import { ConnectionClosedData } from './interfaces'; -export function connectionClosed({ reason, reasonStr }: ConnectionClosedData) { - let message; +export function connectionClosed({ reason, reasonStr }: ConnectionClosedData): void { + let message: string; // @TODO (5) if (reasonStr) { diff --git a/webclient/src/websocket/events/session/gameJoined.ts b/webclient/src/websocket/events/session/gameJoined.ts index 20c2843c7..8c0d49006 100644 --- a/webclient/src/websocket/events/session/gameJoined.ts +++ b/webclient/src/websocket/events/session/gameJoined.ts @@ -1,6 +1,6 @@ import { SessionPersistence } from '../../persistence'; import { GameJoinedData } from './interfaces'; -export function gameJoined(gameJoined: GameJoinedData) { +export function gameJoined(gameJoined: GameJoinedData): void { SessionPersistence.gameJoined(gameJoined); } diff --git a/webclient/src/websocket/events/session/interfaces.ts b/webclient/src/websocket/events/session/interfaces.ts index 79a0ca499..8a1827cbd 100644 --- a/webclient/src/websocket/events/session/interfaces.ts +++ b/webclient/src/websocket/events/session/interfaces.ts @@ -1,8 +1,4 @@ -import { Game, Room, User } from 'types'; - -export interface SessionEvent { - sessionEvent: {} -} +import { Game, NotificationType, Room, User } from 'types'; export interface AddToListData { listName: string; @@ -15,10 +11,37 @@ export interface ConnectionClosedData { reasonStr: string; } +export interface GameJoinedData { + gameInfo: Game; + playerId: number; + spectator: boolean; + resuming: boolean; + judge: boolean; +} + export interface ListRoomsData { roomList: Room[]; } +export interface NotifyUserData { + type: NotificationType; + warningReason: string; + customTitle: string; + customContent: string; +} + +export interface PlayerGamePropertiesData { + playerId: number; + userInfo: User; + spectator: boolean; + conceded: boolean; + readyStart: boolean; + deckHash: string; + pingSeconds: number; + sideboardLocked: boolean; + judge: boolean; +} + export interface RemoveFromListData { listName: string; userName: string; @@ -35,6 +58,11 @@ export interface ServerMessageData { message: string; } +export interface ServerShutdownData { + reason: string; + minutes: number; +} + export interface UserJoinedData { userInfo: User; } @@ -43,10 +71,8 @@ export interface UserLeftData { name: string; } -export interface GameJoinedData { - gameInfo: Game; - playerId: number; - spectator: boolean; - resuming: boolean; - judge: boolean; +export interface UserMessageData { + senderName: string; + receiverName: string; + message: string; } diff --git a/webclient/src/websocket/events/session/listRooms.ts b/webclient/src/websocket/events/session/listRooms.ts index 4255a6bb7..faba2968e 100644 --- a/webclient/src/websocket/events/session/listRooms.ts +++ b/webclient/src/websocket/events/session/listRooms.ts @@ -3,7 +3,7 @@ import { joinRoom } from '../../commands/session'; import { RoomPersistence } from '../../persistence'; import { ListRoomsData } from './interfaces'; -export function listRooms({ roomList }: ListRoomsData) { +export function listRooms({ roomList }: ListRoomsData): void { RoomPersistence.updateRooms(roomList); if (webClient.clientOptions.autojoinrooms) { diff --git a/webclient/src/websocket/events/session/notifyUser.ts b/webclient/src/websocket/events/session/notifyUser.ts index 7723b0a20..f5673fe3f 100644 --- a/webclient/src/websocket/events/session/notifyUser.ts +++ b/webclient/src/websocket/events/session/notifyUser.ts @@ -1,3 +1,7 @@ -export function notifyUser(payload) { - console.info('Event_NotifyUser', payload); +import { SessionPersistence } from '../../persistence'; +import { NotifyUserData } from './interfaces'; + + +export function notifyUser(payload: NotifyUserData): void { + SessionPersistence.notifyUser(payload); } diff --git a/webclient/src/websocket/events/session/playerPropertiesChanges.ts b/webclient/src/websocket/events/session/playerPropertiesChanges.ts index c6ba2e049..0f35b4ae3 100644 --- a/webclient/src/websocket/events/session/playerPropertiesChanges.ts +++ b/webclient/src/websocket/events/session/playerPropertiesChanges.ts @@ -1,3 +1,6 @@ -export function playerPropertiesChanges(payload) { - console.info('Event_PlayerPropertiesChanges', payload); +import { PlayerGamePropertiesData } from './interfaces'; +import { SessionPersistence } from '../../persistence'; + +export function playerPropertiesChanges(payload: PlayerGamePropertiesData): void { + SessionPersistence.playerPropertiesChanged(payload); } diff --git a/webclient/src/websocket/events/session/removeFromList.ts b/webclient/src/websocket/events/session/removeFromList.ts index 42c23090f..20e2d7f54 100644 --- a/webclient/src/websocket/events/session/removeFromList.ts +++ b/webclient/src/websocket/events/session/removeFromList.ts @@ -1,7 +1,7 @@ import { SessionPersistence } from '../../persistence'; import { RemoveFromListData } from './interfaces'; -export function removeFromList({ listName, userName }: RemoveFromListData) { +export function removeFromList({ listName, userName }: RemoveFromListData): void { switch (listName) { case 'buddy': { SessionPersistence.removeFromBuddyList(userName); diff --git a/webclient/src/websocket/events/session/serverIdentification.ts b/webclient/src/websocket/events/session/serverIdentification.ts index 6673533f2..24ae0ad0a 100644 --- a/webclient/src/websocket/events/session/serverIdentification.ts +++ b/webclient/src/websocket/events/session/serverIdentification.ts @@ -16,7 +16,7 @@ import { generateSalt, passwordSaltSupported } from '../../utils'; import { ServerIdentificationData } from './interfaces'; import { SessionPersistence } from '../../persistence'; -export function serverIdentification(info: ServerIdentificationData) { +export function serverIdentification(info: ServerIdentificationData): void { const { serverName, serverVersion, protocolVersion, serverOptions } = info; if (protocolVersion !== webClient.protocolVersion) { updateStatus(StatusEnum.DISCONNECTED, `Protocol version mismatch: ${protocolVersion}`); diff --git a/webclient/src/websocket/events/session/serverMessage.ts b/webclient/src/websocket/events/session/serverMessage.ts index a49a5c589..f9e52aa7a 100644 --- a/webclient/src/websocket/events/session/serverMessage.ts +++ b/webclient/src/websocket/events/session/serverMessage.ts @@ -1,6 +1,6 @@ import { SessionPersistence } from '../../persistence'; import { ServerMessageData } from './interfaces'; -export function serverMessage({ message }: ServerMessageData) { +export function serverMessage({ message }: ServerMessageData): void { SessionPersistence.serverMessage(message); } diff --git a/webclient/src/websocket/events/session/serverShutdown.ts b/webclient/src/websocket/events/session/serverShutdown.ts index c766b807a..cdda893a4 100644 --- a/webclient/src/websocket/events/session/serverShutdown.ts +++ b/webclient/src/websocket/events/session/serverShutdown.ts @@ -1,3 +1,7 @@ -export function serverShutdown(payload) { - console.info('Event_ServerShutdown', payload); +import { SessionPersistence } from '../../persistence'; +import { ServerShutdownData } from './interfaces'; + + +export function serverShutdown(payload: ServerShutdownData): void { + SessionPersistence.serverShutdown(payload); } diff --git a/webclient/src/websocket/events/session/userJoined.ts b/webclient/src/websocket/events/session/userJoined.ts index fad1d2331..cb512db60 100644 --- a/webclient/src/websocket/events/session/userJoined.ts +++ b/webclient/src/websocket/events/session/userJoined.ts @@ -1,6 +1,6 @@ import { SessionPersistence } from '../../persistence'; import { UserJoinedData } from './interfaces'; -export function userJoined({ userInfo }: UserJoinedData) { +export function userJoined({ userInfo }: UserJoinedData): void { SessionPersistence.userJoined(userInfo); } diff --git a/webclient/src/websocket/events/session/userLeft.ts b/webclient/src/websocket/events/session/userLeft.ts index 393b4c1b3..9e00e59e1 100644 --- a/webclient/src/websocket/events/session/userLeft.ts +++ b/webclient/src/websocket/events/session/userLeft.ts @@ -1,6 +1,6 @@ import { SessionPersistence } from '../../persistence'; import { UserLeftData } from './interfaces'; -export function userLeft({ name }: UserLeftData) { +export function userLeft({ name }: UserLeftData): void { SessionPersistence.userLeft(name); } diff --git a/webclient/src/websocket/events/session/userMessage.ts b/webclient/src/websocket/events/session/userMessage.ts index 4f1b79549..bff08460b 100644 --- a/webclient/src/websocket/events/session/userMessage.ts +++ b/webclient/src/websocket/events/session/userMessage.ts @@ -1,3 +1,8 @@ -export function userMessage(payload) { - console.info('Event_UserMessage', payload); +import { SessionPersistence } from '../../persistence'; +import { UserMessageData } from './interfaces'; + + + +export function userMessage(payload: UserMessageData): void { + SessionPersistence.userMessage(payload); } diff --git a/webclient/src/websocket/persistence/SessionPersistence.ts b/webclient/src/websocket/persistence/SessionPersistence.ts index 294d630af..9e803d4b8 100644 --- a/webclient/src/websocket/persistence/SessionPersistence.ts +++ b/webclient/src/websocket/persistence/SessionPersistence.ts @@ -1,9 +1,15 @@ import { ServerDispatch } from 'store'; -import { Log, StatusEnum, User, WebSocketConnectOptions } from 'types'; +import { StatusEnum, User, WebSocketConnectOptions } from 'types'; import { sanitizeHtml } from 'websocket/utils'; +import { + GameJoinedData, + NotifyUserData, + PlayerGamePropertiesData, + ServerShutdownData, + UserMessageData +} from '../events/session/interfaces'; import NormalizeService from '../utils/NormalizeService'; -import { GameJoinedData } from '../events/session/interfaces'; export class SessionPersistence { static initialized() { @@ -114,8 +120,10 @@ export class SessionPersistence { ServerDispatch.registrationSuccess(); } - static registrationFailed(error: string) { - ServerDispatch.registrationFailed(error); + static registrationFailed(reason: string, endTime?: number) { + const reasonMsg = endTime ? NormalizeService.normalizeBannedUserError(reason, endTime) : reason; + + ServerDispatch.registrationFailed(reasonMsg); } static registrationEmailError(error: string) { @@ -173,4 +181,28 @@ export class SessionPersistence { static gameJoined(gameJoinedData: GameJoinedData): void { console.log('gameJoined', gameJoinedData); } + + static notifyUser(payload: NotifyUserData): void { + console.log('notifyUser', payload); + } + + static playerPropertiesChanged(payload: PlayerGamePropertiesData): void { + console.log('playerPropertiesChanged', payload); + } + + static serverShutdown(payload: ServerShutdownData): void { + console.log('serverShutdown', payload); + } + + static userMessage(payload: UserMessageData): void { + console.log('userMessage', payload); + } + + static addToList(list: string, userName: string): void { + console.log('addToList', list, userName); + } + + static removeFromList(list: string, userName: string): void { + console.log('removeFromList', list, userName); + } }