-
Notifications
You must be signed in to change notification settings - Fork 0
/
teamsObjectUpdater.js
271 lines (253 loc) · 9.29 KB
/
teamsObjectUpdater.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
module.exports = {
getTeams: function() {
return teams;
},
initializeTeams: function (teamJsonData, routes, perTeamMessages, perMissionMessages, perStoryMessages) {
dataToTeams(teamJsonData, perTeamMessages);
// Validate dimensions first
if (routes.length !== teams.length || routes[0].length !== teams[0].missions.length) {
console.log('Error in injectRoutes: dimension mismatch.');
return teams;
}
injectRoutes(routes, perMissionMessages, perStoryMessages);
console.log(JSON.stringify(teams));
return teams;
},
teamOnline: function (teamNum) {
teamOnline(teamNum);
return teams;
},
skipMission: function (teamNum) {
skipMission(teamNum);
return teams;
},
advanceMission: function (teamNum) {
advanceMission(teamNum);
return teams;
},
backMission: function (teamNum) {
backMission(teamNum);
return teams;
},
// Function that takes a string with team and code and validates the
// code and updates the team based on action needed.
// Then we return a messaging object array as the response.
// Expecting string in format of 'TEAM CODE' or 'B1\nWORD'
checkCode: function (checkString) {
console.log('Checking code');
// Remove new lines from string and replace with space.
checkString = checkString.replace(/\r?\n|\r/g,' ');
// Split string based on space.
var splitStrings = checkString.split(' ');
if (splitStrings.length < 2) {
// ERR, unexpected string
return CHECK_FORMATERROR_MESSAGE;
}
var teamName = splitStrings[0].toUpperCase();
var codeWord = splitStrings[1];
var teamFound = false;
for (var teamNum in teams) {
if (teams[teamNum].teamName.toUpperCase() === teamName) {
teamFound = true;
break;
}
}
if (teamFound !== true) {
// ERR, no team found with teamName
return CHECK_TEAMERROR_MESSAGE;
}
console.log(teamNum);
var curMissionNum = teams[teamNum].currentMission;
if (curMissionNum === -1) {
// ERR, team is not on a mission
return teams[teamNum].noMissionMessage;
}
console.log('Code word: ' + teams[teamNum].missions[curMissionNum].code);
console.log('Input: ' + codeWord);
// Case insensitve checking
var correctCodeWord = teams[teamNum].missions[curMissionNum].code.toUpperCase();
var enteredCodeWord = codeWord.toUpperCase();
var skipWord = teams[teamNum].skipWord.toUpperCase();
var onlineWord = 'online'.toUpperCase();
if (teams[teamNum].status === 'offline') {
if (onlineWord === enteredCodeWord) {
// Code:Online, respond custom online message.
teamOnline(teamNum);
return combineMessages(teams[teamNum].teamOnlineMessage, teams[teamNum].missions[curMissionNum].clue);
} else {
// Still offline, respond custom help message.
return teams[teamNum].offlineHelpMessage;
}
} else if (correctCodeWord === enteredCodeWord) {
// Code:Correct, respond with next level text.
advanceMission(teamNum);
// New current mission
curMissionNum = teams[teamNum].currentMission;
if (curMissionNum === -1) {
return teams[teamNum].finishMessage;
} else {
return teams[teamNum].missions[curMissionNum].clue;
}
} else if (skipWord === enteredCodeWord) {
// Code:Skip, respond with message to help finish mission.
skipMission(teamNum);
return teams[teamNum].missions[curMissionNum].skipHint;
} else {
// Code:Incorrect, respond with inccorrect message.
return teams[teamNum].missions[curMissionNum].incorrectCodeMessage;
}
// Unreachable
}
};
const CHECK_FORMATERROR_MESSAGE = [
{
delay: 0,
message: 'I don\'t understand the text you entered. Looks like it\'s in the wrong format. (TEAMID CODEWORD)'
}
];
// [
// {
// delay: 0,
// message: 'First message'
// },
// {
// delay: 1500,
// message: 'Delayed message'
// }
// ]
// NOTES: Add these fields to each team. (All are messaging object arrays)
// formatErrorMessage
// teamNotFoundMessage
// noMissionMessage
// finishMessage
// teamOnlineMessage
// offlineHelpMessage
// missions[curMissionNum].clue
// missions[curMissionNum].skipHint
// missions[curMissionNum].incorrectCodeMessage
const CHECK_TEAMERROR_MESSAGE = [
{
delay: 0,
message: 'I can\'t find which team you are a part of. Your text might be in the wrong format. (TEAMID CODEWORD) Make sure your TEAMID is correct.'
}
];
const CHECK_MISSIONERROR_MESSAGE = 'Your unit is not on a mission.';
var teams = [];
function dataToTeams (data, perTeamMessages) {
for (var teamNum in data.teams) {
teams[teamNum] = {};
teams[teamNum].teamName = data.teams[teamNum].name;
teams[teamNum].status = 'offline';
teams[teamNum].skipWord = 'skip';
teams[teamNum].currentMission = -1;
teams[teamNum].missions = [];
for (var missionNum in data.missions) {
teams[teamNum].missions[missionNum] = {};
teams[teamNum].missions[missionNum].missionName = data.missions[missionNum].name;
teams[teamNum].missions[missionNum].code = data.missions[missionNum].code;
teams[teamNum].missions[missionNum].missionStatus = 'offline';
}
for (var messageKey in perTeamMessages) {
teams[teamNum][messageKey] = perTeamMessages[messageKey];
}
}
}
function injectRoutes (routes, perMissionMessages, perStoryMessages) {
console.log(perMissionMessages);
for (var teamNum in teams) {
var prevMissionNum = -1;
var curMissionNum = routes[teamNum][0];
teams[teamNum].currentMission = routes[teamNum][0];
for (var missionNum = 1; missionNum < routes[teamNum].length; missionNum++) {
teams[teamNum].missions[curMissionNum].next = routes[teamNum][missionNum];
teams[teamNum].missions[curMissionNum].prev = prevMissionNum;
teams[teamNum].missions[curMissionNum].clue = combineMessages(perStoryMessages[missionNum - 1].storyLine, perMissionMessages[curMissionNum].clue);
teams[teamNum].missions[curMissionNum].skipHint = perMissionMessages[missionNum - 1].skipHint;
teams[teamNum].missions[curMissionNum].incorrectCodeMessage = perStoryMessages[missionNum - 1].incorrectLine;
prevMissionNum = curMissionNum;
curMissionNum = routes[teamNum][missionNum];
}
teams[teamNum].missions[curMissionNum].next = -1;
teams[teamNum].missions[curMissionNum].prev = prevMissionNum; // End
teams[teamNum].missions[curMissionNum].clue = combineMessages(perStoryMessages[missionNum - 1].storyLine, perMissionMessages[curMissionNum].clue);
teams[teamNum].missions[curMissionNum].skipHint = perMissionMessages[missionNum - 1].skipHint;
teams[teamNum].missions[curMissionNum].incorrectCodeMessage = perStoryMessages[curMissionNum].incorrectLine;
}
}
function combineMessages (messagesArray1, messagesArray2) {
// Make copy before modifying delays.
messagesArray2 = JSON.parse(JSON.stringify(messagesArray2));
// Combine 1 append 2.
const BUFFER = 1000;
var additionalDelay = longestDelay(messagesArray1) + BUFFER;
for (var messageNum in messagesArray2) {
console.log(JSON.stringify(messagesArray2));
console.log(messagesArray2[messageNum].delay);
console.log(additionalDelay);
messagesArray2[messageNum].delay += additionalDelay;
}
return messagesArray1.concat(messagesArray2);
}
function longestDelay (messagesArray) {
var max = 0;
console.log(JSON.stringify(messagesArray));
for (var messageNum in messagesArray) {
var delay = messagesArray[messageNum].delay;
console.log(delay);
if (delay > max) {
max = delay;
}
}
console.log(max);
return max;
}
function teamOnline (teamNum) {
var curMissionNum = teams[teamNum].currentMission;
teams[teamNum].status = 'online';
teams[teamNum].missions[curMissionNum].missionStatus = 'active';
teams[teamNum].missions[curMissionNum].startTime = Date();
}
function skipMission (teamNum) {
var curMissionNum = teams[teamNum].currentMission;
if (curMissionNum === -1) {
//console.log('Warning in skipMission: no mission!')
return false;
}
if (teams[teamNum].missions[curMissionNum].missionStatus === 'skipped') {
// Already skipped mission!
return false;
}
teams[teamNum].missions[curMissionNum].missionStatus = 'skipped';
return true;
}
function advanceMission (teamNum) {
var curMissionNum = teams[teamNum].currentMission;
if (curMissionNum === -1) {
//console.log('Warning in advanceMission: no mission!')
return false;
}
if (teams[teamNum].missions[curMissionNum].missionStatus === 'active') {
teams[teamNum].missions[curMissionNum].missionStatus = 'completed';
}
teams[teamNum].currentMission = teams[teamNum].missions[curMissionNum].next;
curMissionNum = teams[teamNum].currentMission;
if (curMissionNum !== -1) {
teams[teamNum].missions[curMissionNum].missionStatus = 'active';
teams[teamNum].missions[curMissionNum].startTime = Date();
}
return true;
}
function backMission (teamNum) {
var curMissionNum = teams[teamNum].currentMission;
if (curMissionNum === -1) {
return false;
}
var prevMissionNum = teams[teamNum].missions[curMissionNum].prev;
if (prevMissionNum === -1) {
return false;
}
teams[teamNum].missions[curMissionNum].missionStatus = 'offline';
teams[teamNum].currentMission = prevMissionNum;
teams[teamNum].missions[prevMissionNum].missionStatus = 'active';
return true;
}