Data Request API helper #10
3 changed files with 72 additions and 8 deletions
|
|
@ -1,16 +1,15 @@
|
|||
import { cancelRequest } from "./cancelRequest.mjs";
|
||||
import { createNotif } from "./createNotif.mjs";
|
||||
import { dataRequest } from "./dataRequest.mjs";
|
||||
import { localizer } from "../utils/Localizer.mjs";
|
||||
import { Logger } from "../utils/Logger.mjs";
|
||||
import { submitRequest } from "./submitRequest.mjs";
|
||||
|
||||
const events = {
|
||||
// Data Request sockets
|
||||
cancelRequest,
|
||||
createNotif,
|
||||
dataRequest,
|
||||
submitRequest,
|
||||
"query.cancel": cancelRequest,
|
||||
"query.prompt": dataRequest,
|
||||
"query.submit": submitRequest,
|
||||
};
|
||||
|
||||
export function registerSockets() {
|
||||
|
|
@ -19,12 +18,12 @@ export function registerSockets() {
|
|||
game.socket.on(`system.taf`, (data, userID) => {
|
||||
const { event, payload } = data ?? {};
|
||||
if (event == null || payload === undefined) {
|
||||
ui.notifications.error(localizer(`taf.notifs.error.invalid-socket`));
|
||||
ui.notifications.error(game.i18n.format(`taf.notifs.error.invalid-socket`));
|
||||
return;
|
||||
};
|
||||
|
||||
if (events[event] == null) {
|
||||
ui.notifications.error(localizer(`taf.notifs.error.unknown-socket-event`, { event }));
|
||||
ui.notifications.error(game.i18n.format(`taf.notifs.error.unknown-socket-event`, { event }));
|
||||
return;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +1,45 @@
|
|||
export function dataRequest() {};
|
||||
import { DialogManager } from "../utils/DialogManager.mjs";
|
||||
|
||||
export async function dataRequest(payload) {
|
||||
const {
|
||||
id,
|
||||
users,
|
||||
config,
|
||||
request,
|
||||
} = payload;
|
||||
|
||||
if (!id) {
|
||||
ui.notifications.error(game.i18n.format(
|
||||
`taf.notifs.error.malformed-socket-payload`,
|
||||
{
|
||||
event: `query.prompt`,
|
||||
details: `A request ID must be provided`,
|
||||
}),
|
||||
);
|
||||
return;
|
||||
};
|
||||
|
||||
// null/undefined is a special case for "all users but me" by default
|
||||
if (!Array.isArray(users) || users != null) {
|
||||
ui.notifications.error(game.i18n.format(
|
||||
`taf.notifs.error.malformed-socket-payload`,
|
||||
{
|
||||
event: `query.prompt`,
|
||||
details: ``,
|
||||
}),
|
||||
);
|
||||
return;
|
||||
};
|
||||
|
||||
if (!users.includes(game.user.id)) { return };
|
||||
|
||||
request.id = id;
|
||||
const result = await DialogManager.ask(request, config);
|
||||
if (result.state === `fronted`) { return };
|
||||
if (result.state === `errored`) {
|
||||
ui.notifications.error(result.error);
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1 +1,22 @@
|
|||
export function submitRequest() {};
|
||||
import { QueryManager } from "../utils/QueryManager.mjs";
|
||||
|
||||
export function submitRequest(payload, user) {
|
||||
const {
|
||||
id,
|
||||
answers,
|
||||
} = payload;
|
||||
|
||||
if (!id) {
|
||||
ui.notifications.error(game.i18n.format(
|
||||
`taf.notifs.error.malformed-socket-payload`,
|
||||
{
|
||||
event: `query.submit`,
|
||||
details: `A request ID must be provided`,
|
||||
}),
|
||||
);
|
||||
return;
|
||||
};
|
||||
|
||||
if (!QueryManager.has(id)) { return };
|
||||
QueryManager.addResponse(id, user.id, answers);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue