diff --git a/binaries/data/mods/public/gui/session/messages.js b/binaries/data/mods/public/gui/session/messages.js index 857d73b936..18f1fc85d1 100644 --- a/binaries/data/mods/public/gui/session/messages.js +++ b/binaries/data/mods/public/gui/session/messages.js @@ -138,15 +138,14 @@ var g_NotificationsTypes = }, "defeat": function(notification, player) { - playersFinished(notification.allies, notification.message, false); + handlePlayersFinished(notification.allies, notification.message, false); }, "won": function(notification, player) { - playersFinished(notification.allies, notification.message, true); + handlePlayersFinished(notification.allies, notification.message, true); }, "diplomacy": function(notification, player) { - updatePlayerData(); g_DiplomacyColors.onDiplomacyChange(); addChatMessage({ @@ -158,7 +157,6 @@ var g_NotificationsTypes = }, "ceasefire-ended": function(notification, player) { - updatePlayerData(); for (const handler of g_CeasefireEndedHandlers) handler(); }, @@ -415,6 +413,18 @@ function handlePlayerAssignmentsMessage(message) updateGUIObjects(); } +function handlePlayersFinished(players, message, won) +{ + addChatMessage({ + "type": "playerstate", + "message": message, + "players": players + }); + + for (const handler of g_PlayerFinishedHandlers) + handler(players, won); +} + function onClientJoin(guid) { const playerID = g_PlayerAssignments[guid].player; diff --git a/binaries/data/mods/public/gui/session/session.js b/binaries/data/mods/public/gui/session/session.js index 102127cd89..42aab40e3a 100644 --- a/binaries/data/mods/public/gui/session/session.js +++ b/binaries/data/mods/public/gui/session/session.js @@ -287,7 +287,6 @@ async function init(initData, hotloadData) g_PlayerViewControl.registerViewedPlayerChangeHandler(g_DiplomacyColors.updateDisplayedPlayerColors.bind(g_DiplomacyColors)); g_PlayerViewControl.registerViewedPlayerChangeHandler(resetTemplates); g_DiplomacyColors.registerDiplomacyColorsChangeHandler(g_PlayerViewControl.rebuild.bind(g_PlayerViewControl)); - g_DiplomacyColors.registerDiplomacyColorsChangeHandler(updateGUIObjects); g_PauseControl = new PauseControl(); g_PlayerViewControl.registerPreViewedPlayerChangeHandler(removeStatusBarDisplay); g_Ambient = new Ambient(); @@ -358,6 +357,10 @@ async function init(initData, hotloadData) for (const handler of g_HotkeyChangeHandlers) handler(); + registerPlayersFinishedHandler(updatePlayerData); + g_DiplomacyColors.registerDiplomacyColorsChangeHandler(updatePlayerData); + registerCeasefireEndedHandler(updatePlayerData); + if (hotloadData) { g_Selection.selected = hotloadData.selection; @@ -471,6 +474,13 @@ function initializeMusic() if (g_ViewedPlayer != -1 && g_CivData[g_Players[g_ViewedPlayer].civ].Music) global.music.storeTracks(g_CivData[g_Players[g_ViewedPlayer].civ].Music); global.music.setState(global.music.states.PEACE); + + registerPlayersFinishedHandler((players, won) => + { + if (players.includes(Engine.GetPlayerID()) && !Engine.IsAtlasRunning()) + global.music.setState(won ? global.music.states.VICTORY : global.music.states.DEFEAT); + }); + } function resetTemplates() @@ -506,37 +516,6 @@ function controlsPlayer(playerID) playerStates[playerID].state != "defeated"; } -/** - * Called when one or more players have won or were defeated. - * - * @param {array} - IDs of the players who have won or were defeated. - * @param {Object} - a plural string stating the victory reason. - * @param {boolean} - whether these players have won or lost. - */ -function playersFinished(players, victoryString, won) -{ - addChatMessage({ - "type": "playerstate", - "message": victoryString, - "players": players - }); - - updatePlayerData(); - - // TODO: The other calls in this function should move too - for (const handler of g_PlayerFinishedHandlers) - handler(players, won); - - if (players.indexOf(Engine.GetPlayerID()) == -1 || Engine.IsAtlasRunning()) - return; - - global.music.setState( - won ? - global.music.states.VICTORY : - global.music.states.DEFEAT - ); -} - function resumeGame() { g_PauseControl.implicitResume();