1+ import { MODULE_ID } from "./settings.js" ;
2+
13const DEFAULT_CLOCK = {
24 type : "clock" ,
35 value : 0 ,
@@ -26,19 +28,30 @@ export class ClockDatabase extends Collection {
2628 delete ( id ) {
2729 const clocks = this . #getClockData( ) ;
2830 delete clocks [ id ] ;
29- game . settings . set ( "global-progress-clocks" , "activeClocks" , clocks ) ;
31+ game . settings . set ( MODULE_ID , "activeClocks" , clocks ) ;
3032 }
3133
32- update ( data ) {
34+ async update ( data ) {
3335 if ( ! this . #verifyClockData( data ) ) return ;
3436
3537 const clocks = this . #getClockData( ) ;
3638 const existing = clocks [ data . id ] ;
3739 if ( ! existing ) return ;
3840
39- foundry . utils . mergeObject ( existing , data ) ;
40- existing . value = Math . clamp ( existing . value , 0 , existing . max ) ;
41- game . settings . set ( "global-progress-clocks" , "activeClocks" , clocks ) ;
41+ const newData = foundry . utils . mergeObject ( foundry . utils . duplicate ( existing ) , data ) ;
42+ const newValue = Math . clamp ( newData . value , 0 , newData . max ) ;
43+ if ( game . user . hasPermission ( 'SETTINGS_MODIFY' ) ) {
44+ Object . assign ( existing , newData ) ;
45+ existing . value = newValue ;
46+ await game . settings . set ( MODULE_ID , "activeClocks" , clocks ) ;
47+ } else if ( this . canUserEdit ( game . user ) ) {
48+ const gm = game . users . activeGM ;
49+ if ( gm ) {
50+ await gm . query ( "global-progress-clocks" , { action : "update" , clock : { id : newData . id , value : newValue } } ) ;
51+ } else {
52+ ui . notifications . warn ( "GlobalProgressClocks.Warnings.NoActiveGM" , { localize : true } ) ;
53+ }
54+ }
4255 }
4356
4457 move ( id , idx ) {
@@ -50,11 +63,17 @@ export class ClockDatabase extends Collection {
5063 clocks . splice ( idx , 0 , item ) ;
5164
5265 const newData = Object . fromEntries ( clocks . map ( ( c ) => [ c . id , c ] ) ) ;
53- game . settings . set ( "global-progress-clocks" , "activeClocks" , newData ) ;
66+ game . settings . set ( MODULE_ID , "activeClocks" , newData ) ;
67+ }
68+
69+ canUserEdit ( user ) {
70+ // return user.hasPermission('SETTINGS_MODIFY');
71+ const requiredPermission = game . settings . get ( MODULE_ID , "minimumEditorRole" ) ;
72+ return user . role >= requiredPermission ;
5473 }
5574
5675 #getClockData( ) {
57- const entries = game . settings . get ( "global-progress-clocks" , "activeClocks" ) ;
76+ const entries = game . settings . get ( MODULE_ID , "activeClocks" ) ;
5877 for ( const key of Object . keys ( entries ) ) {
5978 entries [ key ] = { ...DEFAULT_CLOCK , ...entries [ key ] } ;
6079 }
@@ -72,6 +91,16 @@ export class ClockDatabase extends Collection {
7291 }
7392 }
7493
94+ handleQuery = async ( data ) => {
95+ const action = data . action ;
96+ if ( action === "update" ) {
97+ if ( ! game . user . isGM ) return ;
98+ const clock = data . clock ;
99+ await this . update ( { id : clock . id , value : clock . value } ) ;
100+ return { ok : true } ;
101+ }
102+ }
103+
75104 // Limit the clock max size to 128
76105 #verifyClockData( data ) {
77106 const maxSize = data . type === "points" ? 99 : 128 ;
0 commit comments