Skip to content

Commit

Permalink
Merge pull request #15 from efrei-craft/dev
Browse files Browse the repository at this point in the history
Update events
  • Loading branch information
JiveOff authored Sep 5, 2023
2 parents 628135e + 8ae41e3 commit 47c0164
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/realms/rest/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export const emitter = new EventEmitter() as Emitter<EmitterMessage>
emitter.setMaxListeners(0)

export enum EmitterMessageTypes {
SET_SUBSCRIPTIONS = "SET_SUBSCRIPTIONS",
HELLO = "HELLO",
setSubscriptions = "setSubscriptions",
hello = "hello",

ONLINE_PLAYERS_CHANGED = "ONLINE_PLAYERS_CHANGED"
serverPlayersChanged = "serverPlayersChanged"
}

export type EmitterMessageType = keyof typeof EmitterMessageTypes
Expand All @@ -19,19 +19,19 @@ export const emitterMessageTypes = Type.Enum(EmitterMessageTypes)
export const emitterMessage = Type.Union(
[
Type.Object({
type: Type.Literal("SET_SUBSCRIPTIONS"),
type: Type.Literal("setSubscriptions"),
payload: Type.Object({
subscriptions: Type.Array(emitterMessageTypes)
})
}),
Type.Object({
type: Type.Literal("HELLO"),
type: Type.Literal("hello"),
payload: Type.Object({
ok: Type.Boolean()
})
}),
Type.Object({
type: Type.Literal("ONLINE_PLAYERS_CHANGED"),
type: Type.Literal("serverPlayersChanged"),
payload: Type.Object({})
})
],
Expand Down
4 changes: 2 additions & 2 deletions src/realms/rest/endpoints/controllers/Player.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ export default class PlayerController {
true,
req.body.username
)
emitter.emit("ONLINE_PLAYERS_CHANGED", {})
return reply.code(200).send(fetchedPlayer)
}

Expand Down Expand Up @@ -162,7 +161,7 @@ export default class PlayerController {
const fetchedPlayer = await this.playerService.disconnectPlayer(
req.params.uuid
)
emitter.emit("ONLINE_PLAYERS_CHANGED", {})
emitter.emit("serverPlayersChanged", {})
return reply.code(200).send(fetchedPlayer)
}

Expand Down Expand Up @@ -193,6 +192,7 @@ export default class PlayerController {
} catch (_) {
/* empty */
}
emitter.emit("serverPlayersChanged", {})
return reply.code(200).send(serverPlayer)
}

Expand Down
6 changes: 3 additions & 3 deletions src/realms/rest/endpoints/services/Misc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export default class MiscService {
try {
const body = JSON.parse(message.toString()) as EmitterMessage

if (body.type === "SET_SUBSCRIPTIONS") {
if (body.type === "setSubscriptions") {
const subscriptions = new Set(body.payload.subscriptions)
this.websockets.set(connection, subscriptions)
} else if (body.type === "HELLO") {
emitter.emit("HELLO", { ok: true })
} else if (body.type === "hello") {
emitter.emit("hello", { ok: true })
}
} catch (e) {
connection.socket.off("message", handleMessage)
Expand Down
2 changes: 1 addition & 1 deletion src/realms/rest/helpers/decorators/IsWSAuthenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function IsWSAuthenticated() {
req.key = await fetchApiKey(key)

connection.socket.send(
JSON.stringify({ type: "HELLO", payload: { ok: true } })
JSON.stringify({ type: "hello", payload: { ok: true } })
)
} catch (e) {
connection.socket.close(401, e.message)
Expand Down

0 comments on commit 47c0164

Please sign in to comment.