Skip to content

Commit efc3dc9

Browse files
authored
Changed socket-messaging from chat to user.query (#25)
* Changed socket-messaging from chat to user.query * Refactor executeOnOwner to always call owner Removed direct execution for the document owner in executeOnOwner method.
1 parent 56be492 commit efc3dc9

File tree

6 files changed

+80
-49
lines changed

6 files changed

+80
-49
lines changed

src/model/message.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ export class WarhammerMessageModel extends foundry.abstract.DataModel
7171
game.canvas.tokens.setTargets([]);
7272

7373
applyData.messageId = messageId;
74-
for(let target of targets)
75-
{
76-
await target.applyEffect(applyData);
77-
}
74+
75+
await Promise.all(
76+
targets.map(target => target.applyEffect(applyData))
77+
);
7878
}
7979

8080
static async onPlaceAreaEffect(ev, target)

src/sheets/actor.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ export class WarhammerActorSheet extends WarhammerSheetMixin(foundry.appv1.sheet
5757
return;
5858
}
5959
game.canvas.tokens.setTargets([]);
60-
61-
for (let target of targets)
62-
{
63-
await target.applyEffect({effectData});
64-
}
60+
61+
await Promise.all(
62+
targets.map(target => target.applyEffect(effectData))
63+
);
6564
}
6665

6766
async _onPlaceAreaEffect(ev)

src/sheets/v2/actor.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,9 @@ export default class WarhammerActorSheetV2 extends WarhammerSheetMixinV2(Handleb
240240
}
241241
game.canvas.tokens.setTargets([]);
242242

243-
for (let target of targets)
244-
{
245-
await target.applyEffect({effectData});
246-
}
243+
await Promise.all(
244+
targets.map(target => target.applyEffect(effectData))
245+
);
247246
}
248247

249248
async _onPlaceAreaEffect(effect)

src/util/area-template.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,15 @@ export default class AreaTemplate extends foundry.canvas.placeables.MeasuredTemp
220220
let now = Date.now(); // Apply a 20ms throttle
221221
if ( now - this.#moveTime <= 20 ) {return;}
222222
const center = event.data.getLocalPosition(this.layer);
223-
const snapped = canvas.grid.getSnappedPosition(center.x, center.y, 2);
224-
this.document.updateSource({x: snapped.x, y: snapped.y});
223+
if (!canvas.grid.isGridless)
224+
{
225+
const snapped = canvas.grid.getSnappedPosition(center.x, center.y, 2);
226+
this.document.updateSource({x: snapped.x, y: snapped.y});
227+
}
228+
else
229+
{
230+
this.document.updateSource({x: center.x, y: center.y});
231+
}
225232
this.refresh();
226233
this.#moveTime = now;
227234
if (this.document.getFlag(game.system.id, "target"))

src/util/socket-handlers.js

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,90 @@ export class SocketHandlers
99
static register(handlers)
1010
{
1111
Object.assign(this, handlers);
12-
game.socket.on(`system.${game.system.id}`, data =>
13-
{
14-
this[data.type]({...data.payload}, data.userId);
15-
});
12+
CONFIG.queries[`${game.system.id}.updateActor`] = (queryData, {timeout}) => { return this.updateActor(queryData); };
13+
CONFIG.queries[`${game.system.id}.updateMessage`] = (queryData, {timeout}) => { return this.updateMessage(queryData); };
14+
CONFIG.queries[`${game.system.id}.updateDrawing`] = (queryData, {timeout}) => { return this.updateDrawing(queryData); };
15+
CONFIG.queries[`${game.system.id}.applyEffect`] = (queryData, {timeout, userId}) => { return this.applyEffect(queryData, userId); };
16+
CONFIG.queries[`${game.system.id}.applyZoneEffect`] = (queryData, {timeout, userId}) => { return this.applyZoneEffect(queryData, userId); };
17+
CONFIG.queries[`${game.system.id}.createActor`] = (queryData, {timeout}) => { return this.createActor(queryData); };
1618
}
1719

18-
static call(type, payload, userId)
20+
static async call(type, payload, userId)
1921
{
20-
game.socket.emit(`system.${game.system.id}`, {type, payload, userId});
22+
let result = [];
23+
let users = [];
24+
if (userId == "GM")
25+
{
26+
users.push(game.users.activeGM);
27+
}
28+
else if (userId == "ALL")
29+
{
30+
users = game.users.filter(u => u.active);
31+
}
32+
else if (userId)
33+
{
34+
const user = game.users.get(userId);
35+
users.push(user);
36+
}
37+
for (const user of users)
38+
{
39+
if (user.id == game.user.id)
40+
{
41+
result.push(this[type]({ ...payload }) ?? 0);
42+
}
43+
else
44+
{
45+
result.push(user.query(`${game.system.id}.${type}`, payload) ?? 0);
46+
}
47+
}
48+
return await Promise.all(result);
2149
}
2250

23-
static updateActor({speaker, update}={})
51+
static async updateActor({speaker, update}={})
2452
{
53+
let result = null;
2554
if (game.user.isGM)
2655
{
27-
return CONFIG.ChatMessage.documentClass.getSpeakerActor(speaker)?.update(update);
56+
result = await CONFIG.ChatMessage.documentClass.getSpeakerActor(speaker)?.update(update);
2857
}
58+
return result;
2959
}
3060

31-
static updateMessage({id, data}={})
61+
static async updateMessage({id, data}={})
3262
{
63+
let result = null;
3364
if (game.user.isGM)
3465
{
35-
return game.messages.get(id)?.update(data);
66+
result = await game.messages.get(id)?.update(data);
3667
}
68+
return result;
3769
}
3870

39-
static updateDrawing({uuid, data}={})
71+
static async updateDrawing({uuid, data}={})
4072
{
73+
let result = null;
4174
if (game.user.isGM)
4275
{
43-
return fromUuidSync(uuid).update(data);
76+
result = await fromUuidSync(uuid).update(data);
4477
}
78+
return result;
4579
}
4680

47-
static applyEffect({effectUuids, effectData, actorUuid, messageId}, userId)
81+
static async applyEffect({effectUuids, effectData, actorUuid, messageId})
4882
{
49-
if (game.user.id == userId)
50-
{
51-
return fromUuidSync(actorUuid)?.applyEffect({effectUuids, effectData, messageId});
52-
}
83+
const result = await fromUuidSync(actorUuid)?.applyEffect({effectUuids, effectData, messageId});
84+
return result;
5385
}
5486

55-
static applyZoneEffect({effectUuids, effectData, regionUuid, messageId}, userId)
87+
static async applyZoneEffect({effectUuids, effectData, regionUuid, messageId})
5688
{
57-
if (game.user.id == userId)
58-
{
59-
return ZoneHelpers.applyEffectToZone({effectUuids, effectData}, fromUuidSync(regionUuid), messageId);
60-
}
89+
const result = await ZoneHelpers.applyEffectToZone({effectUuids, effectData}, fromUuidSync(regionUuid), messageId);
90+
return result;
6191
}
6292

63-
static createActor(data)
93+
static async createActor(data)
6494
{
95+
let result = null;
6596
if (game.user.id == game.users.activeGM?.id)
6697
{
6798
let id = data.fromId;
@@ -71,11 +102,11 @@ export class SocketHandlers
71102
default: 0,
72103
[id]: 3
73104
};
74-
return Actor.implementation.create(actorData, {keepId : true});
105+
result = await Actor.implementation.create(actorData, {keepId : true});
75106
}
107+
return result;
76108
}
77109

78-
79110
/**
80111
* Not used by sockets directly, but is called when a socket handler should be executed by
81112
* the specific user which owns a document. Usually used to invoke tests from other users
@@ -85,15 +116,10 @@ export class SocketHandlers
85116
* @param {object} payload Data for socket handler, should generally include document UUID
86117
* @returns {any} If owner, returns socket operation performed
87118
*/
88-
static executeOnOwner(document, type, payload)
119+
static async executeOnOwner(document, type, payload)
89120
{
90121
let ownerUser = getActiveDocumentOwner(document);
91-
if (game.user.id == ownerUser.id)
92-
{
93-
return this[type](payload);
94-
}
95-
// ui.notifications.notify(game.i18n.format("WH.SendingSocketRequest", {name : ownerUser.name}));
96-
this.call(type, payload, ownerUser.id);
122+
return await this.call(type, payload, ownerUser.id);
97123
}
98124

99-
}
125+
}

src/util/zone-helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ export default class ZoneHelpers
396396
}
397397
else
398398
{
399-
SocketHandlers.executeOnOwner(region, "applyZoneEffect", {effectUuids, effectData, regionUuid : region.uuid, messageId});
399+
SocketHandlers.call("applyZoneEffect", {effectUuids, effectData, regionUuid : region.uuid, messageId}, "GM");
400400
}
401401
}
402402

0 commit comments

Comments
 (0)