@@ -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+ }
0 commit comments