forked from jimbob4000/gameOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
executable file
·423 lines (384 loc) · 10 KB
/
utils.js
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
// gameOS theme
// Copyright (C) 2018-2020 Seth Powell
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// This file contains some helper scripts for formatting data
// For multiplayer games, show the player count as '1-N'
function formatPlayers(playerCount) {
if (playerCount === 1)
return playerCount
return "1-" + playerCount;
}
// Show dates in Y-M-D format
function formatDate(date) {
return Qt.formatDate(date, "yyyy-MM-dd");
}
// Show last played time as text. Based on the code of the default Pegasus theme.
// Note to self: I should probably move this into the API.
function formatLastPlayed(lastPlayed) {
if (isNaN(lastPlayed))
return "never";
var now = new Date();
var elapsedHours = (now.getTime() - lastPlayed.getTime()) / 1000 / 60 / 60;
if (elapsedHours < 24 && now.getDate() === lastPlayed.getDate())
return "today";
var elapsedDays = Math.round(elapsedHours / 24);
if (elapsedDays <= 1)
return "yesterday";
return elapsedDays + " days ago"
}
// Display the play time (provided in seconds) with text.
// Based on the code of the default Pegasus theme.
// Note to self: I should probably move this into the API.
function formatPlayTime(playTime) {
var minutes = Math.ceil(playTime / 60)
if (minutes <= 90)
return Math.round(minutes) + " minutes";
return parseFloat((minutes / 60).toFixed(1)) + " hours"
}
// Process the platform name to make it friendly for the logo
// Unfortunately necessary for LaunchBox
function processPlatformName(platform) {
switch (platform) {
case "panasonic 3do":
return "3do";
break;
case "3do interactive multiplayer":
return "3do";
break;
case "amstrad cpc":
return "amstradcpc";
break;
case "apple ii":
return "apple2";
break;
case "atari 800":
return "atari800";
break;
case "atari 2600":
return "atari2600";
break;
case "atari 5200":
return "atari5200";
break;
case "atari 7800":
return "atari7800";
break;
case "atari jaguar":
return "atarijaguar";
break;
case "atari jaguar cd":
return "atarijaguarcd";
break;
case "atari lynx":
return "atarilynx";
break;
case "atari st":
return "atarist";
break;
case "commodore 64":
return "c64";
break;
case "tandy trs-80":
return "coco";
break;
case "commodore amiga":
return "amiga";
break;
case "sega dreamcast":
return "dreamcast";
break;
case "final burn alpha":
return "fba";
break;
case "sega game gear":
return "gamegear";
break;
case "nintendo game boy":
return "gb";
break;
case "nintendo game boy advance":
return "gba";
break;
case "nintendo game boy color":
return "gbc";
break;
case "nintendo gamecube":
return "gc";
break;
case "sega genesis":
return "genesis";
break;
case "mattel intellivision":
return "intellivision";
break;
case "sammy atomiswave":
return "atomiswave";
break;
case "sega master system":
return "mastersystem";
break;
case "sega mega drive":
return "megadrive";
break;
case "sega genesis":
return "genesis";
break;
case "microsoft msx":
return "msx";
break;
case "nintendo 64":
return "n64";
break;
case "nintendo ds":
return "nds";
break;
case "snk neo geo aes":
return "neogeo";
break;
case "snk neo geo mvs":
return "neogeo";
break;
case "snk neo geo cd":
return "neogeocd";
break;
case "nintendo 64":
return "segacd";
break;
case "nintendo entertainment system":
return "nes";
break;
case "snk neo geo pocket":
return "ngp";
break;
case "snk neo geo pocket color":
return "ngpc";
break;
case "sega cd":
return "segacd";
break;
case "nec turbografx-16":
return "turbografx16";
break;
case "sony psp":
return "psp";
break;
case "sony playstation":
return "psx";
break;
case "sony playstation 2":
return "ps2";
break;
case "sony playstation 3":
return "ps3";
break;
case "sony playstation vita":
return "vita";
break;
case "sega saturn":
return "saturn";
break;
case "sega 32x":
return "sega32x";
break;
case "super nintendo entertainment system":
return "snes";
break;
case "sega cd":
return "segacd";
break;
case "nintendo wii":
return "wii";
break;
case "nintendo wii u":
return "wiiu";
break;
case "nintendo 3ds":
return "3ds";
break;
case "microsoft xbox":
return "xbox";
break;
case "microsoft xbox 360":
return "xbox360";
break;
case "nintendo switch":
return "switch";
break;
default:
return platform;
}
}
function processButtonArt(button) {
var buttonModel;
switch (button) {
case "accept":
buttonModel = api.keys.accept;
break;
case "cancel":
buttonModel = api.keys.cancel;
break;
case "filters":
buttonModel = api.keys.filters;
break;
case "details":
buttonModel = api.keys.details;
break;
case "nextPage":
buttonModel = api.keys.nextPage;
break;
case "prevPage":
buttonModel = api.keys.prevPage;
break;
case "pageUp":
buttonModel = api.keys.pageUp;
break;
case "pageDown":
buttonModel = api.keys.pageDown;
break;
default:
buttonModel = api.keys.accept;
}
var i;
for (i = 0; buttonModel.length; i++) {
if (buttonModel[i].name().includes("Gamepad")) {
var buttonValue = buttonModel[i].key.toString(16)
return buttonValue.substring(buttonValue.length-1, buttonValue.length);
}
}
}
function steamAppID (gameData) {
var str = gameData.assets.boxFront.split("header");
return str[0];
}
function steamBoxArt(gameData) {
return steamAppID(gameData) + '/library_600x900_2x.jpg';
}
function steamLogo(gameData) {
return steamAppID(gameData) + "/logo.png"
}
function steamHero(gameData) {
return steamAppID(gameData) + "/library_hero.jpg"
}
// Just use boxFront?
function steamHeader(gameData) {
return steamAppID(gameData) + "/header.jpg"
}
function boxArt(data) {
if (data != null) {
if (data.assets.boxFront.includes("/header.jpg"))
return steamBoxArt(data);
else {
if (data.assets.screenshots[0] != "")
return data.assets.screenshots[0];
}
}
return "";
}
function logo(data) {
if (data != null) {
if (data.assets.boxFront.includes("/header.jpg"))
return steamLogo(data);
else {
if (data.assets.logo != "")
return data.assets.logo;
}
}
return "";
}
function logocentered(data) {
if (data != null) {
if (data.assets.boxFront.includes("/header.jpg"))
return steamLogo(data);
else {
if (data.assets.titlescreen != "")
return data.assets.titlescreen;
else if (data.assets.logo != "")
return data.assets.logo;
}
}
return "";
}
function fanArt(data) {
if (data != null) {
if (data.assets.boxFront.includes("/header.jpg"))
return steamHero(data);
else {
if (data.assets.background != "")
return data.assets.background;
else if (data.assets.screenshots[0])
return data.assets.screenshots[0];
}
}
return "";
}
// Place Steam collections at the beginning of the list
function reorderCollection(model) {
for(var i=0; i<model.count; i++) {
if (model.get(i).name == "Steam") {
model.move(i,0);
return model;
}
}
return model;
}
// Shuffle function
function shuffle(model){
var currentIndex = model.count, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
// And swap it with the current element.
// the dictionaries maintain their reference so a copy should be made
// https://stackoverflow.com/a/36645492/6622587
temporaryValue = JSON.parse(JSON.stringify(model.get(currentIndex)))
model.set(currentIndex, model.get(randomIndex))
model.set(randomIndex, temporaryValue);
}
return model;
}
function uniqueGameValues(fieldName) {
const set = new Set();
api.allGames.toVarArray().forEach(game => {
game[fieldName].forEach(v => set.add(v));
});
return [...set.values()].sort();
}
function uniqueValuesArray(fieldName) {
let arr = [];
var allGames = api.allGames.toVarArray();
for(var i=0;i<allGames.length;i++) {
arr.push(allGames[i][fieldName]);
}
return arr;
}
function shuffleArray(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function returnRandom(array) {
return array[Math.floor(Math.random() * array.length)];
}