Index: ps/trunk/binaries/data/mods/public/gui/gamesetup/gamesetup.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/gamesetup/gamesetup.js (revision 21300) +++ ps/trunk/binaries/data/mods/public/gui/gamesetup/gamesetup.js (revision 21301) @@ -1,2682 +1,2683 @@ const g_MatchSettings_SP = "config/matchsettings.json"; const g_MatchSettings_MP = "config/matchsettings.mp.json"; const g_Ceasefire = prepareForDropdown(g_Settings && g_Settings.Ceasefire); const g_MapSizes = prepareForDropdown(g_Settings && g_Settings.MapSizes); const g_MapTypes = prepareForDropdown(g_Settings && g_Settings.MapTypes); const g_TriggerDifficulties = prepareForDropdown(g_Settings && g_Settings.TriggerDifficulties); const g_PopulationCapacities = prepareForDropdown(g_Settings && g_Settings.PopulationCapacities); const g_StartingResources = prepareForDropdown(g_Settings && g_Settings.StartingResources); const g_VictoryConditions = prepareForDropdown(g_Settings && g_Settings.VictoryConditions); const g_VictoryDurations = prepareForDropdown(g_Settings && g_Settings.VictoryDurations); var g_GameSpeeds = getGameSpeedChoices(false); /** * Offer users to select playable civs only. * Load unselectable civs as they could appear in scenario maps. */ const g_CivData = loadCivData(false, false); /** * Highlight the "random" dropdownlist item. */ var g_ColorRandom = "orange"; /** * Color for regular dropdownlist items. */ var g_ColorRegular = "white"; /** * Color for "Unassigned"-placeholder item in the dropdownlist. */ var g_PlayerAssignmentColors = { "player": g_ColorRegular, "observer": "170 170 250", "unassigned": "140 140 140", "AI": "70 150 70" }; /** * Used for highlighting the sender of chat messages. */ var g_SenderFont = "sans-bold-13"; /** * This yields [1, 2, ..., MaxPlayers]. */ var g_NumPlayersList = Array(g_MaxPlayers).fill(0).map((v, i) => i + 1); /** * Used for generating the botnames. */ var g_RomanNumbers = [undefined, "I", "II", "III", "IV", "V", "VI", "VII", "VIII"]; var g_PlayerTeamList = prepareForDropdown([{ "label": translateWithContext("team", "None"), "id": -1 }].concat( Array(g_MaxTeams).fill(0).map((v, i) => ({ "label": i + 1, "id": i })) ) ); /** * Number of relics: [1, ..., NumCivs] */ var g_RelicCountList = Object.keys(g_CivData).map((civ, i) => i + 1); var g_PlayerCivList = g_CivData && prepareForDropdown([{ "name": translateWithContext("civilization", "Random"), "tooltip": translate("Picks one civilization at random when the game starts."), "color": g_ColorRandom, "code": "random" }].concat( Object.keys(g_CivData).filter( civ => g_CivData[civ].SelectableInGameSetup ).map(civ => ({ "name": g_CivData[civ].Name, "tooltip": g_CivData[civ].History, "color": g_ColorRegular, "code": civ })).sort(sortNameIgnoreCase) ) ); /** * All selectable playercolors except gaia. */ var g_PlayerColorPickerList = g_Settings && g_Settings.PlayerDefaults.slice(1).map(pData => pData.Color); /** * Directory containing all maps of the given type. */ var g_MapPath = { "random": "maps/random/", "scenario": "maps/scenarios/", "skirmish": "maps/skirmishes/" }; /** * Containing the colors to highlight the ready status of players, * the chat ready messages and * the tooltips and captions for the ready button */ var g_ReadyData = [ { "color": g_ColorRegular, "chat": translate("* %(username)s is not ready."), "caption": translate("I'm ready"), "tooltip": translate("State that you are ready to play.") }, { "color": "green", "chat": translate("* %(username)s is ready!"), "caption": translate("Stay ready"), "tooltip": translate("Stay ready even when the game settings change.") }, { "color": "150 150 250", "chat": "", "caption": translate("I'm not ready!"), "tooltip": translate("State that you are not ready to play.") } ]; /** * Processes a CNetMessage (see NetMessage.h, NetMessages.h) sent by the CNetServer. */ var g_NetMessageTypes = { "netstatus": msg => handleNetStatusMessage(msg), "netwarn": msg => addNetworkWarning(msg), "gamesetup": msg => handleGamesetupMessage(msg), "players": msg => handlePlayerAssignmentMessage(msg), "ready": msg => handleReadyMessage(msg), "start": msg => handleGamestartMessage(msg), "kicked": msg => addChatMessage({ "type": msg.banned ? "banned" : "kicked", "username": msg.username }), "chat": msg => addChatMessage({ "type": "chat", "guid": msg.guid, "text": msg.text }), }; var g_FormatChatMessage = { "system": (msg, user) => systemMessage(msg.text), "settings": (msg, user) => systemMessage(translate('Game settings have been changed')), "connect": (msg, user) => systemMessage(sprintf(translate("%(username)s has joined"), { "username": user })), "disconnect": (msg, user) => systemMessage(sprintf(translate("%(username)s has left"), { "username": user })), "kicked": (msg, user) => systemMessage(sprintf(translate("%(username)s has been kicked"), { "username": user })), "banned": (msg, user) => systemMessage(sprintf(translate("%(username)s has been banned"), { "username": user })), "chat": (msg, user) => sprintf(translate("%(username)s %(message)s"), { "username": senderFont(sprintf(translate("<%(username)s>"), { "username": user })), "message": escapeText(msg.text || "") }), "ready": (msg, user) => sprintf(g_ReadyData[msg.status].chat, { "username": user }), "clientlist": (msg, user) => getUsernameList(), }; var g_MapFilters = [ { "id": "default", "name": translateWithContext("map filter", "Default"), "tooltip": translateWithContext("map filter", "All maps except naval and demo maps."), "filter": mapKeywords => mapKeywords.every(keyword => ["naval", "demo", "hidden"].indexOf(keyword) == -1), "Default": true }, { "id": "naval", "name": translate("Naval Maps"), "tooltip": translateWithContext("map filter", "Maps where ships are needed to reach the enemy."), "filter": mapKeywords => mapKeywords.indexOf("naval") != -1 }, { "id": "demo", "name": translate("Demo Maps"), "tooltip": translateWithContext("map filter", "These maps are not playable but for demonstration purposes only."), "filter": mapKeywords => mapKeywords.indexOf("demo") != -1 }, { "id": "new", "name": translate("New Maps"), "tooltip": translateWithContext("map filter", "Maps that are brand new in this release of the game."), "filter": mapKeywords => mapKeywords.indexOf("new") != -1 }, { "id": "trigger", "name": translate("Trigger Maps"), "tooltip": translateWithContext("map filter", "Maps that come with scripted events and potentially spawn enemy units."), "filter": mapKeywords => mapKeywords.indexOf("trigger") != -1 }, { "id": "all", "name": translate("All Maps"), "tooltip": translateWithContext("map filter", "Every map of the chosen maptype."), "filter": mapKeywords => true }, ]; /** * This contains only filters that have at least one map matching them. */ var g_MapFilterList; /** * Array of biome identifiers supported by the currently selected map. */ var g_BiomeList; /** * Array of trigger difficulties identifiers supported by the currently selected map. */ var g_TriggerDifficultyList; /** * Whether this is a single- or multiplayer match. */ var g_IsNetworked; /** * Is this user in control of game settings (i.e. singleplayer or host of a multiplayergame). */ var g_IsController; /** * Whether this is a tutorial. */ var g_IsTutorial; /** * To report the game to the lobby bot. */ var g_ServerName; var g_ServerPort; /** * IP address and port of the STUN endpoint. */ var g_StunEndpoint; /** * States whether the GUI is currently updated in response to network messages instead of user input * and therefore shouldn't send further messages to the network. */ var g_IsInGuiUpdate = false; /** * Whether the current player is ready to start the game. * 0 - not ready * 1 - ready * 2 - stay ready */ var g_IsReady = 0; /** * Ignore duplicate ready commands on init. */ var g_ReadyInit = true; /** * If noone has changed the ready status, we have no need to spam the settings changed message. * * <=0 - Suppressed settings message * 1 - Will show settings message * 2 - Host's initial ready, suppressed settings message */ var g_ReadyChanged = 2; /** * Used to prevent calling resetReadyData when starting a game. */ var g_GameStarted = false; /** * Selectable options (player, AI, unassigned) in the player assignment dropdowns and * their colorized, textual representation. */ var g_PlayerAssignmentList = {}; /** * Remembers which clients are assigned to which player slots and whether they are ready. * The keys are guids or "local" in Singleplayer. */ var g_PlayerAssignments = {}; var g_DefaultPlayerData = []; var g_GameAttributes = { "settings": {} }; /** * List of translated words that can be used to autocomplete titles of settings * and their values (for example playernames). */ var g_Autocomplete = []; /** * Array of strings formatted as displayed, including playername. */ var g_ChatMessages = []; /** * Minimum amount of pixels required for the chat panel to be visible. */ var g_MinChatWidth = 96; /** * Horizontal space between chat window and settings. */ var g_ChatSettingsMargin = 10; /** * Filename and translated title of all maps, given the currently selected * maptype and filter. Sorted by title, shown in the dropdown. */ var g_MapSelectionList = []; /** * Cache containing the mapsettings. Just-in-time loading. */ var g_MapData = {}; /** * Wait one tick before initializing the GUI objects and * don't process netmessages prior to that. */ var g_LoadingState = 0; /** * Send the current gamesettings to the lobby bot if the settings didn't change for this number of seconds. */ var g_GameStanzaTimeout = 2; /** * Index of the GUI timer. */ var g_GameStanzaTimer; /** * Only send a lobby update if something actually changed. */ var g_LastGameStanza; /** * Remembers if the current player viewed the AI settings of some playerslot. */ var g_LastViewedAIPlayer = -1; /** * Total number of units that the engine can run with smoothly. * It means a 4v4 with 150 population can still run nicely, but more than that might "lag". */ var g_PopulationCapacityRecommendation = 1200; /** * Horizontal space between tab buttons and lobby button. */ var g_LobbyButtonSpacing = 8; /** * Vertical size of a tab button. */ var g_TabButtonHeight = 30; /** * Vertical space between two tab buttons. */ var g_TabButtonDist = 4; /** * Vertical size of a setting object. */ var g_SettingHeight = 32; /** * Vertical space between two setting objects. */ var g_SettingDist = 2; /** * Maximum width of a column in the settings panel. */ var g_MaxColumnWidth = 470; /** * Pixels per millisecond the settings panel slides when opening/closing. */ var g_SlideSpeed = 1.2; /** * Store last tick time. */ var g_LastTickTime = Date.now(); /** * Order in which the GUI elements will be shown. * All valid settings are required to appear here. */ var g_SettingsTabsGUI = [ { "label": translateWithContext("Match settings tab name", "Map"), "settings": [ "mapType", "mapFilter", "mapSelection", "mapSize", "biome", "nomad", "triggerDifficulty", "disableTreasures", "exploreMap", "revealMap" ] }, { "label": translateWithContext("Match settings tab name", "Player"), "settings": [ "numPlayers", "populationCap", "startingResources", "disableSpies", "enableCheats" ] }, { "label": translateWithContext("Match settings tab name", "Game Type"), "settings": [ "victoryCondition", "relicCount", "relicDuration", "wonderDuration", "regicideGarrison", "gameSpeed", "ceasefire", "lockTeams", "lastManStanding", "enableRating" ] } ]; /** * Contains the logic of all multiple-choice gamesettings. * * Logic * ids - Array of identifier strings that indicate the selected value. * default - Returns the index of the default value (not the value itself). * defined - Whether a value for the setting is actually specified. * get - The identifier of the currently selected value. * select - Saves and processes the value of the selected index of the ids array. * * GUI * title - The caption shown in the label. * tooltip - A description shown when hovering the dropdown or a specific item. * labels - Array of translated strings selectable for this dropdown. * colors - Optional array of colors to tint the according dropdown items with. * hidden - If hidden, both the label and dropdown won't be visible. (default: false) * enabled - Only the label will be shown if the setting is disabled. (default: true) * autocomplete - Marks whether to autocomplete translated values of the string. (default: undefined) * If not undefined, must be a number that denotes the priority (higher numbers come first). * If undefined, still autocompletes the translated title of the setting. * initOrder - Settings with lower values will be initialized first. */ var g_Dropdowns = { "mapType": { "title": () => translate("Map Type"), "tooltip": (hoverIdx) => g_MapTypes.Description[hoverIdx] || translate("Select a map type."), "labels": () => g_MapTypes.Title, "ids": () => g_MapTypes.Name, "default": () => g_MapTypes.Default, "defined": () => g_GameAttributes.mapType !== undefined, "get": () => g_GameAttributes.mapType, "select": (itemIdx) => { g_MapData = {}; g_GameAttributes.mapType = g_MapTypes.Name[itemIdx]; g_GameAttributes.mapPath = g_MapPath[g_GameAttributes.mapType]; delete g_GameAttributes.map; if (g_GameAttributes.mapType != "scenario") g_GameAttributes.settings = { "PlayerData": clone(g_DefaultPlayerData.slice(0, 4)) }; reloadMapFilterList(); }, "autocomplete": 0, "initOrder": 1 }, "mapFilter": { "title": () => translate("Map Filter"), "tooltip": (hoverIdx) => g_MapFilterList.tooltip[hoverIdx] || translate("Select a map filter."), "labels": () => g_MapFilterList.name, "ids": () => g_MapFilterList.id, "default": () => g_MapFilterList.Default, "defined": () => g_MapFilterList.id.indexOf(g_GameAttributes.mapFilter || "") != -1, "get": () => g_GameAttributes.mapFilter, "select": (itemIdx) => { g_GameAttributes.mapFilter = g_MapFilterList.id[itemIdx]; delete g_GameAttributes.map; reloadMapList(); }, "autocomplete": 0, "initOrder": 2 }, "mapSelection": { "title": () => translate("Select Map"), "tooltip": (hoverIdx) => g_MapSelectionList.description[hoverIdx] || translate("Select a map to play on."), "labels": () => g_MapSelectionList.name, "colors": () => g_MapSelectionList.color, "ids": () => g_MapSelectionList.file, "default": () => 0, "defined": () => g_GameAttributes.map !== undefined, "get": () => g_GameAttributes.map, "select": (itemIdx) => { selectMap(g_MapSelectionList.file[itemIdx]); }, "autocomplete": 0, "initOrder": 3 }, "mapSize": { "title": () => translate("Map Size"), "tooltip": (hoverIdx) => g_MapSizes.Tooltip[hoverIdx] || translate("Select map size. (Larger sizes may reduce performance.)"), "labels": () => g_MapSizes.Name, "ids": () => g_MapSizes.Tiles, "default": () => g_MapSizes.Default, "defined": () => g_GameAttributes.settings.Size !== undefined, "get": () => g_GameAttributes.settings.Size, "select": (itemIdx) => { g_GameAttributes.settings.Size = g_MapSizes.Tiles[itemIdx]; }, "hidden": () => g_GameAttributes.mapType != "random", "autocomplete": 0, "initOrder": 1000 }, "biome": { "title": () => translate("Biome"), "tooltip": (hoverIdx) => g_BiomeList && g_BiomeList.Description && g_BiomeList.Description[hoverIdx] || translate("Select the flora and fauna."), "labels": () => g_BiomeList ? g_BiomeList.Title : [], "colors": (itemIdx) => g_BiomeList ? g_BiomeList.Color : [], "ids": () => g_BiomeList ? g_BiomeList.Id : [], "default": () => 0, "defined": () => g_GameAttributes.settings.Biome !== undefined, "get": () => g_GameAttributes.settings.Biome, "select": (itemIdx) => { g_GameAttributes.settings.Biome = g_BiomeList && g_BiomeList.Id[itemIdx]; }, "hidden": () => !g_BiomeList, "autocomplete": 0, "initOrder": 1000 }, "numPlayers": { "title": () => translate("Number of Players"), "tooltip": (hoverIdx) => translate("Select number of players."), "labels": () => g_NumPlayersList, "ids": () => g_NumPlayersList, "default": () => g_MaxPlayers - 1, "defined": () => g_GameAttributes.settings.PlayerData !== undefined, "get": () => g_GameAttributes.settings.PlayerData.length, "enabled": () => g_GameAttributes.mapType == "random", "select": (itemIdx) => { let num = itemIdx + 1; let pData = g_GameAttributes.settings.PlayerData; g_GameAttributes.settings.PlayerData = num > pData.length ? pData.concat(clone(g_DefaultPlayerData.slice(pData.length, num))) : pData.slice(0, num); unassignInvalidPlayers(num); sanitizePlayerData(g_GameAttributes.settings.PlayerData); }, "initOrder": 1000 }, "populationCap": { "title": () => translate("Population Cap"), "tooltip": (hoverIdx) => { let popCap = g_PopulationCapacities.Population[hoverIdx]; let players = g_GameAttributes.settings.PlayerData.length; if (hoverIdx == -1 || popCap * players <= g_PopulationCapacityRecommendation) return translate("Select population limit."); return coloredText( sprintf(translate("Warning: There might be performance issues if all %(players)s players reach %(popCap)s population."), { "players": players, "popCap": popCap }), "orange"); }, "labels": () => g_PopulationCapacities.Title, "ids": () => g_PopulationCapacities.Population, "default": () => g_PopulationCapacities.Default, "defined": () => g_GameAttributes.settings.PopulationCap !== undefined, "get": () => g_GameAttributes.settings.PopulationCap, "select": (itemIdx) => { g_GameAttributes.settings.PopulationCap = g_PopulationCapacities.Population[itemIdx]; }, "enabled": () => g_GameAttributes.mapType != "scenario", "initOrder": 1000 }, "startingResources": { "title": () => translate("Starting Resources"), "tooltip": (hoverIdx) => { return hoverIdx >= 0 ? sprintf(translate("Initial amount of each resource: %(resources)s."), { "resources": g_StartingResources.Resources[hoverIdx] }) : translate("Select the game's starting resources."); }, "labels": () => g_StartingResources.Title, "ids": () => g_StartingResources.Resources, "default": () => g_StartingResources.Default, "defined": () => g_GameAttributes.settings.StartingResources !== undefined, "get": () => g_GameAttributes.settings.StartingResources, "select": (itemIdx) => { g_GameAttributes.settings.StartingResources = g_StartingResources.Resources[itemIdx]; }, "hidden": () => g_GameAttributes.mapType == "scenario", "autocomplete": 0, "initOrder": 1000 }, "ceasefire": { "title": () => translate("Ceasefire"), "tooltip": (hoverIdx) => translate("Set time where no attacks are possible."), "labels": () => g_Ceasefire.Title, "ids": () => g_Ceasefire.Duration, "default": () => g_Ceasefire.Default, "defined": () => g_GameAttributes.settings.Ceasefire !== undefined, "get": () => g_GameAttributes.settings.Ceasefire, "select": (itemIdx) => { g_GameAttributes.settings.Ceasefire = g_Ceasefire.Duration[itemIdx]; }, "enabled": () => g_GameAttributes.mapType != "scenario", "initOrder": 1000 }, "victoryCondition": { "title": () => translate("Victory Condition"), "tooltip": (hoverIdx) => g_VictoryConditions.Description[hoverIdx] || translate("Select victory condition."), "labels": () => g_VictoryConditions.Title, "ids": () => g_VictoryConditions.Name, "default": () => g_VictoryConditions.Default, "defined": () => g_GameAttributes.settings.GameType !== undefined, "get": () => g_GameAttributes.settings.GameType, "select": (itemIdx) => { g_GameAttributes.settings.GameType = g_VictoryConditions.Name[itemIdx]; g_GameAttributes.settings.VictoryScripts = g_VictoryConditions.Scripts[itemIdx]; }, "enabled": () => g_GameAttributes.mapType != "scenario", "autocomplete": 0, "initOrder": 1000 }, "relicCount": { "title": () => translate("Relic Count"), "tooltip": (hoverIdx) => translate("Total number of relics spawned on the map. Relic victory is most realistic with only one or two relics. With greater numbers, the relics are important to capture to receive aura bonuses."), "labels": () => g_RelicCountList, "ids": () => g_RelicCountList, "default": () => g_RelicCountList.indexOf(2), "defined": () => g_GameAttributes.settings.RelicCount !== undefined, "get": () => g_GameAttributes.settings.RelicCount, "select": (itemIdx) => { g_GameAttributes.settings.RelicCount = g_RelicCountList[itemIdx]; }, "hidden": () => g_GameAttributes.settings.GameType != "capture_the_relic", "enabled": () => g_GameAttributes.mapType != "scenario", "initOrder": 1000 }, "relicDuration": { "title": () => translate("Relic Duration"), "tooltip": (hoverIdx) => translate("Minutes until the player has achieved Relic Victory."), "labels": () => g_VictoryDurations.Title, "ids": () => g_VictoryDurations.Duration, "default": () => g_VictoryDurations.Default, "defined": () => g_GameAttributes.settings.RelicDuration !== undefined, "get": () => g_GameAttributes.settings.RelicDuration, "select": (itemIdx) => { g_GameAttributes.settings.RelicDuration = g_VictoryDurations.Duration[itemIdx]; }, "hidden": () => g_GameAttributes.settings.GameType != "capture_the_relic", "enabled": () => g_GameAttributes.mapType != "scenario", "initOrder": 1000 }, "wonderDuration": { "title": () => translate("Wonder Duration"), "tooltip": (hoverIdx) => translate("Minutes until the player has achieved Wonder Victory."), "labels": () => g_VictoryDurations.Title, "ids": () => g_VictoryDurations.Duration, "default": () => g_VictoryDurations.Default, "defined": () => g_GameAttributes.settings.WonderDuration !== undefined, "get": () => g_GameAttributes.settings.WonderDuration, "select": (itemIdx) => { g_GameAttributes.settings.WonderDuration = g_VictoryDurations.Duration[itemIdx]; }, "hidden": () => g_GameAttributes.settings.GameType != "wonder", "enabled": () => g_GameAttributes.mapType != "scenario", "initOrder": 1000 }, "gameSpeed": { "title": () => translate("Game Speed"), "tooltip": (hoverIdx) => translate("Select game speed."), "labels": () => g_GameSpeeds.Title, "ids": () => g_GameSpeeds.Speed, "default": () => g_GameSpeeds.Default, "defined": () => g_GameAttributes.gameSpeed !== undefined && g_GameSpeeds.Speed.indexOf(g_GameAttributes.gameSpeed) != -1, "get": () => g_GameAttributes.gameSpeed, "select": (itemIdx) => { g_GameAttributes.gameSpeed = g_GameSpeeds.Speed[itemIdx]; }, "initOrder": 1000 }, "triggerDifficulty": { "title": () => translate("Difficulty"), "tooltip": (hoverIdx) => g_TriggerDifficultyList && g_TriggerDifficultyList.Description[hoverIdx] || translate("Select the difficulty of this scenario."), "labels": () => g_TriggerDifficultyList ? g_TriggerDifficultyList.Title : [], "ids": () => g_TriggerDifficultyList ? g_TriggerDifficultyList.Id : [], "default": () => g_TriggerDifficultyList ? g_TriggerDifficultyList.Default : 0, "defined": () => g_GameAttributes.settings.TriggerDifficulty !== undefined, "get": () => g_GameAttributes.settings.TriggerDifficulty, "select": (itemIdx) => { g_GameAttributes.settings.TriggerDifficulty = g_TriggerDifficultyList && g_TriggerDifficultyList.Id[itemIdx]; }, "hidden": () => !g_TriggerDifficultyList, "initOrder": 1000 }, }; /** * These dropdowns provide a setting that is repeated once for each player * (where playerIdx is starting from 0 for player 1). */ var g_PlayerDropdowns = { "playerAssignment": { "labels": (playerIdx) => g_PlayerAssignmentList.Name || [], "colors": (playerIdx) => g_PlayerAssignmentList.Color || [], "ids": (playerIdx) => g_PlayerAssignmentList.Choice || [], "default": (playerIdx) => "ai:petra", "defined": (playerIdx) => playerIdx < g_GameAttributes.settings.PlayerData.length, "get": (playerIdx) => { for (let guid in g_PlayerAssignments) if (g_PlayerAssignments[guid].player == playerIdx + 1) return "guid:" + guid; for (let ai of g_Settings.AIDescriptions) if (g_GameAttributes.settings.PlayerData[playerIdx].AI == ai.id) return "ai:" + ai.id; return "unassigned"; }, "select": (selectedIdx, playerIdx) => { let choice = g_PlayerAssignmentList.Choice[selectedIdx]; if (choice == "unassigned" || choice.startsWith("ai:")) { if (g_IsNetworked) Engine.AssignNetworkPlayer(playerIdx+1, ""); else if (g_PlayerAssignments.local.player == playerIdx+1) g_PlayerAssignments.local.player = -1; g_GameAttributes.settings.PlayerData[playerIdx].AI = choice.startsWith("ai:") ? choice.substr(3) : ""; } else swapPlayers(choice.substr("guid:".length), playerIdx); }, "autocomplete": 100, }, "playerTeam": { "labels": (playerIdx) => g_PlayerTeamList.label, "ids": (playerIdx) => g_PlayerTeamList.id, "default": (playerIdx) => 0, "defined": (playerIdx) => g_GameAttributes.settings.PlayerData[playerIdx].Team !== undefined, "get": (playerIdx) => g_GameAttributes.settings.PlayerData[playerIdx].Team, "select": (selectedIdx, playerIdx) => { g_GameAttributes.settings.PlayerData[playerIdx].Team = selectedIdx - 1; }, "enabled": () => g_GameAttributes.mapType != "scenario", }, "playerCiv": { "tooltip": (hoverIdx, playerIdx) => g_PlayerCivList.tooltip[hoverIdx] || translate("Chose the civilization for this player"), "labels": (playerIdx) => g_PlayerCivList.name, "colors": (playerIdx) => g_PlayerCivList.color, "ids": (playerIdx) => g_PlayerCivList.code, "default": (playerIdx) => 0, "defined": (playerIdx) => g_GameAttributes.settings.PlayerData[playerIdx].Civ !== undefined, "get": (playerIdx) => g_GameAttributes.settings.PlayerData[playerIdx].Civ, "select": (selectedIdx, playerIdx) => { g_GameAttributes.settings.PlayerData[playerIdx].Civ = g_PlayerCivList.code[selectedIdx]; }, "enabled": () => g_GameAttributes.mapType != "scenario", "autocomplete": 0, }, "playerColorPicker": { "labels": (playerIdx) => g_PlayerColorPickerList.map(color => "■"), "colors": (playerIdx) => g_PlayerColorPickerList.map(color => rgbToGuiColor(color)), "ids": (playerIdx) => g_PlayerColorPickerList.map((color, index) => index), "default": (playerIdx) => playerIdx, "defined": (playerIdx) => g_GameAttributes.settings.PlayerData[playerIdx].Color !== undefined, "get": (playerIdx) => g_PlayerColorPickerList.indexOf(g_GameAttributes.settings.PlayerData[playerIdx].Color), "select": (selectedIdx, playerIdx) => { let playerData = g_GameAttributes.settings.PlayerData; // If someone else has that color, give that player the old color let sameColorPData = playerData.find(pData => sameColor(g_PlayerColorPickerList[selectedIdx], pData.Color)); if (sameColorPData) sameColorPData.Color = playerData[playerIdx].Color; playerData[playerIdx].Color = g_PlayerColorPickerList[selectedIdx]; ensureUniquePlayerColors(playerData); }, "enabled": () => g_GameAttributes.mapType != "scenario", }, }; /** * Contains the logic of all boolean gamesettings. */ var g_Checkboxes = { "regicideGarrison": { "title": () => translate("Hero Garrison"), "tooltip": () => translate("Toggle whether heroes can be garrisoned."), "default": () => false, "defined": () => g_GameAttributes.settings.RegicideGarrison !== undefined, "get": () => g_GameAttributes.settings.RegicideGarrison, "set": checked => { g_GameAttributes.settings.RegicideGarrison = checked; }, "hidden": () => g_GameAttributes.settings.GameType != "regicide", "enabled": () => g_GameAttributes.mapType != "scenario", "initOrder": 1000 }, "nomad": { "title": () => translate("Nomad"), "tooltip": () => translate("In Nomad mode, players start with only few units and have to find a suitable place to build their city. Ceasefire is recommended."), "default": () => false, "defined": () => g_GameAttributes.settings.Nomad !== undefined, "get": () => g_GameAttributes.settings.Nomad, "set": checked => { g_GameAttributes.settings.Nomad = checked; }, "hidden": () => g_GameAttributes.mapType != "random", "initOrder": 1000 }, "revealMap": { "title": () => // Translation: Make sure to differentiate between the revealed map and explored map settings! translate("Revealed Map"), "tooltip": // Translation: Make sure to differentiate between the revealed map and explored map settings! () => translate("Toggle revealed map (see everything)."), "default": () => false, "defined": () => g_GameAttributes.settings.RevealMap !== undefined, "get": () => g_GameAttributes.settings.RevealMap, "set": checked => { g_GameAttributes.settings.RevealMap = checked; if (checked) g_Checkboxes.exploreMap.set(true); }, "enabled": () => g_GameAttributes.mapType != "scenario", "initOrder": 1000 }, "exploreMap": { "title": // Translation: Make sure to differentiate between the revealed map and explored map settings! () => translate("Explored Map"), "tooltip": // Translation: Make sure to differentiate between the revealed map and explored map settings! () => translate("Toggle explored map (see initial map)."), "default": () => false, "defined": () => g_GameAttributes.settings.ExploreMap !== undefined, "get": () => g_GameAttributes.settings.ExploreMap, "set": checked => { g_GameAttributes.settings.ExploreMap = checked; }, "enabled": () => g_GameAttributes.mapType != "scenario" && !g_GameAttributes.settings.RevealMap, "initOrder": 1000 }, "disableTreasures": { "title": () => translate("Disable Treasures"), "tooltip": () => translate("Disable all treasures on the map."), "default": () => false, "defined": () => g_GameAttributes.settings.DisableTreasures !== undefined, "get": () => g_GameAttributes.settings.DisableTreasures, "set": checked => { g_GameAttributes.settings.DisableTreasures = checked; }, "enabled": () => g_GameAttributes.mapType != "scenario", "initOrder": 1000 }, "disableSpies": { "title": () => translate("Disable Spies"), "tooltip": () => translate("Disable spies during the game."), "default": () => false, "defined": () => g_GameAttributes.settings.DisableSpies !== undefined, "get": () => g_GameAttributes.settings.DisableSpies, "set": checked => { g_GameAttributes.settings.DisableSpies = checked; }, "enabled": () => g_GameAttributes.mapType != "scenario", "initOrder": 1000 }, "lockTeams": { "title": () => translate("Teams Locked"), "tooltip": () => translate("Toggle locked teams."), "default": () => Engine.HasXmppClient(), "defined": () => g_GameAttributes.settings.LockTeams !== undefined, "get": () => g_GameAttributes.settings.LockTeams, "set": checked => { g_GameAttributes.settings.LockTeams = checked; g_GameAttributes.settings.LastManStanding = false; }, "enabled": () => g_GameAttributes.mapType != "scenario" && !g_GameAttributes.settings.RatingEnabled, "initOrder": 1000 }, "lastManStanding": { "title": () => translate("Last Man Standing"), "tooltip": () => translate("Toggle whether the last remaining player or the last remaining set of allies wins."), "default": () => false, "defined": () => g_GameAttributes.settings.LastManStanding !== undefined, "get": () => g_GameAttributes.settings.LastManStanding, "set": checked => { g_GameAttributes.settings.LastManStanding = checked; }, "enabled": () => g_GameAttributes.mapType != "scenario" && !g_GameAttributes.settings.LockTeams, "initOrder": 1000 }, "enableCheats": { "title": () => translate("Cheats"), "tooltip": () => translate("Toggle the usability of cheats."), "default": () => !g_IsNetworked, "hidden": () => !g_IsNetworked, "defined": () => g_GameAttributes.settings.CheatsEnabled !== undefined, "get": () => g_GameAttributes.settings.CheatsEnabled, "set": checked => { g_GameAttributes.settings.CheatsEnabled = !g_IsNetworked || checked && !g_GameAttributes.settings.RatingEnabled; }, "enabled": () => !g_GameAttributes.settings.RatingEnabled, "initOrder": 1000 }, "enableRating": { "title": () => translate("Rated Game"), "tooltip": () => translate("Toggle if this game will be rated for the leaderboard."), "default": () => Engine.HasXmppClient(), "hidden": () => !Engine.HasXmppClient(), "defined": () => g_GameAttributes.settings.RatingEnabled !== undefined, "get": () => !!g_GameAttributes.settings.RatingEnabled, "set": checked => { g_GameAttributes.settings.RatingEnabled = Engine.HasXmppClient() ? checked : undefined; Engine.SetRankedGame(!!g_GameAttributes.settings.RatingEnabled); if (checked) { g_Checkboxes.lockTeams.set(true); g_Checkboxes.enableCheats.set(false); } }, "initOrder": 1000 }, }; /** * For setting up arbitrary GUI objects. */ var g_MiscControls = { "chatPanel": { "hidden": () => { if (!g_IsNetworked) return true; let size = Engine.GetGUIObjectByName("chatPanel").getComputedSize(); return size.right - size.left < g_MinChatWidth; }, }, "chatInput": { "tooltip": () => colorizeAutocompleteHotkey(translate("Press %(hotkey)s to autocomplete playernames or settings.")), }, "cheatWarningText": { "hidden": () => !g_IsNetworked || !g_GameAttributes.settings.CheatsEnabled, }, "cancelGame": { "tooltip": () => Engine.HasXmppClient() ? translate("Return to the lobby.") : translate("Return to the main menu."), }, "startGame": { "caption": () => g_IsController ? translate("Start Game!") : g_ReadyData[g_IsReady].caption, "tooltip": (hoverIdx) => !g_IsController ? g_ReadyData[g_IsReady].tooltip : !g_IsNetworked || Object.keys(g_PlayerAssignments).every(guid => g_PlayerAssignments[guid].status || g_PlayerAssignments[guid].player == -1) ? translate("Start a new game with the current settings.") : translate("Start a new game with the current settings (disabled until all players are ready)"), "enabled": () => !g_IsController || Object.keys(g_PlayerAssignments).every(guid => g_PlayerAssignments[guid].status || g_PlayerAssignments[guid].player == -1 || guid == Engine.GetPlayerGUID() && g_IsController), "hidden": () => !g_PlayerAssignments[Engine.GetPlayerGUID()] || g_PlayerAssignments[Engine.GetPlayerGUID()].player == -1 && !g_IsController, }, "civResetButton": { "hidden": () => g_GameAttributes.mapType == "scenario" || !g_IsController, }, "teamResetButton": { "hidden": () => g_GameAttributes.mapType == "scenario" || !g_IsController, }, "lobbyButton": { "onPress": () => function() { if (Engine.HasXmppClient()) Engine.PushGuiPage("page_lobby.xml", { "dialog": true }); }, "hidden": () => !Engine.HasXmppClient() }, "spTips": { "hidden": () => { let settingsPanel = Engine.GetGUIObjectByName("settingsPanel"); let spTips = Engine.GetGUIObjectByName("spTips"); return g_IsNetworked || Engine.ConfigDB_GetValue("user", "gui.gamesetup.enabletips") !== "true" || spTips.size.right > settingsPanel.getComputedSize().left } } }; /** * Contains gui elements that are repeated for every player. */ var g_PlayerMiscElements = { "playerBox": { "size": (playerIdx) => ["0", 32 * playerIdx, "100%", 32 * (playerIdx + 1)].join(" "), }, "playerName": { "caption": (playerIdx) => { let pData = g_GameAttributes.settings.PlayerData[playerIdx]; let assignedGUID = Object.keys(g_PlayerAssignments).find( guid => g_PlayerAssignments[guid].player == playerIdx + 1); let name = translate(pData.Name || g_DefaultPlayerData[playerIdx].Name); if (g_IsNetworked) name = coloredText(name, g_ReadyData[assignedGUID ? g_PlayerAssignments[assignedGUID].status : 2].color); return name; }, }, "playerColor": { "sprite": (playerIdx) => "color:" + rgbToGuiColor(g_GameAttributes.settings.PlayerData[playerIdx].Color, 100), }, "playerConfig": { "hidden": (playerIdx) => !g_GameAttributes.settings.PlayerData[playerIdx].AI, "onPress": (playerIdx) => function() { openAIConfig(playerIdx); }, "tooltip": (playerIdx) => sprintf(translate("Configure AI: %(description)s."), { "description": translateAISettings(g_GameAttributes.settings.PlayerData[playerIdx]) }), }, }; /** * Initializes some globals without touching the GUI. * * @param {Object} attribs - context data sent by the lobby / mainmenu */ function init(attribs) { if (!g_Settings) { cancelSetup(); return; } if (["offline", "server", "client"].indexOf(attribs.type) == -1) { error("Unexpected 'type' in gamesetup init: " + attribs.type); cancelSetup(); return; } g_IsNetworked = attribs.type != "offline"; g_IsController = attribs.type != "client"; g_IsTutorial = !!attribs.tutorial; g_ServerName = attribs.serverName; g_ServerPort = attribs.serverPort; g_StunEndpoint = attribs.stunEndpoint; if (!g_IsNetworked) g_PlayerAssignments = { "local": { "name": singleplayerName(), "player": 1 } }; // Replace empty playername when entering a singleplayermatch for the first time if (!g_IsNetworked) saveSettingAndWriteToUserConfig("playername.singleplayer", singleplayerName()); initDefaults(); supplementDefaults(); setTimeout(displayGamestateNotifications, 1000); } function initDefaults() { // Remove gaia from both arrays g_DefaultPlayerData = clone(g_Settings.PlayerDefaults.slice(1)); let aiDifficulty = +Engine.ConfigDB_GetValue("user", "gui.gamesetup.aidifficulty"); let aiBehavior = Engine.ConfigDB_GetValue("user", "gui.gamesetup.aibehavior"); // Don't change the underlying defaults file, as Atlas uses that file too for (let i in g_DefaultPlayerData) { g_DefaultPlayerData[i].Civ = "random"; g_DefaultPlayerData[i].Team = -1; g_DefaultPlayerData[i].AIDiff = aiDifficulty; g_DefaultPlayerData[i].AIBehavior = aiBehavior; } deepfreeze(g_DefaultPlayerData); } /** * Sets default values for all g_GameAttribute settings which don't have a value set. */ function supplementDefaults() { for (let dropdown in g_Dropdowns) if (!g_Dropdowns[dropdown].defined()) g_Dropdowns[dropdown].select(g_Dropdowns[dropdown].default()); for (let checkbox in g_Checkboxes) if (!g_Checkboxes[checkbox].defined()) g_Checkboxes[checkbox].set(g_Checkboxes[checkbox].default()); for (let dropdown in g_PlayerDropdowns) for (let i = 0; i < g_GameAttributes.settings.PlayerData.length; ++i) if (!isControlArrayElementHidden(i) && !g_PlayerDropdowns[dropdown].defined(i)) g_PlayerDropdowns[dropdown].select(g_PlayerDropdowns[dropdown].default(i), i); } /** * Called after the first tick. */ function initGUIObjects() { for (let tab in g_SettingsTabsGUI) g_SettingsTabsGUI[tab].tooltip = sprintf(translate("Toggle the %(name)s settings tab."), { "name": g_SettingsTabsGUI[tab].label }) + colorizeHotkey("\n" + translate("Use %(hotkey)s to move a settings tab down."), "tab.next") + colorizeHotkey("\n" + translate("Use %(hotkey)s to move a settings tab up."), "tab.prev"); // Copy all initOrder values into one object let initOrder = {}; for (let dropdown in g_Dropdowns) initOrder[dropdown] = g_Dropdowns[dropdown].initOrder; for (let checkbox in g_Checkboxes) initOrder[checkbox] = g_Checkboxes[checkbox].initOrder; // Sort the object on initOrder so we can init the settings in an arbitrary order for (let setting of Object.keys(initOrder).sort((a, b) => initOrder[a] - initOrder[b])) if (g_Dropdowns[setting]) initDropdown(setting); else if (g_Checkboxes[setting]) initCheckbox(setting); else warn('The setting "' + setting + '" is not defined.'); for (let dropdown in g_PlayerDropdowns) initPlayerDropdowns(dropdown); let settingTabButtons = Engine.GetGUIObjectByName("settingTabButtons"); let settingTabButtonsSize = settingTabButtons.size; settingTabButtonsSize.bottom = settingTabButtonsSize.top + g_SettingsTabsGUI.length * (g_TabButtonHeight + g_TabButtonDist); settingTabButtonsSize.right = g_MiscControls.lobbyButton.hidden() ? settingTabButtonsSize.right : Engine.GetGUIObjectByName("lobbyButton").size.left - g_LobbyButtonSpacing; settingTabButtons.size = settingTabButtonsSize; let settingTabButtonsBackground = Engine.GetGUIObjectByName("settingTabButtonsBackground"); settingTabButtonsBackground.size = settingTabButtonsSize; let gameDescription = Engine.GetGUIObjectByName("mapInfoDescriptionFrame"); let gameDescriptionSize = gameDescription.size; gameDescriptionSize.top = settingTabButtonsSize.bottom + 3; gameDescription.size = gameDescriptionSize; placeTabButtons( g_SettingsTabsGUI, g_TabButtonHeight, g_TabButtonDist, category => { selectPanel(category == g_TabCategorySelected ? undefined : category); }, () => { updateGUIObjects(); Engine.GetGUIObjectByName("settingsPanel").hidden = false; }); initSPTips(); loadPersistMatchSettings(); updateGameAttributes(); sendRegisterGameStanzaImmediate(); if (g_IsTutorial) { launchTutorial(); return; } // Don't lift the curtain until the controls are updated the first time if (!g_IsNetworked) hideLoadingWindow(); } /** * Slide settings panel. * @param {number} dt - Time in milliseconds since last call. */ function updateSettingsPanelPosition(dt) { let slideSpeed = Engine.ConfigDB_GetValue("user", "gui.gamesetup.settingsslide") == "true" ? g_SlideSpeed : Infinity; let settingsPanel = Engine.GetGUIObjectByName("settingsPanel"); let rightBorder = Engine.GetGUIObjectByName("settingTabButtons").size.left; let offset = 0; if (g_TabCategorySelected === undefined) { let maxOffset = rightBorder - settingsPanel.size.left; if (maxOffset > 0) offset = Math.min(slideSpeed * dt, maxOffset); } else if (rightBorder > settingsPanel.size.right) offset = Math.min(slideSpeed * dt, rightBorder - settingsPanel.size.right); else { let maxOffset = settingsPanel.size.right - rightBorder; if (maxOffset > 0) offset = -Math.min(slideSpeed * dt, maxOffset); } let size = settingsPanel.size; size.left += offset; size.right += offset; settingsPanel.size = size; let settingsBackground = Engine.GetGUIObjectByName("settingsBackground"); let backgroundSize = settingsBackground.size; backgroundSize.left = size.left; settingsBackground.size = backgroundSize; let chatPanel = Engine.GetGUIObjectByName("chatPanel"); let chatSize = chatPanel.size; chatSize.right = size.left - g_ChatSettingsMargin; chatPanel.size = chatSize; chatPanel.hidden = g_MiscControls.chatPanel.hidden(); let spTips = Engine.GetGUIObjectByName("spTips"); spTips.hidden = g_MiscControls.spTips.hidden(); } function hideLoadingWindow() { let loadingWindow = Engine.GetGUIObjectByName("loadingWindow"); if (loadingWindow.hidden) return; loadingWindow.hidden = true; Engine.GetGUIObjectByName("setupWindow").hidden = false; if (!Engine.GetGUIObjectByName("chatPanel").hidden) Engine.GetGUIObjectByName("chatInput").focus(); } /** * Settings under the settings tabs use a generic name. * Player settings use custom names. */ function getGUIObjectNameFromSetting(setting) { let idxOffset = 0; for (let category of g_SettingsTabsGUI) { let idx = category.settings.indexOf(setting); if (idx != -1) return [ "setting", g_Dropdowns[setting] ? "Dropdown" : "Checkbox", "[" + (idx + idxOffset) + "]" ]; idxOffset += category.settings.length; } // Assume there is a GUI object with exactly that setting name return [setting, "", ""]; } function initDropdown(name, playerIdx) { let [guiName, guiType, guiIdx] = getGUIObjectNameFromSetting(name); let idxName = playerIdx === undefined ? "" : "[" + playerIdx + "]"; let data = (playerIdx === undefined ? g_Dropdowns : g_PlayerDropdowns)[name]; let dropdown = Engine.GetGUIObjectByName(guiName + guiType + guiIdx + idxName); dropdown.list = data.labels(playerIdx).map((label, id) => data.colors && data.colors(playerIdx) ? coloredText(label, data.colors(playerIdx)[id]) : label); dropdown.list_data = data.ids(playerIdx); dropdown.onSelectionChange = function() { if (!g_IsController || g_IsInGuiUpdate || !this.list_data[this.selected] || data.hidden && data.hidden(playerIdx) || data.enabled && !data.enabled(playerIdx)) return; data.select(this.selected, playerIdx); supplementDefaults(); updateGameAttributes(); }; if (data.tooltip) dropdown.onHoverChange = function() { this.tooltip = data.tooltip(this.hovered, playerIdx); }; } function initPlayerDropdowns(name) { for (let i = 0; i < g_MaxPlayers; ++i) initDropdown(name, i); } function initCheckbox(name) { let [guiName, guiType, guiIdx] = getGUIObjectNameFromSetting(name); Engine.GetGUIObjectByName(guiName + guiType + guiIdx).onPress = function() { let obj = g_Checkboxes[name]; if (!g_IsController || g_IsInGuiUpdate || obj.enabled && !obj.enabled() || obj.hidden && obj.hidden()) return; obj.set(this.checked); supplementDefaults(); updateGameAttributes(); }; } function initSPTips() { if (g_IsNetworked || Engine.ConfigDB_GetValue("user", "gui.gamesetup.enabletips") !== "true") return; Engine.GetGUIObjectByName("spTips").hidden = false; Engine.GetGUIObjectByName("displaySPTips").checked = true; Engine.GetGUIObjectByName("aiTips").caption = Engine.TranslateLines(Engine.ReadFile("gui/gamesetup/ai.txt")); } /** * Distribute the currently visible settings over the settings panel. * First calculate the number of columns required, then place the objects. */ function distributeSettings() { let setupWindowSize = Engine.GetGUIObjectByName("setupWindow").getComputedSize(); let columnWidth = Math.min( g_MaxColumnWidth, (setupWindowSize.right - setupWindowSize.left + Engine.GetGUIObjectByName("settingTabButtons").size.left) / 2); let settingsPanel = Engine.GetGUIObjectByName("settingsPanel"); let actualSettingsPanelSize = settingsPanel.getComputedSize(); let maxPerColumn = Math.floor((actualSettingsPanelSize.bottom - actualSettingsPanelSize.top) / g_SettingHeight); let childCount = settingsPanel.children.filter(child => !child.hidden).length; let perColumn = childCount / Math.ceil(childCount / maxPerColumn); let yPos = g_SettingDist; let column = 0; let thisColumn = 0; let settingsPanelSize = settingsPanel.size; for (let child of settingsPanel.children) { if (child.hidden) continue; if (thisColumn >= perColumn) { yPos = g_SettingDist; ++column; thisColumn = 0; } let childSize = child.size; child.size = new GUISize( column * columnWidth, yPos, column * columnWidth + columnWidth - 10, yPos + g_SettingHeight - g_SettingDist); yPos += g_SettingHeight; ++thisColumn; } settingsPanelSize.right = settingsPanelSize.left + (column + 1) * columnWidth; settingsPanel.size = settingsPanelSize; } /** * Called when the client disconnects. * The other cases from NetClient should never occur in the gamesetup. */ function handleNetStatusMessage(message) { if (message.status != "disconnected") { error("Unrecognised netstatus type " + message.status); return; } cancelSetup(); reportDisconnect(message.reason, true); } /** * Called whenever a client clicks on ready (or not ready). */ function handleReadyMessage(message) { --g_ReadyChanged; if (g_ReadyChanged < 1 && g_PlayerAssignments[message.guid].player != -1) addChatMessage({ "type": "ready", "status": message.status, "guid": message.guid }); g_PlayerAssignments[message.guid].status = message.status; updateGUIObjects(); } /** * Called after every player is ready and the host decided to finally start the game. */ function handleGamestartMessage(message) { // Immediately inform the lobby server instead of waiting for the load to finish if (g_IsController && Engine.HasXmppClient()) { sendRegisterGameStanzaImmediate(); let clients = formatClientsForStanza(); Engine.SendChangeStateGame(clients.connectedPlayers, clients.list); } Engine.SwitchGuiPage("page_loading.xml", { "attribs": g_GameAttributes, "isNetworked": g_IsNetworked, "playerAssignments": g_PlayerAssignments, "isController": g_IsController }); } /** * Called whenever the host changed any setting. */ function handleGamesetupMessage(message) { if (!message.data) return; g_GameAttributes = message.data; if (!!g_GameAttributes.settings.RatingEnabled) { g_GameAttributes.settings.CheatsEnabled = false; g_GameAttributes.settings.LockTeams = true; g_GameAttributes.settings.LastManStanding = false; } Engine.SetRankedGame(!!g_GameAttributes.settings.RatingEnabled); resetReadyData(); updateGUIObjects(); hideLoadingWindow(); } /** * Called whenever a client joins/leaves or any gamesetting is changed. */ function handlePlayerAssignmentMessage(message) { let playerChange = false; for (let guid in message.newAssignments) if (!g_PlayerAssignments[guid]) { onClientJoin(guid, message.newAssignments); playerChange = true; } for (let guid in g_PlayerAssignments) if (!message.newAssignments[guid]) { onClientLeave(guid); playerChange = true; } g_PlayerAssignments = message.newAssignments; sanitizePlayerData(g_GameAttributes.settings.PlayerData); updateGUIObjects(); if (playerChange) sendRegisterGameStanzaImmediate(); else sendRegisterGameStanza(); } function onClientJoin(newGUID, newAssignments) { let playername = newAssignments[newGUID].name; addChatMessage({ "type": "connect", "guid": newGUID, "username": playername }); let isRejoiningPlayer = newAssignments[newGUID].player != -1; // Assign the client (or only buddies if prefered) to an unused playerslot and rejoining players to their old slot if (!isRejoiningPlayer && playername != newAssignments[Engine.GetPlayerGUID()].name) { let assignOption = Engine.ConfigDB_GetValue("user", "gui.gamesetup.assignplayers"); if (assignOption == "disabled" || assignOption == "buddies" && g_Buddies.indexOf(splitRatingFromNick(playername).nick) == -1) return; } let freeSlot = g_GameAttributes.settings.PlayerData.findIndex((v, i) => Object.keys(g_PlayerAssignments).every(guid => g_PlayerAssignments[guid].player != i + 1) ); // Client is not and cannot become assigned as player if (!isRejoiningPlayer && freeSlot == -1) return; // Assign the joining client to the free slot if (g_IsController && !isRejoiningPlayer) Engine.AssignNetworkPlayer(freeSlot + 1, newGUID); resetReadyData(); } function onClientLeave(guid) { addChatMessage({ "type": "disconnect", "guid": guid }); if (g_PlayerAssignments[guid].player != -1) resetReadyData(); } /** * Doesn't translate, so that lobby clients can do that locally * (even if they don't have that map). */ function getMapDisplayName(map) { if (map == "random") return map; let mapData = loadMapData(map); if (!mapData || !mapData.settings || !mapData.settings.Name) return map; return mapData.settings.Name; } function getMapPreview(map) { let mapData = loadMapData(map); if (!mapData || !mapData.settings || !mapData.settings.Preview) return "nopreview.png"; return mapData.settings.Preview; } /** * Filter maps with filterFunc and by chosen map type. * * @param {function} filterFunc - Filter function that should be applied. * @return {Array} the maps that match the filterFunc and the chosen map type. */ function getFilteredMaps(filterFunc) { if (!g_MapPath[g_GameAttributes.mapType]) { error("Unexpected map type: " + g_GameAttributes.mapType); return []; } let maps = []; // TODO: Should verify these are valid maps before adding to list for (let mapFile of listFiles(g_GameAttributes.mapPath, g_GameAttributes.mapType == "random" ? ".json" : ".xml", false)) { if (mapFile.startsWith("_")) continue; let file = g_GameAttributes.mapPath + mapFile; let mapData = loadMapData(file); if (!mapData.settings || filterFunc && !filterFunc(mapData.settings.Keywords || [])) continue; maps.push({ "file": file, "name": translate(getMapDisplayName(file)), "color": g_ColorRegular, "description": translate(mapData.settings.Description) }); } return maps; } /** * Initialize the dropdown containing all map filters for the selected maptype. */ function reloadMapFilterList() { g_MapFilterList = prepareForDropdown(g_MapFilters.filter( mapFilter => getFilteredMaps(mapFilter.filter).length )); initDropdown("mapFilter"); reloadMapList(); } /** * Initialize the dropdown containing all maps for the selected maptype and mapfilter. */ function reloadMapList() { let filterID = g_MapFilterList.id.findIndex(id => id == g_GameAttributes.mapFilter); let filterFunc = g_MapFilterList.filter[filterID]; let mapList = getFilteredMaps(filterFunc).sort(sortNameIgnoreCase); if (g_GameAttributes.mapType == "random") mapList.unshift({ "file": "random", "name": translateWithContext("map selection", "Random"), "color": g_ColorRandom, "description": translate("Pick any of the given maps at random.") }); g_MapSelectionList = prepareForDropdown(mapList); initDropdown("mapSelection"); } /** * Initialize the dropdowns specific to each map. */ function reloadMapSpecific() { reloadBiomeList(); reloadTriggerDifficulties(); } function reloadBiomeList() { let biomeList; if (g_GameAttributes.mapType == "random" && g_GameAttributes.settings.SupportedBiomes) { if (typeof g_GameAttributes.settings.SupportedBiomes == "string") biomeList = g_Settings.Biomes.filter(biome => biome.Id.startsWith(g_GameAttributes.settings.SupportedBiomes)); else biomeList = g_Settings.Biomes.filter( biome => g_GameAttributes.settings.SupportedBiomes.indexOf(biome.Id) != -1); } g_BiomeList = biomeList && prepareForDropdown( [{ "Id": "random", "Title": translateWithContext("biome", "Random"), "Description": translate("Pick a biome at random."), "Color": g_ColorRandom }].concat(biomeList.map(biome => ({ "Id": biome.Id, "Title": biome.Title, "Description": biome.Description, "Color": g_ColorRegular })))); initDropdown("biome"); updateGUIDropdown("biome"); } function reloadTriggerDifficulties() { g_TriggerDifficultyList = undefined; if (!g_GameAttributes.settings.SupportedTriggerDifficulties) return; let triggerDifficultyList; if (g_GameAttributes.settings.SupportedTriggerDifficulties.Values === true) triggerDifficultyList = g_Settings.TriggerDifficulties; else { triggerDifficultyList = g_Settings.TriggerDifficulties.filter( diff => g_GameAttributes.settings.SupportedTriggerDifficulties.Values.indexOf(diff.Name) != -1); if (!triggerDifficultyList.length) return; } g_TriggerDifficultyList = prepareForDropdown( triggerDifficultyList.map(diff => ({ "Id": diff.Difficulty, "Title": diff.Title, "Description": diff.Tooltip, "Default": diff.Name == g_GameAttributes.settings.SupportedTriggerDifficulties.Default }))); initDropdown("triggerDifficulty"); updateGUIDropdown("triggerDifficulty"); } function reloadGameSpeedChoices() { g_GameSpeeds = getGameSpeedChoices(Object.keys(g_PlayerAssignments).every(guid => g_PlayerAssignments[guid].player == -1)); initDropdown("gameSpeed"); supplementDefaults(); } function loadMapData(name) { if (!name || !g_MapPath[g_GameAttributes.mapType]) return undefined; if (name == "random") return { "settings": { "Name": "", "Description": "" } }; if (!g_MapData[name]) g_MapData[name] = g_GameAttributes.mapType == "random" ? Engine.ReadJSONFile(name + ".json") : Engine.LoadMapSettings(name); return g_MapData[name]; } /** * Sets the gameattributes the way they were the last time the user left the gamesetup. */ function loadPersistMatchSettings() { if (!g_IsController || Engine.ConfigDB_GetValue("user", "persistmatchsettings") != "true" || g_IsTutorial) return; let settingsFile = g_IsNetworked ? g_MatchSettings_MP : g_MatchSettings_SP; if (!Engine.FileExists(settingsFile)) return; let attrs = Engine.ReadJSONFile(settingsFile); if (!attrs || !attrs.settings) return; g_IsInGuiUpdate = true; let mapName = attrs.map || ""; let mapSettings = attrs.settings; g_GameAttributes = attrs; if (!g_IsNetworked) mapSettings.CheatsEnabled = true; // Replace unselectable civs with random civ let playerData = mapSettings.PlayerData; if (playerData && g_GameAttributes.mapType != "scenario") for (let i in playerData) if (!g_CivData[playerData[i].Civ] || !g_CivData[playerData[i].Civ].SelectableInGameSetup) playerData[i].Civ = "random"; // Apply map settings let newMapData = loadMapData(mapName); if (newMapData && newMapData.settings) { for (let prop in newMapData.settings) mapSettings[prop] = newMapData.settings[prop]; if (playerData) mapSettings.PlayerData = playerData; } if (mapSettings.PlayerData) sanitizePlayerData(mapSettings.PlayerData); // Reload, as the maptype or mapfilter might have changed reloadMapFilterList(); reloadMapSpecific(); g_GameAttributes.settings.RatingEnabled = Engine.HasXmppClient(); Engine.SetRankedGame(g_GameAttributes.settings.RatingEnabled); supplementDefaults(); g_IsInGuiUpdate = false; } function savePersistMatchSettings() { if (g_IsTutorial) return; let attributes = Engine.ConfigDB_GetValue("user", "persistmatchsettings") == "true" ? g_GameAttributes : {}; Engine.WriteJSONFile(g_IsNetworked ? g_MatchSettings_MP : g_MatchSettings_SP, attributes); } function sanitizePlayerData(playerData) { // Remove gaia if (playerData.length && !playerData[0]) playerData.shift(); playerData.forEach((pData, index) => { // Use defaults if the map doesn't specify a value for (let prop in g_DefaultPlayerData[index]) if (!(prop in pData)) pData[prop] = clone(g_DefaultPlayerData[index][prop]); // Replace colors with the best matching color of PlayerDefaults if (g_GameAttributes.mapType != "scenario") { let colorDistances = g_PlayerColorPickerList.map(color => colorDistance(color, pData.Color)); let smallestDistance = colorDistances.find(distance => colorDistances.every(distance2 => (distance2 >= distance))); pData.Color = g_PlayerColorPickerList.find(color => colorDistance(color, pData.Color) == smallestDistance); } // If there is a player in that slot, then there can't be an AI if (Object.keys(g_PlayerAssignments).some(guid => g_PlayerAssignments[guid].player == index + 1)) pData.AI = ""; }); ensureUniquePlayerColors(playerData); } function cancelSetup() { if (g_IsController) savePersistMatchSettings(); Engine.DisconnectNetworkGame(); if (Engine.HasXmppClient()) { Engine.LobbySetPlayerPresence("available"); if (g_IsController) Engine.SendUnregisterGame(); Engine.SwitchGuiPage("page_lobby.xml", { "dialog": false }); } else Engine.SwitchGuiPage("page_pregame.xml"); } /** * Can't init the GUI before the first tick. * Process netmessages afterwards. */ function onTick() { if (!g_Settings) return; // First tick happens before first render, so don't load yet if (g_LoadingState == 0) ++g_LoadingState; else if (g_LoadingState == 1) { initGUIObjects(); ++g_LoadingState; } else if (g_LoadingState == 2) handleNetMessages(); updateTimers(); let now = Date.now(); let tickLength = now - g_LastTickTime; g_LastTickTime = now; updateSettingsPanelPosition(tickLength); } /** * Handles all pending messages sent by the net client. */ function handleNetMessages() { while (g_IsNetworked) { let message = Engine.PollNetworkClient(); if (!message) break; log("Net message: " + uneval(message)); if (g_NetMessageTypes[message.type]) g_NetMessageTypes[message.type](message); else error("Unrecognised net message type " + message.type); } } /** * Called when the map or the number of players changes. */ function unassignInvalidPlayers(maxPlayers) { if (g_IsNetworked) // Remove invalid playerIDs from the servers playerassignments copy for (let playerID = +maxPlayers + 1; playerID <= g_MaxPlayers; ++playerID) Engine.AssignNetworkPlayer(playerID, ""); else if (g_PlayerAssignments.local.player > maxPlayers) g_PlayerAssignments.local.player = -1; } function ensureUniquePlayerColors(playerData) { for (let i = playerData.length - 1; i >= 0; --i) // If someone else has that color, assign an unused color if (playerData.some((pData, j) => i != j && sameColor(playerData[i].Color, pData.Color))) playerData[i].Color = g_PlayerColorPickerList.find(color => playerData.every(pData => !sameColor(color, pData.Color))); } function selectMap(name) { // Reset some map specific properties which are not necessarily redefined on each map for (let prop of ["TriggerScripts", "CircularMap", "Garrison", "DisabledTemplates", "Biome", "SupportedBiomes", "SupportedTriggerDifficulties", "TriggerDifficulty"]) g_GameAttributes.settings[prop] = undefined; let mapData = loadMapData(name); let mapSettings = mapData && mapData.settings ? clone(mapData.settings) : {}; if (g_GameAttributes.mapType != "random") { delete g_GameAttributes.settings.Nomad; let victoryIdx = g_VictoryConditions.Name.indexOf(mapSettings.GameType || "") != -1 ? g_VictoryConditions.Name.indexOf(mapSettings.GameType) : g_VictoryConditions.Default; g_GameAttributes.settings.GameType = g_VictoryConditions.Name[victoryIdx]; g_GameAttributes.settings.VictoryScripts = g_VictoryConditions.Scripts[victoryIdx]; } if (g_GameAttributes.mapType == "scenario") { delete g_GameAttributes.settings.RelicDuration; delete g_GameAttributes.settings.WonderDuration; delete g_GameAttributes.settings.LastManStanding; delete g_GameAttributes.settings.RegicideGarrison; } if (mapSettings.PlayerData) sanitizePlayerData(mapSettings.PlayerData); // Copy any new settings g_GameAttributes.map = name; g_GameAttributes.script = mapSettings.Script; if (g_GameAttributes.map !== "random") for (let prop in mapSettings) g_GameAttributes.settings[prop] = mapSettings[prop]; reloadMapSpecific(); unassignInvalidPlayers(g_GameAttributes.settings.PlayerData.length); supplementDefaults(); } function isControlArrayElementHidden(playerIdx) { return playerIdx !== undefined && playerIdx >= g_GameAttributes.settings.PlayerData.length; } /** * @param playerIdx - Only specified for dropdown arrays. */ function updateGUIDropdown(name, playerIdx = undefined) { let [guiName, guiType, guiIdx] = getGUIObjectNameFromSetting(name); let idxName = playerIdx === undefined ? "" : "[" + playerIdx + "]"; let dropdown = Engine.GetGUIObjectByName(guiName + guiType + guiIdx + idxName); let label = Engine.GetGUIObjectByName(guiName + "Text" + guiIdx + idxName); let frame = Engine.GetGUIObjectByName(guiName + "Frame" + guiIdx + idxName); let title = Engine.GetGUIObjectByName(guiName + "Title" + guiIdx + idxName); if (guiType == "Dropdown") Engine.GetGUIObjectByName(guiName + "Checkbox" + guiIdx).hidden = true; let indexHidden = isControlArrayElementHidden(playerIdx); let obj = (playerIdx === undefined ? g_Dropdowns : g_PlayerDropdowns)[name]; let hidden = indexHidden || obj.hidden && obj.hidden(playerIdx); let selected = hidden ? -1 : dropdown.list_data.indexOf(String(obj.get(playerIdx))); let enabled = !indexHidden && (!obj.enabled || obj.enabled(playerIdx)); dropdown.enabled = g_IsController && enabled; dropdown.hidden = !g_IsController || !enabled || hidden; dropdown.selected = selected; dropdown.tooltip = !indexHidden && obj.tooltip ? obj.tooltip(-1, playerIdx) : ""; if (frame) frame.hidden = hidden; if (title && obj.title && !indexHidden) title.caption = sprintf(translateWithContext("Title for specific setting", "%(setting)s:"), { "setting": obj.title(playerIdx) }); if (label && !indexHidden) { label.hidden = g_IsController && enabled || hidden; label.caption = selected == -1 ? translateWithContext("settings value", "Unknown") : dropdown.list[selected]; } } /** * Not used for the player assignments, so playerCheckboxes are not implemented, * hence no index. */ function updateGUICheckbox(name) { let obj = g_Checkboxes[name]; let checked = obj.get(); let hidden = obj.hidden && obj.hidden(); let enabled = !obj.enabled || obj.enabled(); let [guiName, guiType, guiIdx] = getGUIObjectNameFromSetting(name); let checkbox = Engine.GetGUIObjectByName(guiName + guiType + guiIdx); let label = Engine.GetGUIObjectByName(guiName + "Text" + guiIdx); let frame = Engine.GetGUIObjectByName(guiName + "Frame" + guiIdx); let title = Engine.GetGUIObjectByName(guiName + "Title" + guiIdx); if (guiType == "Checkbox") Engine.GetGUIObjectByName(guiName + "Dropdown" + guiIdx).hidden = true; checkbox.checked = checked; checkbox.enabled = g_IsController && enabled; checkbox.hidden = hidden || !g_IsController; checkbox.tooltip = obj.tooltip ? obj.tooltip() : ""; label.caption = checked ? translate("Yes") : translate("No"); label.hidden = hidden || g_IsController; if (frame) frame.hidden = hidden; if (title && obj.title) title.caption = sprintf(translate("%(setting)s:"), { "setting": obj.title() }); } function updateGUIMiscControl(name, playerIdx) { let idxName = playerIdx === undefined ? "" : "[" + playerIdx + "]"; let obj = (playerIdx === undefined ? g_MiscControls : g_PlayerMiscElements)[name]; let control = Engine.GetGUIObjectByName(name + idxName); if (!control) warn("No GUI object with name '" + name + "'"); let hide = isControlArrayElementHidden(playerIdx); control.hidden = hide; if (hide) return; for (let property in obj) control[property] = obj[property](playerIdx); } function launchGame() { if (!g_IsController) { error("Only host can start game"); return; } if (!g_GameAttributes.map) return; savePersistMatchSettings(); // Select random map if (g_GameAttributes.map == "random") { let victoryScriptsSelected = g_GameAttributes.settings.VictoryScripts; let gameTypeSelected = g_GameAttributes.settings.GameType; selectMap(pickRandom(g_Dropdowns.mapSelection.ids().slice(1))); g_GameAttributes.settings.VictoryScripts = victoryScriptsSelected; g_GameAttributes.settings.GameType = gameTypeSelected; } if (g_GameAttributes.settings.Biome == "random") g_GameAttributes.settings.Biome = pickRandom( typeof g_GameAttributes.settings.SupportedBiomes == "string" ? g_BiomeList.Id.slice(1).filter(biomeID => biomeID.startsWith(g_GameAttributes.settings.SupportedBiomes)) : g_GameAttributes.settings.SupportedBiomes); g_GameAttributes.settings.TriggerScripts = g_GameAttributes.settings.VictoryScripts.concat(g_GameAttributes.settings.TriggerScripts || []); // Prevent reseting the readystate g_GameStarted = true; g_GameAttributes.settings.mapType = g_GameAttributes.mapType; // Get a unique array of selectable cultures let cultures = Object.keys(g_CivData).filter(civ => g_CivData[civ].SelectableInGameSetup).map(civ => g_CivData[civ].Culture); cultures = cultures.filter((culture, index) => cultures.indexOf(culture) === index); // Determine random civs and botnames for (let i in g_GameAttributes.settings.PlayerData) { // Pick a random civ of a random culture let chosenCiv = g_GameAttributes.settings.PlayerData[i].Civ || "random"; if (chosenCiv == "random") { let culture = pickRandom(cultures); chosenCiv = pickRandom(Object.keys(g_CivData).filter(civ => g_CivData[civ].Culture == culture)); } g_GameAttributes.settings.PlayerData[i].Civ = chosenCiv; // Pick one of the available botnames for the chosen civ if (g_GameAttributes.mapType === "scenario" || !g_GameAttributes.settings.PlayerData[i].AI) continue; let chosenName = pickRandom(g_CivData[chosenCiv].AINames); if (!g_IsNetworked) chosenName = translate(chosenName); // Count how many players use the chosenName let usedName = g_GameAttributes.settings.PlayerData.filter(pData => pData.Name && pData.Name.indexOf(chosenName) !== -1).length; g_GameAttributes.settings.PlayerData[i].Name = !usedName ? chosenName : sprintf(translate("%(playerName)s %(romanNumber)s"), { "playerName": chosenName, "romanNumber": g_RomanNumbers[usedName+1] }); } // Copy playernames for the purpose of replays for (let guid in g_PlayerAssignments) { let player = g_PlayerAssignments[guid]; if (player.player > 0) // not observer or GAIA g_GameAttributes.settings.PlayerData[player.player - 1].Name = player.name; } // Seed used for both map generation and simulation g_GameAttributes.settings.Seed = randIntExclusive(0, Math.pow(2, 32)); g_GameAttributes.settings.AISeed = randIntExclusive(0, Math.pow(2, 32)); // Used for identifying rated game reports for the lobby g_GameAttributes.matchID = Engine.GetMatchID(); if (g_IsNetworked) { Engine.SetNetworkGameAttributes(g_GameAttributes); Engine.StartNetworkGame(); } else { // Find the player ID which the user has been assigned to let playerID = -1; for (let i in g_GameAttributes.settings.PlayerData) { let assignBox = Engine.GetGUIObjectByName("playerAssignment[" + i + "]"); if (assignBox.list_data[assignBox.selected] == "guid:local") playerID = +i + 1; } Engine.StartGame(g_GameAttributes, playerID); Engine.SwitchGuiPage("page_loading.xml", { "attribs": g_GameAttributes, "isNetworked": g_IsNetworked, "playerAssignments": g_PlayerAssignments }); } } function launchTutorial() { g_GameAttributes.mapType = "scenario"; selectMap("maps/tutorials/starting_economy_walkthrough"); launchGame(); } /** * Don't set any attributes here, just show the changes in the GUI. * * Unless the mapsettings don't specify a property and the user didn't set it in g_GameAttributes previously. */ function updateGUIObjects() { g_IsInGuiUpdate = true; reloadMapFilterList(); reloadMapSpecific(); reloadGameSpeedChoices(); reloadPlayerAssignmentChoices(); // Hide exceeding dropdowns and checkboxes for (let setting of Engine.GetGUIObjectByName("settingsPanel").children) setting.hidden = true; // Show the relevant ones if (g_TabCategorySelected !== undefined) { for (let name in g_Dropdowns) if (g_SettingsTabsGUI[g_TabCategorySelected].settings.indexOf(name) != -1) updateGUIDropdown(name); for (let name in g_Checkboxes) if (g_SettingsTabsGUI[g_TabCategorySelected].settings.indexOf(name) != -1) updateGUICheckbox(name); } for (let i = 0; i < g_MaxPlayers; ++i) { for (let name in g_PlayerDropdowns) updateGUIDropdown(name, i); for (let name in g_PlayerMiscElements) updateGUIMiscControl(name, i); } for (let name in g_MiscControls) updateGUIMiscControl(name); updateGameDescription(); distributeSettings(); rightAlignCancelButton(); updateAutocompleteEntries(); g_IsInGuiUpdate = false; // Refresh AI config page if (g_LastViewedAIPlayer != -1) { Engine.PopGuiPage(); openAIConfig(g_LastViewedAIPlayer); } } function rightAlignCancelButton() { let offset = 10; let startGame = Engine.GetGUIObjectByName("startGame"); let right = startGame.hidden ? startGame.size.right : startGame.size.left - offset; let cancelGame = Engine.GetGUIObjectByName("cancelGame"); let cancelGameSize = cancelGame.size; let buttonWidth = cancelGameSize.right - cancelGameSize.left; cancelGameSize.right = right; right -= buttonWidth; for (let element of ["cheatWarningText", "onscreenToolTip"]) { let elementSize = Engine.GetGUIObjectByName(element).size; elementSize.right = right - (cancelGameSize.left - elementSize.right); Engine.GetGUIObjectByName(element).size = elementSize; } cancelGameSize.left = right; cancelGame.size = cancelGameSize; } function updateGameDescription() { setMapPreviewImage("mapPreview", getMapPreview(g_GameAttributes.map)); Engine.GetGUIObjectByName("mapInfoName").caption = translateMapTitle(getMapDisplayName(g_GameAttributes.map)); Engine.GetGUIObjectByName("mapInfoDescription").caption = getGameDescription(); } /** * Broadcast the changed settings to all clients and the lobbybot. */ function updateGameAttributes() { if (g_IsInGuiUpdate || !g_IsController) return; if (g_IsNetworked) { Engine.SetNetworkGameAttributes(g_GameAttributes); if (g_LoadingState >= 2) sendRegisterGameStanza(); resetReadyData(); } else updateGUIObjects(); } function openAIConfig(playerSlot) { g_LastViewedAIPlayer = playerSlot; Engine.PushGuiPage("page_aiconfig.xml", { "callback": "AIConfigCallback", "isController": g_IsController, "playerSlot": playerSlot, "id": g_GameAttributes.settings.PlayerData[playerSlot].AI, "difficulty": g_GameAttributes.settings.PlayerData[playerSlot].AIDiff, "behavior": g_GameAttributes.settings.PlayerData[playerSlot].AIBehavior }); } /** * Called after closing the dialog. */ function AIConfigCallback(ai) { g_LastViewedAIPlayer = -1; if (!ai.save || !g_IsController) return; g_GameAttributes.settings.PlayerData[ai.playerSlot].AI = ai.id; g_GameAttributes.settings.PlayerData[ai.playerSlot].AIDiff = ai.difficulty; g_GameAttributes.settings.PlayerData[ai.playerSlot].AIBehavior = ai.behavior; updateGameAttributes(); } function reloadPlayerAssignmentChoices() { let playerChoices = sortGUIDsByPlayerID().map(guid => ({ "Choice": "guid:" + guid, "Color": g_PlayerAssignments[guid].player == -1 ? g_PlayerAssignmentColors.observer : g_PlayerAssignmentColors.player, "Name": g_PlayerAssignments[guid].name })); // Only display hidden AIs if the map preselects them let aiChoices = g_Settings.AIDescriptions .filter(ai => !ai.data.hidden || g_GameAttributes.settings.PlayerData.some(pData => pData.AI == ai.id)) .map(ai => ({ "Choice": "ai:" + ai.id, "Name": sprintf(translate("AI: %(ai)s"), { "ai": translate(ai.data.name) }), "Color": g_PlayerAssignmentColors.AI })); let unassignedSlot = [{ "Choice": "unassigned", "Name": translate("Unassigned"), "Color": g_PlayerAssignmentColors.unassigned }]; g_PlayerAssignmentList = prepareForDropdown(playerChoices.concat(aiChoices).concat(unassignedSlot)); initPlayerDropdowns("playerAssignment"); } function swapPlayers(guidToSwap, newSlot) { // Player slots are indexed from 0 as Gaia is omitted. let newPlayerID = newSlot + 1; let playerID = g_PlayerAssignments[guidToSwap].player; // Attempt to swap the player or AI occupying the target slot, // if any, into the slot this player is currently in. if (playerID != -1) { for (let guid in g_PlayerAssignments) { // Move the player in the destination slot into the current slot. if (g_PlayerAssignments[guid].player != newPlayerID) continue; if (g_IsNetworked) Engine.AssignNetworkPlayer(playerID, guid); else g_PlayerAssignments[guid].player = playerID; break; } // Transfer the AI from the target slot to the current slot. g_GameAttributes.settings.PlayerData[playerID - 1].AI = g_GameAttributes.settings.PlayerData[newSlot].AI; g_GameAttributes.settings.PlayerData[playerID - 1].AIDiff = g_GameAttributes.settings.PlayerData[newSlot].AIDiff; g_GameAttributes.settings.PlayerData[playerID - 1].AIBehavior = g_GameAttributes.settings.PlayerData[newSlot].AIBehavior; // Swap civilizations and colors if they aren't fixed if (g_GameAttributes.mapType != "scenario") { [g_GameAttributes.settings.PlayerData[playerID - 1].Civ, g_GameAttributes.settings.PlayerData[newSlot].Civ] = [g_GameAttributes.settings.PlayerData[newSlot].Civ, g_GameAttributes.settings.PlayerData[playerID - 1].Civ]; [g_GameAttributes.settings.PlayerData[playerID - 1].Color, g_GameAttributes.settings.PlayerData[newSlot].Color] = [g_GameAttributes.settings.PlayerData[newSlot].Color, g_GameAttributes.settings.PlayerData[playerID - 1].Color]; } } if (g_IsNetworked) Engine.AssignNetworkPlayer(newPlayerID, guidToSwap); else g_PlayerAssignments[guidToSwap].player = newPlayerID; g_GameAttributes.settings.PlayerData[newSlot].AI = ""; } function submitChatInput() { let chatInput = Engine.GetGUIObjectByName("chatInput"); let text = chatInput.caption; if (!text.length) return; chatInput.caption = ""; if (!executeNetworkCommand(text)) Engine.SendNetworkChat(text); chatInput.focus(); } function senderFont(text) { return '[font="' + g_SenderFont + '"]' + text + '[/font]'; } function systemMessage(message) { return senderFont(sprintf(translate("== %(message)s"), { "message": message })); } function colorizePlayernameByGUID(guid, username = "") { // TODO: Maybe the host should have the moderator-prefix? if (!username) username = g_PlayerAssignments[guid] ? escapeText(g_PlayerAssignments[guid].name) : translate("Unknown Player"); let playerID = g_PlayerAssignments[guid] ? g_PlayerAssignments[guid].player : -1; let color = g_ColorRegular; if (playerID > 0) { color = g_GameAttributes.settings.PlayerData[playerID - 1].Color; // Enlighten playercolor to improve readability let [h, s, l] = rgbToHsl(color.r, color.g, color.b); let [r, g, b] = hslToRgb(h, s, Math.max(0.6, l)); color = rgbToGuiColor({ "r": r, "g": g, "b": b }); } return coloredText(username, color); } function addChatMessage(msg) { if (!g_FormatChatMessage[msg.type]) return; if (msg.type == "chat") { let userName = g_PlayerAssignments[Engine.GetPlayerGUID()].name; if (userName != g_PlayerAssignments[msg.guid].name && msg.text.toLowerCase().indexOf(splitRatingFromNick(userName).nick.toLowerCase()) != -1) soundNotification("nick"); } let user = colorizePlayernameByGUID(msg.guid || -1, msg.username || ""); let text = g_FormatChatMessage[msg.type](msg, user); if (!text) return; if (Engine.ConfigDB_GetValue("user", "chat.timestamp") == "true") text = sprintf(translate("%(time)s %(message)s"), { "time": sprintf(translate("\\[%(time)s]"), { "time": Engine.FormatMillisecondsIntoDateStringLocal(Date.now(), translate("HH:mm")) }), "message": text }); g_ChatMessages.push(text); Engine.GetGUIObjectByName("chatText").caption = g_ChatMessages.join("\n"); } function resetCivilizations() { for (let i in g_GameAttributes.settings.PlayerData) g_GameAttributes.settings.PlayerData[i].Civ = "random"; updateGameAttributes(); } function resetTeams() { for (let i in g_GameAttributes.settings.PlayerData) g_GameAttributes.settings.PlayerData[i].Team = -1; updateGameAttributes(); } function toggleReady() { setReady((g_IsReady + 1) % 3, true); } function setReady(ready, sendMessage) { g_IsReady = ready; if (sendMessage) Engine.SendNetworkReady(g_IsReady); updateGUIObjects(); } function resetReadyData() { if (g_GameStarted) return; if (g_ReadyChanged < 1) addChatMessage({ "type": "settings" }); else if (g_ReadyChanged == 2 && !g_ReadyInit) return; // duplicate calls on init else g_ReadyInit = false; g_ReadyChanged = 2; if (!g_IsNetworked) g_IsReady = 2; else if (g_IsController) { Engine.ClearAllPlayerReady(); setReady(2, true); } else if (g_IsReady != 2) setReady(0, false); } /** * Send a list of playernames and distinct between players and observers. * Don't send teams, AIs or anything else until the game was started. * The playerData format from g_GameAttributes is kept to reuse the GUI function presenting the data. */ function formatClientsForStanza() { let connectedPlayers = 0; let playerData = []; for (let guid in g_PlayerAssignments) { let pData = { "Name": g_PlayerAssignments[guid].name }; if (g_GameAttributes.settings.PlayerData[g_PlayerAssignments[guid].player - 1]) ++connectedPlayers; else pData.Team = "observer"; playerData.push(pData); } return { "list": playerDataToStringifiedTeamList(playerData), "connectedPlayers": connectedPlayers }; } /** * Send the relevant gamesettings to the lobbybot immediately. */ function sendRegisterGameStanzaImmediate() { if (!g_IsController || !Engine.HasXmppClient()) return; if (g_GameStanzaTimer !== undefined) { clearTimeout(g_GameStanzaTimer); g_GameStanzaTimer = undefined; } let clients = formatClientsForStanza(); let stanza = { "name": g_ServerName, "port": g_ServerPort, "hostUsername": Engine.LobbyGetNick(), "mapName": g_GameAttributes.map, "niceMapName": getMapDisplayName(g_GameAttributes.map), "mapSize": g_GameAttributes.mapType == "random" ? g_GameAttributes.settings.Size : "Default", "mapType": g_GameAttributes.mapType, "victoryCondition": g_GameAttributes.settings.GameType, "nbp": clients.connectedPlayers, "maxnbp": g_GameAttributes.settings.PlayerData.length, "players": clients.list, "stunIP": g_StunEndpoint ? g_StunEndpoint.ip : "", "stunPort": g_StunEndpoint ? g_StunEndpoint.port : "", + "mods": JSON.stringify(Engine.GetEngineInfo().mods), }; // Only send the stanza if the relevant settings actually changed if (g_LastGameStanza && Object.keys(stanza).every(prop => g_LastGameStanza[prop] == stanza[prop])) return; g_LastGameStanza = stanza; Engine.SendRegisterGame(stanza); } /** * Send the relevant gamesettings to the lobbybot in a deferred manner. */ function sendRegisterGameStanza() { if (!g_IsController || !Engine.HasXmppClient()) return; if (g_GameStanzaTimer !== undefined) clearTimeout(g_GameStanzaTimer); g_GameStanzaTimer = setTimeout(sendRegisterGameStanzaImmediate, g_GameStanzaTimeout * 1000); } /** * Figures out all strings that can be autocompleted and sorts * them by priority (so that playernames are always autocompleted first). */ function updateAutocompleteEntries() { let autocomplete = { "0": [] }; for (let control of [g_Dropdowns, g_Checkboxes]) for (let name in control) autocomplete[0] = autocomplete[0].concat(control[name].title()); for (let dropdown of [g_Dropdowns, g_PlayerDropdowns]) for (let name in dropdown) { let priority = dropdown[name].autocomplete; if (priority === undefined) continue; autocomplete[priority] = (autocomplete[priority] || []).concat(dropdown[name].labels()); } g_Autocomplete = Object.keys(autocomplete).sort().reverse().reduce((all, priority) => all.concat(autocomplete[priority]), []); } Index: ps/trunk/binaries/data/mods/public/gui/lobby/lobby.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/lobby/lobby.js (revision 21300) +++ ps/trunk/binaries/data/mods/public/gui/lobby/lobby.js (revision 21301) @@ -1,1561 +1,1574 @@ /** * Used for the gamelist-filtering. */ const g_MapSizes = prepareForDropdown(g_Settings && g_Settings.MapSizes); /** * Used for the gamelist-filtering. */ const g_MapTypes = prepareForDropdown(g_Settings && g_Settings.MapTypes); /** * Used for civ settings display of the selected game. */ const g_CivData = loadCivData(false, false); /** * A symbol which is prepended to the username of moderators. */ var g_ModeratorPrefix = "@"; /** * Current username. Cannot contain whitespace. */ const g_Username = Engine.LobbyGetNick(); /** * Lobby server address to construct host JID. */ const g_LobbyServer = Engine.ConfigDB_GetValue("user", "lobby.server"); /** * Current games will be listed in these colors. */ var g_GameColors = { "init": "0 219 0", "waiting": "255 127 0", - "running": "219 0 0" + "running": "219 0 0", + "incompatible": "gray" }; /** * Initial sorting order of the gamelist. */ -var g_GameStatusOrder = ["init", "waiting", "running"]; +var g_GameStatusOrder = ["init", "waiting", "running", "incompatible"]; /** * The playerlist will be assembled using these values. */ var g_PlayerStatuses = { "available": { "color": "0 219 0", "status": translate("Online") }, "away": { "color": "229 76 13", "status": translate("Away") }, "playing": { "color": "200 0 0", "status": translate("Busy") }, "offline": { "color": "0 0 0", "status": translate("Offline") }, "unknown": { "color": "178 178 178", "status": translateWithContext("lobby presence", "Unknown") } }; var g_RoleNames = { "moderator": translate("Moderator"), "participant": translate("Player"), "visitor": translate("Muted Player") }; /** * Color for error messages in the chat. */ var g_SystemColor = "150 0 0"; /** * Color for private messages in the chat. */ var g_PrivateMessageColor = "0 150 0"; /** * Used for highlighting the sender of chat messages. */ var g_SenderFont = "sans-bold-13"; /** * Color to highlight chat commands in the explanation. */ var g_ChatCommandColor = "200 200 255"; /** * Indicates if the lobby is opened as a dialog or window. */ var g_Dialog = false; /** * All chat messages received since init (i.e. after lobby join and after returning from a game). */ var g_ChatMessages = []; /** * Rating of the current user. * Contains the number or an empty string in case the user has no rating. */ var g_UserRating = ""; /** * All games currently running. */ var g_GameList = []; /** * Used to restore the selection after updating the playerlist. */ var g_SelectedPlayer = ""; /** * Used to restore the selection after updating the gamelist. */ var g_SelectedGameIP = ""; /** * Used to restore the selection after updating the gamelist. */ var g_SelectedGamePort = ""; /** * Whether the current user has been kicked or banned. */ var g_Kicked = false; /** * Whether the player was already asked to reconnect to the lobby. * Ensures that no more than one message box is opened at a time. */ var g_AskedReconnect = false; /** * Processing of notifications sent by XmppClient.cpp. * * @returns true if the playerlist GUI must be updated. */ var g_NetMessageTypes = { "system": { // Three cases are handled in prelobby.js "registered": msg => false, "connected": msg => { g_AskedReconnect = false; updateConnectedState(); return false; }, "disconnected": msg => { updateGameList(); updateLeaderboard(); updateConnectedState(); if (!g_Kicked) { addChatMessage({ "from": "system", "time": msg.time, "text": translate("Disconnected.") + " " + msg.reason }); reconnectMessageBox(); } return true; }, "error": msg => { addChatMessage({ "from": "system", "time": msg.time, "text": msg.text }); return false; } }, "chat": { "subject": msg => { updateSubject(msg.subject); if (msg.nick) addChatMessage({ "text": "/special " + sprintf(translate("%(nick)s changed the lobby subject to %(subject)s"), { "nick": msg.nick, "subject": msg.subject }), "time": msg.time, "isSpecial": true }); return false; }, "join": msg => { addChatMessage({ "text": "/special " + sprintf(translate("%(nick)s has joined."), { "nick": msg.nick }), "time": msg.time, "isSpecial": true }); return true; }, "leave": msg => { addChatMessage({ "text": "/special " + sprintf(translate("%(nick)s has left."), { "nick": msg.nick }), "time": msg.time, "isSpecial": true }); if (msg.nick == g_Username) Engine.DisconnectXmppClient(); return true; }, "presence": msg => true, "role": msg => { Engine.GetGUIObjectByName("chatInput").hidden = Engine.LobbyGetPlayerRole(g_Username) == "visitor"; let me = g_Username == msg.nick; let newrole = Engine.LobbyGetPlayerRole(msg.nick); let txt = newrole == "visitor" ? me ? translate("You have been muted.") : translate("%(nick)s has been muted.") : newrole == "moderator" ? me ? translate("You are now a moderator.") : translate("%(nick)s is now a moderator.") : msg.oldrole == "visitor" ? me ? translate("You have been unmuted.") : translate("%(nick)s has been unmuted.") : me ? translate("You are not a moderator anymore.") : translate("%(nick)s is not a moderator anymore."); addChatMessage({ "text": "/special " + sprintf(txt, { "nick": msg.nick }), "time": msg.time, "isSpecial": true }); if (g_SelectedPlayer == msg.nick) updateUserRoleText(g_SelectedPlayer); return false; }, "nick": msg => { addChatMessage({ "text": "/special " + sprintf(translate("%(oldnick)s is now known as %(newnick)s."), { "oldnick": msg.oldnick, "newnick": msg.newnick }), "time": msg.time, "isSpecial": true }); return true; }, "kicked": msg => { handleKick(false, msg.nick, msg.reason, msg.time, msg.historic); return true; }, "banned": msg => { handleKick(true, msg.nick, msg.reason, msg.time, msg.historic); return true; }, "room-message": msg => { addChatMessage({ "from": escapeText(msg.from), "text": escapeText(msg.text), "time": msg.time, "historic": msg.historic }); return false; }, "private-message": msg => { // Announcements and the Message of the Day are sent by the server directly if (!msg.from) messageBox( 400, 250, msg.text.trim(), translate("Notice") ); // We intend to not support private messages between users if (!msg.from || Engine.LobbyGetPlayerRole(msg.from) == "moderator") // some XMPP clients send trailing whitespace addChatMessage({ "from": escapeText(msg.from || "system"), "text": escapeText(msg.text.trim()), "time": msg.time, "historic": msg.historic, "private": true }); return false; } }, "game": { "gamelist": msg => { updateGameList(); return false; }, "profile": msg => { updateProfile(); return false; }, "leaderboard": msg => { updateLeaderboard(); return false; }, "ratinglist": msg => { return true; } } }; /** * Commands that can be entered by clients via chat input. * A handler returns true if the user input should be sent as a chat message. */ var g_ChatCommands = { "away": { "description": translate("Set your state to 'Away'."), "handler": args => { Engine.LobbySetPlayerPresence("away"); return false; } }, "back": { "description": translate("Set your state to 'Online'."), "handler": args => { Engine.LobbySetPlayerPresence("available"); return false; } }, "kick": { "description": translate("Kick a specified user from the lobby. Usage: /kick nick reason"), "handler": args => { Engine.LobbyKick(args[0] || "", args[1] || ""); return false; }, "moderatorOnly": true }, "ban": { "description": translate("Ban a specified user from the lobby. Usage: /ban nick reason"), "handler": args => { Engine.LobbyBan(args[0] || "", args[1] || ""); return false; }, "moderatorOnly": true }, "help": { "description": translate("Show this help."), "handler": args => { let isModerator = Engine.LobbyGetPlayerRole(g_Username) == "moderator"; let text = translate("Chat commands:"); for (let command in g_ChatCommands) if (!g_ChatCommands[command].moderatorOnly || isModerator) // Translation: Chat command help format text += "\n" + sprintf(translate("%(command)s - %(description)s"), { "command": coloredText(command, g_ChatCommandColor), "description": g_ChatCommands[command].description }); addChatMessage({ "from": "system", "text": text }); return false; } }, "me": { "description": translate("Send a chat message about yourself. Example: /me goes swimming."), "handler": args => true }, "say": { "description": translate("Send text as a chat message (even if it starts with slash). Example: /say /help is a great command."), "handler": args => true }, "clear": { "description": translate("Clear all chat scrollback."), "handler": args => { clearChatMessages(); return false; } }, "quit": { "description": translate("Return to the main menu."), "handler": args => { leaveLobby(); return false; } } }; /** * Called after the XmppConnection succeeded and when returning from a game. * * @param {Object} attribs */ function init(attribs) { g_Dialog = attribs && attribs.dialog; if (!g_Settings) { leaveLobby(); return; } initMusic(); global.music.setState(global.music.states.MENU); initDialogStyle(); initGameFilters(); updateConnectedState(); Engine.LobbySetPlayerPresence("available"); // When rejoining the lobby after a game, we don't need to process presence changes Engine.LobbyClearPresenceUpdates(); updatePlayerList(); updateSubject(Engine.LobbyGetRoomSubject()); updateLobbyColumns(); updateToggleBuddy(); Engine.GetGUIObjectByName("chatInput").tooltip = colorizeAutocompleteHotkey(); // Get all messages since the login for (let msg of Engine.LobbyGuiPollHistoricMessages()) g_NetMessageTypes[msg.type][msg.level](msg); if (!Engine.IsXmppClientConnected()) reconnectMessageBox(); } function reconnectMessageBox() { if (g_AskedReconnect) return; g_AskedReconnect = true; messageBox( 400, 200, translate("You have been disconnected from the lobby. Do you want to reconnect?"), translate("Confirmation"), [translate("No"), translate("Yes")], [null, Engine.ConnectXmppClient]); } /** * Set style of GUI elements and the window style. */ function initDialogStyle() { let lobbyWindow = Engine.GetGUIObjectByName("lobbyWindow"); lobbyWindow.sprite = g_Dialog ? "ModernDialog" : "ModernWindow"; lobbyWindow.size = g_Dialog ? "42 42 100%-42 100%-42" : "0 0 100% 100%"; Engine.GetGUIObjectByName("lobbyWindowTitle").size = g_Dialog ? "50%-128 -16 50%+128 16" : "50%-128 4 50%+128 36"; Engine.GetGUIObjectByName("leaveButton").caption = g_Dialog ? translateWithContext("previous page", "Back") : translateWithContext("previous page", "Main Menu"); Engine.GetGUIObjectByName("hostButton").hidden = g_Dialog; Engine.GetGUIObjectByName("joinGameButton").hidden = g_Dialog; Engine.GetGUIObjectByName("gameInfoEmpty").size = "0 0 100% 100%-24" + (g_Dialog ? "" : "-30"); Engine.GetGUIObjectByName("gameInfo").size = "0 0 100% 100%-24" + (g_Dialog ? "" : "-60"); Engine.GetGUIObjectByName("middlePanel").size = "20%+5 " + (g_Dialog ? "18" : "40") + " 100%-255 100%-20"; Engine.GetGUIObjectByName("rightPanel").size = "100%-250 " + (g_Dialog ? "18" : "40") + " 100%-20 100%-20"; Engine.GetGUIObjectByName("leftPanel").size = "20 " + (g_Dialog ? "18" : "40") + " 20% 100%-315"; if (g_Dialog) { Engine.GetGUIObjectByName("lobbyDialogToggle").onPress = leaveLobby; Engine.GetGUIObjectByName("cancelDialog").onPress = leaveLobby; } } /** * Set style of GUI elements according to the connection state of the lobby. */ function updateConnectedState() { Engine.GetGUIObjectByName("chatInput").hidden = !Engine.IsXmppClientConnected(); for (let button of ["host", "leaderboard", "userprofile", "toggleBuddy"]) Engine.GetGUIObjectByName(button + "Button").enabled = Engine.IsXmppClientConnected(); } function updateLobbyColumns() { let gameRating = Engine.ConfigDB_GetValue("user", "lobby.columns.gamerating") == "true"; // Only show the selected columns let gamesBox = Engine.GetGUIObjectByName("gamesBox"); gamesBox.hidden_mapType = gameRating; gamesBox.hidden_gameRating = !gameRating; // Only show the filters of selected columns let mapTypeFilter = Engine.GetGUIObjectByName("mapTypeFilter"); mapTypeFilter.hidden = gameRating; let gameRatingFilter = Engine.GetGUIObjectByName("gameRatingFilter"); gameRatingFilter.hidden = !gameRating; // Keep filters right above the according column let playersNumberFilter = Engine.GetGUIObjectByName("playersNumberFilter"); let size = playersNumberFilter.size; size.rleft = gameRating ? 74 : 90; size.rright = gameRating ? 84 : 100; playersNumberFilter.size = size; } function leaveLobby() { if (g_Dialog) { Engine.LobbySetPlayerPresence("playing"); Engine.PopGuiPage(); } else { Engine.StopXmppClient(); Engine.SwitchGuiPage("page_pregame.xml"); } } function initGameFilters() { let mapSizeFilter = Engine.GetGUIObjectByName("mapSizeFilter"); mapSizeFilter.list = [translateWithContext("map size", "Any")].concat(g_MapSizes.Name); mapSizeFilter.list_data = [""].concat(g_MapSizes.Tiles); let playersArray = Array(g_MaxPlayers).fill(0).map((v, i) => i + 1); // 1, 2, ... MaxPlayers let playersNumberFilter = Engine.GetGUIObjectByName("playersNumberFilter"); playersNumberFilter.list = [translateWithContext("player number", "Any")].concat(playersArray); playersNumberFilter.list_data = [""].concat(playersArray); let mapTypeFilter = Engine.GetGUIObjectByName("mapTypeFilter"); mapTypeFilter.list = [translateWithContext("map", "Any")].concat(g_MapTypes.Title); mapTypeFilter.list_data = [""].concat(g_MapTypes.Name); let gameRatingOptions = [">1500", ">1400", ">1300", ">1200", "<1200", "<1100", "<1000"]; gameRatingOptions = prepareForDropdown(gameRatingOptions.map(r => ({ "value": r, "label": sprintf( r[0] == ">" ? translateWithContext("gamelist filter", "> %(rating)s") : translateWithContext("gamelist filter", "< %(rating)s"), { "rating": r.substr(1) }) }))); let gameRatingFilter = Engine.GetGUIObjectByName("gameRatingFilter"); gameRatingFilter.list = [translateWithContext("map", "Any")].concat(gameRatingOptions.label); gameRatingFilter.list_data = [""].concat(gameRatingOptions.value); resetFilters(); } function resetFilters() { Engine.GetGUIObjectByName("mapSizeFilter").selected = 0; Engine.GetGUIObjectByName("playersNumberFilter").selected = 0; Engine.GetGUIObjectByName("mapTypeFilter").selected = g_MapTypes.Default; Engine.GetGUIObjectByName("gameRatingFilter").selected = 0; Engine.GetGUIObjectByName("filterOpenGames").checked = false; applyFilters(); } function applyFilters() { updateGameList(); updateGameSelection(); } /** * Filter a game based on the status of the filter dropdowns. * * @param {Object} game * @returns {boolean} - True if game should not be displayed. */ function filterGame(game) { let mapSizeFilter = Engine.GetGUIObjectByName("mapSizeFilter"); let playersNumberFilter = Engine.GetGUIObjectByName("playersNumberFilter"); let mapTypeFilter = Engine.GetGUIObjectByName("mapTypeFilter"); let gameRatingFilter = Engine.GetGUIObjectByName("gameRatingFilter"); let filterOpenGames = Engine.GetGUIObjectByName("filterOpenGames"); // We assume index 0 means display all for any given filter. if (mapSizeFilter.selected != 0 && game.mapSize != mapSizeFilter.list_data[mapSizeFilter.selected]) return true; if (playersNumberFilter.selected != 0 && game.maxnbp != playersNumberFilter.list_data[playersNumberFilter.selected]) return true; if (mapTypeFilter.selected != 0 && game.mapType != mapTypeFilter.list_data[mapTypeFilter.selected]) return true; if (filterOpenGames.checked && (game.nbp >= game.maxnbp || game.state != "init")) return true; if (gameRatingFilter.selected > 0) { let selected = gameRatingFilter.list_data[gameRatingFilter.selected]; if (selected.startsWith(">") && +selected.substr(1) >= game.gameRating || selected.startsWith("<") && +selected.substr(1) <= game.gameRating) return true; } return false; } function handleKick(banned, nick, reason, time, historic) { let kickString = nick == g_Username ? banned ? translate("You have been banned from the lobby!") : translate("You have been kicked from the lobby!") : banned ? translate("%(nick)s has been banned from the lobby.") : translate("%(nick)s has been kicked from the lobby."); if (reason) reason = sprintf(translateWithContext("lobby kick", "Reason: %(reason)s"), { "reason": reason }); if (nick != g_Username) { addChatMessage({ "text": "/special " + sprintf(kickString, { "nick": nick }) + " " + reason, "time": time, "historic": historic, "isSpecial": true }); return; } addChatMessage({ "from": "system", "time": time, "text": kickString + " " + reason, }); g_Kicked = true; Engine.DisconnectXmppClient(); messageBox( 400, 250, kickString + "\n" + reason, banned ? translate("BANNED") : translate("KICKED") ); } /** * Update the subject GUI object. */ function updateSubject(newSubject) { Engine.GetGUIObjectByName("subject").caption = newSubject; // If the subject is only whitespace, hide it and reposition the logo. let subjectBox = Engine.GetGUIObjectByName("subjectBox"); subjectBox.hidden = !newSubject.trim(); let logo = Engine.GetGUIObjectByName("logo"); if (subjectBox.hidden) logo.size = "50%-110 50%-50 50%+110 50%+50"; else logo.size = "50%-110 40 50%+110 140"; } /** * Update the caption of the toggle buddy button. */ function updateToggleBuddy() { let playerList = Engine.GetGUIObjectByName("playersBox"); let playerName = playerList.list[playerList.selected]; let toggleBuddyButton = Engine.GetGUIObjectByName("toggleBuddyButton"); toggleBuddyButton.caption = g_Buddies.indexOf(playerName) != -1 ? translate("Unmark as Buddy") : translate("Mark as Buddy"); toggleBuddyButton.enabled = playerName && playerName != g_Username; } /** * Do a full update of the player listing, including ratings from cached C++ information. */ function updatePlayerList() { let playersBox = Engine.GetGUIObjectByName("playersBox"); let sortBy = playersBox.selected_column || "name"; let sortOrder = playersBox.selected_column_order || 1; let buddyStatusList = []; let playerList = []; let presenceList = []; let nickList = []; let ratingList = []; let cleanPlayerList = Engine.GetPlayerList().map(player => { player.isBuddy = g_Buddies.indexOf(player.name) != -1; return player; }).sort((a, b) => { let sortA, sortB; let statusOrder = Object.keys(g_PlayerStatuses); let statusA = statusOrder.indexOf(a.presence) + a.name.toLowerCase(); let statusB = statusOrder.indexOf(b.presence) + b.name.toLowerCase(); switch (sortBy) { case 'buddy': sortA = (a.isBuddy ? 1 : 2) + statusA; sortB = (b.isBuddy ? 1 : 2) + statusB; break; case 'rating': sortA = +a.rating; sortB = +b.rating; break; case 'status': sortA = statusA; sortB = statusB; break; case 'name': default: sortA = a.name.toLowerCase(); sortB = b.name.toLowerCase(); break; } if (sortA < sortB) return -sortOrder; if (sortA > sortB) return +sortOrder; return 0; }); // Colorize list entries for (let player of cleanPlayerList) { if (player.rating && player.name == g_Username) g_UserRating = player.rating; let rating = player.rating ? (" " + player.rating).substr(-5) : " -"; let presence = g_PlayerStatuses[player.presence] ? player.presence : "unknown"; if (presence == "unknown") warn("Unknown presence:" + player.presence); let statusColor = g_PlayerStatuses[presence].color; buddyStatusList.push(player.isBuddy ? coloredText(g_BuddySymbol, statusColor) : ""); playerList.push(colorPlayerName((player.role == "moderator" ? g_ModeratorPrefix : "") + player.name)); presenceList.push(coloredText(g_PlayerStatuses[presence].status, statusColor)); ratingList.push(coloredText(rating, statusColor)); nickList.push(player.name); } playersBox.list_buddy = buddyStatusList; playersBox.list_name = playerList; playersBox.list_status = presenceList; playersBox.list_rating = ratingList; playersBox.list = nickList; playersBox.selected = playersBox.list.indexOf(g_SelectedPlayer); } /** * Toggle buddy state for a player in playerlist within the user config */ function toggleBuddy() { let playerList = Engine.GetGUIObjectByName("playersBox"); let name = playerList.list[playerList.selected]; if (!name || name == g_Username || name.indexOf(g_BuddyListDelimiter) != -1) return; let index = g_Buddies.indexOf(name); if (index != -1) g_Buddies.splice(index, 1); else g_Buddies.push(name); updateToggleBuddy(); saveSettingAndWriteToUserConfig("lobby.buddies", g_Buddies.filter(nick => nick).join(g_BuddyListDelimiter) || g_BuddyListDelimiter); updatePlayerList(); updateGameList(); } /** * Select the game where the selected player is currently playing, observing or offline. * Selects in that order to account for players that occur in multiple games. */ function selectGameFromPlayername() { if (!g_SelectedPlayer) return; let gameList = Engine.GetGUIObjectByName("gamesBox"); let foundAsObserver = false; for (let i = 0; i < g_GameList.length; ++i) for (let player of stringifiedTeamListToPlayerData(g_GameList[i].players)) { if (g_SelectedPlayer != splitRatingFromNick(player.Name).nick) continue; gameList.auto_scroll = true; if (player.Team == "observer") { foundAsObserver = true; gameList.selected = i; } else if (!player.Offline) { gameList.selected = i; return; } else if (!foundAsObserver) gameList.selected = i; } } function onPlayerListSelection() { let playerList = Engine.GetGUIObjectByName("playersBox"); if (playerList.selected == playerList.list.indexOf(g_SelectedPlayer)) return; g_SelectedPlayer = playerList.list[playerList.selected]; lookupSelectedUserProfile("playersBox"); updateToggleBuddy(); selectGameFromPlayername(); } function setLeaderboardVisibility(visible) { if (visible) Engine.SendGetBoardList(); lookupSelectedUserProfile(visible ? "leaderboardBox" : "playersBox"); Engine.GetGUIObjectByName("leaderboard").hidden = !visible; Engine.GetGUIObjectByName("fade").hidden = !visible; } function setUserProfileVisibility(visible) { Engine.GetGUIObjectByName("profileFetch").hidden = !visible; Engine.GetGUIObjectByName("fade").hidden = !visible; } /** * Display the profile of the player in the user profile window. */ function lookupUserProfile() { Engine.SendGetProfile(Engine.GetGUIObjectByName("fetchInput").caption); } /** * Display the profile of the selected player in the main window. * Displays N/A for all stats until updateProfile is called when the stats * are actually received from the bot. */ function lookupSelectedUserProfile(guiObjectName) { let playerList = Engine.GetGUIObjectByName(guiObjectName); let playerName = playerList.list[playerList.selected]; Engine.GetGUIObjectByName("profileArea").hidden = !playerName && !Engine.GetGUIObjectByName("usernameText").caption; if (!playerName) return; Engine.SendGetProfile(playerName); Engine.GetGUIObjectByName("usernameText").caption = playerName; Engine.GetGUIObjectByName("rankText").caption = translate("N/A"); Engine.GetGUIObjectByName("highestRatingText").caption = translate("N/A"); Engine.GetGUIObjectByName("totalGamesText").caption = translate("N/A"); Engine.GetGUIObjectByName("winsText").caption = translate("N/A"); Engine.GetGUIObjectByName("lossesText").caption = translate("N/A"); Engine.GetGUIObjectByName("ratioText").caption = translate("N/A"); updateUserRoleText(playerName); } function updateUserRoleText(playerName) { Engine.GetGUIObjectByName("roleText").caption = g_RoleNames[Engine.LobbyGetPlayerRole(playerName) || "participant"]; } /** * Update the profile of the selected player with data from the bot. */ function updateProfile() { let attributes = Engine.GetProfile()[0]; let user = colorPlayerName(attributes.player, attributes.rating); if (!Engine.GetGUIObjectByName("profileFetch").hidden) { let profileFound = attributes.rating != "-2"; Engine.GetGUIObjectByName("profileWindowArea").hidden = !profileFound; Engine.GetGUIObjectByName("profileErrorText").hidden = profileFound; if (!profileFound) { Engine.GetGUIObjectByName("profileErrorText").caption = sprintf( translate("Player \"%(nick)s\" not found."), { "nick": attributes.player } ); return; } Engine.GetGUIObjectByName("profileUsernameText").caption = user; Engine.GetGUIObjectByName("profileRankText").caption = attributes.rank; Engine.GetGUIObjectByName("profileHighestRatingText").caption = attributes.highestRating; Engine.GetGUIObjectByName("profileTotalGamesText").caption = attributes.totalGamesPlayed; Engine.GetGUIObjectByName("profileWinsText").caption = attributes.wins; Engine.GetGUIObjectByName("profileLossesText").caption = attributes.losses; Engine.GetGUIObjectByName("profileRatioText").caption = formatWinRate(attributes); return; } let playerList; if (!Engine.GetGUIObjectByName("leaderboard").hidden) playerList = Engine.GetGUIObjectByName("leaderboardBox"); else playerList = Engine.GetGUIObjectByName("playersBox"); if (attributes.rating == "-2") return; // Make sure the stats we have received coincide with the selected player. if (attributes.player != playerList.list[playerList.selected]) return; Engine.GetGUIObjectByName("usernameText").caption = user; Engine.GetGUIObjectByName("rankText").caption = attributes.rank; Engine.GetGUIObjectByName("highestRatingText").caption = attributes.highestRating; Engine.GetGUIObjectByName("totalGamesText").caption = attributes.totalGamesPlayed; Engine.GetGUIObjectByName("winsText").caption = attributes.wins; Engine.GetGUIObjectByName("lossesText").caption = attributes.losses; Engine.GetGUIObjectByName("ratioText").caption = formatWinRate(attributes); } /** * Update the leaderboard from data cached in C++. */ function updateLeaderboard() { let leaderboard = Engine.GetGUIObjectByName("leaderboardBox"); let boardList = Engine.GetBoardList().sort((a, b) => b.rating - a.rating); let list = []; let list_name = []; let list_rank = []; let list_rating = []; for (let i in boardList) { list_name.push(boardList[i].name); list_rating.push(boardList[i].rating); list_rank.push(+i + 1); list.push(boardList[i].name); } leaderboard.list_name = list_name; leaderboard.list_rating = list_rating; leaderboard.list_rank = list_rank; leaderboard.list = list; if (leaderboard.selected >= leaderboard.list.length) leaderboard.selected = -1; } /** * Update the game listing from data cached in C++. */ function updateGameList() { let gamesBox = Engine.GetGUIObjectByName("gamesBox"); let sortBy = gamesBox.selected_column; let sortOrder = gamesBox.selected_column_order; if (gamesBox.selected > -1) { g_SelectedGameIP = g_GameList[gamesBox.selected].ip; g_SelectedGamePort = g_GameList[gamesBox.selected].port; } g_GameList = Engine.GetGameList().map(game => { game.hasBuddies = 0; // Compute average rating of participating players let playerRatings = []; for (let player of stringifiedTeamListToPlayerData(game.players)) { let playerNickRating = splitRatingFromNick(player.Name); if (player.Team != "observer") playerRatings.push(playerNickRating.rating || g_DefaultLobbyRating); // Sort games with playing buddies above games with spectating buddies if (game.hasBuddies < 2 && g_Buddies.indexOf(playerNickRating.nick) != -1) game.hasBuddies = player.Team == "observer" ? 1 : 2; } game.gameRating = playerRatings.length ? Math.round(playerRatings.reduce((sum, current) => sum + current) / playerRatings.length) : g_DefaultLobbyRating; + if (!hasSameMods(JSON.parse(game.mods), Engine.GetEngineInfo().mods)) + game.state = "incompatible"; + return game; }).filter(game => !filterGame(game)).sort((a, b) => { let sortA, sortB; switch (sortBy) { case 'name': sortA = g_GameStatusOrder.indexOf(a.state) + a.name.toLowerCase(); sortB = g_GameStatusOrder.indexOf(b.state) + b.name.toLowerCase(); break; case 'gameRating': case 'mapSize': case 'mapType': sortA = a[sortBy]; sortB = b[sortBy]; break; case 'buddy': sortA = String(b.hasBuddies) + g_GameStatusOrder.indexOf(a.state) + a.name.toLowerCase(); sortB = String(a.hasBuddies) + g_GameStatusOrder.indexOf(b.state) + b.name.toLowerCase(); break; case 'mapName': sortA = translate(a.niceMapName); sortB = translate(b.niceMapName); break; case 'nPlayers': sortA = a.maxnbp; sortB = b.maxnbp; break; } if (sortA < sortB) return -sortOrder; if (sortA > sortB) return +sortOrder; return 0; }); let list_buddy = []; let list_name = []; let list_mapName = []; let list_mapSize = []; let list_mapType = []; let list_nPlayers = []; let list_gameRating = []; let list = []; let list_data = []; let selectedGameIndex = -1; for (let i in g_GameList) { let game = g_GameList[i]; let gameName = escapeText(game.name); let mapTypeIdx = g_MapTypes.Name.indexOf(game.mapType); if (game.ip == g_SelectedGameIP && game.port == g_SelectedGamePort) selectedGameIndex = +i; list_buddy.push(game.hasBuddies ? coloredText(g_BuddySymbol, g_GameColors[game.state]) : ""); list_name.push(coloredText(gameName, g_GameColors[game.state])); list_mapName.push(translateMapTitle(game.niceMapName)); list_mapSize.push(translateMapSize(game.mapSize)); list_mapType.push(g_MapTypes.Title[mapTypeIdx] || ""); list_nPlayers.push(game.nbp + "/" + game.maxnbp); list_gameRating.push(game.gameRating); list.push(gameName); list_data.push(i); } gamesBox.list_buddy = list_buddy; gamesBox.list_name = list_name; gamesBox.list_mapName = list_mapName; gamesBox.list_mapSize = list_mapSize; gamesBox.list_mapType = list_mapType; gamesBox.list_nPlayers = list_nPlayers; gamesBox.list_gameRating = list_gameRating; // Change these last, otherwise crash gamesBox.list = list; gamesBox.list_data = list_data; gamesBox.auto_scroll = false; gamesBox.selected = selectedGameIndex; updateGameSelection(); } /** * Populate the game info area with information on the current game selection. */ function updateGameSelection() { let game = selectedGame(); Engine.GetGUIObjectByName("gameInfo").hidden = !game; Engine.GetGUIObjectByName("joinGameButton").hidden = g_Dialog || !game; Engine.GetGUIObjectByName("gameInfoEmpty").hidden = game; if (!game) return; Engine.GetGUIObjectByName("sgMapName").caption = translateMapTitle(game.niceMapName); let sgGameStartTime = Engine.GetGUIObjectByName("sgGameStartTime"); let sgNbPlayers = Engine.GetGUIObjectByName("sgNbPlayers"); let sgPlayersNames = Engine.GetGUIObjectByName("sgPlayersNames"); let playersNamesSize = sgPlayersNames.size; playersNamesSize.top = game.startTime ? sgGameStartTime.size.bottom : sgNbPlayers.size.bottom; playersNamesSize.rtop = game.startTime ? sgGameStartTime.size.rbottom : sgNbPlayers.size.rbottom; sgPlayersNames.size = playersNamesSize; sgGameStartTime.hidden = !game.startTime; if (game.startTime) sgGameStartTime.caption = sprintf( // Translation: %(time)s is the hour and minute here. translate("Game started at %(time)s"), { "time": Engine.FormatMillisecondsIntoDateStringLocal(+game.startTime * 1000, translate("HH:mm")) }); sgNbPlayers.caption = sprintf( translate("Players: %(current)s/%(total)s"), { "current": game.nbp, "total": game.maxnbp }); sgPlayersNames.caption = formatPlayerInfo(stringifiedTeamListToPlayerData(game.players)); Engine.GetGUIObjectByName("sgMapSize").caption = translateMapSize(game.mapSize); let mapTypeIdx = g_MapTypes.Name.indexOf(game.mapType); Engine.GetGUIObjectByName("sgMapType").caption = g_MapTypes.Title[mapTypeIdx] || ""; let mapData = getMapDescriptionAndPreview(game.mapType, game.mapName); Engine.GetGUIObjectByName("sgMapDescription").caption = mapData.description; setMapPreviewImage("sgMapPreview", mapData.preview); } function selectedGame() { let gamesBox = Engine.GetGUIObjectByName("gamesBox"); if (gamesBox.selected < 0) return undefined; return g_GameList[gamesBox.list_data[gamesBox.selected]]; } /** * Immediately rejoin and join gamesetups. Otherwise confirm late-observer join attempt. */ function joinButton() { let game = selectedGame(); if (!game || g_Dialog) return; let rating = getRejoinRating(game); let username = rating ? g_Username + " (" + rating + ")" : g_Username; - if (game.state == "init" || stringifiedTeamListToPlayerData(game.players).some(player => player.Name == username)) + if (game.state == "incompatible") + messageBox( + 400, 200, + translate("Your active mods do not match the mods of this game.") + "\n\n" + + comparedModsString(JSON.parse(game.mods), Engine.GetEngineInfo().mods), + translate("Incompatible mods"), + [translate("Ok")], + [null] + ); + else if (game.state == "init" || stringifiedTeamListToPlayerData(game.players).some(player => player.Name == username)) joinSelectedGame(); else messageBox( 400, 200, translate("The game has already started. Do you want to join as observer?"), translate("Confirmation"), [translate("No"), translate("Yes")], [null, joinSelectedGame] ); } /** * Attempt to join the selected game without asking for confirmation. */ function joinSelectedGame() { let game = selectedGame(); if (!game) return; let ip; let port; if (game.stunIP) { ip = game.stunIP; port = game.stunPort; } else { ip = game.ip; port = game.port; } if (ip.split('.').length != 4) { addChatMessage({ "from": "system", "text": sprintf( translate("This game's address '%(ip)s' does not appear to be valid."), { "ip": game.ip } ) }); return; } Engine.PushGuiPage("page_gamesetup_mp.xml", { "multiplayerGameType": "join", "ip": ip, "port": port, "name": g_Username, "rating": getRejoinRating(game), "useSTUN": !!game.stunIP, "hostJID": game.hostUsername + "@" + g_LobbyServer + "/0ad" }); } /** * Rejoin games with the original playername, even if the rating changed meanwhile. */ function getRejoinRating(game) { for (let player of stringifiedTeamListToPlayerData(game.players)) { let playerNickRating = splitRatingFromNick(player.Name); if (playerNickRating.nick == g_Username) return playerNickRating.rating; } return g_UserRating; } /** * Open the dialog box to enter the game name. */ function hostGame() { Engine.PushGuiPage("page_gamesetup_mp.xml", { "multiplayerGameType": "host", "name": g_Username, "rating": g_UserRating }); } /** * Processes GUI messages sent by the XmppClient. */ function onTick() { updateTimers(); let updateList = false; while (true) { let msg = Engine.LobbyGuiPollNewMessage(); if (!msg) break; if (!g_NetMessageTypes[msg.type]) { warn("Unrecognised message type: " + msg.type); continue; } if (!g_NetMessageTypes[msg.type][msg.level]) { warn("Unrecognised message level: " + msg.level); continue; } if (g_NetMessageTypes[msg.type][msg.level](msg)) updateList = true; } // To improve performance, only update the playerlist GUI when // the last update in the current stack is processed if (updateList) updatePlayerList(); } /** * Executes a lobby command or sends GUI input directly as chat. */ function submitChatInput() { let input = Engine.GetGUIObjectByName("chatInput"); let text = input.caption; if (!text.length) return; if (handleChatCommand(text)) Engine.LobbySendMessage(text); input.caption = ""; } /** * Handle all '/' commands. * * @param {string} text - Text to be checked for commands. * @returns {boolean} true if the text should be sent via chat. */ function handleChatCommand(text) { if (text[0] != '/') return true; let [cmd, args] = ircSplit(text); args = ircSplit("/" + args); if (!g_ChatCommands[cmd]) { addChatMessage({ "from": "system", "text": sprintf( translate("The command '%(cmd)s' is not supported."), { "cmd": coloredText(cmd, g_ChatCommandColor) }) }); return false; } if (g_ChatCommands[cmd].moderatorOnly && Engine.LobbyGetPlayerRole(g_Username) != "moderator") { addChatMessage({ "from": "system", "text": sprintf( translate("The command '%(cmd)s' is restricted to moderators."), { "cmd": coloredText(cmd, g_ChatCommandColor) }) }); return false; } return g_ChatCommands[cmd].handler(args); } /** * Process and if appropriate, display a formatted message. * * @param {Object} msg - The message to be processed. */ function addChatMessage(msg) { if (msg.from) { if (Engine.LobbyGetPlayerRole(msg.from) == "moderator") msg.from = g_ModeratorPrefix + msg.from; // Highlight local user's nick if (g_Username != msg.from) { msg.text = msg.text.replace(g_Username, colorPlayerName(g_Username)); if (!msg.historic && msg.text.toLowerCase().indexOf(g_Username.toLowerCase()) != -1) soundNotification("nick"); } } let formatted = ircFormat(msg); if (!formatted) return; g_ChatMessages.push(formatted); Engine.GetGUIObjectByName("chatText").caption = g_ChatMessages.join("\n"); } /** * Splits given input into command and argument. */ function ircSplit(string) { let idx = string.indexOf(' '); if (idx != -1) return [string.substr(1, idx - 1), string.substr(idx + 1)]; return [string.substr(1), ""]; } /** * Format text in an IRC-like way. * * @param {Object} msg - Received chat message. * @returns {string} - Formatted text. */ function ircFormat(msg) { let formattedMessage = ""; let coloredFrom = msg.from && colorPlayerName(msg.from); // Handle commands allowed past handleChatCommand. if (msg.text[0] == '/') { let [command, message] = ircSplit(msg.text); switch (command) { case "me": { // Translation: IRC message prefix when the sender uses the /me command. let senderString = sprintf(translate("* %(sender)s"), { "sender": coloredFrom }); // Translation: IRC message issued using the ‘/me’ command. formattedMessage = sprintf(translate("%(sender)s %(action)s"), { "sender": senderFont(senderString), "action": message }); break; } case "say": { // Translation: IRC message prefix. let senderString = sprintf(translate("<%(sender)s>"), { "sender": coloredFrom }); // Translation: IRC message. formattedMessage = sprintf(translate("%(sender)s %(message)s"), { "sender": senderFont(senderString), "message": message }); break; } case "special": { if (msg.isSpecial) // Translation: IRC system message. formattedMessage = senderFont(sprintf(translate("== %(message)s"), { "message": message })); else { // Translation: IRC message prefix. let senderString = sprintf(translate("<%(sender)s>"), { "sender": coloredFrom }); // Translation: IRC message. formattedMessage = sprintf(translate("%(sender)s %(message)s"), { "sender": senderFont(senderString), "message": message }); } break; } default: return ""; } } else { let senderString; // Translation: IRC message prefix. if (msg.private) senderString = sprintf(translateWithContext("lobby private message", "(%(private)s) <%(sender)s>"), { "private": coloredText(translate("Private"), g_PrivateMessageColor), "sender": coloredFrom }); else senderString = sprintf(translate("<%(sender)s>"), { "sender": coloredFrom }); // Translation: IRC message. formattedMessage = sprintf(translate("%(sender)s %(message)s"), { "sender": senderFont(senderString), "message": msg.text }); } // Add chat message timestamp if (Engine.ConfigDB_GetValue("user", "chat.timestamp") != "true") return formattedMessage; // Translation: Time as shown in the multiplayer lobby (when you enable it in the options page). // For a list of symbols that you can use, see: // https://sites.google.com/site/icuprojectuserguide/formatparse/datetime?pli=1#TOC-Date-Field-Symbol-Table let timeString = Engine.FormatMillisecondsIntoDateStringLocal(msg.time ? msg.time * 1000 : Date.now(), translate("HH:mm")); // Translation: Time prefix as shown in the multiplayer lobby (when you enable it in the options page). let timePrefixString = sprintf(translate("\\[%(time)s]"), { "time": timeString }); // Translation: IRC message format when there is a time prefix. return sprintf(translate("%(time)s %(message)s"), { "time": timePrefixString, "message": formattedMessage }); } /** * Generate a (mostly) unique color for this player based on their name. * @see http://stackoverflow.com/questions/3426404/create-a-hexadecimal-colour-based-on-a-string-with-jquery-javascript * @param {string} playername */ function getPlayerColor(playername) { if (playername == "system") return g_SystemColor; // Generate a probably-unique hash for the player name and use that to create a color. let hash = 0; for (let i in playername) hash = playername.charCodeAt(i) + ((hash << 5) - hash); // First create the color in RGB then HSL, clamp the lightness so it's not too dark to read, and then convert back to RGB to display. // The reason for this roundabout method is this algorithm can generate values from 0 to 255 for RGB but only 0 to 100 for HSL; this gives // us much more variety if we generate in RGB. Unfortunately, enforcing that RGB values are a certain lightness is very difficult, so // we convert to HSL to do the computation. Since our GUI code only displays RGB colors, we have to convert back. let [h, s, l] = rgbToHsl(hash >> 24 & 0xFF, hash >> 16 & 0xFF, hash >> 8 & 0xFF); return hslToRgb(h, s, Math.max(0.7, l)).join(" "); } /** * Returns the given playername wrapped in an appropriate color-tag. * * @param {string} playername * @param {string} rating */ function colorPlayerName(playername, rating) { return coloredText( (rating ? sprintf( translate("%(nick)s (%(rating)s)"), { "nick": playername, "rating": rating }) : playername ), getPlayerColor(playername.replace(g_ModeratorPrefix, ""))); } function senderFont(text) { return '[font="' + g_SenderFont + '"]' + text + "[/font]"; } function formatWinRate(attr) { if (!attr.totalGamesPlayed) return translateWithContext("Used for an undefined winning rate", "-"); return sprintf(translate("%(percentage)s%%"), { "percentage": (attr.wins / attr.totalGamesPlayed * 100).toFixed(2) }); } Index: ps/trunk/source/lobby/XmppClient.cpp =================================================================== --- ps/trunk/source/lobby/XmppClient.cpp (revision 21300) +++ ps/trunk/source/lobby/XmppClient.cpp (revision 21301) @@ -1,1151 +1,1153 @@ /* Copyright (C) 2018 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "XmppClient.h" #include "StanzaExtensions.h" #ifdef WIN32 # include #endif #include "i18n/L10n.h" #include "lib/external_libraries/enet.h" #include "lib/utf8.h" #include "network/NetServer.h" #include "network/StunClient.h" #include "ps/CLogger.h" #include "ps/ConfigDB.h" #include "ps/Pyrogenesis.h" #include "scriptinterface/ScriptInterface.h" //debug #if 1 #define DbgXMPP(x) #else #define DbgXMPP(x) std::cout << x << std::endl; static std::string tag_xml(const glooxwrapper::IQ& iq) { std::string ret; glooxwrapper::Tag* tag = iq.tag(); ret = tag->xml().to_string(); glooxwrapper::Tag::free(tag); return ret; } #endif static std::string tag_name(const glooxwrapper::IQ& iq) { std::string ret; glooxwrapper::Tag* tag = iq.tag(); ret = tag->name().to_string(); glooxwrapper::Tag::free(tag); return ret; } IXmppClient* IXmppClient::create(const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, const int historyRequestSize,bool regOpt) { return new XmppClient(sUsername, sPassword, sRoom, sNick, historyRequestSize, regOpt); } /** * Construct the XMPP client. * * @param sUsername Username to login with of register. * @param sPassword Password to login with or register. * @param sRoom MUC room to join. * @param sNick Nick to join with. * @param historyRequestSize Number of stanzas of room history to request. * @param regOpt If we are just registering or not. */ XmppClient::XmppClient(const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, const int historyRequestSize, bool regOpt) : m_client(NULL), m_mucRoom(NULL), m_registration(NULL), m_username(sUsername), m_password(sPassword), m_nick(sNick), m_initialLoadComplete(false), m_isConnected(false), m_sessionManager() { // Read lobby configuration from default.cfg std::string sServer; std::string sXpartamupp; CFG_GET_VAL("lobby.server", sServer); CFG_GET_VAL("lobby.xpartamupp", sXpartamupp); m_xpartamuppId = sXpartamupp + "@" + sServer + "/CC"; glooxwrapper::JID clientJid(sUsername + "@" + sServer + "/0ad"); glooxwrapper::JID roomJid(sRoom + "@conference." + sServer + "/" + sNick); // If we are connecting, use the full jid and a password // If we are registering, only use the server name if (!regOpt) m_client = new glooxwrapper::Client(clientJid, sPassword); else m_client = new glooxwrapper::Client(sServer); // Disable TLS as we haven't set a certificate on the server yet m_client->setTls(gloox::TLSDisabled); // Disable use of the SASL PLAIN mechanism, to prevent leaking credentials // if the server doesn't list any supported SASL mechanism or the response // has been modified to exclude those. const int mechs = gloox::SaslMechAll ^ gloox::SaslMechPlain; m_client->setSASLMechanisms(mechs); m_client->registerConnectionListener(this); m_client->setPresence(gloox::Presence::Available, -1); m_client->disco()->setVersion("Pyrogenesis", engine_version); m_client->disco()->setIdentity("client", "bot"); m_client->setCompression(false); m_client->registerStanzaExtension(new GameListQuery()); m_client->registerIqHandler(this, EXTGAMELISTQUERY); m_client->registerStanzaExtension(new BoardListQuery()); m_client->registerIqHandler(this, EXTBOARDLISTQUERY); m_client->registerStanzaExtension(new ProfileQuery()); m_client->registerIqHandler(this, EXTPROFILEQUERY); m_client->registerMessageHandler(this); // Uncomment to see the raw stanzas //m_client->getWrapped()->logInstance().registerLogHandler( gloox::LogLevelDebug, gloox::LogAreaAll, this ); if (!regOpt) { // Create a Multi User Chat Room m_mucRoom = new glooxwrapper::MUCRoom(m_client, roomJid, this, 0); // Get room history. m_mucRoom->setRequestHistory(historyRequestSize, gloox::MUCRoom::HistoryMaxStanzas); } else { // Registration m_registration = new glooxwrapper::Registration(m_client); m_registration->registerRegistrationHandler(this); } m_sessionManager = new glooxwrapper::SessionManager(m_client, this); // Register plugins to allow gloox parse them in incoming sessions m_sessionManager->registerPlugins(); } /** * Destroy the xmpp client */ XmppClient::~XmppClient() { DbgXMPP("XmppClient destroyed"); delete m_registration; delete m_mucRoom; // Workaround for memory leak in gloox 1.0/1.0.1 m_client->removePresenceExtension(gloox::ExtCaps); delete m_client; for (const glooxwrapper::Tag* const& t : m_GameList) glooxwrapper::Tag::free(t); for (const glooxwrapper::Tag* const& t : m_BoardList) glooxwrapper::Tag::free(t); for (const glooxwrapper::Tag* const& t : m_Profile) glooxwrapper::Tag::free(t); } /// Network void XmppClient::connect() { m_initialLoadComplete = false; m_client->connect(false); } void XmppClient::disconnect() { m_client->disconnect(); } bool XmppClient::isConnected() { return m_isConnected; } void XmppClient::recv() { m_client->recv(1); } /** * Log (debug) Handler */ void XmppClient::handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message) { std::cout << "log: level: " << level << ", area: " << area << ", message: " << message << std::endl; } /***************************************************** * Connection handlers * *****************************************************/ /** * Handle connection */ void XmppClient::onConnect() { if (m_mucRoom) { m_isConnected = true; CreateGUIMessage("system", "connected"); m_mucRoom->join(); } if (m_registration) m_registration->fetchRegistrationFields(); } /** * Handle disconnection */ void XmppClient::onDisconnect(gloox::ConnectionError error) { // Make sure we properly leave the room so that // everything works if we decide to come back later if (m_mucRoom) m_mucRoom->leave(); // Clear game, board and player lists. for (const glooxwrapper::Tag* const& t : m_GameList) glooxwrapper::Tag::free(t); for (const glooxwrapper::Tag* const& t : m_BoardList) glooxwrapper::Tag::free(t); for (const glooxwrapper::Tag* const& t : m_Profile) glooxwrapper::Tag::free(t); m_BoardList.clear(); m_GameList.clear(); m_PlayerMap.clear(); m_Profile.clear(); m_HistoricGuiMessages.clear(); m_isConnected = false; CreateGUIMessage("system", "disconnected", "reason", ConnectionErrorToString(error)); } /** * Handle TLS connection */ bool XmppClient::onTLSConnect(const glooxwrapper::CertInfo& info) { UNUSED2(info); DbgXMPP("onTLSConnect"); DbgXMPP( "status: " << info.status << "\nissuer: " << info.issuer << "\npeer: " << info.server << "\nprotocol: " << info.protocol << "\nmac: " << info.mac << "\ncipher: " << info.cipher << "\ncompression: " << info.compression ); return true; } /** * Handle MUC room errors */ void XmppClient::handleMUCError(glooxwrapper::MUCRoom*, gloox::StanzaError err) { CreateGUIMessage("system", "error", "text", StanzaErrorToString(err)); } /***************************************************** * Requests to server * *****************************************************/ /** * Request the leaderboard data from the server. */ void XmppClient::SendIqGetBoardList() { glooxwrapper::JID xpartamuppJid(m_xpartamuppId); // Send IQ BoardListQuery* b = new BoardListQuery(); b->m_Command = "getleaderboard"; glooxwrapper::IQ iq(gloox::IQ::Get, xpartamuppJid, m_client->getID()); iq.addExtension(b); DbgXMPP("SendIqGetBoardList [" << tag_xml(iq) << "]"); m_client->send(iq); } /** * Request the profile data from the server. */ void XmppClient::SendIqGetProfile(const std::string& player) { glooxwrapper::JID xpartamuppJid(m_xpartamuppId); // Send IQ ProfileQuery* b = new ProfileQuery(); b->m_Command = player; glooxwrapper::IQ iq(gloox::IQ::Get, xpartamuppJid, m_client->getID()); iq.addExtension(b); DbgXMPP("SendIqGetProfile [" << tag_xml(iq) << "]"); m_client->send(iq); } /** * Send game report containing numerous game properties to the server. * * @param data A JS array of game statistics */ void XmppClient::SendIqGameReport(const ScriptInterface& scriptInterface, JS::HandleValue data) { glooxwrapper::JID xpartamuppJid(m_xpartamuppId); // Setup some base stanza attributes GameReport* game = new GameReport(); glooxwrapper::Tag* report = glooxwrapper::Tag::allocate("game"); // Iterate through all the properties reported and add them to the stanza. std::vector properties; scriptInterface.EnumeratePropertyNamesWithPrefix(data, "", properties); for (const std::string& p : properties) { std::wstring value; scriptInterface.GetProperty(data, p.c_str(), value); report->addAttribute(p, utf8_from_wstring(value)); } // Add stanza to IQ game->m_GameReport.emplace_back(report); // Send IQ glooxwrapper::IQ iq(gloox::IQ::Set, xpartamuppJid, m_client->getID()); iq.addExtension(game); DbgXMPP("SendGameReport [" << tag_xml(iq) << "]"); m_client->send(iq); }; /** * Send a request to register a game to the server. * * @param data A JS array of game attributes */ void XmppClient::SendIqRegisterGame(const ScriptInterface& scriptInterface, JS::HandleValue data) { glooxwrapper::JID xpartamuppJid(m_xpartamuppId); // Setup some base stanza attributes GameListQuery* g = new GameListQuery(); g->m_Command = "register"; glooxwrapper::Tag* game = glooxwrapper::Tag::allocate("game"); // Add a fake ip which will be overwritten by the ip stamp XMPP module on the server. game->addAttribute("ip", "fake"); // Iterate through all the properties reported and add them to the stanza. std::vector properties; scriptInterface.EnumeratePropertyNamesWithPrefix(data, "", properties); for (const std::string& p : properties) { std::wstring value; scriptInterface.GetProperty(data, p.c_str(), value); game->addAttribute(p, utf8_from_wstring(value)); } // Push the stanza onto the IQ g->m_GameList.emplace_back(game); // Send IQ glooxwrapper::IQ iq(gloox::IQ::Set, xpartamuppJid, m_client->getID()); iq.addExtension(g); DbgXMPP("SendIqRegisterGame [" << tag_xml(iq) << "]"); m_client->send(iq); } /** * Send a request to unregister a game to the server. */ void XmppClient::SendIqUnregisterGame() { glooxwrapper::JID xpartamuppJid(m_xpartamuppId); // Send IQ GameListQuery* g = new GameListQuery(); g->m_Command = "unregister"; g->m_GameList.emplace_back(glooxwrapper::Tag::allocate("game")); glooxwrapper::IQ iq(gloox::IQ::Set, xpartamuppJid, m_client->getID()); iq.addExtension(g); DbgXMPP("SendIqUnregisterGame [" << tag_xml(iq) << "]"); m_client->send(iq); } /** * Send a request to change the state of a registered game on the server. * * A game can either be in the 'running' or 'waiting' state - the server * decides which - but we need to update the current players that are * in-game so the server can make the calculation. */ void XmppClient::SendIqChangeStateGame(const std::string& nbp, const std::string& players) { glooxwrapper::JID xpartamuppJid(m_xpartamuppId); // Send IQ GameListQuery* g = new GameListQuery(); g->m_Command = "changestate"; glooxwrapper::Tag* game = glooxwrapper::Tag::allocate("game"); game->addAttribute("nbp", nbp); game->addAttribute("players", players); g->m_GameList.emplace_back(game); glooxwrapper::IQ iq(gloox::IQ::Set, xpartamuppJid, m_client->getID()); iq.addExtension(g); DbgXMPP("SendIqChangeStateGame [" << tag_xml(iq) << "]"); m_client->send(iq); } /***************************************************** * Account registration * *****************************************************/ void XmppClient::handleRegistrationFields(const glooxwrapper::JID&, int fields, glooxwrapper::string) { glooxwrapper::RegistrationFields vals; vals.username = m_username; vals.password = m_password; m_registration->createAccount(fields, vals); } void XmppClient::handleRegistrationResult(const glooxwrapper::JID&, gloox::RegistrationResult result) { if (result == gloox::RegistrationSuccess) CreateGUIMessage("system", "registered"); else CreateGUIMessage("system", "error", "text", RegistrationResultToString(result)); disconnect(); } void XmppClient::handleAlreadyRegistered(const glooxwrapper::JID&) { DbgXMPP("the account already exists"); } void XmppClient::handleDataForm(const glooxwrapper::JID&, const glooxwrapper::DataForm&) { DbgXMPP("dataForm received"); } void XmppClient::handleOOB(const glooxwrapper::JID&, const glooxwrapper::OOB&) { DbgXMPP("OOB registration requested"); } /***************************************************** * Requests from GUI * *****************************************************/ /** * Handle requests from the GUI for the list of players. * * @return A JS array containing all known players and their presences */ void XmppClient::GUIGetPlayerList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); scriptInterface.Eval("([])", ret); // Convert the internal data structure to a Javascript object. for (const std::pair >& p : m_PlayerMap) { JS::RootedValue player(cx); scriptInterface.Eval("({})", &player); scriptInterface.SetProperty(player, "name", wstring_from_utf8(p.first)); scriptInterface.SetProperty(player, "presence", wstring_from_utf8(p.second[0])); scriptInterface.SetProperty(player, "rating", wstring_from_utf8(p.second[1])); scriptInterface.SetProperty(player, "role", wstring_from_utf8(p.second[2])); scriptInterface.CallFunctionVoid(ret, "push", player); } } /** * Handle requests from the GUI for the list of all active games. * * @return A JS array containing all known games */ void XmppClient::GUIGetGameList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); scriptInterface.Eval("([])", ret); - const char* stats[] = { "name", "ip", "port", "stunIP", "stunPort", "hostUsername", "state", "nbp", "maxnbp", "players", "mapName", "niceMapName", "mapSize", "mapType", "victoryCondition", "startTime" }; + const char* stats[] = { "name", "ip", "port", "stunIP", "stunPort", "hostUsername", "state", + "nbp", "maxnbp", "players", "mapName", "niceMapName", "mapSize", "mapType", + "victoryCondition", "startTime", "mods" }; for(const glooxwrapper::Tag* const& t : m_GameList) { JS::RootedValue game(cx); scriptInterface.Eval("({})", &game); for (size_t i = 0; i < ARRAY_SIZE(stats); ++i) scriptInterface.SetProperty(game, stats[i], wstring_from_utf8(t->findAttribute(stats[i]).to_string())); scriptInterface.CallFunctionVoid(ret, "push", game); } } /** * Handle requests from the GUI for leaderboard data. * * @return A JS array containing all known leaderboard data */ void XmppClient::GUIGetBoardList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); scriptInterface.Eval("([])", ret); const char* attributes[] = { "name", "rank", "rating" }; for(const glooxwrapper::Tag* const& t : m_BoardList) { JS::RootedValue board(cx); scriptInterface.Eval("({})", &board); for (size_t i = 0; i < ARRAY_SIZE(attributes); ++i) scriptInterface.SetProperty(board, attributes[i], wstring_from_utf8(t->findAttribute(attributes[i]).to_string())); scriptInterface.CallFunctionVoid(ret, "push", board); } } /** * Handle requests from the GUI for profile data. * * @return A JS array containing the specific user's profile data */ void XmppClient::GUIGetProfile(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); scriptInterface.Eval("([])", ret); const char* stats[] = { "player", "rating", "totalGamesPlayed", "highestRating", "wins", "losses", "rank" }; for (const glooxwrapper::Tag* const& t : m_Profile) { JS::RootedValue profile(cx); scriptInterface.Eval("({})", &profile); for (size_t i = 0; i < ARRAY_SIZE(stats); ++i) scriptInterface.SetProperty(profile, stats[i], wstring_from_utf8(t->findAttribute(stats[i]).to_string())); scriptInterface.CallFunctionVoid(ret, "push", profile); } } /***************************************************** * Message interfaces * *****************************************************/ void XmppClient::CreateGUIMessage( const std::string& type, const std::string& level, const std::string& property1_name, const std::string& property1_value, const std::string& property2_name, const std::string& property2_value, const std::time_t time) { GUIMessage message; message.type = type; message.level = level; message.property1_name = property1_name; message.property1_value = property1_value; message.property2_name = property2_name; message.property2_value = property2_value; message.time = time; m_GuiMessageQueue.push_back(std::move(message)); } JS::Value XmppClient::GuiMessageToJSVal(const ScriptInterface& scriptInterface, const GUIMessage& message, const bool historic) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); JS::RootedValue ret(cx); scriptInterface.Eval("({})", &ret); scriptInterface.SetProperty(ret, "type", wstring_from_utf8(message.type)); if (!message.level.empty()) scriptInterface.SetProperty(ret, "level", wstring_from_utf8(message.level)); if (!message.property1_name.empty()) scriptInterface.SetProperty(ret, message.property1_name.c_str(), wstring_from_utf8(message.property1_value)); if (!message.property2_name.empty()) scriptInterface.SetProperty(ret, message.property2_name.c_str(), wstring_from_utf8(message.property2_value)); scriptInterface.SetProperty(ret, "time", (double)message.time); scriptInterface.SetProperty(ret, "historic", historic); return ret; } JS::Value XmppClient::GuiPollNewMessage(const ScriptInterface& scriptInterface) { if (m_GuiMessageQueue.empty()) return JS::UndefinedValue(); GUIMessage message = m_GuiMessageQueue.front(); m_GuiMessageQueue.pop_front(); // Since there can be hundreds of presence changes while playing a game, ignore these for performance if (message.type == "chat" && message.level != "presence") m_HistoricGuiMessages.push_back(message); return GuiMessageToJSVal(scriptInterface, message, false); } JS::Value XmppClient::GuiPollHistoricMessages(const ScriptInterface& scriptInterface) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); JS::RootedObject ret(cx, JS_NewArrayObject(cx, 0)); uint32_t i = 0; for (const GUIMessage& message : m_HistoricGuiMessages) { JS::RootedValue msg(cx, GuiMessageToJSVal(scriptInterface, message, true)); JS_SetElement(cx, ret, i++, msg); } return JS::ObjectValue(*ret); } /** * Send a standard MUC textual message. */ void XmppClient::SendMUCMessage(const std::string& message) { m_mucRoom->send(message); } /** * Clears all presence updates from the message queue. * Used when rejoining the lobby, since we don't need to handle past presence changes. */ void XmppClient::ClearPresenceUpdates() { m_GuiMessageQueue.erase( std::remove_if(m_GuiMessageQueue.begin(), m_GuiMessageQueue.end(), [](XmppClient::GUIMessage& message) { return message.type == "chat" && message.level == "presence"; } ), m_GuiMessageQueue.end()); } /** * Handle a room message. */ void XmppClient::handleMUCMessage(glooxwrapper::MUCRoom*, const glooxwrapper::Message& msg, bool priv) { DbgXMPP(msg.from().resource() << " said " << msg.body()); CreateGUIMessage( "chat", priv ? "private-message" : "room-message", "from", msg.from().resource().to_string(), "text", msg.body().to_string(), ComputeTimestamp(msg)); } /** * Handle a private message. */ void XmppClient::handleMessage(const glooxwrapper::Message& msg, glooxwrapper::MessageSession*) { DbgXMPP("type " << msg.subtype() << ", subject " << msg.subject() << ", message " << msg.body() << ", thread id " << msg.thread()); CreateGUIMessage( "chat", "private-message", "from", msg.from().resource().to_string(), "text", msg.body().to_string(), ComputeTimestamp(msg)); } /** * Handle portions of messages containing custom stanza extensions. */ bool XmppClient::handleIq(const glooxwrapper::IQ& iq) { DbgXMPP("handleIq [" << tag_xml(iq) << "]"); if (iq.subtype() == gloox::IQ::Result) { const GameListQuery* gq = iq.findExtension(EXTGAMELISTQUERY); const BoardListQuery* bq = iq.findExtension(EXTBOARDLISTQUERY); const ProfileQuery* pq = iq.findExtension(EXTPROFILEQUERY); if (gq) { for (const glooxwrapper::Tag* const& t : m_GameList) glooxwrapper::Tag::free(t); m_GameList.clear(); for (const glooxwrapper::Tag* const& t : gq->m_GameList) m_GameList.emplace_back(t->clone()); CreateGUIMessage("game", "gamelist"); } if (bq) { if (bq->m_Command == "boardlist") { for (const glooxwrapper::Tag* const& t : m_BoardList) glooxwrapper::Tag::free(t); m_BoardList.clear(); for (const glooxwrapper::Tag* const& t : bq->m_StanzaBoardList) m_BoardList.emplace_back(t->clone()); CreateGUIMessage("game", "leaderboard"); } else if (bq->m_Command == "ratinglist") { for (const glooxwrapper::Tag* const& t : bq->m_StanzaBoardList) { std::string name = t->findAttribute("name").to_string(); if (m_PlayerMap.find(name) != m_PlayerMap.end()) m_PlayerMap[name][1] = t->findAttribute("rating").to_string(); } CreateGUIMessage("game", "ratinglist"); } } if (pq) { for (const glooxwrapper::Tag* const& t : m_Profile) glooxwrapper::Tag::free(t); m_Profile.clear(); for (const glooxwrapper::Tag* const& t : pq->m_StanzaProfile) m_Profile.emplace_back(t->clone()); CreateGUIMessage("game", "profile"); } } else if (iq.subtype() == gloox::IQ::Error) CreateGUIMessage("system", "error", "text", StanzaErrorToString(iq.error_error())); else { CreateGUIMessage("system", "error", "text", g_L10n.Translate("unknown subtype (see logs)")); LOGMESSAGE("unknown subtype '%s'", tag_name(iq).c_str()); } return true; } /***************************************************** * Presence, nickname, and subject * *****************************************************/ /** * Update local data when a user changes presence. */ void XmppClient::handleMUCParticipantPresence(glooxwrapper::MUCRoom*, const glooxwrapper::MUCRoomParticipant participant, const glooxwrapper::Presence& presence) { std::string nick = participant.nick->resource().to_string(); gloox::Presence::PresenceType presenceType = presence.presence(); std::string presenceString, roleString; GetPresenceString(presenceType, presenceString); GetRoleString(participant.role, roleString); if (presenceType == gloox::Presence::Unavailable) { if (!participant.newNick.empty() && (participant.flags & (gloox::UserNickChanged | gloox::UserSelf))) { // we have a nick change std::string newNick = participant.newNick.to_string(); m_PlayerMap[newNick].resize(3); m_PlayerMap[newNick][0] = presenceString; m_PlayerMap[newNick][2] = roleString; DbgXMPP(nick << " is now known as " << participant.newNick.to_string()); CreateGUIMessage("chat", "nick", "oldnick", nick, "newnick", participant.newNick.to_string()); } else if (participant.flags & gloox::UserKicked) { DbgXMPP(nick << " was kicked. Reason: " << participant.reason.to_string()); CreateGUIMessage("chat", "kicked", "nick", nick, "reason", participant.reason.to_string()); } else if (participant.flags & gloox::UserBanned) { DbgXMPP(nick << " was banned. Reason: " << participant.reason.to_string()); CreateGUIMessage("chat", "banned", "nick", nick, "reason", participant.reason.to_string()); } else { DbgXMPP(nick << " left the room (flags " << participant.flags << ")"); CreateGUIMessage("chat", "leave", "nick", nick); } m_PlayerMap.erase(nick); } else { /* During the initialization process, we recieve join messages for everyone * currently in the room. We don't want to display these, so we filter them * out. We will always be the last to join during initialization. */ if (!m_initialLoadComplete) { if (m_mucRoom->nick().to_string() == nick) m_initialLoadComplete = true; } else if (m_PlayerMap.find(nick) == m_PlayerMap.end()) CreateGUIMessage("chat", "join", "nick", nick); else if (m_PlayerMap[nick][2] != roleString) CreateGUIMessage("chat", "role", "nick", nick, "oldrole", m_PlayerMap[nick][2]); else CreateGUIMessage("chat", "presence", "nick", nick); DbgXMPP(nick << " is in the room, presence : " << (int)presenceType); m_PlayerMap[nick].resize(3); m_PlayerMap[nick][0] = presenceString; m_PlayerMap[nick][2] = roleString; } } /** * Update local cache when subject changes. */ void XmppClient::handleMUCSubject(glooxwrapper::MUCRoom*, const glooxwrapper::string& nick, const glooxwrapper::string& subject) { m_Subject = subject.c_str(); CreateGUIMessage("chat", "subject", "nick", nick.c_str(), "subject", m_Subject); } /** * Get current subject. * * @param topic Variable to store subject in. */ void XmppClient::GetSubject(std::string& subject) { subject = m_Subject; } /** * Request nick change, real change via mucRoomHandler. * * @param nick Desired nickname */ void XmppClient::SetNick(const std::string& nick) { m_mucRoom->setNick(nick); } /** * Get current nickname. * * @param nick Variable to store the nickname in. */ void XmppClient::GetNick(std::string& nick) { nick = m_mucRoom->nick().to_string(); } /** * Kick a player from the current room. * * @param nick Nickname to be kicked * @param reason Reason the player was kicked */ void XmppClient::kick(const std::string& nick, const std::string& reason) { m_mucRoom->kick(nick, reason); } /** * Ban a player from the current room. * * @param nick Nickname to be banned * @param reason Reason the player was banned */ void XmppClient::ban(const std::string& nick, const std::string& reason) { m_mucRoom->ban(nick, reason); } /** * Change the xmpp presence of the client. * * @param presence A string containing the desired presence */ void XmppClient::SetPresence(const std::string& presence) { #define IF(x,y) if (presence == x) m_mucRoom->setPresence(gloox::Presence::y) IF("available", Available); else IF("chat", Chat); else IF("away", Away); else IF("playing", DND); else IF("offline", Unavailable); // The others are not to be set #undef IF else LOGERROR("Unknown presence '%s'", presence.c_str()); } /** * Get the current xmpp presence of the given nick. * * @param nick Nickname to look up presence for * @param presence Variable to store the presence in */ void XmppClient::GetPresence(const std::string& nick, std::string& presence) { if (m_PlayerMap.find(nick) != m_PlayerMap.end()) presence = m_PlayerMap[nick][0]; else presence = "offline"; } /** * Get the current xmpp role of the given nick. * * @param nick Nickname to look up presence for * @param role Variable to store the role in */ void XmppClient::GetRole(const std::string& nick, std::string& role) { if (m_PlayerMap.find(nick) != m_PlayerMap.end()) role = m_PlayerMap[nick][2]; else role = ""; } /***************************************************** * Utilities * *****************************************************/ /** * Parse and return the timestamp of a historic chat message and return the current time for new chat messages. * Historic chat messages are implement as DelayedDelivers as specified in XEP-0203. * Hence, their timestamp MUST be in UTC and conform to the DateTime format XEP-0082. * * @returns Seconds since the epoch. */ std::time_t XmppClient::ComputeTimestamp(const glooxwrapper::Message& msg) const { // Only historic messages contain a timestamp! if (!msg.when()) return std::time(nullptr); // The locale is irrelevant, because the XMPP date format doesn't contain written month names for (const std::string& format : std::vector{ "Y-M-d'T'H:m:sZ", "Y-M-d'T'H:m:s.SZ" }) { UDate dateTime = g_L10n.ParseDateTime(msg.when()->stamp().to_string(), format, Locale::getUS()); if (dateTime) return dateTime / 1000.0; } return std::time(nullptr); } /** * Convert a gloox presence type to string. * * @param p Presence to be converted * @param presence Variable to store the converted presence string in */ void XmppClient::GetPresenceString(const gloox::Presence::PresenceType p, std::string& presence) const { switch(p) { #define CASE(x,y) case gloox::Presence::x: presence = y; break CASE(Available, "available"); CASE(Chat, "chat"); CASE(Away, "away"); CASE(DND, "playing"); CASE(XA, "away"); CASE(Unavailable, "offline"); CASE(Probe, "probe"); CASE(Error, "error"); CASE(Invalid, "invalid"); default: LOGERROR("Unknown presence type '%d'", (int)p); break; #undef CASE } } /** * Convert a gloox role type to string. * * @param p Role to be converted * @param presence Variable to store the converted role string in */ void XmppClient::GetRoleString(const gloox::MUCRoomRole r, std::string& role) const { switch(r) { #define CASE(X, Y) case gloox::X: role = Y; break CASE(RoleNone, "none"); CASE(RoleVisitor, "visitor"); CASE(RoleParticipant, "participant"); CASE(RoleModerator, "moderator"); CASE(RoleInvalid, "invalid"); default: LOGERROR("Unknown role type '%d'", (int)r); break; #undef CASE } } /** * Convert a gloox stanza error type to string. * Keep in sync with Gloox documentation * * @param err Error to be converted * @return Converted error string */ std::string XmppClient::StanzaErrorToString(gloox::StanzaError err) const { #define CASE(X, Y) case gloox::X: return Y #define DEBUG_CASE(X, Y) case gloox::X: return g_L10n.Translate("Error") + " (" + Y + ")" switch (err) { CASE(StanzaErrorUndefined, g_L10n.Translate("No error")); DEBUG_CASE(StanzaErrorBadRequest, "Server received malformed XML"); CASE(StanzaErrorConflict, g_L10n.Translate("Player already logged in")); DEBUG_CASE(StanzaErrorFeatureNotImplemented, "Server does not implement requested feature"); CASE(StanzaErrorForbidden, g_L10n.Translate("Forbidden")); DEBUG_CASE(StanzaErrorGone, "Unable to find message receipiant"); CASE(StanzaErrorInternalServerError, g_L10n.Translate("Internal server error")); DEBUG_CASE(StanzaErrorItemNotFound, "Message receipiant does not exist"); DEBUG_CASE(StanzaErrorJidMalformed, "JID (XMPP address) malformed"); DEBUG_CASE(StanzaErrorNotAcceptable, "Receipiant refused message. Possible policy issue"); CASE(StanzaErrorNotAllowed, g_L10n.Translate("Not allowed")); CASE(StanzaErrorNotAuthorized, g_L10n.Translate("Not authorized")); DEBUG_CASE(StanzaErrorNotModified, "Requested item has not changed since last request"); DEBUG_CASE(StanzaErrorPaymentRequired, "This server requires payment"); CASE(StanzaErrorRecipientUnavailable, g_L10n.Translate("Recipient temporarily unavailable")); DEBUG_CASE(StanzaErrorRedirect, "Request redirected"); CASE(StanzaErrorRegistrationRequired, g_L10n.Translate("Registration required")); DEBUG_CASE(StanzaErrorRemoteServerNotFound, "Remote server not found"); DEBUG_CASE(StanzaErrorRemoteServerTimeout, "Remote server timed out"); DEBUG_CASE(StanzaErrorResourceConstraint, "The recipient is unable to process the message due to resource constraints"); CASE(StanzaErrorServiceUnavailable, g_L10n.Translate("Service unavailable")); DEBUG_CASE(StanzaErrorSubscribtionRequired, "Service requires subscription"); DEBUG_CASE(StanzaErrorUnexpectedRequest, "Attempt to send from invalid stanza address"); DEBUG_CASE(StanzaErrorUnknownSender, "Invalid 'from' address"); default: return g_L10n.Translate("Unknown error"); } #undef DEBUG_CASE #undef CASE } /** * Convert a gloox connection error enum to string * Keep in sync with Gloox documentation * * @param err Error to be converted * @return Converted error string */ std::string XmppClient::ConnectionErrorToString(gloox::ConnectionError err) const { #define CASE(X, Y) case gloox::X: return Y #define DEBUG_CASE(X, Y) case gloox::X: return g_L10n.Translate("Error") + " (" + Y + ")" switch (err) { CASE(ConnNoError, g_L10n.Translate("No error")); CASE(ConnStreamError, g_L10n.Translate("Stream error")); CASE(ConnStreamVersionError, g_L10n.Translate("The incoming stream version is unsupported")); CASE(ConnStreamClosed, g_L10n.Translate("The stream has been closed by the server")); DEBUG_CASE(ConnProxyAuthRequired, "The HTTP/SOCKS5 proxy requires authentication"); DEBUG_CASE(ConnProxyAuthFailed, "HTTP/SOCKS5 proxy authentication failed"); DEBUG_CASE(ConnProxyNoSupportedAuth, "The HTTP/SOCKS5 proxy requires an unsupported authentication mechanism"); CASE(ConnIoError, g_L10n.Translate("An I/O error occurred")); DEBUG_CASE(ConnParseError, "An XML parse error occurred"); CASE(ConnConnectionRefused, g_L10n.Translate("The connection was refused by the server")); CASE(ConnDnsError, g_L10n.Translate("Resolving the server's hostname failed")); CASE(ConnOutOfMemory, g_L10n.Translate("This system is out of memory")); DEBUG_CASE(ConnNoSupportedAuth, "The authentication mechanisms the server offered are not supported or no authentication mechanisms were available"); CASE(ConnTlsFailed, g_L10n.Translate("The server's certificate could not be verified or the TLS handshake did not complete successfully")); CASE(ConnTlsNotAvailable, g_L10n.Translate("The server did not offer required TLS encryption")); DEBUG_CASE(ConnCompressionFailed, "Negotiation/initializing compression failed"); CASE(ConnAuthenticationFailed, g_L10n.Translate("Authentication failed. Incorrect password or account does not exist")); CASE(ConnUserDisconnected, g_L10n.Translate("The user or system requested a disconnect")); CASE(ConnNotConnected, g_L10n.Translate("There is no active connection")); default: return g_L10n.Translate("Unknown error"); } #undef DEBUG_CASE #undef CASE } /** * Convert a gloox registration result enum to string * Keep in sync with Gloox documentation * * @param err Enum to be converted * @return Converted string */ std::string XmppClient::RegistrationResultToString(gloox::RegistrationResult res) const { #define CASE(X, Y) case gloox::X: return Y #define DEBUG_CASE(X, Y) case gloox::X: return g_L10n.Translate("Error") + " (" + Y + ")" switch (res) { CASE(RegistrationSuccess, g_L10n.Translate("Your account has been successfully registered")); CASE(RegistrationNotAcceptable, g_L10n.Translate("Not all necessary information provided")); CASE(RegistrationConflict, g_L10n.Translate("Username already exists")); DEBUG_CASE(RegistrationNotAuthorized, "Account removal timeout or insufficiently secure channel for password change"); DEBUG_CASE(RegistrationBadRequest, "Server received an incomplete request"); DEBUG_CASE(RegistrationForbidden, "Registration forbidden"); DEBUG_CASE(RegistrationRequired, "Account cannot be removed as it does not exist"); DEBUG_CASE(RegistrationUnexpectedRequest, "This client is unregistered with the server"); DEBUG_CASE(RegistrationNotAllowed, "Server does not permit password changes"); default: return ""; } #undef DEBUG_CASE #undef CASE } void XmppClient::SendStunEndpointToHost(StunClient::StunEndpoint* stunEndpoint, const std::string& hostJIDStr) { ENSURE(stunEndpoint); char ipStr[256] = "(error)"; ENetAddress addr; addr.host = ntohl(stunEndpoint->ip); enet_address_get_host_ip(&addr, ipStr, ARRAY_SIZE(ipStr)); glooxwrapper::JID hostJID(hostJIDStr); glooxwrapper::Jingle::Session session = m_sessionManager->createSession(hostJID); session.sessionInitiate(ipStr, stunEndpoint->port); } void XmppClient::handleSessionAction(gloox::Jingle::Action action, glooxwrapper::Jingle::Session* UNUSED(session), const glooxwrapper::Jingle::Session::Jingle* jingle) { if (action == gloox::Jingle::SessionInitiate) handleSessionInitiation(jingle); } void XmppClient::handleSessionInitiation(const glooxwrapper::Jingle::Session::Jingle* jingle) { glooxwrapper::Jingle::ICEUDP::Candidate candidate = jingle->getCandidate(); if (candidate.ip.empty()) { LOGERROR("Failed to retrieve Jingle candidate"); return; } g_NetServer->SendHolePunchingMessage(candidate.ip.to_string(), candidate.port); }