Ticket #3150: summary_counters_v2.patch

File summary_counters_v2.patch, 13.5 KB (added by Imarok, 8 years ago)

Move teamHelperData calculations into a seperate function and fix: team map exploration score, team feminisation, team barter efficency, color of team outcome and team total score. Also done some cleanup.

  • binaries/data/mods/public/gui/credits/texts/programming.json

     
    8383            {"nick": "h20", "name": "Daniel Wilhelm"},
    8484            {"nick": "historic_bruno", "name": "Ben Brian"},
    8585            {"nick": "idanwin"},
     86            {"nick": "Imarok", "name": "J. S."},
    8687            {"nick": "infyquest", "name": "Vijay Kiran Kamuju"},
    8788            {"nick": "IronNerd", "name": "Matthew McMullan"},
    8889            {"nick": "Itms", "name": "Nicolas Auvray"},
  • binaries/data/mods/public/gui/summary/counters.js

     
    11// FUNCTIONS FOR CALCULATING SCORES
    2 var g_TeamMiscHelperData = [];
     2var g_TeamHelperData = [];
    33
    44function resetDataHelpers()
    55{
    6     g_TeamMiscHelperData = [];
     6    g_TeamHelperData = [];
    77}
    88
    99function updateCountersPlayer(playerState, counters, idGUI)
     
    1515    }
    1616}
    1717
    18 function calculateEconomyScore(playerState, position)
     18// Calculates the data for the team counters that can only got via payerState
     19// and saves it to g_TeamHelperData
     20function calculateTeamCounters(playerState)
    1921{
     22    if (!g_TeamHelperData[playerState.team])
     23        g_TeamHelperData[playerState.team] = {
     24            "food": 0,
     25            "vegetarianFood": 0,
     26            "female": 0,
     27            "worker": 0,
     28            "enemyUnitsKilled": 0,
     29            "unitsLost": 0,
     30            "percentMapControlled": 0,
     31            "peakPercentMapControlled": 0,
     32            "percentMapExplored": 0,
     33            "totalBought": 0,
     34            "totalSold": 0
     35        };
     36
     37    g_TeamHelperData[playerState.team].food += playerState.statistics.resourcesGathered.food;
     38    g_TeamHelperData[playerState.team].vegetarianFood += playerState.statistics.resourcesGathered.vegetarianFood;
     39
     40    g_TeamHelperData[playerState.team].female += playerState.statistics.unitsTrained.Female;
     41    g_TeamHelperData[playerState.team].worker += playerState.statistics.unitsTrained.Worker;
     42
     43    g_TeamHelperData[playerState.team].enemyUnitsKilled += playerState.statistics.enemyUnitsKilled.total;
     44    g_TeamHelperData[playerState.team].unitsLost += playerState.statistics.unitsLost.total;
     45
     46    g_TeamHelperData[playerState.team].percentMapControlled = playerState.statistics.teamPercentMapControlled;
     47    g_TeamHelperData[playerState.team].peakPercentMapControlled = playerState.statistics.teamPeakPercentMapControlled;
     48
     49    g_TeamHelperData[playerState.team].percentMapExplored = playerState.statistics.teamPercentMapExplored;
     50
     51    for (let type in playerState.statistics.resourcesBought)
     52        g_TeamHelperData[playerState.team].totalBought += playerState.statistics.resourcesBought[type];
     53
     54    for (let type in playerState.statistics.resourcesSold)
     55        g_TeamHelperData[playerState.team].totalSold += playerState.statistics.resourcesSold[type];
     56}
     57
     58function calculateEconomyScore(playerState)
     59{
    2060    let total = 0;
    2161    for (let type in playerState.statistics.resourcesGathered)
    2262        total += playerState.statistics.resourcesGathered[type];
     
    2464    return Math.round(total / 10);
    2565}
    2666
    27 function calculateMilitaryScore(playerState, position)
     67function calculateMilitaryScore(playerState)
    2868{
    2969    return Math.round((playerState.statistics.enemyUnitsKilledValue +
    3070        playerState.statistics.enemyBuildingsDestroyedValue) / 10);
    3171}
    3272
    33 function calculateExplorationScore(playerState, position)
     73function calculateExplorationScore(playerState)
    3474{
    3575    return playerState.statistics.percentMapExplored * 10;
    3676}
    3777
    38 function calculateScoreTotal(playerState, position)
     78function calculateScoreTotal(playerState)
    3979{
    4080    return calculateEconomyScore(playerState) +
    4181        calculateMilitaryScore(playerState) +
     
    4989        if (t == -1)
    5090            continue;
    5191
     92        let teamTotalScore = 0;
    5293        for (let w in counters)
    5394        {
    5495            let total = 0;
    55             for (let p = 0; p < g_Teams[t]; ++p)
    56                 total += (+Engine.GetGUIObjectByName("valueDataTeam[" + t + "][" + p + "][" + w + "]").caption);
     96            // w == 2 is the team exploration score
     97            // The players exploration score can't be added because there might be regions both players explored
     98            if (w == 2)
     99                total = g_TeamHelperData[t].percentMapExplored * 10;
     100            else
     101                for (let p = 0; p < g_Teams[t]; ++p)
     102                    total += +Engine.GetGUIObjectByName("valueDataTeam[" + t + "][" + p + "][" + w + "]").caption;
    57103
     104            if ( w != 3)
     105                teamTotalScore += total;
     106            else
     107                total = teamTotalScore;
     108
    58109            Engine.GetGUIObjectByName("valueDataTeam[" + t + "][" + w + "]").caption = total;
    59110        }
    60111    }
     
    119170        g_OutcomeColor + (playerState.statistics.resourcesUsed[type] - playerState.statistics.resourcesSold[type]) + '[/color]';
    120171}
    121172
    122 function calculateTotalResources(playerState, position)
     173function calculateTotalResources(playerState)
    123174{
    124175    let totalGathered = 0;
    125176    let totalUsed = 0;
     
    133184    return g_IncomeColor + totalGathered + '[/color] / ' + g_OutcomeColor + totalUsed + '[/color]';
    134185}
    135186
    136 function calculateTreasureCollected(playerState, position)
     187function calculateTreasureCollected(playerState)
    137188{
    138189    return playerState.statistics.treasuresCollected;
    139190}
    140191
    141 function calculateLootCollected(playerState, position)
     192function calculateLootCollected(playerState)
    142193{
    143194    return playerState.statistics.lootCollected;
    144195}
    145196
    146 function calculateTributeSent(playerState, position)
     197function calculateTributeSent(playerState)
    147198{
    148199    return g_IncomeColor + playerState.statistics.tributesSent + "[/color] / " +
    149200           g_OutcomeColor + playerState.statistics.tributesReceived + "[/color]";
     
    192243    }
    193244}
    194245
    195 function calculateResourceExchanged(playerState, position)
     246function calculateResourceExchanged(playerState , position)
    196247{
    197248    let type = g_ResourcesTypes[position];
    198249    return g_IncomeColor + '+' + playerState.statistics.resourcesBought[type] + '[/color] ' +
     
    199250        g_OutcomeColor + '-' + playerState.statistics.resourcesSold[type] + '[/color]';
    200251}
    201252
    202 function calculateBatteryEfficiency(playerState, position)
     253function calculateBarterEfficiency(playerState)
    203254{
    204255    let totalBought = 0;
    205256    let totalSold = 0;
     
    213264    return Math.floor(totalSold > 0 ? (totalBought / totalSold) * 100 : 0) + "%";
    214265}
    215266
    216 function calculateTradeIncome(playerState, position)
     267function calculateTradeIncome(playerState)
    217268{
    218269    return playerState.statistics.tradeIncome;
    219270}
     
    250301                }
    251302            }
    252303
    253             if (w >= 4)
     304            if(w == 4)
     305                teamTotal = Math.floor(g_TeamHelperData[t].totalSold > 0 ? (g_TeamHelperData[t].totalBought / g_TeamHelperData[t].totalSold) * 100 : 0) + "%"
     306            else if (w > 4)
    254307                teamTotal = total.i +(w == 4 ? "%" : "");
    255308            else
    256                 teamTotal = g_IncomeColor + '+' + total.i + '[/color] ' + g_IncomeColor + '-' + total.o + '[/color]';
     309                teamTotal = g_IncomeColor + '+' + total.i + '[/color] ' + g_OutcomeColor + '-' + total.o + '[/color]';
    257310
    258311            Engine.GetGUIObjectByName("valueDataTeam[" + t + "][" + w + "]").caption = teamTotal;
    259312        }
     
    260313    }
    261314}
    262315
    263 function calculateVegetarianRatio(playerState, position)
     316function calculateVegetarianRatio(playerState)
    264317{
    265     if (!g_TeamMiscHelperData[playerState.team])
    266         g_TeamMiscHelperData[playerState.team] = [];
    267 
    268     if (!g_TeamMiscHelperData[playerState.team][position])
    269         g_TeamMiscHelperData[playerState.team][position] = { "food": 0, "vegetarianFood": 0 };
    270 
    271318    if (playerState.statistics.resourcesGathered.vegetarianFood && playerState.statistics.resourcesGathered.food)
    272     {
    273         g_TeamMiscHelperData[playerState.team][position].food += playerState.statistics.resourcesGathered.food;
    274         g_TeamMiscHelperData[playerState.team][position].vegetarianFood += playerState.statistics.resourcesGathered.vegetarianFood;
    275319        return Math.floor((playerState.statistics.resourcesGathered.vegetarianFood / playerState.statistics.resourcesGathered.food) * 100) + "%";
    276     }
    277320    else
    278321        return 0 + "%";
    279322}
    280323
    281 function calculateFeminization(playerState, position)
     324function calculateFeminization(playerState)
    282325{
    283     if (!g_TeamMiscHelperData[playerState.team])
    284         g_TeamMiscHelperData[playerState.team] = [];
    285 
    286     if (!g_TeamMiscHelperData[playerState.team][position])
    287         g_TeamMiscHelperData[playerState.team][position] = { "Female": 0, "Worker": 0 };
    288 
    289326    if (playerState.statistics.unitsTrained.Worker && playerState.statistics.unitsTrained.Female)
    290     {
    291         g_TeamMiscHelperData[playerState.team][position].Female = playerState.statistics.unitsTrained.Female;
    292         g_TeamMiscHelperData[playerState.team][position].Worker = playerState.statistics.unitsTrained.Worker;
    293327        return Math.floor((playerState.statistics.unitsTrained.Female / playerState.statistics.unitsTrained.Worker) * 100) + "%";
    294     }
    295328    else
    296329        return 0 + "%";
    297330}
    298331
    299 function calculateKillDeathRatio(playerState, position)
     332function calculateKillDeathRatio(playerState)
    300333{
    301     if (!g_TeamMiscHelperData[playerState.team])
    302         g_TeamMiscHelperData[playerState.team] = [];
    303 
    304     if (!g_TeamMiscHelperData[playerState.team][position])
    305         g_TeamMiscHelperData[playerState.team][position] = { "enemyUnitsKilled": 0, "unitsLost": 0 };
    306 
    307     g_TeamMiscHelperData[playerState.team][position].enemyUnitsKilled = playerState.statistics.enemyUnitsKilled.total;
    308     g_TeamMiscHelperData[playerState.team][position].unitsLost = playerState.statistics.unitsLost.total;
    309 
    310334    if (!playerState.statistics.enemyUnitsKilled.total)
    311335        return g_DefaultDecimal;
    312336
     
    316340    return Math.round((playerState.statistics.enemyUnitsKilled.total / playerState.statistics.unitsLost.total)*100)/100;
    317341}
    318342
    319 function calculateMapExploration(playerState, position)
     343function calculateMapExploration(playerState)
    320344{
    321     if (!g_TeamMiscHelperData[playerState.team])
    322         g_TeamMiscHelperData[playerState.team] = [];
    323 
    324     g_TeamMiscHelperData[playerState.team][position] = playerState.statistics.teamPercentMapExplored;
    325 
    326345    return playerState.statistics.percentMapExplored + "%";
    327346}
    328347
    329 function calculateMapFinalControl(playerState, position)
     348function calculateMapFinalControl(playerState)
    330349{
    331     if (!g_TeamMiscHelperData[playerState.team])
    332         g_TeamMiscHelperData[playerState.team] = [];
    333 
    334     g_TeamMiscHelperData[playerState.team][position] = playerState.statistics.teamPercentMapControlled;
    335 
    336350    return playerState.statistics.percentMapControlled + "%";
    337351}
    338352
    339 function calculateMapPeakControl(playerState, position)
     353function calculateMapPeakControl(playerState)
    340354{
    341     if (!g_TeamMiscHelperData[playerState.team])
    342         g_TeamMiscHelperData[playerState.team] = [];
    343 
    344     g_TeamMiscHelperData[playerState.team][position] = playerState.statistics.teamPeakPercentMapControlled;
    345 
    346355    return playerState.statistics.peakPercentMapControlled + "%";
    347356}
    348357
     
    358367            let teamTotal = "undefined";
    359368
    360369            if (w == 0)
    361                 teamTotal = (g_TeamMiscHelperData[t][w].food == 0 ? "0" : Math.floor((g_TeamMiscHelperData[t][w].vegetarianFood / g_TeamMiscHelperData[t][w].food) * 100)) + "%";
     370                teamTotal = (g_TeamHelperData[t].food == 0 ? "0" : Math.floor((g_TeamHelperData[t].vegetarianFood / g_TeamHelperData[t].food) * 100)) + "%";
    362371            else if (w == 1)
    363                 teamTotal = (g_TeamMiscHelperData[t][w].Worker == 0 ? "0" : Math.floor((g_TeamMiscHelperData[t][w].Female / g_TeamMiscHelperData[t][w].Worker) * 100)) + "%";
     372                teamTotal = (g_TeamHelperData[t].worker == 0 ? "0" : Math.floor((g_TeamHelperData[t].female / g_TeamHelperData[t].worker) * 100)) + "%";
    364373            else if (w == 2)
    365374            {
    366                 if (!g_TeamMiscHelperData[t][w].enemyUnitsKilled)
     375                if (!g_TeamHelperData[t].enemyUnitsKilled)
    367376                    teamTotal = g_DefaultDecimal;
    368                 else if (!g_TeamMiscHelperData[t][w].unitsLost) // and enemyUnitsKilled.total > 0
     377                else if (!g_TeamHelperData[t].unitsLost)    // and enemyUnitsKilled.total > 0
    369378                    teamTotal = g_InfiniteSymbol; // infinity symbol
    370379                else
    371                     teamTotal = Math.round((g_TeamMiscHelperData[t][w].enemyUnitsKilled / g_TeamMiscHelperData[t][w].unitsLost) * 100) / 100;
     380                    teamTotal = Math.round((g_TeamHelperData[t].enemyUnitsKilled / g_TeamHelperData[t].unitsLost) * 100) / 100;
    372381            }
    373             else if (w >= 3)
    374                 teamTotal = g_TeamMiscHelperData[t][w] + "%";
     382            else if (w == 3)
     383                teamTotal = g_TeamHelperData[t].percentMapExplored + "%";
     384            else if (w == 4)
     385                teamTotal = g_TeamHelperData[t].percentMapControlled + "%";
     386            else if (w == 5)
     387                teamTotal = g_TeamHelperData[t].peakPercentMapControlled + "%";
    375388
    376389            Engine.GetGUIObjectByName("valueDataTeam[" + t + "][" + w + "]").caption = teamTotal;
    377390        }
  • binaries/data/mods/public/gui/summary/layout.js

     
    113113            { "width": 100, "fn": calculateResourceExchanged },
    114114            { "width": 100, "fn": calculateResourceExchanged },
    115115            { "width": 100, "fn": calculateResourceExchanged },
    116             { "width": 100, "fn": calculateBatteryEfficiency },
     116            { "width": 100, "fn": calculateBarterEfficiency },
    117117            { "width": 100, "fn": calculateTradeIncome }
    118118        ],
    119119        "teamCounterFn": calculateMarketTeam
  • binaries/data/mods/public/gui/summary/summary.js

     
    119119
    120120        // Update counters
    121121        updateCountersPlayer(playerState, panelInfo.counters, playerCounterValue);
     122
     123        // Calculate g_TeamHelperData
     124        calculateTeamCounters(playerState);
    122125    }
    123126    // Update team counters
    124127    let teamCounterFn = panelInfo.teamCounterFn;