Ticket #2866: summary.js

File summary.js, 34.7 KB (added by Jaison, 10 years ago)

Fixed version of Summary.js

Line 
1// Max player slots for any map (should read from config)
2const MAX_SLOTS = 8;
3
4/**
5 * Select active panel
6 * @param panelNumber Number of panel, which should get active state (integer)
7 */
8function selectPanel(panelNumber)
9{
10 var panelNames = [ 'scorePanel', 'buildingsPanel', 'unitsPanel', 'resourcesPanel', 'marketPanel', 'miscPanel' ];
11
12 function adjustTabDividers(tabSize)
13 {
14 var leftSpacer = Engine.GetGUIObjectByName("tabDividerLeft");
15 var rightSpacer = Engine.GetGUIObjectByName("tabDividerRight");
16 leftSpacer.size = "20 " + leftSpacer.size.top + " " + (tabSize.left + 2) + " " + leftSpacer.size.bottom;
17 rightSpacer.size = (tabSize.right - 2) + " " + rightSpacer.size.top + " 100%-20 " + rightSpacer.size.bottom;
18 }
19
20 for (var i = 0; i < panelNames.length; i++)
21 {
22 if (i != panelNumber)
23 {
24 Engine.GetGUIObjectByName(panelNames[i]).hidden = true;
25 Engine.GetGUIObjectByName(panelNames[i] + 'Button').sprite = "BackgroundTab";
26 }
27 else
28 {
29 Engine.GetGUIObjectByName(panelNames[i]).hidden = false;
30 Engine.GetGUIObjectByName(panelNames[i] + 'Button').sprite = "ForegroundTab";
31 adjustTabDividers(Engine.GetGUIObjectByName(panelNames[i] + 'Button').size);
32 }
33 }
34}
35
36function init(data)
37{
38 // LOCAL CONSTS, VARIABLES & FUNCTIONS
39 // const for filtering long collective headings
40 const LONG_HEADING_WIDTH = 260;
41 // number of panels
42 const PANELS_COUNT = 6;
43 // alpha for player box
44 const PLAYER_BOX_ALPHA = " 32";
45 // alpha for player colour box
46 const PLAYER_COLOUR_BOX_ALPHA = " 255";
47 // yStart value for spaceing teams boxes (and noTeamsBox)
48 const TEAMS_BOX_Y_START = 65;
49 // vertical size of player box
50 const PLAYER_BOX_Y_SIZE = 30;
51 // gap between players boxes
52 const PLAYER_BOX_GAP = 2;
53
54 // colours used for units and buildings
55 const TRAINED_COLOR = '[color="201 255 200"]';
56 const LOST_COLOR = '[color="255 213 213"]';
57 const KILLED_COLOR = '[color="196 198 255"]';
58
59 // colours used for gathered and traded resources
60 const INCOME_COLOR = '[color="201 255 200"]';
61 const OUTCOME_COLOR = '[color="255 213 213"]';
62
63 const BUILDINGS_TYPES = [ "total", "House", "Economic", "Outpost", "Military", "Fortress", "CivCentre", "Wonder" ];
64 const UNITS_TYPES = [ "total", "Infantry", "Worker", "Cavalry", "Champion", "Hero", "Ship" ];
65 const RESOURCES_TYPES = [ "food", "wood", "stone", "metal" ];
66
67 var panels = {
68 "score": { // score panel
69 "headings": { // headings on score panel
70 "playerName0Heading": { "yStart": 26, "width": 200 },
71 "economyScoreHeading": { "yStart": 16, "width": 100 },
72 "militaryScoreHeading": { "yStart": 16, "width": 100 },
73 "explorationScoreHeading": { "yStart": 16, "width": 100 },
74 "totalScoreHeading": { "yStart": 16, "width": 100 }
75 },
76 "counters": { // counters on score panel
77 "economyScore": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
78 "militaryScore": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
79 "explorationScore": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
80 "totalScore": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] }
81 }
82 },
83 "buildings": { // buildings panel
84 "headings": { // headings on buildings panel
85 "playerName1Heading": {"yStart": 26, "width": 200 },
86 "buildingsHeading": {"yStart": 16, "width": (85 * 7 + 105) }, // width = 735
87 "totalBuildingsHeading": {"yStart": 34, "width": 105 },
88 "houseBuildingsHeading": {"yStart": 34, "width": 85 },
89 "economicBuildingsHeading": {"yStart": 34, "width": 90 },
90 "outpostBuildingsHeading": {"yStart": 34, "width": 87 },
91 "militaryBuildingsHeading": {"yStart": 34, "width": 85 },
92 "fortressBuildingsHeading": {"yStart": 34, "width": 85 },
93 "civCentreBuildingsHeading": {"yStart": 34, "width": 110 },
94 "wonderBuildingsHeading": {"yStart": 34, "width": 85 }
95 },
96 "counters": { // counters on buildings panel
97 "totalBuildings": {"width": 105, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
98 "houseBuildings": {"width": 85, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
99 "economicBuildings": {"width": 90, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
100 "outpostBuildings": {"width": 87, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
101 "militaryBuildings": {"width": 85, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
102 "fortressBuildings": {"width": 85, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
103 "civCentreBuildings": {"width": 110, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
104 "wonderBuildings": {"width": 85, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] }
105 }
106 },
107 "units": { // units panel
108 "headings": { // headings on units panel
109 "playerName2Heading": {"yStart": 26, "width": 200 },
110 "unitsHeading": {"yStart": 16, "width": (100 * 6 + 120) }, // width = 720
111 "totalUnitsHeading": {"yStart": 34, "width": 120 },
112 "infantryUnitsHeading": {"yStart": 34, "width": 100 },
113 "workerUnitsHeading": {"yStart": 34, "width": 100 },
114 "cavalryUnitsHeading": {"yStart": 34, "width": 100 },
115 "championUnitsHeading": {"yStart": 34, "width": 100 },
116 "heroesUnitsHeading": {"yStart": 34, "width": 100 },
117 "navyUnitsHeading": {"yStart": 34, "width": 100 }
118 },
119 "counters": { // counters on units panel
120 "totalUnits": {"width": 120, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
121 "infantryUnits": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
122 "workerUnits": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
123 "cavalryUnits": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
124 "championUnits": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
125 "heroesUnits": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
126 "navyUnits": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] }
127 }
128 },
129 "resources": { // resources panel
130 "headings": { // headings on resources panel
131 "playerName3Heading": {"yStart": 26, "width": 200 },
132 "resourceHeading": {"yStart": 16, "width": (100 * 4 + 110) },//width = 510
133 "foodGatheredHeading": {"yStart": 34, "width": 100 },
134 "woodGatheredHeading": {"yStart": 34, "width": 100 },
135 "stoneGatheredHeading": {"yStart": 34, "width": 100 },
136 "metalGatheredHeading": {"yStart": 34, "width": 100 },
137 "totalGatheredHeading": {"yStart": 34, "width": 110 },
138 "treasuresCollectedHeading": {"yStart": 16, "width": 100 },
139 "resourcesTributedHeading": {"yStart": 16, "width": 121 }
140 },
141 "counters": { // counters on resources panel
142 "foodGathered": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
143 "woodGathered": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
144 "stoneGathered": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
145 "metalGathered": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
146 "totalGathered": {"width": 110, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
147 "treasuresCollected": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
148 "resourcesTributed": {"width": 121, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] }
149 }
150 },
151 "market": { // market panel
152 "headings": { // headings on market panel
153 "playerName4Heading": {"yStart": 26, "width": 200 },
154 "exchangedFoodHeading": {"yStart": 16, "width": 100 },
155 "exchangedWoodHeading": {"yStart": 16, "width": 100 },
156 "exchangedStoneHeading": {"yStart": 16, "width": 100 },
157 "exchangedMetalHeading": {"yStart": 16, "width": 100 },
158 "barterEfficiencyHeading": {"yStart": 16, "width": 110 },
159 "tradeIncomeHeading": {"yStart": 16, "width": 130 }
160 },
161 "counters": { // counters on market panel
162 "exchangedFood": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
163 "exchangedWood": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
164 "exchangedStone": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
165 "exchangedMetal": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
166 "barterEfficiency": {"width": 110, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
167 "tradeIncome": {"width": 130, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] }
168 }
169 },
170 "miscelanous": { // miscelanous panel
171 "headings": { // headings on miscelanous panel
172 "playerName5Heading": {"yStart": 26, "width": 200 },
173 "vegetarianRatioHeading": {"yStart": 16, "width": 100 },
174 "feminisationHeading": {"yStart": 26, "width": 100 },
175 "killDeathRatioHeading": {"yStart": 16, "width": 100 },
176 "mapExplorationHeading": {"yStart": 16, "width": 100 }
177 },
178 "counters": { // counters on miscelanous panel
179 "vegetarianRatio": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
180 "feminisation": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
181 "killDeathRatio": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] },
182 "mapExploration": {"width": 100, "objects": [ ], "teamsScores": [ ], "teamsScoresCaption": [ ] }
183 }
184 }
185 };
186
187 function alignHeaders(headings)
188 {
189 left = 50;
190 for (var h in headings)
191 {
192 Engine.GetGUIObjectByName(h).size = left + " " + headings[h].yStart + " " + (left + headings[h].width) + " 100%";
193 if (headings[h].width < LONG_HEADING_WIDTH)
194 left += headings[h].width;
195 }
196 }
197
198 function alignCounters(counters, player)
199 {
200 left = 240;
201 for each (var counter in counters)
202 {
203 counter.objects[player].size = left + " 2 " + (left + counter.width) + " 100%";
204 left += counter.width;
205 }
206
207 return left;
208 }
209
210 // caption counters functions
211 function captionEconomyScore()
212 {
213 var total = 0;
214 for each (var res in playerState.statistics.resourcesGathered)
215 total += res;
216
217 return Math.round(total / 10);
218 }
219
220 function captionBuildings(object, type)
221 {
222 object.caption = TRAINED_COLOR + playerState.statistics.buildingsConstructed[type] + '[/color] / ' +
223 LOST_COLOR + playerState.statistics.buildingsLost[type] + '[/color] / ' +
224 KILLED_COLOR + playerState.statistics.enemyBuildingsDestroyed[type] + '[/color]';
225 }
226
227 function captionUnits(object, type)
228 {
229 object.caption = TRAINED_COLOR + playerState.statistics.unitsTrained[type] + '[/color] / ' +
230 LOST_COLOR + playerState.statistics.unitsLost[type] + '[/color] / ' +
231 KILLED_COLOR + playerState.statistics.enemyUnitsKilled[type] + '[/color]';
232 }
233
234 function captionResourcesGathered(object, type)
235 {
236 object.caption = INCOME_COLOR + playerState.statistics.resourcesGathered[type] + '[/color] / ' +
237 OUTCOME_COLOR + (playerState.statistics.resourcesUsed[type] - playerState.statistics.resourcesSold[type]) + '[/color]';
238 }
239
240 function captionTotalResourcesGathered()
241 {
242 var totalGathered = 0;
243 var totalUsed = 0;
244
245 for each (var type in RESOURCES_TYPES)
246 {
247 totalGathered += playerState.statistics.resourcesGathered[type];
248 totalUsed += playerState.statistics.resourcesUsed[type] - playerState.statistics.resourcesSold[type];
249 }
250
251 return INCOME_COLOR + totalGathered + '[/color] / ' + OUTCOME_COLOR + totalUsed + '[/color]';
252 }
253
254 function captionResourcesTributed()
255 {
256 return INCOME_COLOR + playerState.statistics.tributesSent + "[/color] / " + OUTCOME_COLOR + playerState.statistics.tributesReceived + "[/color]";
257 }
258
259 function captionResourcesExchanged(object, type)
260 {
261 object.caption = INCOME_COLOR + '+' + playerState.statistics.resourcesBought[type] + '[/color] ' +
262 OUTCOME_COLOR + '-' + playerState.statistics.resourcesSold[type] + '[/color]';
263 }
264
265 function captionBarterEfficiency()
266 {
267 var totalBought = 0;
268 for each (var boughtAmount in playerState.statistics.resourcesBought)
269 totalBought += boughtAmount;
270 var totalSold = 0;
271 for each (var soldAmount in playerState.statistics.resourcesSold)
272 totalSold += soldAmount;
273
274 return Math.floor(totalSold > 0 ? (totalBought / totalSold) * 100 : 0) + "%";
275 }
276
277 function captionVegetarianRatio()
278 {
279 if (playerState.statistics.resourcesGathered.vegetarianFood && playerState.statistics.resourcesGathered.food)
280 return Math.floor((playerState.statistics.resourcesGathered.vegetarianFood / playerState.statistics.resourcesGathered.food) * 100) + "%";
281 else
282 return 0 + "%";
283 }
284
285 function captionFeminisation()
286 {
287 if (playerState.statistics.unitsTrained.Worker && playerState.statistics.unitsTrained.Female)
288 return Math.floor((playerState.statistics.unitsTrained.Female / playerState.statistics.unitsTrained.Worker) * 100) + "%";
289 else
290 return 0 + "%";
291 }
292
293 function captionKillDeathRatio()
294 {
295 if (!playerState.statistics.enemyUnitsKilled.total)
296 return "0.00";
297 if (!playerState.statistics.unitsLost.total) // and enemyUnitsKilled.total > 0
298 return "\u221E"; // infinity symbol
299 return Math.round((playerState.statistics.enemyUnitsKilled.total / playerState.statistics.unitsLost.total)*100)/100;
300 }
301
302 function sumTeamBuildings(counter, type)
303 {
304 if (counter.teamsScores[playerState.team] == 0)
305 {
306 counter.teamsScores[playerState.team] = { };
307 counter.teamsScores[playerState.team].buildingsConstructed = 0;
308 counter.teamsScores[playerState.team].buildingsLost = 0;
309 counter.teamsScores[playerState.team].enemyBuildingsDestroyed = 0;
310 }
311
312 counter.teamsScores[playerState.team].buildingsConstructed += playerState.statistics.buildingsConstructed[type];
313 counter.teamsScores[playerState.team].buildingsLost += playerState.statistics.buildingsLost[type];
314 counter.teamsScores[playerState.team].enemyBuildingsDestroyed += playerState.statistics.enemyBuildingsDestroyed[type];
315
316 counter.teamsScoresCaption[playerState.team] = TRAINED_COLOR + counter.teamsScores[playerState.team].buildingsConstructed + '[/color] / ' +
317 LOST_COLOR + counter.teamsScores[playerState.team].buildingsLost + '[/color] / ' +
318 KILLED_COLOR + counter.teamsScores[playerState.team].enemyBuildingsDestroyed + '[/color]';
319 }
320
321 function sumTeamUnits(counter, type)
322 {
323 if (counter.teamsScores[playerState.team] == 0)
324 {
325 counter.teamsScores[playerState.team] = { };
326 counter.teamsScores[playerState.team].unitsTrained = 0;
327 counter.teamsScores[playerState.team].unitsLost = 0;
328 counter.teamsScores[playerState.team].enemyUnitsKilled = 0;
329 }
330
331 counter.teamsScores[playerState.team].unitsTrained += playerState.statistics.unitsTrained[type];
332 counter.teamsScores[playerState.team].unitsLost += playerState.statistics.unitsLost[type];
333 counter.teamsScores[playerState.team].enemyUnitsKilled += playerState.statistics.enemyUnitsKilled[type];
334
335 counter.teamsScoresCaption[playerState.team] = TRAINED_COLOR + counter.teamsScores[playerState.team].unitsTrained + '[/color] / ' +
336 LOST_COLOR + counter.teamsScores[playerState.team].unitsLost + '[/color] / ' +
337 KILLED_COLOR + counter.teamsScores[playerState.team].enemyUnitsKilled + '[/color]';
338 }
339
340 function sumResourcesGathered(counter, type)
341 {
342 if (counter.teamsScores[playerState.team] == 0)
343 {
344 counter.teamsScores[playerState.team] = { };
345 counter.teamsScores[playerState.team].resourcesGathered = 0;
346 counter.teamsScores[playerState.team].resourcesUsed = 0;
347 }
348
349 counter.teamsScores[playerState.team].resourcesGathered += playerState.statistics.resourcesGathered[type];
350 counter.teamsScores[playerState.team].resourcesUsed += playerState.statistics.resourcesUsed[type] - playerState.statistics.resourcesSold[type];
351
352 counter.teamsScoresCaption[playerState.team] = INCOME_COLOR + counter.teamsScores[playerState.team].resourcesGathered + '[/color] / ' +
353 OUTCOME_COLOR + counter.teamsScores[playerState.team].resourcesUsed + '[/color]';
354 }
355
356 function sumTotalResourcesGathered()
357 {
358 if (panels.resources.counters.totalGathered.teamsScores[playerState.team] == 0)
359 {
360 panels.resources.counters.totalGathered.teamsScores[playerState.team] = { };
361 panels.resources.counters.totalGathered.teamsScores[playerState.team].resourcesGathered = 0;
362 panels.resources.counters.totalGathered.teamsScores[playerState.team].resourcesUsed = 0;
363 }
364
365 for each (var type in RESOURCES_TYPES)
366 {
367 panels.resources.counters.totalGathered.teamsScores[playerState.team].resourcesGathered += playerState.statistics.resourcesGathered[type];
368 panels.resources.counters.totalGathered.teamsScores[playerState.team].resourcesUsed +=
369 playerState.statistics.resourcesUsed[type] - playerState.statistics.resourcesSold[type];
370 }
371
372 panels.resources.counters.totalGathered.teamsScoresCaption[playerState.team] =
373 INCOME_COLOR + panels.resources.counters.totalGathered.teamsScores[playerState.team].resourcesGathered + '[/color] / ' +
374 OUTCOME_COLOR + panels.resources.counters.totalGathered.teamsScores[playerState.team].resourcesUsed + '[/color]';
375 }
376
377 function sumResourcesTributed()
378 {
379 if (panels.resources.counters.resourcesTributed.teamsScores[playerState.team] == 0)
380 {
381 panels.resources.counters.resourcesTributed.teamsScores[playerState.team] = { };
382 panels.resources.counters.resourcesTributed.teamsScores[playerState.team].tributesSent = 0;
383 panels.resources.counters.resourcesTributed.teamsScores[playerState.team].tributesReceived = 0;
384 }
385
386 panels.resources.counters.resourcesTributed.teamsScores[playerState.team].tributesSent += playerState.statistics.tributesSent;
387 panels.resources.counters.resourcesTributed.teamsScores[playerState.team].tributesReceived += playerState.statistics.tributesReceived;
388
389 panels.resources.counters.resourcesTributed.teamsScoresCaption[playerState.team] =
390 INCOME_COLOR + panels.resources.counters.resourcesTributed.teamsScores[playerState.team].tributesSent + "[/color] / " +
391 OUTCOME_COLOR + panels.resources.counters.resourcesTributed.teamsScores[playerState.team].tributesReceived + "[/color]";
392 }
393
394 function sumResourcesExchanged(counter, type)
395 {
396 if (counter.teamsScores[playerState.team] == 0)
397 {
398 counter.teamsScores[playerState.team] = { };
399 counter.teamsScores[playerState.team].resourcesBought = 0;
400 counter.teamsScores[playerState.team].resourcesSold = 0;
401 }
402
403 counter.teamsScores[playerState.team].resourcesBought += playerState.statistics.resourcesBought[type];
404 counter.teamsScores[playerState.team].resourcesSold += playerState.statistics.resourcesSold[type];
405
406 counter.teamsScoresCaption[playerState.team] =
407 INCOME_COLOR + '+' + counter.teamsScores[playerState.team].resourcesBought + '[/color] ' +
408 OUTCOME_COLOR + '-' + counter.teamsScores[playerState.team].resourcesSold + '[/color]';
409 }
410
411 function sumBarterEfficiency()
412 {
413 if (panels.market.counters.barterEfficiency.teamsScores[playerState.team] == 0)
414 {
415 panels.market.counters.barterEfficiency.teamsScores[playerState.team] = { };
416 panels.market.counters.barterEfficiency.teamsScores[playerState.team].resourcesBought = 0;
417 panels.market.counters.barterEfficiency.teamsScores[playerState.team].resourcesSold = 0;
418 }
419
420 for each (var boughtAmount in playerState.statistics.resourcesBought)
421 panels.market.counters.barterEfficiency.teamsScores[playerState.team].resourcesBought += boughtAmount;
422 for each (var soldAmount in playerState.statistics.resourcesSold)
423 panels.market.counters.barterEfficiency.teamsScores[playerState.team].resourcesSold += soldAmount;
424
425 panels.market.counters.barterEfficiency.teamsScoresCaption[playerState.team] =
426 Math.floor(panels.market.counters.barterEfficiency.teamsScores[playerState.team].resourcesSold > 0 ?
427 (panels.market.counters.barterEfficiency.teamsScores[playerState.team].resourcesBought /
428 panels.market.counters.barterEfficiency.teamsScores[playerState.team].resourcesSold) * 100 : 0) + "%";
429 }
430
431 function sumVegetarianRatio()
432 {
433 if (panels.miscelanous.counters.vegetarianRatio.teamsScores[playerState.team] == 0)
434 {
435 panels.miscelanous.counters.vegetarianRatio.teamsScores[playerState.team] = { };
436 panels.miscelanous.counters.vegetarianRatio.teamsScores[playerState.team].vegetarianFood = 0;
437 panels.miscelanous.counters.vegetarianRatio.teamsScores[playerState.team].food = 0;
438 }
439
440 panels.miscelanous.counters.vegetarianRatio.teamsScores[playerState.team].vegetarianFood += playerState.statistics.resourcesGathered.vegetarianFood;
441 panels.miscelanous.counters.vegetarianRatio.teamsScores[playerState.team].food += playerState.statistics.resourcesGathered.food;
442
443 if (panels.miscelanous.counters.vegetarianRatio.teamsScores[playerState.team].food &&
444 panels.miscelanous.counters.vegetarianRatio.teamsScores[playerState.team].vegetarianFood)
445 {
446 panels.miscelanous.counters.vegetarianRatio.teamsScoresCaption[playerState.team] =
447 Math.floor((panels.miscelanous.counters.vegetarianRatio.teamsScores[playerState.team].vegetarianFood /
448 panels.miscelanous.counters.vegetarianRatio.teamsScores[playerState.team].food) * 100) + "%";
449 return;
450 }
451 panels.miscelanous.counters.vegetarianRatio.teamsScoresCaption[playerState.team] = 0 + "%";
452 }
453
454 function sumFeminisation()
455 {
456 if (panels.miscelanous.counters.feminisation.teamsScores[playerState.team] == 0)
457 {
458 panels.miscelanous.counters.feminisation.teamsScores[playerState.team] = { };
459 panels.miscelanous.counters.feminisation.teamsScores[playerState.team].femalesTrained = 0;
460 panels.miscelanous.counters.feminisation.teamsScores[playerState.team].workersTrained = 0;
461 }
462
463 panels.miscelanous.counters.feminisation.teamsScores[playerState.team].femalesTrained += playerState.statistics.unitsTrained.Female;
464 panels.miscelanous.counters.feminisation.teamsScores[playerState.team].workersTrained += playerState.statistics.unitsTrained.Worker;
465
466 if (panels.miscelanous.counters.feminisation.teamsScores[playerState.team].femalesTrained &&
467 panels.miscelanous.counters.feminisation.teamsScores[playerState.team].workersTrained)
468 {
469 panels.miscelanous.counters.feminisation.teamsScoresCaption[playerState.team] =
470 Math.floor((panels.miscelanous.counters.feminisation.teamsScores[playerState.team].femalesTrained /
471 panels.miscelanous.counters.feminisation.teamsScores[playerState.team].workersTrained) * 100) + "%";
472 return;
473 }
474 panels.miscelanous.counters.feminisation.teamsScoresCaption[playerState.team] = 0 + "%";
475 }
476
477 function sumKillDeathRatio()
478 {
479 if (panels.miscelanous.counters.killDeathRatio.teamsScores[playerState.team] == 0)
480 {
481 panels.miscelanous.counters.killDeathRatio.teamsScores[playerState.team] = { };
482 panels.miscelanous.counters.killDeathRatio.teamsScores[playerState.team].enemyUnitsKilled = 0;
483 panels.miscelanous.counters.killDeathRatio.teamsScores[playerState.team].unitsLost = 0;
484 }
485
486 panels.miscelanous.counters.killDeathRatio.teamsScores[playerState.team].enemyUnitsKilled += playerState.statistics.enemyUnitsKilled.total;
487 panels.miscelanous.counters.killDeathRatio.teamsScores[playerState.team].unitsLost += playerState.statistics.unitsLost.total;
488
489 if (!panels.miscelanous.counters.killDeathRatio.teamsScores[playerState.team].enemyUnitsKilled)
490 {
491 panels.miscelanous.counters.killDeathRatio.teamsScoresCaption[playerState.team] = "0.00";
492 return;
493 }
494 if (!panels.miscelanous.counters.killDeathRatio.teamsScores[playerState.team].unitsLost)
495 {
496 panels.miscelanous.counters.killDeathRatio.teamsScoresCaption[playerState.team] = "\u221E"; // infinity symbol
497 return;
498 }
499 panels.miscelanous.counters.killDeathRatio.teamsScoresCaption[playerState.team] =
500 Math.round((panels.miscelanous.counters.killDeathRatio.teamsScores[playerState.team].enemyUnitsKilled /
501 panels.miscelanous.counters.killDeathRatio.teamsScores[playerState.team].unitsLost) * 100)/100;
502 }
503
504 // FUNCTION BODY
505 // Load data
506 var civData = loadCivData();
507 // Map
508 var mapDisplayType = translate("Scenario");
509
510 Engine.GetGUIObjectByName("timeElapsed").caption = sprintf(translate("Time elapsed: %(time)s"), { time: timeToString(data.timeElapsed) });
511
512 Engine.GetGUIObjectByName("summaryText").caption = data.gameResult;
513
514 // This is only defined for random maps
515 if (data.mapSettings.Size)
516 {
517 // load the map sizes from the JSON file
518 var mapSizes = initMapSizes();
519
520 // retrieve the index of the map size
521 for (var mapSizeIndex in mapSizes.tiles)
522 {
523 if (mapSizes.tiles[mapSizeIndex] == data.mapSettings.Size)
524 {
525 mapDisplayType = mapSizes.names[mapSizeIndex];
526 break;
527 }
528 }
529 }
530
531 Engine.GetGUIObjectByName("mapName").caption = sprintf(translate("%(mapName)s - %(mapType)s"), { mapName: translate(data.mapSettings.Name), mapType: mapDisplayType});
532
533 // Panels
534 // Align headers
535 var left = 50;
536 for each (var panel in panels) // for all panels
537 alignHeaders(panel.headings);
538
539 // TODO set maxPlayers as playerCounters.length
540 var maxPlayers = data.playerStates.length - 1;
541 var maxTeams = 0;
542
543 var teams = [ ];
544 if (data.mapSettings.LockTeams) // teams ARE locked
545 {
546 // count teams
547 for(var t = 0; t < maxPlayers; ++t)
548 {
549 if (!teams[data.playerStates[t+1].team])
550 {
551 teams[data.playerStates[t+1].team] = 1;
552 continue;
553 }
554 teams[data.playerStates[t+1].team]++;
555 }
556
557 if (teams.length == maxPlayers)
558 teams = false; // Each player has his own team. Displaying teams makes no sense.
559 }
560 else // teams are NOT locked
561 teams = false;
562
563 // Erase teams data if teams are not displayed
564 if (!teams)
565 {
566 for(var p = 0; p < maxPlayers; ++p)
567 data.playerStates[p+1].team = -1;
568 }
569
570 // Count players without team (or all if teams are not displayed)
571 var withoutTeam = maxPlayers;
572 if (teams)
573 {
574 // count players without team (or all if teams are not displayed)
575 for (var i = 0; i < teams.length; ++i)
576 withoutTeam -= teams[i];
577
578 // Display teams boxes
579 var p = 0;
580 for each (var panel in panels)
581 {
582 var yStart = TEAMS_BOX_Y_START + withoutTeam * (PLAYER_BOX_Y_SIZE + PLAYER_BOX_GAP);
583 for (var i = 0; i < teams.length; ++i)
584 {
585 var teamBox = Engine.GetGUIObjectByName("teamBox"+p+"t"+i);
586 teamBox.hidden = false;
587 var teamBoxSize = teamBox.size;
588 teamBoxSize.top = yStart;
589 teamBox.size = teamBoxSize;
590
591 yStart += 30 + teams[i] * (PLAYER_BOX_Y_SIZE + PLAYER_BOX_GAP) + 32;
592
593 Engine.GetGUIObjectByName("teamNameHeading"+p+"t"+i).caption = "Team "+(i+1);
594
595 // Make place to store team scores
596 for each (var counter in panel.counters)
597 {
598 counter.teamsScores[i] = 0;
599 counter.teamsScoresCaption[i] = "0";
600 }
601 }
602
603 // If there are no players without team, hide "player name" heading
604 if (!withoutTeam)
605 Engine.GetGUIObjectByName("playerName"+p+"Heading").caption = "";
606
607 p++;
608 }
609 }
610
611 if (withoutTeam)
612 {
613 // Show boxes for no teams
614 for (var b = 0; b < PANELS_COUNT; ++b)
615 Engine.GetGUIObjectByName("noTeamsBox"+b).hidden = false;
616 }
617
618 var playerBoxesCounts = [ ];
619 for (var i = 0; i < maxPlayers; ++i)
620 {
621 var tn = "";
622 var playerState = data.playerStates[i+1];
623
624 if (!playerBoxesCounts[playerState.team+1])
625 playerBoxesCounts[playerState.team+1] = 1;
626 else
627 playerBoxesCounts[playerState.team+1] += 1;
628
629 if (playerState.team != -1)
630 tn = "t"+playerState.team+"p";
631
632 var j = 0;
633 for each (var panel in panels)
634 {
635 var playerIdentityString = tn+"["+(playerBoxesCounts[playerState.team+1]-1)+"]";
636 // Display boxes for players
637 var playerBox = Engine.GetGUIObjectByName("playerBox"+j+playerIdentityString);
638 playerBox.hidden = false;
639
640 var boxSize = playerBox.size;
641 boxSize.top += (playerBoxesCounts[playerState.team+1] - 1) * (PLAYER_BOX_Y_SIZE + PLAYER_BOX_GAP);
642 boxSize.bottom = boxSize.top + PLAYER_BOX_Y_SIZE;
643 playerBox.size = boxSize;
644
645 var colourString = "colour: "
646 + Math.floor(playerState.colour.r * 255) + " "
647 + Math.floor(playerState.colour.g * 255) + " "
648 + Math.floor(playerState.colour.b * 255);
649
650 playerBox.sprite = colourString + PLAYER_BOX_ALPHA;
651
652 var playerColourBox = Engine.GetGUIObjectByName("playerColourBox"+j+playerIdentityString);
653 playerColourBox.sprite = colourString + PLAYER_COLOUR_BOX_ALPHA;
654
655 // Show the multiplayer name, e.g. "Foobar" rather than "Player 1".
656 // TODO: Perhaps show both the multiplayer and map-specific name?
657 var playerName = Engine.GetGUIObjectByName("playerName"+j+playerIdentityString);
658 playerName.caption = data.players[i+1].name;
659
660 var civIcon = Engine.GetGUIObjectByName("civIcon"+j+playerIdentityString);
661 civIcon.sprite = "stretched:"+civData[playerState.civ].Emblem;
662 civIcon.tooltip = civData[playerState.civ].Name;
663
664 // Get counters
665 for (var c in panel.counters)
666 {
667 panel.counters[c].objects[i] = Engine.GetGUIObjectByName(c+playerIdentityString);
668 }
669
670 // Align counters
671 var right = alignCounters(panel.counters, i);
672 boxSize.right = right;
673 playerBox.size = boxSize;
674
675 j++;
676 }
677
678 // Assign counters
679 // score panel
680 panels.score.counters.economyScore.objects[i].caption = captionEconomyScore();
681 panels.score.counters.militaryScore.objects[i].caption = Math.round((playerState.statistics.enemyUnitsKilledValue +
682 playerState.statistics.enemyBuildingsDestroyedValue) / 10);
683 panels.score.counters.explorationScore.objects[i].caption = playerState.statistics.percentMapExplored * 10;
684 panels.score.counters.totalScore.objects[i].caption = (+panels.score.counters.economyScore.objects[i].caption) +
685 (+panels.score.counters.militaryScore.objects[i].caption) +
686 (+panels.score.counters.explorationScore.objects[i].caption);
687 // buildings panel
688 var t = 0;
689 for each (var counter in panels.buildings.counters)
690 {
691 captionBuildings(counter.objects[i], BUILDINGS_TYPES[t]);
692 t++;
693 }
694 // units panel
695 t = 0;
696 for each (var counter in panels.units.counters)
697 {
698 captionUnits(counter.objects[i], UNITS_TYPES[t]);
699 t++;
700 }
701 // resources panel
702 t = 0;
703 for each (var counter in panels.resources.counters)
704 {
705 if (t >= 4) // only 4 first counters
706 break;
707
708 captionResourcesGathered(counter.objects[i], RESOURCES_TYPES[t]);
709 t++;
710 }
711 panels.resources.counters.totalGathered.objects[i].caption = captionTotalResourcesGathered();
712 panels.resources.counters.treasuresCollected.objects[i].caption = playerState.statistics.treasuresCollected;
713 panels.resources.counters.resourcesTributed.objects[i].caption = captionResourcesTributed();
714 // market panel
715 t = 0;
716 for (var c in panels.market.counters)
717 {
718 if (t >= 4) // only 4 first counters
719 break;
720
721 captionResourcesExchanged(panels.market.counters[c].objects[i], RESOURCES_TYPES[t]);
722 t++;
723 }
724 panels.market.counters.barterEfficiency.objects[i].caption = captionBarterEfficiency();
725 panels.market.counters.tradeIncome.objects[i].caption = playerState.statistics.tradeIncome;
726 // miscelanous panel
727 panels.miscelanous.counters.vegetarianRatio.objects[i].caption = captionVegetarianRatio();
728 panels.miscelanous.counters.feminisation.objects[i].caption = captionFeminisation();
729 panels.miscelanous.counters.killDeathRatio.objects[i].caption = captionKillDeathRatio();
730 panels.miscelanous.counters.mapExploration.objects[i].caption = playerState.statistics.percentMapExplored + "%";
731
732 if (!teams)
733 continue;
734
735 if (playerState.team == -1)
736 continue;
737
738 // Evaluate team total score
739 // score panel
740 for (var c in panels.score.counters)
741 {
742 panels.score.counters[c].teamsScores[playerState.team] += (+panels.score.counters[c].objects[i].caption);
743 panels.score.counters[c].teamsScoresCaption[playerState.team] = panels.score.counters[c].teamsScores[playerState.team];
744 }
745 // buildings panel
746 var t = 0;
747 for each (var counter in panels.buildings.counters)
748 {
749 sumTeamBuildings(counter, BUILDINGS_TYPES[t]);
750 t++;
751 }
752 // units panel
753 t = 0;
754 for each (var counter in panels.units.counters)
755 {
756 sumTeamUnits(counter, UNITS_TYPES[t]);
757 t++;
758 }
759 // resources panel
760 t = 0;
761 for each (var counter in panels.resources.counters)
762 {
763 if (t >= 4) // only 4 first counters
764 break;
765
766 sumResourcesGathered(counter, RESOURCES_TYPES[t]);
767 t++;
768 }
769 sumTotalResourcesGathered();
770 panels.resources.counters.treasuresCollected.teamsScores[playerState.team] += playerState.statistics.treasuresCollected;
771 panels.resources.counters.treasuresCollected.teamsScoresCaption[playerState.team] = panels.resources.counters.treasuresCollected.teamsScores[playerState.team];
772 sumResourcesTributed();
773 // market panel
774 t = 0;
775 for (var c in panels.market.counters)
776 {
777 if (t >= 4) // only 4 first counters
778 break;
779
780 sumResourcesExchanged(panels.market.counters[c], RESOURCES_TYPES[t]);
781 t++;
782 }
783 sumBarterEfficiency();
784 panels.market.counters.tradeIncome.teamsScores[playerState.team] += playerState.statistics.tradeIncome;
785 panels.market.counters.tradeIncome.teamsScoresCaption[playerState.team] = panels.market.counters.tradeIncome.teamsScores[playerState.team];
786 // miscelanous panel
787 sumVegetarianRatio();
788 sumFeminisation();
789 sumKillDeathRatio();
790 // TODO: probably change from simple sum to union from range manager
791 panels.miscelanous.counters.mapExploration.teamsScores[playerState.team] += playerState.statistics.percentMapExplored;
792 panels.miscelanous.counters.mapExploration.teamsScoresCaption[playerState.team] = panels.miscelanous.counters.mapExploration.teamsScores[playerState.team] + "%";
793 }
794
795 if (!teams)
796 {
797 selectPanel(0);
798 return;
799 }
800
801 // Display teams totals counters
802 for (var i = 0; i < teams.length; ++i)
803 {
804 var pn = 0;
805 for each (var panel in panels)
806 {
807 var teamHeading = Engine.GetGUIObjectByName("teamHeading"+pn+"t"+i);
808 var yStart = 30 + teams[i] * (PLAYER_BOX_Y_SIZE + PLAYER_BOX_GAP) + 2;
809 teamHeading.size = "50 "+yStart+" 100% "+(yStart+20);
810 teamHeading.caption = "Team total";
811
812 var left = 250;
813 for (var c in panel.counters)
814 {
815 var counter = Engine.GetGUIObjectByName(c+"t"+i);
816 counter.size = left + " " + yStart + " " + (left + panel.counters[c].width) + " " + (yStart+20);
817 counter.caption = panel.counters[c].teamsScoresCaption[i];
818
819 left += panel.counters[c].width;
820 }
821 pn++;
822 }
823 }
824
825 selectPanel(0);
826}
827
828function onTick()
829{
830}