Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/gui/lobby/ProfilePage/ProfilePage.js
/** | /** | ||||
* The profile page enables the player to lookup statistics of an arbitrary player. | * The profile page enables the player to lookup statistics of an arbitrary player. | ||||
*/ | */ | ||||
class ProfilePage | class ProfilePage | ||||
{ | { | ||||
constructor(xmppMessages) | constructor(xmppMessages) | ||||
{ | { | ||||
this.requestedPlayer = undefined; | this.requestedPlayer = undefined; | ||||
this.closePageHandlers = new Set(); | this.closePageHandlers = new Set(); | ||||
this.profilePage = Engine.GetGUIObjectByName("profilePage"); | this.profilePage = Engine.GetGUIObjectByName("profilePage"); | ||||
this.fetchInput = Engine.GetGUIObjectByName("fetchInput"); | this.fetchInput = Engine.GetGUIObjectByName("fetchInput"); | ||||
this.fetchInput.onPress = this.onPressLookup.bind(this); | this.fetchInput.onPress = this.onPressLookup.bind(this); | ||||
this.fetchInput.onTab = this.autocomplete.bind(this); | this.fetchInput.onTab = this.autocomplete.bind(this); | ||||
this.fetchInput.tooltip = colorizeAutocompleteHotkey(); | this.fetchInput.tooltip = colorizeAutocompleteHotkey(); | ||||
Engine.GetGUIObjectByName("viewProfileButton").onPress = this.onPressLookup.bind(this); | Engine.GetGUIObjectByName("viewProfileButton").onPress = this.onPressLookup.bind(this); | ||||
Engine.GetGUIObjectByName("profileBackButton").onPress = this.onPressClose.bind(this, true); | Engine.GetGUIObjectByName("profileBackButton").onPress = this.onPressClose.bind(this, true); | ||||
this.profilePlayernameText = Engine.GetGUIObjectByName("profilePlayernameText"); | this.profilePlayernameText = Engine.GetGUIObjectByName("profilePlayernameText"); | ||||
this.profileRankText = Engine.GetGUIObjectByName("profileRankText"); | this.profileRankText = Engine.GetGUIObjectByName("profileRankText"); | ||||
this.profileHighestRatingText = Engine.GetGUIObjectByName("profileHighestRatingText"); | this.profileHighestRatingText = Engine.GetGUIObjectByName("profileHighestRatingText"); | ||||
this.profileTotalGamesText = Engine.GetGUIObjectByName("profileTotalGamesText"); | this.profileTotalGamesText = Engine.GetGUIObjectByName("profileTotalGamesText"); | ||||
this.profileWinsText = Engine.GetGUIObjectByName("profileWinsText"); | this.profileWinsText = Engine.GetGUIObjectByName("profileWinsText"); | ||||
this.profileLossesText = Engine.GetGUIObjectByName("profileLossesText"); | this.profileLossesText = Engine.GetGUIObjectByName("profileLossesText"); | ||||
this.profileRatioText = Engine.GetGUIObjectByName("profileRatioText"); | this.profileRatioText = Engine.GetGUIObjectByName("profileRatioText"); | ||||
this.profileErrorText = Engine.GetGUIObjectByName("profileErrorText"); | this.profileErrorText = Engine.GetGUIObjectByName("profileErrorText"); | ||||
this.profileWindowArea = Engine.GetGUIObjectByName("profileWindowArea"); | this.profileWindowArea = Engine.GetGUIObjectByName("profileWindowArea"); | ||||
xmppMessages.registerXmppMessageHandler("game", "profile", this.onProfile.bind(this)); | xmppMessages.registerXmppMessageHandler("game", "profile", this.onProfile.bind(this)); | ||||
} | } | ||||
registerClosePageHandler(handler) | registerClosePageHandler(handler) | ||||
{ | { | ||||
this.closePageHandlers.add(handler); | this.closePageHandlers.add(handler); | ||||
} | } | ||||
openPage() | openPage() | ||||
{ | { | ||||
this.profilePage.hidden = false; | this.profilePage.hidden = false; | ||||
Engine.SetGlobalHotkey("cancel", "Press", this.onPressClose.bind(this)); | Engine.SetGlobalHotkey("cancel", "Press", this.onPressClose.bind(this)); | ||||
} | } | ||||
onPressLookup() | onPressLookup() | ||||
{ | { | ||||
this.requestedPlayer = this.fetchInput.caption; | this.requestedPlayer = this.fetchInput.caption; | ||||
Engine.SendGetProfile(this.requestedPlayer); | Engine.SendGetProfile(this.requestedPlayer); | ||||
} | } | ||||
autocomplete() | autocomplete() | ||||
{ | { | ||||
const listPlayerNames = Engine.GetPlayerList().map(player => escapeText(player.name)); | const listPlayerNames = Engine.GetPlayerList().map(player => escapeText(player.name)); | ||||
// Remove duplicates with the board list. The board list has lower case names. | // Remove duplicates with the board list. The board list has lower case names. | ||||
const listPlayerNamesLower = listPlayerNames.map(playerName => playerName.toLowerCase()); | const listPlayerNamesLower = listPlayerNames.map(playerName => playerName.toLowerCase()); | ||||
for (const entry of Engine.GetBoardList()) | for (const entry of Engine.GetBoardList()) | ||||
{ | { | ||||
const escapedName = escapeText(entry.name); | const escapedName = escapeText(entry.name); | ||||
if (!listPlayerNamesLower.includes(escapedName)) | if (!listPlayerNamesLower.includes(escapedName)) | ||||
listPlayerNames.push(escapedName); | listPlayerNames.push(escapedName); | ||||
} | } | ||||
autoCompleteText(this.fetchInput, listPlayerNames); | autoCompleteText(this.fetchInput, listPlayerNames); | ||||
} | } | ||||
onPressClose() | onPressClose() | ||||
{ | { | ||||
this.profilePage.hidden = true; | this.profilePage.hidden = true; | ||||
for (let handler of this.closePageHandlers) | for (let handler of this.closePageHandlers) | ||||
handler(); | handler(); | ||||
} | } | ||||
onProfile() | onProfile() | ||||
{ | { | ||||
let attributes = Engine.GetProfile()[0]; | let attributes = Engine.GetProfile()[0]; | ||||
if (this.profilePage.hidden || this.requestedPlayer != attributes.player) | if (this.profilePage.hidden || this.requestedPlayer != attributes.player) | ||||
return; | return; | ||||
let profileFound = attributes.rating != "-2"; | let profileFound = attributes.rating != "-2"; | ||||
this.profileWindowArea.hidden = !profileFound; | this.profileWindowArea.hidden = !profileFound; | ||||
this.profileErrorText.hidden = profileFound; | this.profileErrorText.hidden = profileFound; | ||||
if (!profileFound) | if (!profileFound) | ||||
{ | { | ||||
this.profileErrorText.caption = | this.profileErrorText.caption = | ||||
sprintf(translate("Player \"%(nick)s\" not found."), { | sprintf(translate("Player \"%(nick)s\" not found."), { | ||||
"nick": escapeText(attributes.player) | "nick": escapeText(attributes.player) | ||||
}); | }); | ||||
return; | return; | ||||
} | } | ||||
this.profilePlayernameText.caption = coloredText(escapeText(attributes.player), PlayerColor.ColorPlayerNameByRole(Engine.LobbyGetPlayerRole(attributes.player))) + ' (' + coloredText(attributes.rating, PlayerColor.GetColorByRating(attributes.rating)) + ')'; | |||||
this.profilePlayernameText.caption = PlayerColor.ColorPlayerName(escapeText(attributes.player), attributes.rating); | |||||
this.profileRankText.caption = attributes.rank; | this.profileRankText.caption = attributes.rank; | ||||
this.profileHighestRatingText.caption = attributes.highestRating; | this.profileHighestRatingText.caption = coloredText(attributes.highestRating, PlayerColor.GetColorByRating(attributes.highestRating)); | ||||
this.profileTotalGamesText.caption = attributes.totalGamesPlayed; | this.profileTotalGamesText.caption = attributes.totalGamesPlayed; | ||||
this.profileWinsText.caption = attributes.wins; | this.profileWinsText.caption = attributes.wins; | ||||
this.profileLossesText.caption = attributes.losses; | this.profileLossesText.caption = attributes.losses; | ||||
this.profileRatioText.caption = ProfilePanel.FormatWinRate(attributes); | this.profileRatioText.caption = ProfilePanel.FormatWinRate(attributes); | ||||
} | } | ||||
} | } |
Wildfire Games · Phabricator