Ticket #3143: 3143_lobby_data_v2.6.6.patch

File 3143_lobby_data_v2.6.6.patch, 16.6 KB (added by Imarok, 3 years ago)

use whitelist in sendLobbyStanzaUpdate and changed the comment of the format function

  • binaries/data/mods/public/gui/common/functions_utility.js

     
    5959 * Escape tag start and escape characters, so users cannot use special formatting.
    6060 * Also limit string length to 256 characters (not counting escape characters).
    6161 */
    62 function escapeText(text)
     62function escapeText(text, limitLength = true)
    6363{
    6464    if (!text)
    6565        return text;
    6666
    67     return text.substr(0, 255).replace(/\\/g, "\\\\").replace(/\[/g, "\\[");
     67    if (limitLength)
     68        text = text.substr(0, 255);
     69
     70    return text.replace(/\\/g, "\\\\").replace(/\[/g, "\\[");
    6871}
    6972
     73function unescapeText(text)
     74{
     75    if (!text)
     76        return text;
     77    return text.replace(/\\\\/g, "\\").replace(/\\\[/g, "\[");
     78}
     79
     80/**
     81 * Merge players by team to remove duplicate team entries, thus reducing the packet size.
     82 */
     83function playerDataToStringifiedTeamList(originalplayerDataArray)
     84{
     85    let playerDataArray = clone(originalplayerDataArray);
     86    let teamList = {};
     87    for (let playerData of playerDataArray)
     88    {
     89        if (playerData.Team === undefined)
     90            playerData.Team = -1;
     91        if (!teamList[playerData.Team])
     92            teamList[playerData.Team] = [];
     93        teamList[playerData.Team].push(playerData);
     94        delete teamList[playerData.Team].Team;
     95    }
     96    return escapeText(JSON.stringify(teamList), false);
     97}
     98
     99function stringifiedTeamListToPlayerData(stringifiedTeamList)
     100{
     101    let teamList = JSON.parse(unescapeText(stringifiedTeamList));
     102    let playerData = [];
     103    for (let team in teamList)
     104    {
     105        for (let player of teamList[team])
     106        {
     107            player.Team = team;
     108            playerData.push(player);
     109        }
     110    }
     111    return playerData;
     112}
     113
    70114function translateMapTitle(mapTitle)
    71115{
    72116    return mapTitle == "random" ? translateWithContext("map selection", "Random") : translate(mapTitle);
     
    226270
    227271    for (let playerData of playerDataArray)
    228272    {
    229         if (playerData == null || playerData.Civ == "gaia")
     273        if (playerData == null || playerData.Civ && playerData.Civ == "gaia")
    230274            continue;
    231275
    232276        ++playerIdx;
    233277        let teamIdx = playerData.Team;
    234278        let isAI = playerData.AI && playerData.AI != "";
    235         let playerState = playerStates && playerStates[playerIdx];
     279        let playerState = playerStates && playerStates[playerIdx] || playerData.State;
    236280        let isActive = !playerState || playerState == "active";
     281        let isOffline = playerData.Offline;
    237282
    238283        let playerDescription;
    239284        if (isAI)
    240285        {
    241             if (isActive)
    242                 // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
    243                 playerDescription = translate("%(playerName)s (%(civ)s, %(AIdifficulty)s %(AIname)s)");
     286            if (playerData.Civ)
     287            {
     288                if (isActive)
     289                    // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
     290                    playerDescription = translate("%(playerName)s (%(civ)s, %(AIdifficulty)s %(AIname)s)");
     291                else
     292                    // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
     293                    playerDescription = translate("%(playerName)s (%(civ)s, %(AIdifficulty)s %(AIname)s, %(state)s)");
     294            }
    244295            else
    245                 // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
    246                 playerDescription = translate("%(playerName)s (%(civ)s, %(AIdifficulty)s %(AIname)s, %(state)s)");
     296            {
     297                if (isActive)
     298                    // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
     299                    playerDescription = translate("%(playerName)s (%(AIdifficulty)s %(AIname)s)");
     300                else
     301                    // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
     302                    playerDescription = translate("%(playerName)s (%(AIdifficulty)s %(AIname)s, %(state)s)");
     303            }
    247304        }
    248305        else
    249306        {
    250             if (isActive)
    251                 // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
    252                 playerDescription = translate("%(playerName)s (%(civ)s)");
     307            // Can only occur in the lobby for now, so no strings with civ needed
     308            if (isOffline)
     309            {
     310                if (isActive)
     311                    // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
     312                    playerDescription = translate("%(playerName)s (OFFLINE)");
     313                else
     314                    // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
     315                    playerDescription = translate("%(playerName)s (OFFLINE, %(state)s)");
     316            }
    253317            else
    254                 // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
    255                 playerDescription = translate("%(playerName)s (%(civ)s, %(state)s)");
     318            {
     319                if (playerData.Civ)
     320                    if (isActive)
     321                        // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
     322                        playerDescription = translate("%(playerName)s (%(civ)s)");
     323                    else
     324                        // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
     325                        playerDescription = translate("%(playerName)s (%(civ)s, %(state)s)");
     326                else
     327                    if (isActive)
     328                        // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
     329                        playerDescription = translate("%(playerName)s");
     330                    else
     331                        // Translation: Describe a player in a selected game, f.e. in the replay- or savegame menu
     332                        playerDescription = translate("%(playerName)s (%(state)s)");
     333            }
    256334        }
    257335
    258336        // Sort player descriptions by team
     
    262340        playerDescriptions[teamIdx].push(sprintf(playerDescription, {
    263341            "playerName":
    264342                '[color="' +
    265                 rgbToGuiColor(playerData.Color || g_Settings.PlayerDefaults[playerIdx].Color) +
     343                (typeof getPlayerColor == 'function' ?
     344                    (isAI ?
     345                        "255 255 255" :
     346                        getPlayerColor(playerData.Name)) :
     347                    rgbToGuiColor(playerData.Color || g_Settings.PlayerDefaults[playerIdx].Color)) +
    266348                '"]' + escapeText(playerData.Name) + "[/color]",
    267349
    268350            "civ":
     
    283365    }
    284366
    285367    let teams = Object.keys(playerDescriptions);
     368    if (teams.indexOf("observer") > -1)
     369        teams.splice(teams.indexOf("observer"), 1);
    286370
     371    let teamDescription = [];
     372
    287373    // If there are no teams, merge all playersDescriptions
    288374    if (teams.length == 1)
    289         return playerDescriptions[teams[0]].join("\n") + "\n";
     375        teamDescription.push(playerDescriptions[teams[0]].join("\n"));
    290376
    291377    // If there are teams, merge "Team N:" + playerDescriptions
    292     return teams.map(team => {
     378    else
     379        teamDescription = teamDescription.concat(teams.map(team => {
    293380
    294         let teamCaption = team == -1 ?
    295             translate("No Team") :
    296             sprintf(translate("Team %(team)s"), { "team": +team + 1 });
     381            let teamCaption = team == -1 ?
     382                translate("No Team") :
     383                sprintf(translate("Team %(team)s"), { "team": +team + 1 });
    297384
    298         // Translation: Describe players of one team in a selected game, f.e. in the replay- or savegame menu or lobby
    299         return sprintf(translate("%(team)s:\n%(playerDescriptions)s"), {
    300             "team": '[font="sans-bold-14"]' + teamCaption + "[/font]",
    301             "playerDescriptions": playerDescriptions[team].join("\n")
    302         });
    303     }).join("\n\n");
     385            // Translation: Describe players of one team in a selected game, f.e. in the replay- or savegame menu or lobby
     386            return sprintf(translate("%(team)s:\n%(playerDescriptions)s"), {
     387                "team": '[font="sans-bold-14"]' + teamCaption + "[/font]",
     388                "playerDescriptions": playerDescriptions[team].join("\n")
     389            });
     390        }));
     391
     392    if (playerDescriptions.observer)
     393        teamDescription.push(sprintf(translate("%(team)s:\n%(playerDescriptions)s"), {
     394            "team": '[font="sans-bold-14"]' + translatePlural("Observer", "Observers", playerDescriptions.observer.length) + "[/font]",
     395            "playerDescriptions": playerDescriptions.observer.join("\n")
     396        }));
     397
     398    return teamDescription.join("\n\n");
    304399}
  • binaries/data/mods/public/gui/gamesetup/gamesetup.js

     
    678678{
    679679    if (g_IsController && Engine.HasXmppClient())
    680680    {
    681         let playerNames = Object.keys(g_PlayerAssignments).map(guid => g_PlayerAssignments[guid].name);
    682         Engine.SendChangeStateGame(playerNames.length, playerNames.join(", "));
     681        let clients = formatClientsForStanza();
     682        Engine.SendChangeStateGame(clients.nbp, clients.list);
    683683    }
    684684
    685685    Engine.SwitchGuiPage("page_loading.xml", {
     
    19381938        setReady(false, false);
    19391939}
    19401940
     1941function formatClientsForStanza()
     1942{
     1943    let numberOfPlayers = 0;
     1944    let list = [];
     1945    for (let guid in g_PlayerAssignments)
     1946    {
     1947        if (g_GameAttributes.settings.PlayerData[g_PlayerAssignments[guid].player - 1])
     1948        {
     1949            ++numberOfPlayers;
     1950            list.push({
     1951                "Name": g_PlayerAssignments[guid].name
     1952            });
     1953        }
     1954        else
     1955            list.push({
     1956                "Name": g_PlayerAssignments[guid].name,
     1957                "Team": "observer"
     1958            });
     1959    }
     1960    return {
     1961        "list": playerDataToStringifiedTeamList(list),
     1962        "nbp": numberOfPlayers
     1963    };
     1964}
     1965
    19411966/**
    19421967 * Send the relevant gamesettings to the lobbybot.
    19431968 */
     
    19511976
    19521977    let mapSize = g_GameAttributes.mapType == "random" ? Engine.GetGUIObjectByName("mapSize").list_data[selectedMapSize] : "Default";
    19531978    let victoryCondition = Engine.GetGUIObjectByName("victoryCondition").list[selectedVictoryCondition];
    1954     let playerNames = Object.keys(g_PlayerAssignments).map(guid => g_PlayerAssignments[guid].name).sort();
     1979    let clients = formatClientsForStanza();
    19551980
    19561981    let stanza = {
    19571982        "name": g_ServerName,
     
    19611986        "mapSize": mapSize,
    19621987        "mapType": g_GameAttributes.mapType,
    19631988        "victoryCondition": victoryCondition,
    1964         "nbp": Object.keys(g_PlayerAssignments).length || 1,
    1965         "tnbp": g_GameAttributes.settings.PlayerData.length,
    1966         "players": playerNames.join(", ")
     1989        "nbp": clients.nbp,
     1990        "maxnbp": g_GameAttributes.settings.PlayerData.length,
     1991        "players": clients.list,
    19671992    };
    19681993
    19691994    // Only send the stanza if the relevant settings actually changed
  • binaries/data/mods/public/gui/lobby/lobby.js

     
    285285        return true;
    286286
    287287    if (playersNumberFilter.selected != 0 &&
    288         game.tnbp != playersNumberFilter.list_data[playersNumberFilter.selected])
     288        game.maxnbp != playersNumberFilter.list_data[playersNumberFilter.selected])
    289289        return true;
    290290
    291291    if (mapTypeFilter.selected != 0 &&
     
    292292        game.mapType != mapTypeFilter.list_data[mapTypeFilter.selected])
    293293        return true;
    294294
    295     if (!showFullFilter.checked && game.tnbp <= game.nbp)
     295    if (!showFullFilter.checked && game.maxnbp <= game.nbp)
    296296        return true;
    297297
    298298    return false;
     
    546546            break;
    547547        case 'nPlayers':
    548548            // Compare playercount ratio
    549             sortA = a.nbp * b.tnbp;
    550             sortB = b.nbp * a.tnbp;
     549            sortA = a.nbp * b.maxnbp;
     550            sortB = b.nbp * a.maxnbp;
    551551            break;
    552552        case 'status':
    553553        default:
     
    582582        list_mapName.push(translateMapTitle(game.niceMapName));
    583583        list_mapSize.push(translateMapSize(game.mapSize));
    584584        list_mapType.push(g_MapTypes.Title[mapTypeIdx] || "");
    585         list_nPlayers.push(game.nbp + "/" + game.tnbp);
     585        list_nPlayers.push(game.nbp + "/" + game.maxnbp);
    586586        list.push(gameName);
    587587        list_data.push(i);
    588588    }
     
    616616
    617617    Engine.GetGUIObjectByName("sgMapName").caption = translateMapTitle(game.niceMapName);
    618618    Engine.GetGUIObjectByName("sgNbPlayers").caption = sprintf(
    619         translate("Players: %(current)s/%(total)s"), {
     619        translate("Players: %(current)s/%(max)s"), {
    620620            "current": game.nbp,
    621             "total": game.tnbp
     621            "max": game.maxnbp
    622622        });
    623623
    624     Engine.GetGUIObjectByName("sgPlayersNames").caption = game.players;
     624    Engine.GetGUIObjectByName("sgPlayersNames").caption = formatPlayerInfo(stringifiedTeamListToPlayerData(game.players));
    625625    Engine.GetGUIObjectByName("sgMapSize").caption = translateMapSize(game.mapSize);
    626626
    627627    let mapTypeIdx = g_MapTypes.Name.indexOf(game.mapType);
     
    653653
    654654    let username = g_UserRating ? g_Username + " (" + g_UserRating + ")" : g_Username;
    655655
    656     if (game.state == "init" || game.players.split(", ").indexOf(username) > -1)
     656    if (game.state == "init" || stringifiedTeamListToPlayerData(game.players).some(player => player.Name == username))
    657657        joinSelectedGame();
    658658    else
    659659        messageBox(
  • binaries/data/mods/public/gui/session/messages.js

     
    544544
    545545    // Update lobby gamestatus
    546546    if (g_IsController && Engine.HasXmppClient())
    547     {
    548         let players = Object.keys(g_PlayerAssignments).map(guid => g_PlayerAssignments[guid].name);
    549         Engine.SendChangeStateGame(Object.keys(g_PlayerAssignments).length, players.join(", "));
    550     }
     547        sendLobbyStanzaUpdate();
    551548}
    552549
    553550function onClientJoin(guid)
     
    746743
    747744function formatDefeatMessage(msg)
    748745{
     746    if (g_IsController && Engine.HasXmppClient())
     747        sendLobbyStanzaUpdate();
     748
    749749    return sprintf(
    750750        msg.resign ?
    751751            translate("%(player)s has resigned.") :
  • binaries/data/mods/public/gui/session/session.js

     
    288288    if (hotloadData)
    289289        g_Selection.selected = hotloadData.selection;
    290290
     291    if (g_IsController && Engine.HasXmppClient())
     292        sendLobbyStanzaUpdate();
     293
    291294    onSimulationUpdate();
    292295
    293296    setTimeout(displayGamestateNotifications, 1000);
  • binaries/data/mods/public/gui/session/utility_functions.js

     
    131131    alpha = alpha > 125 ? 125 : alpha;
    132132    return "color:255 0 0 " + alpha;
    133133}
     134 
     135function sendLobbyStanzaUpdate()
     136{
     137    let numberOfPlayers = 0;
     138    let playerStates = {};
     139    let playerDataArray = g_GameAttributes.settings.PlayerData.map(pData => ({
     140        "Name": pData.Name,
     141        "Team": pData.Team,
     142        "AI": pData.AI,
     143        "AIDiff": pData.AI
     144    }));
     145
     146    for (let player of g_Players)
     147        playerStates[player.name] = {
     148            "State": player.state,
     149            "Offline": player.offline
     150        };
     151
     152    for (let playerData of playerDataArray)
     153    {
     154        if (!g_GameAttributes.settings.LockTeams)
     155            delete playerData.Team;
     156
     157        let pState = playerStates[playerData.Name];
     158        if (pState)
     159        {
     160            playerData.State = pState.State;
     161            playerData.Offline = pState.Offline;
     162        }
     163    }
     164
     165    for (let guid in g_PlayerAssignments)
     166    {
     167        let pData = g_GameAttributes.settings.PlayerData[g_PlayerAssignments[guid].player - 1];
     168
     169        if (pData)
     170            ++numberOfPlayers;
     171        else
     172            playerDataArray.push({
     173                "Name": g_PlayerAssignments[guid].name,
     174                "Team": "observer"
     175            });
     176    }
     177    Engine.SendChangeStateGame(numberOfPlayers, playerDataToStringifiedTeamList(playerDataArray));
     178}
  • source/lobby/XmppClient.cpp

     
    502502    JSAutoRequest rq(cx);
    503503
    504504    scriptInterface.Eval("([])", ret);
    505     const char* stats[] = { "name", "ip", "port", "state", "nbp", "tnbp", "players", "mapName", "niceMapName", "mapSize", "mapType", "victoryCondition" };
     505    const char* stats[] = { "name", "ip", "port", "state", "nbp", "maxnbp", "players", "mapName", "niceMapName", "mapSize", "mapType", "victoryCondition" };
    506506    for(const glooxwrapper::Tag* const& t : m_GameList)
    507507    {
    508508        JS::RootedValue game(cx);
  • source/tools/XpartaMuPP/XpartaMuPP.py

     
    301301    if JID in self.gameList:
    302302      if self.gameList[JID]['nbp-init'] > data['nbp']:
    303303        logging.debug("change game (%s) state from %s to %s", JID, self.gameList[JID]['state'], 'waiting')
    304         self.gameList[JID]['nbp'] = data['nbp']
    305304        self.gameList[JID]['state'] = 'waiting'
    306305      else:
    307306        logging.debug("change game (%s) state from %s to %s", JID, self.gameList[JID]['state'], 'running')
    308         self.gameList[JID]['nbp'] = data['nbp']
    309307        self.gameList[JID]['state'] = 'running'
     308      self.gameList[JID]['nbp'] = data['nbp']
     309      self.gameList[JID]['players'] = data['players']
    310310
    311311## Class which manages different game reports from clients ##
    312312##   and calls leaderboard functions as appropriate.       ##