forked from tcecspectator/mytcecgui
-
Notifications
You must be signed in to change notification settings - Fork 16
/
lib.js
211 lines (184 loc) · 4.88 KB
/
lib.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
/*
globals
exports, process, require
*/
'use strict';
let {Assign, Keys, LS} = require('./js/common.js');
function perror(errormsg) {
LS('Error:', errormsg);
process.exit();
}
function getPrevPGN(id, name, menuData) {
var found = 0;
var data = menuData;
var prevpgn = [];
Keys(data.Seasons).forEach(key => {
let value = data.Seasons[key];
if (found)
{
return false;
}
Keys(value.sub).forEach(subkey => {
let subvalue = value.sub[subkey];
if ((parseInt(subvalue.dno) <= id) && (value.seasonName == name))
{
var pgnStr = subvalue.abb + ".pgn";
prevpgn.push(pgnStr);
}
});
});
var prevpgnStr = prevpgn.join();
return prevpgnStr;
}
function getPGN(id, jsonMenuData) {
var found = 0;
var localPgn = {};
var data = jsonMenuData;
localPgn.found = 0;
localPgn.prevpgn = 0;
localPgn.bonus = 0;
localPgn.cup = 0;
localPgn.eventtag = 0;
Keys(data.Seasons).forEach(key => {
let value = data.Seasons[key];
if (found)
return false;
Keys(value.sub).forEach(subkey => {
let subvalue = value.sub[subkey];
if ((subvalue.id == id) || (subvalue.idf == id)) {
localPgn.abb = subvalue.abb;
localPgn.pgnfile = subvalue.abb + "_link.pgn";
localPgn.scjson = subvalue.abb + "_Schedule.json";
localPgn.download = value.download;
localPgn.eventtag = value.eventtag;
localPgn.url = subvalue.url;
localPgn.cup = value.cup;
localPgn.bonus = subvalue.bonus;
found = 1;
localPgn.found = 1;
if (localPgn.cup)
{
localPgn.prevpgnlist = getPrevPGN(parseInt(subvalue.dno), value.seasonName, jsonMenuData);
LS("XXXXXXX: localPgn.prevpgn:" + localPgn.prevpgnlist);
}
// LS("STRXX:::::" + JSON.stringify(value.sub, null, '\t'));
return false;
}
});
});
return localPgn;
}
function getUpdatedJSON(totalId, jsonMenuData) {
// var found = 0;
var data = jsonMenuData;
let dno = 0;
Keys(data.Seasons).forEach(key => {
let value = data.Seasons[key];
if (key.localeCompare(totalId.no))
{
return true;
}
LS("Found season:" + key);
Keys(value.sub).forEach(subkey => {
let subvalue = value.sub[subkey];
dno = subvalue.dno;
});
dno = (parseInt(dno) + 4).toString();
totalId.dno = dno.padStart(4, '0');
value.sub.push (totalId);
// LS("STRXX:::::" + " jsonkey: " + value + " ," + JSON.stringify(data, null, '\t'));
});
data.newaddedid = totalId.id;
return data;
}
function getRandomSalt(fileName) {
var timestamp = new Date().getTime().toString();
var return_string = fileName + "_" + timestamp;
return return_string;
}
function getNewIdStruc(stringMessage, jsonMenuData) {
var narray = stringMessage.split("_");
let ncompName = narray[0];
let nseasonName = narray[1];
let nseasonNo = narray[2];
let nDivision = narray[4];
let nTag = narray[5];
let nTagId = narray[6];
if (nTagId == undefined)
{
nTagId = '';
}
let nTagMenu = narray.slice(5).join(' ');
var nIdStr = '';
var totalId = {};
var bonus = 0;
if (ncompName.localeCompare("TCEC"))
{
perror("Name is not TCEC, given name is " + ncompName);
}
if (nseasonName.localeCompare("Season"))
{
perror("Name is not Season, given name is " + nseasonName);
}
else
{
nIdStr = "s";
}
if (isNaN(nseasonNo))
{
if (nseasonNo.localeCompare("Bonus"))
{
perror("season no is not number, given no is " + nseasonNo);
}
else
{
nIdStr += nseasonNo;
}
}
else
{
nIdStr += parseInt(nseasonNo);
}
if (nDivision.localeCompare("Division"))
{
perror("Name is not Division, given name is " + nDivision);
}
else
{
nIdStr += "division";
}
nIdStr += nTag;
totalId.id = nIdStr + nTagId;
totalId.id = totalId.id.toLowerCase();
if (bonus)
{
totalId.menu = nseasonName + " " + nseasonNo + " " + nTagMenu;
}
else
{
totalId.menu = nTagMenu;
}
totalId.url = "season=" + nseasonNo + "&" + "div=" + nTag + nTagId;
totalId.url = totalId.url.toLowerCase();
totalId.no = nseasonNo;
totalId.abb = stringMessage;
var updJSON = getUpdatedJSON(totalId, jsonMenuData);
/*
{
"id" : "s13divisionsf",
"abb" : "TCEC_Season_13_-_Superfinal",
"menu" : "Superfinal",
"url" : "season=13&div=sf",
"no" : "13",
"dno" : "sf"
}
*/
// LS(narray + " ,id:\n" + JSON.stringify(totalId, null, '\t'));
return updJSON;
}
Assign(exports, {
getNewIdStruc: getNewIdStruc,
getPGN: getPGN,
getRandomSalt: getRandomSalt,
getUpdatedJSON: getUpdatedJSON,
});