-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting2.js
More file actions
632 lines (555 loc) · 21.8 KB
/
testing2.js
File metadata and controls
632 lines (555 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*:
* @target MZ
* @plugindesc Manages dynamic switches and provides enhanced functionality for RPG Maker MZ.
* @author Undermax Games | Maxii1996
* @url https://undermax.itch.io/
* @help
*
* @param lockSwitches
* @text Lock Switches
* @type switch[]
* @desc Identifies switches that cannot be manually modified in-game.
*
* @param initActiveSwitches
* @text Initialize Active Switches
* @type switch[]
* @desc Select switches to initialize as active when the game starts.
*
* @command setDynamicSwitch
* @text Set Dynamic Switch
* @desc Activates a dynamic switch that will be deactivated after a specified time period.
*
* @arg switchId
* @text Switch ID
* @type number
* @desc ID of the switch to be deactivated.
*
* @arg unit
* @text Time Unit
* @type select
* @option Seconds
* @option Minutes
* @option Hours
* @option Days
* @desc Unit of time to wait before the switch is activated.
*
* @arg amount
* @text Amount
* @type number
* @desc Amount of time to wait before the switch is activated.
*
* @command unsetDynamicSwitch
* @text Unset Dynamic Switch
* @desc Deactivates a dynamic switch, removing it from the list of active switches.
*
* @arg switchId
* @text Switch ID
* @type number
* @desc ID of the switch to be deactivated.
*
* @command showSwitchStatus
* @text Show Switch Status
* @desc Displays a scene with the status of all pending switches.
*
* @command resetAllDynamicSwitches
* @text Reset All Dynamic Switches
* @desc Resets all dynamic switches to true, maintaining the locked logic from the plugin configuration.
*
* @command getTimeRemainingForSwitch
* @text Get Time Remaining for Switch
* @desc Retrieves the remaining time for a switch and stores it in a variable.
*
* @arg switchId
* @text Switch ID
* @type number
* @desc ID of the switch for which to get the remaining time.
*
* @arg variableId
* @text Variable ID
* @type variable
* @desc ID of the game variable where the remaining time will be stored.
*
* @command showProgressBarInEvent
* @text Show Progress Bar in Event
* @desc Displays a progress bar in a specific event. If used after setting a switch, a delay of at least 10 frames is recommended.
*
* @arg eventId
* @text Event ID
* @type number
* @desc ID of the event where the progress bar will be displayed.
*
* @arg switchId
* @text Dynamic Switch ID
* @type number
* @desc ID of the dynamic switch from which the information will be retrieved.
*
* @arg backgroundColor
* @text Background Color
* @type text
* @default gray
* @desc Background color of the progress bar.
*
* @arg barColor
* @text Bar Color
* @type text
* @default green
* @desc Color of the progress bar.
*
* @arg width
* @text Width
* @type number
* @min 1
* @default 100
* @desc Width of the progress bar.
*
* @arg height
* @text Height
* @type number
* @min 1
* @default 10
* @desc Height of the progress bar.
*
* @arg offsetX
* @text Offset X
* @type number
* @default 0
* @desc Horizontal offset of the progress bar from the event's position.
*
* @arg offsetY
* @text Offset Y
* @type number
* @default -40
* @desc Vertical offset of the progress bar from the event's position.
*
* @arg showTime
* @text Display Timer on Bar?
* @type boolean
* @default false
* @desc To display correctly, set the Height to a large number, e.g., 24 or more.
*
* @command setProgressBarVisibility
* @text Set Progress Bar Visibility
* @desc Sets the visibility of the progress bar. True to display the progress bar, false to hide it.
*
* @arg visibility
* @text Visibility
* @type boolean
* @default true
*/
const fs = require('fs');
const crypto = require('crypto');
const path = require('path');
const baseGamePath = path.dirname(window.location.pathname).replace(/^\/|\/$/g, '');
const filePath = path.join(baseGamePath, 'js', 'gameData.dat');
const keyPath = path.join(baseGamePath, 'js', 'gameConfig.dat');
const iv = crypto.randomBytes(16);
let secretKey;
if (fs.existsSync(keyPath)) {
secretKey = fs.readFileSync(keyPath, 'utf8');
} else {
secretKey = crypto.randomBytes(32).toString('hex');
fs.writeFileSync(keyPath, secretKey, 'utf8');
}
function encrypt(text) {
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(secretKey, 'hex'), iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
function decrypt(text) {
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(secretKey, 'hex'), iv);
let decrypted = decipher.update(text, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
(() => {
const pluginParameters = PluginManager.parameters('DynamicSwitch');
const initialLockedSwitches = JSON.parse(pluginParameters.lockSwitches || '[]').map(Number);
const initActiveSwitches = JSON.parse(pluginParameters.initActiveSwitches || '[]').map(Number);
let isNewGame = false;
const _DataManager_setupNewGame = DataManager.setupNewGame;
DataManager.setupNewGame = function () {
_DataManager_setupNewGame.call(this);
isNewGame = true;
};
const _Game_Map_setup = Game_Map.prototype.setup;
Game_Map.prototype.setup = function (mapId) {
_Game_Map_setup.call(this, mapId);
if (isNewGame) {
this.initializeSwitches();
isNewGame = false;
}
};
Game_Map.prototype.initializeSwitches = function () {
$gameSwitches._allowingSet = true;
initActiveSwitches.forEach(switchId => {
$gameSwitches.setValue(switchId, true);
});
$gameSwitches._allowingSet = false;
};
class DynamicSwitch {
constructor(switchId, activationTime, onlineObtained) {
this.switchId = switchId;
this.activationTime = activationTime;
this.onlineObtained = onlineObtained;
}
}
let dynamicSwitches = [];
try {
if (fs.existsSync(filePath)) {
const savedData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
const iv = Buffer.from(savedData.iv, 'hex');
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(secretKey, 'hex'), iv);
let decrypted = decipher.update(savedData.data, 'hex', 'utf8');
decrypted += decipher.final('utf8');
try {
dynamicSwitches = JSON.parse(decrypted) || [];
} catch (error) {
if (error instanceof SyntaxError) {
} else {
throw error;
}
}
}
} catch (error) {
}
const _Game_Switches_setValue = Game_Switches.prototype.setValue;
Game_Switches.prototype.setValue = function (switchId, value) {
if (!this._allowingSet && (initialLockedSwitches.includes(switchId) || dynamicSwitches.some(ds => ds.switchId === switchId))) return;
_Game_Switches_setValue.call(this, switchId, value);
};
PluginManager.registerCommand('DynamicSwitch', 'setDynamicSwitch', async args => {
let onlineObtained = false;
let currentTime;
try {
currentTime = await getCurrentTime();
onlineObtained = true;
} catch (error) {
onlineObtained = false;
currentTime = new Date().getTime();
}
try {
const switchId = Number(args.switchId);
const existingSwitch = dynamicSwitches.find(ds => ds.switchId === switchId);
if (existingSwitch) return;
const unit = args.unit;
const amount = Number(args.amount);
let multiplier;
switch (unit) {
case 'Seconds': multiplier = 1000; break;
case 'Minutes': multiplier = 1000 * 60; break;
case 'Hours': multiplier = 1000 * 60 * 60; break;
case 'Days': multiplier = 1000 * 60 * 60 * 24; break;
default: throw new Error('Invalid time unit');
}
const activationTime = currentTime + (amount * multiplier);
$gameSwitches._allowingSet = true;
$gameSwitches.setValue(switchId, false);
$gameSwitches._allowingSet = false;
dynamicSwitches.push(new DynamicSwitch(switchId, activationTime, onlineObtained));
localStorage.setItem('dynamicSwitches', JSON.stringify(dynamicSwitches));
} catch (error) {
}
});
Scene_Map.prototype.removeProgressBar = function (switchId) {
if (this._progressBar && this._progressBar.switchId === switchId) {
this.removeChild(this._progressBar);
this._progressBar = null;
progressBarInfo = null;
}
};
PluginManager.registerCommand('DynamicSwitch', 'unsetDynamicSwitch', args => {
const switchId = Number(args.switchId);
dynamicSwitches = dynamicSwitches.filter(ds => ds.switchId !== switchId);
localStorage.setItem('dynamicSwitches', JSON.stringify(dynamicSwitches));
const scene = SceneManager._scene;
if (scene instanceof Scene_Map) {
scene.removeProgressBar(switchId);
}
});
PluginManager.registerCommand('DynamicSwitch', 'resetAllDynamicSwitches', args => {
dynamicSwitches.forEach(ds => {
$gameSwitches._allowingSet = true;
$gameSwitches.setValue(ds.switchId, true);
$gameSwitches._allowingSet = false;
});
dynamicSwitches = [];
localStorage.setItem('dynamicSwitches', JSON.stringify(dynamicSwitches));
});
async function getCurrentTime() {
try {
const response = await fetch('http://worldtimeapi.org/api/timezone/Etc/UTC');
if (!response.ok) throw new Error('Network response was not ok');
const data = await response.json();
return new Date(data.datetime).getTime();
} catch (error) {
throw error;
}
}
function saveDynamicSwitches() {
try {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(secretKey, 'hex'), iv);
let encrypted = cipher.update(JSON.stringify(dynamicSwitches), 'utf8', 'hex');
encrypted += cipher.final('hex');
const dataToSave = JSON.stringify({ iv: iv.toString('hex'), data: encrypted });
fs.writeFileSync(filePath, dataToSave, 'utf8');
} catch (error) {
}
}
const _Original_Scene_Map_update = Scene_Map.prototype.update;
Scene_Map.prototype.update = function () {
_Original_Scene_Map_update.call(this);
const currentTime = new Date().getTime();
dynamicSwitches.forEach(ds => {
if (ds.activationTime <= currentTime) {
$gameSwitches._allowingSet = true;
$gameSwitches.setValue(ds.switchId, true);
$gameSwitches._allowingSet = false;
}
});
dynamicSwitches = dynamicSwitches.filter(ds => ds.activationTime > currentTime);
saveDynamicSwitches();
if (this._progressBar) {
const event = $gameMap.event(progressBarInfo.eventId);
const maxTime = progressBarInfo.totalTime;
const startTime = progressBarInfo.startTime;
if (event) {
const screenX = event.screenX() + progressBarInfo.offsetX;
const screenY = event.screenY() + progressBarInfo.offsetY;
this._progressBar.x = screenX - this._progressBar.width / 2;
this._progressBar.y = screenY - this._progressBar.height;
}
this._progressBar.update();
if (this._progressBar._remainingTime <= 0) {
this.removeChild(this._progressBar);
this._progressBar = null;
progressBarInfo = null;
}
}
};
class ProgressBar extends Sprite {
constructor(switchId, settings) {
super();
this.switchId = switchId;
this._maxWidth = settings.width || 100;
this._height = settings.height || 10;
this._backgroundColor = settings.backgroundColor || 'gray';
this._barColor = settings.barColor || 'green';
this._showTime = settings.showTime || false;
this.bitmap = new Bitmap(this._maxWidth, this._height);
this._remainingTime = 0;
this._totalTime = 0;
this._initializeTime();
}
_initializeTime() {
const dynamicSwitch = dynamicSwitches.find(ds => ds.switchId === this.switchId);
if (dynamicSwitch) {
if (!progressBarInfo.startTime || !progressBarInfo.totalTime) {
this._startTime = new Date().getTime();
this._activationTime = dynamicSwitch.activationTime;
this._totalTime = this._activationTime - this._startTime;
progressBarInfo.startTime = this._startTime;
progressBarInfo.totalTime = this._totalTime;
} else {
this._startTime = progressBarInfo.startTime;
this._totalTime = progressBarInfo.totalTime;
this._activationTime = this._startTime + this._totalTime;
}
this._remainingTime = this._activationTime - new Date().getTime();
}
}
update() {
super.update();
this.visible = progressBarVisibility;
const currentTime = new Date().getTime();
this._remainingTime = this._activationTime - currentTime;
if (this._remainingTime <= 0) {
if (this.parent) this.parent.removeChild(this);
return;
}
this._drawProgressBar();
if (this._showTime) {
this._drawRemainingTime();
}
}
_drawProgressBar() {
if (this._remainingTime <= 0) return;
const percentage = this._remainingTime / this._totalTime;
const width = this._maxWidth * percentage;
this.bitmap.clear();
this.bitmap.fillRect(0, 0, this._maxWidth, this._height, this._backgroundColor);
this.bitmap.fillRect(0, 0, width, this._height, this._barColor);
}
_drawRemainingTime() {
const fontSize = 18;
const padding = 4;
const x = 0;
const y = 0;
let delta = this._remainingTime;
delta = delta < 0 ? 0 : delta;
const hours = Math.floor(delta / (1000 * 60 * 60));
delta -= hours * (1000 * 60 * 60);
const minutes = Math.floor(delta / (1000 * 60));
delta -= minutes * (1000 * 60);
const seconds = Math.floor(delta / 1000);
const timeText = `${hours > 0 ? hours + ":" : ""}${minutes > 0 ? String(minutes).padStart(2, '0') + ":" : "00:"}${String(seconds).padStart(2, '0')}`;
this.bitmap.fontSize = fontSize;
this.bitmap.clearRect(x, y, this._maxWidth, fontSize + padding);
this.bitmap.drawText(timeText, x, y, this._maxWidth, fontSize, 'center');
}
}
let progressBarInfo = null;
PluginManager.registerCommand('DynamicSwitch', 'showProgressBarInEvent', args => {
const eventId = Number(args.eventId);
const switchId = Number(args.switchId);
const dynamicSwitch = dynamicSwitches.find(ds => ds.switchId === switchId);
if (!dynamicSwitch) {
return;
}
const currentTime = new Date().getTime();
let delta = dynamicSwitch.activationTime - currentTime;
delta = delta < 0 ? 0 : delta;
if (delta === 0) {
return;
}
progressBarInfo = {
eventId,
switchId,
maxTime: progressBarInfo ? progressBarInfo.maxTime : 0,
startTime: progressBarInfo ? progressBarInfo.startTime : new Date().getTime(),
totalTime: progressBarInfo ? progressBarInfo.totalTime : 0,
backgroundColor: args.backgroundColor || 'gray',
barColor: args.barColor || 'green',
width: Number(args.width) || 100,
height: Number(args.height) || 10,
offsetX: Number(args.offsetX) || 0,
offsetY: Number(args.offsetY) || -40,
showTime: args.showTime === 'true'
};
const scene = SceneManager._scene;
if (scene instanceof Scene_Map) {
scene._createProgressBar();
}
});
const _Scene_Map_createDisplayObjects = Scene_Map.prototype.createDisplayObjects;
Scene_Map.prototype.createDisplayObjects = function () {
_Scene_Map_createDisplayObjects.call(this);
this._createProgressBar();
};
Scene_Map.prototype._createProgressBar = function () {
if (progressBarInfo) {
this._progressBar = new ProgressBar(progressBarInfo.switchId, progressBarInfo);
this.addChild(this._progressBar);
}
};
const _DataManager_makeSaveContents = DataManager.makeSaveContents;
DataManager.makeSaveContents = function () {
const contents = _DataManager_makeSaveContents.call(this);
contents.progressBarInfo = progressBarInfo;
return contents;
};
const _DataManager_extractSaveContents = DataManager.extractSaveContents;
DataManager.extractSaveContents = function (contents) {
_DataManager_extractSaveContents.call(this, contents);
progressBarInfo = contents.progressBarInfo;
};
class Window_SwitchStatus extends Window_Selectable {
constructor(rect) {
super(rect);
this.refresh();
this._lastUpdate = 0;
}
maxItems() {
return dynamicSwitches.length;
}
drawItem(index) {
const switchInfo = dynamicSwitches[index];
if (!switchInfo) return;
const rect = this.itemLineRect(index);
this.contents.fontSize = 18;
const onlineObtained = switchInfo.onlineObtained ? 'True' : 'False';
const remainingTime = this.calculateRemainingTime(switchInfo.activationTime);
const idText = `ID: ${switchInfo.switchId}`;
const onlineText = `Online: ${onlineObtained}`;
this.drawText(idText, rect.x, rect.y, 120, 'left');
this.drawText(onlineText, rect.x + 130, rect.y, 150, 'left');
this.drawText(remainingTime, rect.x + 290, rect.y, rect.width - 290, 'left');
}
calculateRemainingTime(activationTime) {
const currentTime = new Date().getTime();
let delta = activationTime - currentTime;
delta = delta < 0 ? 0 : delta;
const days = Math.floor(delta / (1000 * 60 * 60 * 24));
delta -= days * (1000 * 60 * 60 * 24);
const hours = Math.floor(delta / (1000 * 60 * 60));
delta -= hours * (1000 * 60 * 60);
const minutes = Math.floor(delta / (1000 * 60));
delta -= minutes * (1000 * 60);
const seconds = Math.floor(delta / 1000);
return `${days > 0 ? days + " days, " : ""}${hours > 0 ? hours + " hours, " : ""}${minutes > 0 ? minutes + " minutes, " : ""}${seconds} seconds`;
}
update() {
super.update();
if (Date.now() - this._lastUpdate >= 1000) {
this._lastUpdate = Date.now();
this.refresh();
}
}
}
class Scene_SwitchStatus extends Scene_MenuBase {
create() {
super.create();
const screenWidth = Graphics.boxWidth;
const screenHeight = Graphics.boxHeight;
const rect = new Rectangle(0, 0, screenWidth, screenHeight);
this._statusWindow = new Window_SwitchStatus(rect);
this.addWindow(this._statusWindow);
}
update() {
super.update();
if (Input.isTriggered('cancel')) {
this.popScene();
}
}
}
PluginManager.registerCommand('DynamicSwitch', 'showSwitchStatus', args => {
SceneManager.push(Scene_SwitchStatus);
});
let progressBarVisibility = true;
PluginManager.registerCommand('DynamicSwitch', 'setProgressBarVisibility', args => {
progressBarVisibility = args.visibility === 'true';
localStorage.setItem('progressBarVisibility', progressBarVisibility.toString());
});
window.addEventListener('load', () => {
const storedVisibility = localStorage.getItem('progressBarVisibility');
if (storedVisibility !== null) {
progressBarVisibility = storedVisibility === 'true';
}
});
PluginManager.registerCommand('DynamicSwitch', 'getTimeRemainingForSwitch', args => {
const switchId = Number(args.switchId);
const variableId = Number(args.variableId);
const dynamicSwitch = dynamicSwitches.find(ds => ds.switchId === switchId);
let timeRemaining = "0 seconds";
if (dynamicSwitch) {
const currentTime = new Date().getTime();
let delta = dynamicSwitch.activationTime - currentTime;
delta = delta < 0 ? 0 : delta;
const days = Math.floor(delta / (1000 * 60 * 60 * 24));
delta -= days * (1000 * 60 * 60 * 24);
const hours = Math.floor(delta / (1000 * 60 * 60));
delta -= hours * (1000 * 60 * 60);
const minutes = Math.floor(delta / (1000 * 60));
delta -= minutes * (1000 * 60);
const seconds = Math.floor(delta / 1000);
const parts = [];
if (days > 0) parts.push(`${days} days`);
if (hours > 0) parts.push(`${hours} hours`);
if (minutes > 0) parts.push(`${minutes} minutes`);
if (seconds > 0 || parts.length === 0) parts.push(`${seconds} seconds`);
timeRemaining = parts.join(', ');
}
$gameVariables.setValue(variableId, timeRemaining);
});
})();