Get the QueryStatus application displaying the status more appropriately
This commit is contained in:
parent
5770abb7e8
commit
0362342419
11 changed files with 117 additions and 14 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { __ID__, filePath } from "../consts.mjs";
|
||||
import { Logger } from "../utils/Logger.mjs";
|
||||
import { QueryManager } from "../utils/QueryManager.mjs";
|
||||
|
||||
const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api;
|
||||
|
|
@ -10,15 +11,22 @@ export class QueryStatus extends HandlebarsApplicationMixin(ApplicationV2) {
|
|||
__ID__,
|
||||
`QueryStatus`,
|
||||
],
|
||||
position: {
|
||||
width: 300,
|
||||
height: `auto`,
|
||||
},
|
||||
window: {
|
||||
resizable: true,
|
||||
},
|
||||
};
|
||||
|
||||
static PARTS = {
|
||||
users: {
|
||||
template: filePath(`templates/QueryStatus/users.hbs`),
|
||||
},
|
||||
// controls: {
|
||||
// template: filePath(`templates/QueryStatus/controls.hbs`),
|
||||
// },
|
||||
controls: {
|
||||
template: filePath(`templates/QueryStatus/controls.hbs`),
|
||||
},
|
||||
};
|
||||
// #endregion Options
|
||||
|
||||
|
|
@ -27,6 +35,10 @@ export class QueryStatus extends HandlebarsApplicationMixin(ApplicationV2) {
|
|||
requestID,
|
||||
...opts
|
||||
}) {
|
||||
if (!requestID) {
|
||||
Logger.error(`A requestID must be provided for QueryStatus applications`);
|
||||
return null;
|
||||
};
|
||||
super(opts);
|
||||
this.requestID = requestID;
|
||||
};
|
||||
|
|
@ -60,7 +72,7 @@ export class QueryStatus extends HandlebarsApplicationMixin(ApplicationV2) {
|
|||
users.push({
|
||||
id: userID,
|
||||
name: user.name,
|
||||
colour: user.color,
|
||||
active: user.active,
|
||||
answers: query.responses[userID] ?? null,
|
||||
});
|
||||
};
|
||||
|
|
|
|||
5
module/hooks/userConnected.mjs
Normal file
5
module/hooks/userConnected.mjs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import { QueryManager } from "../utils/QueryManager.mjs";
|
||||
|
||||
Hooks.on(`userConnected`, (user) => {
|
||||
QueryManager.userActivity(user.id);
|
||||
});
|
||||
|
|
@ -1,2 +1,3 @@
|
|||
import "./api.mjs";
|
||||
import "./hooks/init.mjs";
|
||||
import "./hooks/userConnected.mjs";
|
||||
|
|
|
|||
|
|
@ -4,9 +4,12 @@
|
|||
* @property {Function} resolve
|
||||
* @property {Record<string, object>} responses
|
||||
* @property {(() => Promise<void>)|null} onSubmit
|
||||
* @property {QueryStatus|null} app
|
||||
*/
|
||||
|
||||
import { filePath } from "../consts.mjs";
|
||||
import { Logger } from "./Logger.mjs";
|
||||
import { QueryStatus } from "../apps/QueryStatus.mjs";
|
||||
|
||||
async function sendBasicNotification(userID, answers) {
|
||||
const content = await foundry.applications.handlebars.renderTemplate(
|
||||
|
|
@ -34,7 +37,7 @@ export class QueryManager {
|
|||
delete cloned.onSubmit;
|
||||
delete cloned.resolve;
|
||||
|
||||
return cloned;
|
||||
return foundry.utils.deepFreeze(cloned);
|
||||
};
|
||||
|
||||
static async query(
|
||||
|
|
@ -42,7 +45,8 @@ export class QueryManager {
|
|||
{
|
||||
onSubmit = sendBasicNotification,
|
||||
users = null,
|
||||
config = undefined,
|
||||
showStatusApp = true,
|
||||
...config
|
||||
} = {},
|
||||
) {
|
||||
if (!request.id) {
|
||||
|
|
@ -68,13 +72,21 @@ export class QueryManager {
|
|||
this.#queries.set(
|
||||
request.id,
|
||||
{
|
||||
users: users ?? game.users.filter(u => u.id !== game.user.id),
|
||||
users: users ?? game.users.filter(u => u.id !== game.user.id).map(u => u.id),
|
||||
resolve,
|
||||
responses: {},
|
||||
onSubmit,
|
||||
app: null,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
if (showStatusApp) {
|
||||
const app = new QueryStatus({ requestID: request.id });
|
||||
app.render({ force: true });
|
||||
this.#queries.get(request.id).app = app;
|
||||
};
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
|
|
@ -86,7 +98,10 @@ export class QueryManager {
|
|||
|
||||
// Validate for responses from everyone
|
||||
if (data.users.length === Object.keys(data.responses).length) {
|
||||
data.app.close();
|
||||
data.resolve(data.responses);
|
||||
} else {
|
||||
data.app?.render({ parts: [ `users` ] });
|
||||
};
|
||||
};
|
||||
|
||||
|
|
@ -110,4 +125,27 @@ export class QueryManager {
|
|||
payload: { id: requestID },
|
||||
});
|
||||
};
|
||||
|
||||
static async setApplication(requestID, app) {
|
||||
if (!this.#queries.has(requestID)) { return };
|
||||
if (!(app instanceof QueryStatus)) { return };
|
||||
const query = this.#queries.get(requestID);
|
||||
if (query.app) {
|
||||
Logger.error(`Cannot set an application for a query that has one already`);
|
||||
return;
|
||||
};
|
||||
query.app = app;
|
||||
};
|
||||
|
||||
static async userActivity(userID) {
|
||||
for (const query of this.#queries.values()) {
|
||||
if (query.users.includes(userID)) {
|
||||
query.app.render({ parts: [ `users` ] });
|
||||
|
||||
// TODO: if the user is connecting, we want to open
|
||||
// the ask modal on their browser so that they can
|
||||
// actually fill in the data
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue