diff --git a/binaries/data/mods/public/gui/lobby/lobby.js b/binaries/data/mods/public/gui/lobby/lobby.js index 3f74a1cee0..b8a639bed4 100644 --- a/binaries/data/mods/public/gui/lobby/lobby.js +++ b/binaries/data/mods/public/gui/lobby/lobby.js @@ -144,7 +144,7 @@ function updateSubject(newSubject) } /** - * Do a full update of the player listing, including ratings. + * Do a full update of the player listing, including ratings from C++. * * @return Array containing the player, presence, nickname, and rating listings. */ @@ -152,29 +152,24 @@ function updatePlayerList() { var playersBox = Engine.GetGUIObjectByName("playersBox"); [playerList, presenceList, nickList, ratingList] = [[],[],[],[]]; - var ratingsmap = Engine.GetRatingList(); - var defaultrating; - for each (var p in Engine.GetPlayerList()) + var cleanPlayerList = Engine.GetPlayerList(); + for (var i = 0; i < cleanPlayerList.length; i++) { - defaultrating = ' -'; //Start with unrated. If no rating is found, keep it unrated. - for each (var user in ratingsmap) - { - if (user.name.toLowerCase() == p.name.toLowerCase()) - { - defaultrating = user.rating; - break; - } - } - var [name, status, rating] = formatPlayerListEntry(p.name, p.presence, defaultrating); + // Add a "-" for unrated players. + if (!cleanPlayerList[i].rating) + cleanPlayerList[i].rating = " -"; + // Colorize. + var [name, status, rating] = formatPlayerListEntry(cleanPlayerList[i].name, cleanPlayerList[i].presence, cleanPlayerList[i].rating); + // Push to lists. playerList.push(name); presenceList.push(status); - nickList.push(p.name); + nickList.push(cleanPlayerList[i].name); ratingList.push(String(" " + rating)); } playersBox.list_name = playerList; playersBox.list_status = presenceList; playersBox.list_rating = ratingList; - playersBox.list = nickList; + playersBox.list = nickList; if (playersBox.selected >= playersBox.list.length) playersBox.selected = -1; return [playerList, presenceList, nickList, ratingList]; diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp index 81fbd26720..173d53eaeb 100644 --- a/source/gui/scripting/ScriptFunctions.cpp +++ b/source/gui/scripting/ScriptFunctions.cpp @@ -905,7 +905,6 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) scriptInterface.RegisterFunction("GetPlayerList"); scriptInterface.RegisterFunction("GetGameList"); scriptInterface.RegisterFunction("GetBoardList"); - scriptInterface.RegisterFunction("GetRatingList"); scriptInterface.RegisterFunction("LobbyGuiPollMessage"); scriptInterface.RegisterFunction("LobbySendMessage"); scriptInterface.RegisterFunction("LobbySetPlayerPresence"); diff --git a/source/lobby/XmppClient.cpp b/source/lobby/XmppClient.cpp index 66f977f68c..5f839772dc 100644 --- a/source/lobby/XmppClient.cpp +++ b/source/lobby/XmppClient.cpp @@ -477,18 +477,45 @@ void XmppClient::handleOOB(const glooxwrapper::JID&, const glooxwrapper::OOB&) */ CScriptValRooted XmppClient::GUIGetPlayerList(ScriptInterface& scriptInterface) { + const int PLAYER_NUM = m_PlayerMap.size(); + std::string playerListArray[PLAYER_NUM][3]; std::string presence; CScriptValRooted playerList; - scriptInterface.Eval("({})", playerList); - for(std::map::const_iterator it = m_PlayerMap.begin(); it != m_PlayerMap.end(); ++it) + scriptInterface.Eval("([])", playerList); + + int i = 0; + // Load name and presence data. + for (std::map::const_iterator it = m_PlayerMap.begin(); it != m_PlayerMap.end(); ++it, i++) + { + playerListArray[i][0] = it->first; + GetPresenceString(it->second, presence); + playerListArray[i][1] = presence; + } + + // Load rating data. + for (std::vector::const_iterator it = m_RatingList.begin(); it != m_RatingList.end(); ++it) + { + // Try to match the names in the rating list to the names from the player map. + for (i = 0; i < PLAYER_NUM; i++) + { + std::string name = (*it)->findAttribute("name").to_string(); + if (playerListArray[i][0].compare(name) == 0) + { + playerListArray[i][2] = (*it)->findAttribute("rating").to_string(); + break; + } + } + } + + // Convert the array to a Javascript object. + for (i = 0; i < PLAYER_NUM; i++) { CScriptValRooted player; - GetPresenceString(it->second, presence); scriptInterface.Eval("({})", player); - scriptInterface.SetProperty(player.get(), "name", wstring_from_utf8(it->first)); - scriptInterface.SetProperty(player.get(), "presence", wstring_from_utf8(presence)); - - scriptInterface.SetProperty(playerList.get(), wstring_from_utf8(it->first).c_str(), player); + scriptInterface.SetProperty(player.get(), "name", wstring_from_utf8(playerListArray[i][0])); + scriptInterface.SetProperty(player.get(), "presence", wstring_from_utf8(playerListArray[i][1])); + scriptInterface.SetProperty(player.get(), "rating", wstring_from_utf8(playerListArray[i][2])); + scriptInterface.CallFunctionVoid(playerList.get(), "push", player); } return playerList; @@ -544,31 +571,6 @@ CScriptValRooted XmppClient::GUIGetBoardList(ScriptInterface& scriptInterface) return boardList; } -/** - * Handle requests from the GUI for rating list data. - * - * @return A JS array containing all known leaderboard data - */ -CScriptValRooted XmppClient::GUIGetRatingList(ScriptInterface& scriptInterface) -{ - CScriptValRooted ratingList; - scriptInterface.Eval("([])", ratingList); - for(std::vector::const_iterator it = m_RatingList.begin(); it != m_RatingList.end(); ++it) - { - CScriptValRooted rating; - scriptInterface.Eval("({})", rating); - - const char* attributes[] = { "name", "rank", "rating" }; - short attributes_length = 3; - for (short i = 0; i < attributes_length; i++) - scriptInterface.SetProperty(rating.get(), attributes[i], wstring_from_utf8((*it)->findAttribute(attributes[i]).to_string())); - - scriptInterface.CallFunctionVoid(ratingList.get(), "push", rating); - } - - return ratingList; -} - /***************************************************** * Message interfaces * *****************************************************/ diff --git a/source/lobby/XmppClient.h b/source/lobby/XmppClient.h index 0327635034..0ba769577e 100644 --- a/source/lobby/XmppClient.h +++ b/source/lobby/XmppClient.h @@ -76,7 +76,6 @@ public: CScriptValRooted GUIGetPlayerList(ScriptInterface& scriptInterface); CScriptValRooted GUIGetGameList(ScriptInterface& scriptInterface); CScriptValRooted GUIGetBoardList(ScriptInterface& scriptInterface); - CScriptValRooted GUIGetRatingList(ScriptInterface& scriptInterface); //Script ScriptInterface& GetScriptInterface(); diff --git a/source/lobby/scripting/JSInterface_Lobby.h b/source/lobby/scripting/JSInterface_Lobby.h index d4e18d85de..7db1252979 100644 --- a/source/lobby/scripting/JSInterface_Lobby.h +++ b/source/lobby/scripting/JSInterface_Lobby.h @@ -43,7 +43,6 @@ namespace JSI_Lobby CScriptVal GetPlayerList(ScriptInterface::CxPrivate* pCxPrivate); CScriptVal GetGameList(ScriptInterface::CxPrivate* pCxPrivate); CScriptVal GetBoardList(ScriptInterface::CxPrivate* pCxPrivate); - CScriptVal GetRatingList(ScriptInterface::CxPrivate* pCxPrivate); CScriptVal LobbyGuiPollMessage(ScriptInterface::CxPrivate* pCxPrivate); void LobbySendMessage(ScriptInterface::CxPrivate* pCxPrivate, std::wstring message); void LobbySetPlayerPresence(ScriptInterface::CxPrivate* pCxPrivate, std::wstring presence);