Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/gui/lobby/lobby.js
Show First 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | |||||
* Rating of the current user. | * Rating of the current user. | ||||
* Contains the number or an empty string in case the user has no rating. | * Contains the number or an empty string in case the user has no rating. | ||||
*/ | */ | ||||
var g_UserRating = ""; | var g_UserRating = ""; | ||||
/** | /** | ||||
* All games currently running. | * All games currently running. | ||||
*/ | */ | ||||
var g_GameList = {}; | var g_GameList = { "uninitialized": true }; | ||||
/** | |||||
* Filtered and sorted list of currently running games. | |||||
*/ | |||||
var g_GameListPrevious = []; | |||||
/** | /** | ||||
* Used to restore the selection after updating the playerlist. | * Used to restore the selection after updating the playerlist. | ||||
*/ | */ | ||||
var g_SelectedPlayer = ""; | var g_SelectedPlayer = ""; | ||||
/** | /** | ||||
* Used to restore the selection after updating the gamelist. | * Used to restore the selection after updating the gamelist. | ||||
▲ Show 20 Lines • Show All 168 Lines • ▼ Show 20 Lines | "private-message": msg => { | ||||
"historic": msg.historic, | "historic": msg.historic, | ||||
"private": true | "private": true | ||||
}); | }); | ||||
return false; | return false; | ||||
} | } | ||||
}, | }, | ||||
"game": { | "game": { | ||||
"gamelist": msg => { | "gamelist": msg => { | ||||
if (g_GameList.uninitialized) | |||||
g_GameList = { "initial": true }; | |||||
updateGameList(); | updateGameList(); | ||||
return false; | return false; | ||||
}, | }, | ||||
"profile": msg => { | "profile": msg => { | ||||
updateProfile(); | updateProfile(); | ||||
return false; | return false; | ||||
}, | }, | ||||
"leaderboard": msg => { | "leaderboard": msg => { | ||||
▲ Show 20 Lines • Show All 605 Lines • ▼ Show 20 Lines | if (leaderboard.selected >= leaderboard.list.length) | ||||
leaderboard.selected = -1; | leaderboard.selected = -1; | ||||
} | } | ||||
/** | /** | ||||
* Update the game listing from data cached in C++. | * Update the game listing from data cached in C++. | ||||
*/ | */ | ||||
function updateGameList() | function updateGameList() | ||||
{ | { | ||||
if (g_GameList.uninitialized) | |||||
return; | |||||
let gamesBox = Engine.GetGUIObjectByName("gamesBox"); | let gamesBox = Engine.GetGUIObjectByName("gamesBox"); | ||||
let sortBy = gamesBox.selected_column; | let sortBy = gamesBox.selected_column; | ||||
let sortOrder = gamesBox.selected_column_order; | let sortOrder = gamesBox.selected_column_order; | ||||
if (gamesBox.selected > -1) | if (gamesBox.selected > -1) | ||||
{ | { | ||||
g_SelectedGameIP = g_GameList[gamesBox.selected].ip; | g_SelectedGameIP = g_GameList[gamesBox.selected].ip; | ||||
g_SelectedGamePort = g_GameList[gamesBox.selected].port; | g_SelectedGamePort = g_GameList[gamesBox.selected].port; | ||||
} | } | ||||
g_GameList = Engine.GetGameList().map(game => { | let previousGameIndex = 0; | ||||
g_GameListPrevious = Engine.GetGameList().map(game => { | |||||
let previousGame = g_GameListPrevious[previousGameIndex] && g_GameListPrevious[previousGameIndex].name == game.name ? | |||||
g_GameListPrevious[previousGameIndex++] : g_GameListPrevious.find( | |||||
(previousGame, foundPreviousGameIndex) => previousGame.name == game.name && (previousGameIndex = ++foundPreviousGameIndex) | |||||
); | |||||
game.joinedBuddies = previousGame ? previousGame.joinedBuddies : []; | |||||
game.hasBuddies = 0; | game.hasBuddies = 0; | ||||
// Compute average rating of participating players | // Compute average rating of participating players | ||||
let playerRatings = []; | let playerRatings = []; | ||||
let buddyJoined = nick => g_Buddies.indexOf(nick) != -1 && game.joinedBuddies.indexOf(nick) == -1 && game.joinedBuddies.push(nick); | |||||
if (game.state == "init" && buddyJoined(game.hostUsername) && !g_GameList.initial) | |||||
addInfoBuddyJoinedGame(translate("%(buddy)s has opened new game %(game)s."), game, game.hostUsername); | |||||
for (let player of stringifiedTeamListToPlayerData(game.players)) | for (let player of stringifiedTeamListToPlayerData(game.players)) | ||||
{ | { | ||||
let [nick, rating] = splitRatingFromNick(player.Name); | let [nick, rating] = splitRatingFromNick(player.Name); | ||||
if (player.Team != "observer") | if (player.Team != "observer") | ||||
playerRatings.push(rating || g_DefaultLobbyRating); | playerRatings.push(rating || g_DefaultLobbyRating); | ||||
// Sort games with playing buddies above games with spectating buddies | // Sort games with playing buddies above games with spectating buddies | ||||
if (game.hasBuddies < 2 && g_Buddies.indexOf(nick) != -1) | if (game.hasBuddies < 2 && g_Buddies.indexOf(nick) != -1) | ||||
game.hasBuddies = player.Team == "observer" ? 1 : 2; | game.hasBuddies = player.Team == "observer" ? 1 : 2; | ||||
if (buddyJoined(playerNickRating.nick) && !g_GameList.initial) | |||||
addInfoBuddyJoinedGame(translate("%(buddy)s has joined game %(game)s."), game, playerNickRating.nick); | |||||
} | } | ||||
game.gameRating = | game.gameRating = | ||||
playerRatings.length ? | playerRatings.length ? | ||||
Math.round(playerRatings.reduce((sum, current) => sum + current) / playerRatings.length) : | Math.round(playerRatings.reduce((sum, current) => sum + current) / playerRatings.length) : | ||||
g_DefaultLobbyRating; | g_DefaultLobbyRating; | ||||
return game; | return game; | ||||
}).filter(game => !filterGame(game)).sort((a, b) => { | }); | ||||
g_GameList = g_GameListPrevious.filter(game => !filterGame(game)).sort((a, b) => { | |||||
let sortA, sortB; | let sortA, sortB; | ||||
switch (sortBy) | switch (sortBy) | ||||
{ | { | ||||
case 'name': | case 'name': | ||||
sortA = g_GameStatusOrder.indexOf(a.state) + a.name.toLowerCase(); | sortA = g_GameStatusOrder.indexOf(a.state) + a.name.toLowerCase(); | ||||
sortB = g_GameStatusOrder.indexOf(b.state) + b.name.toLowerCase(); | sortB = g_GameStatusOrder.indexOf(b.state) + b.name.toLowerCase(); | ||||
break; | break; | ||||
case 'gameRating': | case 'gameRating': | ||||
▲ Show 20 Lines • Show All 341 Lines • ▼ Show 20 Lines | function addChatMessage(msg) | ||||
let formatted = ircFormat(msg); | let formatted = ircFormat(msg); | ||||
if (!formatted) | if (!formatted) | ||||
return; | return; | ||||
g_ChatMessages.push(formatted); | g_ChatMessages.push(formatted); | ||||
Engine.GetGUIObjectByName("chatText").caption = g_ChatMessages.join("\n"); | Engine.GetGUIObjectByName("chatText").caption = g_ChatMessages.join("\n"); | ||||
} | } | ||||
function addInfoBuddyJoinedGame(text, game, nickName) | |||||
{ | |||||
addChatMessage({ "info": true, "text": sprintf(text, { | |||||
"buddy": coloredText(g_BuddySymbol + " " + nickName, getPlayerColor(nickName)), | |||||
"game": coloredText(g_BuddySymbol + " " + game.name, g_GameColors[game.state]) | |||||
}) | |||||
}); | |||||
} | |||||
/** | /** | ||||
* Splits given input into command and argument. | * Splits given input into command and argument. | ||||
*/ | */ | ||||
function ircSplit(string) | function ircSplit(string) | ||||
{ | { | ||||
let idx = string.indexOf(' '); | let idx = string.indexOf(' '); | ||||
if (idx != -1) | if (idx != -1) | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | case "special": | ||||
}); | }); | ||||
} | } | ||||
break; | break; | ||||
} | } | ||||
default: | default: | ||||
return ""; | return ""; | ||||
} | } | ||||
} | } | ||||
else if (msg.info) | |||||
formattedMessage = sprintf(translate("%(info)s"), { | |||||
"info": senderFont('* ') + msg.text | |||||
}); | |||||
else | else | ||||
{ | { | ||||
let senderString; | let senderString; | ||||
// Translation: IRC message prefix. | // Translation: IRC message prefix. | ||||
if (msg.private) | if (msg.private) | ||||
senderString = sprintf(translateWithContext("lobby private message", "(%(private)s) <%(sender)s>"), { | senderString = sprintf(translateWithContext("lobby private message", "(%(private)s) <%(sender)s>"), { | ||||
"private": coloredText(translate("Private"), g_PrivateMessageColor), | "private": coloredText(translate("Private"), g_PrivateMessageColor), | ||||
▲ Show 20 Lines • Show All 90 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator