2021-06-24 10:09:10 +02:00
|
|
|
import { IRoomManagerServer } from "./Messages/generated/messages_grpc_pb";
|
2020-11-13 18:00:22 +01:00
|
|
|
import {
|
2020-12-11 12:23:50 +01:00
|
|
|
AdminGlobalMessage,
|
|
|
|
AdminMessage,
|
2021-03-11 16:14:34 +01:00
|
|
|
AdminPusherToBackMessage,
|
2021-03-01 17:47:00 +01:00
|
|
|
AdminRoomMessage,
|
2021-01-17 22:44:22 +01:00
|
|
|
BanMessage,
|
2021-07-19 15:57:50 +02:00
|
|
|
BatchToPusherMessage,
|
|
|
|
BatchToPusherRoomMessage,
|
2021-03-31 11:21:06 +02:00
|
|
|
EmotePromptMessage,
|
2021-01-17 03:07:46 +01:00
|
|
|
EmptyMessage,
|
2020-12-11 12:23:50 +01:00
|
|
|
ItemEventMessage,
|
|
|
|
JoinRoomMessage,
|
|
|
|
PlayGlobalMessage,
|
|
|
|
PusherToBackMessage,
|
2021-06-24 10:09:10 +02:00
|
|
|
QueryJitsiJwtMessage,
|
|
|
|
RefreshRoomPromptMessage,
|
2021-07-19 15:57:50 +02:00
|
|
|
RoomMessage,
|
2020-12-11 12:23:50 +01:00
|
|
|
ServerToAdminClientMessage,
|
|
|
|
SilentMessage,
|
|
|
|
UserMovesMessage,
|
2021-07-19 15:57:50 +02:00
|
|
|
VariableMessage,
|
2021-06-24 10:09:10 +02:00
|
|
|
WebRtcSignalToServerMessage,
|
|
|
|
WorldFullWarningToRoomMessage,
|
|
|
|
ZoneMessage,
|
2020-11-13 18:00:22 +01:00
|
|
|
} from "./Messages/generated/messages_pb";
|
2021-06-24 10:09:10 +02:00
|
|
|
import { sendUnaryData, ServerDuplexStream, ServerUnaryCall, ServerWritableStream } from "grpc";
|
|
|
|
import { socketManager } from "./Services/SocketManager";
|
2021-07-19 15:57:50 +02:00
|
|
|
import { emitError, emitErrorOnRoomSocket, emitErrorOnZoneSocket } from "./Services/MessageHelpers";
|
2021-06-24 10:09:10 +02:00
|
|
|
import { User, UserSocket } from "./Model/User";
|
|
|
|
import { GameRoom } from "./Model/GameRoom";
|
2020-11-13 18:00:22 +01:00
|
|
|
import Debug from "debug";
|
2021-06-24 10:09:10 +02:00
|
|
|
import { Admin } from "./Model/Admin";
|
2020-11-13 18:00:22 +01:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
const debug = Debug("roommanager");
|
2020-11-13 18:00:22 +01:00
|
|
|
|
2020-12-10 17:46:15 +01:00
|
|
|
export type AdminSocket = ServerDuplexStream<AdminPusherToBackMessage, ServerToAdminClientMessage>;
|
2021-07-19 10:16:43 +02:00
|
|
|
export type ZoneSocket = ServerWritableStream<ZoneMessage, BatchToPusherMessage>;
|
2021-07-07 17:17:28 +02:00
|
|
|
export type RoomSocket = ServerWritableStream<RoomMessage, BatchToPusherRoomMessage>;
|
2020-11-13 18:00:22 +01:00
|
|
|
|
|
|
|
const roomManager: IRoomManagerServer = {
|
|
|
|
joinRoom: (call: UserSocket): void => {
|
2021-11-24 16:20:07 +01:00
|
|
|
console.log("joinRoom called");
|
2020-11-13 18:00:22 +01:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
let room: GameRoom | null = null;
|
|
|
|
let user: User | null = null;
|
2020-11-13 18:00:22 +01:00
|
|
|
|
2021-07-22 17:14:15 +02:00
|
|
|
call.on("data", (message: PusherToBackMessage) => {
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
if (room === null || user === null) {
|
|
|
|
if (message.hasJoinroommessage()) {
|
|
|
|
socketManager
|
|
|
|
.handleJoinRoom(call, message.getJoinroommessage() as JoinRoomMessage)
|
|
|
|
.then(({ room: gameRoom, user: myUser }) => {
|
|
|
|
if (call.writable) {
|
|
|
|
room = gameRoom;
|
|
|
|
user = myUser;
|
|
|
|
} else {
|
2021-07-27 14:29:09 +02:00
|
|
|
//Connection may have been closed before the init was finished, so we have to manually disconnect the user.
|
2021-07-22 17:14:15 +02:00
|
|
|
socketManager.leaveRoom(gameRoom, myUser);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((e) => emitError(call, e));
|
|
|
|
} else {
|
|
|
|
throw new Error("The first message sent MUST be of type JoinRoomMessage");
|
2021-01-17 03:07:46 +01:00
|
|
|
}
|
2020-11-13 18:00:22 +01:00
|
|
|
} else {
|
2021-07-22 17:14:15 +02:00
|
|
|
if (message.hasJoinroommessage()) {
|
|
|
|
throw new Error("Cannot call JoinRoomMessage twice!");
|
|
|
|
} else if (message.hasUsermovesmessage()) {
|
|
|
|
socketManager.handleUserMovesMessage(
|
|
|
|
room,
|
|
|
|
user,
|
|
|
|
message.getUsermovesmessage() as UserMovesMessage
|
|
|
|
);
|
|
|
|
} else if (message.hasSilentmessage()) {
|
|
|
|
socketManager.handleSilentMessage(room, user, message.getSilentmessage() as SilentMessage);
|
|
|
|
} else if (message.hasItemeventmessage()) {
|
2021-07-22 17:17:25 +02:00
|
|
|
socketManager.handleItemEvent(
|
|
|
|
room,
|
|
|
|
user,
|
|
|
|
message.getItemeventmessage() as ItemEventMessage
|
|
|
|
);
|
2021-07-22 17:14:15 +02:00
|
|
|
} else if (message.hasVariablemessage()) {
|
|
|
|
await socketManager.handleVariableEvent(
|
|
|
|
room,
|
|
|
|
user,
|
|
|
|
message.getVariablemessage() as VariableMessage
|
|
|
|
);
|
|
|
|
} else if (message.hasWebrtcsignaltoservermessage()) {
|
|
|
|
socketManager.emitVideo(
|
|
|
|
room,
|
|
|
|
user,
|
|
|
|
message.getWebrtcsignaltoservermessage() as WebRtcSignalToServerMessage
|
|
|
|
);
|
|
|
|
} else if (message.hasWebrtcscreensharingsignaltoservermessage()) {
|
|
|
|
socketManager.emitScreenSharing(
|
|
|
|
room,
|
|
|
|
user,
|
|
|
|
message.getWebrtcscreensharingsignaltoservermessage() as WebRtcSignalToServerMessage
|
|
|
|
);
|
|
|
|
} else if (message.hasPlayglobalmessage()) {
|
2021-07-22 17:17:25 +02:00
|
|
|
socketManager.emitPlayGlobalMessage(
|
|
|
|
room,
|
|
|
|
message.getPlayglobalmessage() as PlayGlobalMessage
|
|
|
|
);
|
2021-07-22 17:14:15 +02:00
|
|
|
} else if (message.hasQueryjitsijwtmessage()) {
|
|
|
|
socketManager.handleQueryJitsiJwtMessage(
|
|
|
|
user,
|
|
|
|
message.getQueryjitsijwtmessage() as QueryJitsiJwtMessage
|
|
|
|
);
|
|
|
|
} else if (message.hasEmotepromptmessage()) {
|
|
|
|
socketManager.handleEmoteEventMessage(
|
|
|
|
room,
|
|
|
|
user,
|
|
|
|
message.getEmotepromptmessage() as EmotePromptMessage
|
|
|
|
);
|
|
|
|
} else if (message.hasSendusermessage()) {
|
|
|
|
const sendUserMessage = message.getSendusermessage();
|
|
|
|
if (sendUserMessage !== undefined) {
|
|
|
|
socketManager.handlerSendUserMessage(user, sendUserMessage);
|
|
|
|
}
|
|
|
|
} else if (message.hasBanusermessage()) {
|
|
|
|
const banUserMessage = message.getBanusermessage();
|
|
|
|
if (banUserMessage !== undefined) {
|
|
|
|
socketManager.handlerBanUserMessage(room, user, banUserMessage);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error("Unhandled message type");
|
|
|
|
}
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
2021-07-22 17:14:15 +02:00
|
|
|
} catch (e) {
|
2021-11-24 16:20:07 +01:00
|
|
|
console.error(e);
|
2021-07-22 17:14:15 +02:00
|
|
|
emitError(call, e);
|
|
|
|
call.end();
|
2020-11-13 18:00:22 +01:00
|
|
|
}
|
2021-11-24 16:20:07 +01:00
|
|
|
})().catch((e) => console.error(e));
|
2020-11-13 18:00:22 +01:00
|
|
|
});
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
call.on("end", () => {
|
|
|
|
debug("joinRoom ended");
|
2020-11-13 18:00:22 +01:00
|
|
|
if (user !== null && room !== null) {
|
|
|
|
socketManager.leaveRoom(room, user);
|
|
|
|
}
|
|
|
|
call.end();
|
|
|
|
room = null;
|
|
|
|
user = null;
|
|
|
|
});
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
call.on("error", (err: Error) => {
|
2021-11-24 16:20:07 +01:00
|
|
|
console.error("An error occurred in joinRoom stream:", err);
|
2020-11-13 18:00:22 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
listenZone(call: ZoneSocket): void {
|
2021-06-24 10:09:10 +02:00
|
|
|
debug("listenZone called");
|
2020-11-13 18:00:22 +01:00
|
|
|
const zoneMessage = call.request;
|
|
|
|
|
2021-07-19 15:57:50 +02:00
|
|
|
socketManager
|
|
|
|
.addZoneListener(call, zoneMessage.getRoomid(), zoneMessage.getX(), zoneMessage.getY())
|
|
|
|
.catch((e) => {
|
|
|
|
emitErrorOnZoneSocket(call, e.toString());
|
|
|
|
});
|
2020-11-13 18:00:22 +01:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
call.on("cancelled", () => {
|
|
|
|
debug("listenZone cancelled");
|
2021-07-19 15:57:50 +02:00
|
|
|
socketManager
|
|
|
|
.removeZoneListener(call, zoneMessage.getRoomid(), zoneMessage.getX(), zoneMessage.getY())
|
2021-11-24 16:20:07 +01:00
|
|
|
.catch((e) => console.error(e));
|
2020-11-13 18:00:22 +01:00
|
|
|
call.end();
|
2021-06-24 10:09:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
call.on("close", () => {
|
|
|
|
debug("listenZone connection closed");
|
2021-07-19 15:57:50 +02:00
|
|
|
socketManager
|
|
|
|
.removeZoneListener(call, zoneMessage.getRoomid(), zoneMessage.getX(), zoneMessage.getY())
|
2021-11-24 16:20:07 +01:00
|
|
|
.catch((e) => console.error(e));
|
2021-06-24 10:09:10 +02:00
|
|
|
}).on("error", (e) => {
|
2021-11-24 16:20:07 +01:00
|
|
|
console.error("An error occurred in listenZone stream:", e);
|
2021-07-19 15:57:50 +02:00
|
|
|
socketManager
|
|
|
|
.removeZoneListener(call, zoneMessage.getRoomid(), zoneMessage.getX(), zoneMessage.getY())
|
2021-11-24 16:20:07 +01:00
|
|
|
.catch((e) => console.error(e));
|
2020-11-13 18:00:22 +01:00
|
|
|
call.end();
|
|
|
|
});
|
|
|
|
},
|
2020-12-10 17:46:15 +01:00
|
|
|
|
2021-07-07 17:17:28 +02:00
|
|
|
listenRoom(call: RoomSocket): void {
|
|
|
|
debug("listenRoom called");
|
|
|
|
const roomMessage = call.request;
|
|
|
|
|
2021-07-19 15:57:50 +02:00
|
|
|
socketManager.addRoomListener(call, roomMessage.getRoomid()).catch((e) => {
|
2021-07-19 10:16:43 +02:00
|
|
|
emitErrorOnRoomSocket(call, e.toString());
|
|
|
|
});
|
2021-07-07 17:17:28 +02:00
|
|
|
|
|
|
|
call.on("cancelled", () => {
|
|
|
|
debug("listenRoom cancelled");
|
2021-11-24 16:20:07 +01:00
|
|
|
socketManager.removeRoomListener(call, roomMessage.getRoomid()).catch((e) => console.error(e));
|
2021-07-07 17:17:28 +02:00
|
|
|
call.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
call.on("close", () => {
|
|
|
|
debug("listenRoom connection closed");
|
2021-11-24 16:20:07 +01:00
|
|
|
socketManager.removeRoomListener(call, roomMessage.getRoomid()).catch((e) => console.error(e));
|
2021-07-07 17:17:28 +02:00
|
|
|
}).on("error", (e) => {
|
2021-11-24 16:20:07 +01:00
|
|
|
console.error("An error occurred in listenRoom stream:", e);
|
|
|
|
socketManager.removeRoomListener(call, roomMessage.getRoomid()).catch((e) => console.error(e));
|
2020-11-13 18:00:22 +01:00
|
|
|
call.end();
|
|
|
|
});
|
|
|
|
},
|
2020-12-10 17:46:15 +01:00
|
|
|
|
|
|
|
adminRoom(call: AdminSocket): void {
|
2021-11-24 16:20:07 +01:00
|
|
|
console.log("adminRoom called");
|
2020-12-10 17:46:15 +01:00
|
|
|
|
|
|
|
const admin = new Admin(call);
|
2021-06-24 10:09:10 +02:00
|
|
|
let room: GameRoom | null = null;
|
2020-12-10 17:46:15 +01:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
call.on("data", (message: AdminPusherToBackMessage) => {
|
2020-12-10 17:46:15 +01:00
|
|
|
try {
|
|
|
|
if (room === null) {
|
|
|
|
if (message.hasSubscribetoroom()) {
|
|
|
|
const roomId = message.getSubscribetoroom();
|
2021-07-19 15:57:50 +02:00
|
|
|
socketManager
|
|
|
|
.handleJoinAdminRoom(admin, roomId)
|
|
|
|
.then((gameRoom: GameRoom) => {
|
|
|
|
room = gameRoom;
|
|
|
|
})
|
2021-11-24 16:20:07 +01:00
|
|
|
.catch((e) => console.error(e));
|
2020-12-10 17:46:15 +01:00
|
|
|
} else {
|
2021-06-24 10:09:10 +02:00
|
|
|
throw new Error("The first message sent MUST be of type JoinRoomMessage");
|
2020-12-10 17:46:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emitError(call, e);
|
|
|
|
call.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
call.on("end", () => {
|
|
|
|
debug("joinRoom ended");
|
2020-12-10 17:46:15 +01:00
|
|
|
if (room !== null) {
|
|
|
|
socketManager.leaveAdminRoom(room, admin);
|
|
|
|
}
|
|
|
|
call.end();
|
|
|
|
room = null;
|
|
|
|
});
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
call.on("error", (err: Error) => {
|
2021-11-24 16:20:07 +01:00
|
|
|
console.error("An error occurred in joinAdminRoom stream:", err);
|
2020-12-10 17:46:15 +01:00
|
|
|
});
|
|
|
|
},
|
2020-12-11 12:23:50 +01:00
|
|
|
sendAdminMessage(call: ServerUnaryCall<AdminMessage>, callback: sendUnaryData<EmptyMessage>): void {
|
2021-07-19 15:57:50 +02:00
|
|
|
socketManager
|
|
|
|
.sendAdminMessage(call.request.getRoomid(), call.request.getRecipientuuid(), call.request.getMessage())
|
2021-11-24 16:20:07 +01:00
|
|
|
.catch((e) => console.error(e));
|
2020-12-11 12:23:50 +01:00
|
|
|
|
|
|
|
callback(null, new EmptyMessage());
|
|
|
|
},
|
|
|
|
sendGlobalAdminMessage(call: ServerUnaryCall<AdminGlobalMessage>, callback: sendUnaryData<EmptyMessage>): void {
|
2021-06-24 10:09:10 +02:00
|
|
|
throw new Error("Not implemented yet");
|
2020-12-11 12:23:50 +01:00
|
|
|
// TODO
|
|
|
|
callback(null, new EmptyMessage());
|
|
|
|
},
|
|
|
|
ban(call: ServerUnaryCall<BanMessage>, callback: sendUnaryData<EmptyMessage>): void {
|
2021-01-18 15:07:40 +01:00
|
|
|
// FIXME Work in progress
|
2021-07-19 15:57:50 +02:00
|
|
|
socketManager
|
|
|
|
.banUser(call.request.getRoomid(), call.request.getRecipientuuid(), call.request.getMessage())
|
2021-11-24 16:20:07 +01:00
|
|
|
.catch((e) => console.error(e));
|
2020-12-11 12:23:50 +01:00
|
|
|
|
|
|
|
callback(null, new EmptyMessage());
|
|
|
|
},
|
2021-03-01 17:47:00 +01:00
|
|
|
sendAdminMessageToRoom(call: ServerUnaryCall<AdminRoomMessage>, callback: sendUnaryData<EmptyMessage>): void {
|
2021-07-19 10:16:43 +02:00
|
|
|
// FIXME: we could improve return message by returning a Success|ErrorMessage message
|
2021-07-19 15:57:50 +02:00
|
|
|
socketManager
|
2021-07-22 16:26:01 +02:00
|
|
|
.sendAdminRoomMessage(call.request.getRoomid(), call.request.getMessage(), call.request.getType())
|
2021-11-24 16:20:07 +01:00
|
|
|
.catch((e) => console.error(e));
|
2021-03-01 17:47:00 +01:00
|
|
|
callback(null, new EmptyMessage());
|
|
|
|
},
|
2021-06-24 10:09:10 +02:00
|
|
|
sendWorldFullWarningToRoom(
|
|
|
|
call: ServerUnaryCall<WorldFullWarningToRoomMessage>,
|
|
|
|
callback: sendUnaryData<EmptyMessage>
|
|
|
|
): void {
|
2021-07-19 10:16:43 +02:00
|
|
|
// FIXME: we could improve return message by returning a Success|ErrorMessage message
|
2021-11-24 16:20:07 +01:00
|
|
|
socketManager.dispatchWorldFullWarning(call.request.getRoomid()).catch((e) => console.error(e));
|
2021-03-11 16:14:34 +01:00
|
|
|
callback(null, new EmptyMessage());
|
|
|
|
},
|
2021-06-24 10:09:10 +02:00
|
|
|
sendRefreshRoomPrompt(
|
|
|
|
call: ServerUnaryCall<RefreshRoomPromptMessage>,
|
|
|
|
callback: sendUnaryData<EmptyMessage>
|
|
|
|
): void {
|
2021-07-19 10:16:43 +02:00
|
|
|
// FIXME: we could improve return message by returning a Success|ErrorMessage message
|
2021-11-24 16:20:07 +01:00
|
|
|
socketManager.dispatchRoomRefresh(call.request.getRoomid()).catch((e) => console.error(e));
|
2021-04-01 16:43:12 +02:00
|
|
|
callback(null, new EmptyMessage());
|
|
|
|
},
|
2020-11-13 18:00:22 +01:00
|
|
|
};
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
export { roomManager };
|