Index: ps/trunk/binaries/data/mods/public/gui/lobby/LeaderboardPage/LeaderboardList.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/lobby/LeaderboardPage/LeaderboardList.js +++ ps/trunk/binaries/data/mods/public/gui/lobby/LeaderboardPage/LeaderboardList.js @@ -0,0 +1,64 @@ +/** + * This is class manages the content of the leaderboardlist, i.e. the list of highest rated players. + */ +class LeaderboardList +{ + constructor(xmppMessages) + { + this.selectionChangeHandlers = new Set(); + + this.leaderboardBox = Engine.GetGUIObjectByName("leaderboardBox"); + this.leaderboardBox.onSelectionChange = this.onSelectionChange.bind(this); + + let rebuild = this.rebuild.bind(this); + xmppMessages.registerXmppMessageHandler("game", "leaderboard", rebuild); + xmppMessages.registerXmppMessageHandler("system", "disconnected", rebuild); + + this.rebuild(); + } + + registerSelectionChangeHandler(handler) + { + this.selectionChangeHandlers.add(handler); + } + + onSelectionChange() + { + let playerName = this.selectedPlayer(); + for (let handler of this.selectionChangeHandlers) + handler(playerName); + } + + selectedPlayer() + { + return this.leaderboardBox.list[this.leaderboardBox.selected] || undefined; + } + + /** + * Update the leaderboard from data cached in C++. + */ + rebuild() + { + // TODO: Display placeholder if the data is not available + let boardList = Engine.GetBoardList().sort( + (a, b) => b.rating - a.rating); + + let list_name = []; + let list_rank = []; + let list_rating = []; + + boardList.forEach((entry, i) => { + list_name.push(escapeText(entry.name)); + list_rating.push(entry.rating); + list_rank.push(i + 1); + }); + + this.leaderboardBox.list_name = list_name; + this.leaderboardBox.list_rating = list_rating; + this.leaderboardBox.list_rank = list_rank; + this.leaderboardBox.list = list_name; + + if (this.leaderboardBox.selected >= this.leaderboardBox.list.length) + this.leaderboardBox.selected = -1; + } +} Index: ps/trunk/binaries/data/mods/public/gui/lobby/LeaderboardPage/LeaderboardPage.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/lobby/LeaderboardPage/LeaderboardPage.js +++ ps/trunk/binaries/data/mods/public/gui/lobby/LeaderboardPage/LeaderboardPage.js @@ -0,0 +1,52 @@ +/** + * The leaderboard page allows the player to view the highest rated players and update that list. + */ +class LeaderboardPage +{ + constructor(xmppMessages) + { + this.openPageHandlers = new Set(); + this.closePageHandlers = new Set(); + + this.leaderboardList = new LeaderboardList(xmppMessages); + + this.leaderboardPage = Engine.GetGUIObjectByName("leaderboardPage"); + + Engine.GetGUIObjectByName("leaderboardUpdateButton").onPress = this.onPressUpdate.bind(this); + Engine.GetGUIObjectByName("leaderboardPageBack").onPress = this.onPressClose.bind(this); + } + + registerOpenPageHandler(handler) + { + this.openPageHandlers.add(handler); + } + + registerClosePageHandler(handler) + { + this.closePageHandlers.add(handler); + } + + openPage() + { + this.leaderboardPage.hidden = false; + Engine.SetGlobalHotkey("cancel", this.onPressClose.bind(this)); + Engine.SendGetBoardList(); + + let playerName = this.leaderboardList.selectedPlayer(); + for (let handler of this.openPageHandlers) + handler(playerName); + } + + onPressUpdate() + { + Engine.SendGetBoardList(); + } + + onPressClose() + { + this.leaderboardPage.hidden = true; + + for (let handler of this.closePageHandlers) + handler(); + } +} Index: ps/trunk/binaries/data/mods/public/gui/lobby/LeaderboardPage/LeaderboardPage.xml =================================================================== --- ps/trunk/binaries/data/mods/public/gui/lobby/LeaderboardPage/LeaderboardPage.xml +++ ps/trunk/binaries/data/mods/public/gui/lobby/LeaderboardPage/LeaderboardPage.xml @@ -0,0 +1,33 @@ + +