mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-10 08:14:47 -07:00
WebClient: refactor protobuf method structure (#5014)
This commit is contained in:
parent
f174614496
commit
be5d42baba
53 changed files with 1014 additions and 1263 deletions
75
webclient/src/websocket/commands/room/RoomCommands.spec.ts
Normal file
75
webclient/src/websocket/commands/room/RoomCommands.spec.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
2
webclient/src/websocket/commands/room/index.ts
Normal file
2
webclient/src/websocket/commands/room/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export * from './leaveRoom';
|
||||
export * from './roomSay';
|
||||
22
webclient/src/websocket/commands/room/leaveRoom.ts
Normal file
22
webclient/src/websocket/commands/room/leaveRoom.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
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
|
||||
});
|
||||
|
||||
webClient.protobuf.sendRoomCommand(roomId, rc, (raw) => {
|
||||
const { responseCode } = raw;
|
||||
|
||||
switch (responseCode) {
|
||||
case webClient.protobuf.controller.Response.ResponseCode.RespOk:
|
||||
RoomPersistence.leaveRoom(roomId);
|
||||
break;
|
||||
default:
|
||||
console.log(`Failed to leave Room ${roomId} [${responseCode}] : `, raw);
|
||||
}
|
||||
});
|
||||
}
|
||||
19
webclient/src/websocket/commands/room/roomSay.ts
Normal file
19
webclient/src/websocket/commands/room/roomSay.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import webClient from '../../WebClient';
|
||||
|
||||
export function roomSay(roomId: number, message: string): void {
|
||||
const trimmed = message.trim();
|
||||
|
||||
if (!trimmed) {
|
||||
return;
|
||||
}
|
||||
|
||||
const CmdRoomSay = webClient.protobuf.controller.Command_RoomSay.create({
|
||||
'message': trimmed
|
||||
});
|
||||
|
||||
const rc = webClient.protobuf.controller.RoomCommand.create({
|
||||
'.Command_RoomSay.ext': CmdRoomSay
|
||||
});
|
||||
|
||||
webClient.protobuf.sendRoomCommand(roomId, rc);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue