Add socket event handling foundations and an updateSands event in anticipation of hasty rolls
This commit is contained in:
parent
7c0fb75e0f
commit
c0a9731b02
5 changed files with 73 additions and 1 deletions
27
module/sockets/_index.mjs
Normal file
27
module/sockets/_index.mjs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { localizer } from "../utils/Localizer.mjs";
|
||||
import { Logger } from "../utils/Logger.mjs";
|
||||
import { updateSands } from "./updateSands.mjs";
|
||||
|
||||
const events = {
|
||||
updateSands,
|
||||
};
|
||||
|
||||
export function registerSockets() {
|
||||
Logger.info(`Setting up socket listener`);
|
||||
|
||||
game.socket.on(`system.${game.system.id}`, (data, userID) => {
|
||||
const { event, payload } = data ?? {};
|
||||
if (event == null || payload === undefined) {
|
||||
ui.notifications.error(localizer(`RipCrypt.notifs.error.invalid-socket`));
|
||||
return;
|
||||
};
|
||||
|
||||
if (events[event] == null) {
|
||||
ui.notifications.error(localizer(`RipCrypt.notifs.error.unknown-socket-event`, { event }));
|
||||
return;
|
||||
};
|
||||
|
||||
const user = game.users.get(userID);
|
||||
events[event](payload, user);
|
||||
});
|
||||
};
|
||||
36
module/sockets/updateSands.mjs
Normal file
36
module/sockets/updateSands.mjs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { clamp } from "../utils/clamp.mjs";
|
||||
import { localizer } from "../utils/Localizer.mjs";
|
||||
|
||||
export function updateSands(payload) {
|
||||
if (!game.user.isActiveGM) { return };
|
||||
|
||||
// Assert payload validity
|
||||
const { value, delta } = payload;
|
||||
if (value == null && delta == null) {
|
||||
ui.notifications.error(localizer(
|
||||
`RipCrypt.notifs.error.malformed-socket-payload`,
|
||||
{
|
||||
event: `updateSands`,
|
||||
details: `Either value or delta must be provided`,
|
||||
},
|
||||
));
|
||||
return;
|
||||
};
|
||||
|
||||
// Take action
|
||||
if (value != null) {
|
||||
const initial = game.settings.get(game.system.id, `sandsOfFateInitial`);
|
||||
let sands = clamp(0, value, initial);
|
||||
if (sands === 0) {
|
||||
ui.delveDice.alertCrypticEvent();
|
||||
sands = initial;
|
||||
};
|
||||
game.settings.set(
|
||||
game.system.id,
|
||||
`sandsOfFate`,
|
||||
sands,
|
||||
);
|
||||
} else if (delta != null) {
|
||||
ui.delveDice.sandsOfFateDelta(delta);
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue