Ticket #3244: t3244_keep_player_and_gamelist_selection_v4_16839.patch

File t3244_keep_player_and_gamelist_selection_v4_16839.patch, 4.7 KB (added by elexis, 9 years ago)

Changed g_filteredGameList so that it only stores IP addresses (to save some memory). Couldn't find any trailing whitespace. Only one local variable var selectedIndex = -1; was added. Used var instead of let because it is used everywhere else in that code and changing every occurance without thorough testing might be a bad idea.

  • binaries/data/mods/public/gui/lobby/lobby.js

     
    11var g_ChatMessages = [];
    22var g_Name = "unknown";
    33var g_GameList = {}
     4var g_filteredGameListIPs = [];
     5var g_selectedGameIP = "";
     6var g_selectedNick = "";
    47var g_GameListSortBy = "name";
    58var g_PlayerListSortBy = "name";
    69var g_GameListOrder = 1; // 1 for ascending sort, and -1 for descending
    710var g_PlayerListOrder = 1;
    811var g_specialKey = Math.random();
     
    170173 * @return Array containing the player, presence, nickname, and rating listings.
    171174 */
    172175function updatePlayerList()
    173176{
    174177    var playersBox = Engine.GetGUIObjectByName("playersBox");
     178   
     179    // Save previously selected player
     180    if (playersBox.selected > -1)
     181        g_selectedNick = playersBox.list[playersBox.selected];
     182       
    175183    var playerList = [];
    176184    var presenceList = [];
    177185    var nickList = [];
    178186    var ratingList = [];
    179187    var cleanPlayerList = Engine.GetPlayerList();
     
    229237    }
    230238    playersBox.list_name = playerList;
    231239    playersBox.list_status = presenceList;
    232240    playersBox.list_rating = ratingList;
    233241    playersBox.list = nickList;
    234     if (playersBox.selected >= playersBox.list.length)
    235         playersBox.selected = -1;
     242   
     243    // Select the previously selected player
     244    playersBox.selected = playersBox.list.indexOf(g_selectedNick);
     245   
    236246    return [playerList, presenceList, nickList, ratingList];
    237247}
    238248
    239249/**
    240250 * Display the profile of the selected player.
     
    395405 * Update the game listing from data cached in C++.
    396406 */
    397407function updateGameList()
    398408{
    399409    var gamesBox = Engine.GetGUIObjectByName("gamesBox");
    400     var gameList = Engine.GetGameList();
     410
     411    // Save previously selected game by ip
     412    if (gamesBox.selected > -1)
     413        g_selectedGameIP = g_filteredGameListIPs[gamesBox.selected];
     414   
    401415    // Store the game whole game list data so that we can access it later
    402416    // to update the game info panel.
    403     g_GameList = gameList;
     417    g_GameList = Engine.GetGameList();
    404418
    405419    // Sort the list of games to that games 'waiting' are displayed at the top, followed by 'init', followed by 'running'.
    406420    var gameStatuses = ['waiting', 'init', 'running'];
    407421    g_GameList.sort(function (a,b) {
    408422        switch (g_GameListSortBy)
     
    453467    var list_nPlayers = [];
    454468    var list = [];
    455469    var list_data = [];
    456470
    457471    var c = 0;
    458     for (var g of gameList)
     472    var  selectedIndex = -1;
     473    g_filteredGameListIPs = [];
     474    for (var g of g_GameList)
    459475    {
    460476        if (!filterGame(g))
    461477        {
    462478            // 'waiting' games are highlighted in orange, 'running' in red, and 'init' in green.
    463479            let name = escapeText(g.name);
     
    467483                name = '[color="255 127 0"]' + name + '[/color]';
    468484            else
    469485                name = '[color="255 0 0"]' + name + '[/color]';
    470486            list_name.push(name);
    471487            list_ip.push(g.ip);
     488            if (g.ip == g_selectedGameIP)
     489                selectedIndex = list_ip.length - 1;
    472490            list_mapName.push(translate(g.niceMapName));
    473491            list_mapSize.push(translatedMapSize(g.mapSize));
    474492            let idx = g_mapTypes.indexOf(g.mapType);
    475493            list_mapType.push(idx != -1 ? g_mapTypesText[idx] : "");
    476494            list_nPlayers.push(g.nbp + "/" +g.tnbp);
    477495            list.push(name);
    478496            list_data.push(c);
     497            g_filteredGameListIPs.push(g.ip);
    479498        }
    480499        c++;
    481500    }
    482501
    483502    gamesBox.list_name = list_name;
     
    486505    gamesBox.list_mapType = list_mapType;
    487506    gamesBox.list_nPlayers = list_nPlayers;
    488507    gamesBox.list = list;
    489508    gamesBox.list_data = list_data;
    490509
    491     if (gamesBox.selected >= gamesBox.list_name.length)
    492         gamesBox.selected = -1;
    493 
     510    // Select previously selected game
     511    gamesBox.selected = selectedIndex;
     512   
    494513    // Update info box about the game currently selected
    495514    updateGameSelection();
    496515}
    497516
    498517/**
     
    694713            var playerList = playersBox.list_name;
    695714            var presenceList = playersBox.list_status;
    696715            var nickList = playersBox.list;
    697716            var ratingList = playersBox.list_rating;
    698717            var nickIndex = nickList.indexOf(nick);
     718
     719            // Save the previously selected nickname
     720            if (playersBox.selected > -1)
     721                g_selectedNick = playersBox.list[playersBox.selected];
     722
    699723            switch(message.level)
    700724            {
    701725            case "join":
    702726                if (nick == g_Name)
    703727                {
     
    760784            }
    761785            // Push new data to GUI
    762786            playersBox.list_name = playerList;
    763787            playersBox.list_status = presenceList;
    764788            playersBox.list_rating = ratingList;
    765             playersBox.list = nickList;     
    766             if (playersBox.selected >= playersBox.list.length)
    767                 playersBox.selected = -1;
     789            playersBox.list = nickList;
     790            // Select the previously selected player
     791            playersBox.selected = nickList.indexOf(g_selectedNick);
    768792            break;
    769793        case "system":
    770794            switch (message.level)
    771795            {
    772796            case "standard":