Move calls in playersFinished to handlers

This patch fixes the TODO comment in playersFinished and moves
all of the calls to handlers. The only reason for adding back the
function `handlePlayersFinished` is to prevent duplication in the "won"
and "defeated" message handlers. In the future, code to execute when
players finish in the future should exclusively be added by registering
new handlers, not by adding it to that function.
This commit is contained in:
Vantha
2026-03-20 17:06:22 +01:00
committed by Vantha
parent 6456bccebe
commit f39e71cca0
2 changed files with 25 additions and 36 deletions
@@ -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;
@@ -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();