diff --git a/gui-patches/battle/record-local-battle-awards/patch.jsonc b/gui-patches/battle/record-local-battle-awards/patch.jsonc index 463c669..e12001f 100644 --- a/gui-patches/battle/record-local-battle-awards/patch.jsonc +++ b/gui-patches/battle/record-local-battle-awards/patch.jsonc @@ -1,6 +1,6 @@ -// Record the local player's lives, combat events and the battle's kill log in a LocalMP battle, -// show the reward banner, and pass each fact to the local service live to show the battle awards -// (streaks) it completes. +// Record the local player's lives, combat events, the battle's kill log and how the battle ended +// in a LocalMP battle, show the reward banner, and pass each fact to the local service live to +// show the battle awards (streaks) it completes. // Why the client: online the battle server prices each event, judges the awards and sends both // banners (packet 0xb03d, streak messages); a LocalMP host has no battle server, so the native // bridge reports the facts as the HUD event "wtoLocalMpExpEvent" and only the client sees spawns, diff --git a/gui-patches/battle/record-local-battle-awards/replace.nut b/gui-patches/battle/record-local-battle-awards/replace.nut index 31e86ca..d6b5dfe 100644 --- a/gui-patches/battle/record-local-battle-awards/replace.nut +++ b/gui-patches/battle/record-local-battle-awards/replace.nut @@ -151,22 +151,42 @@ function wtoOnHudMessage(msg) { wtoSendLiveFact("kill", kill) } +// The battle ends for the local player: close the open life and keep the end time, which the +// settlement report needs after the mission has been left (the mission is unloaded before the +// debriefing, so its time and status are gone by then). The first end counts. +function wtoEndBattleRecord() { + let { ndbExists, ndbWrite } = require("nestdb") + if (!ndbExists(["wto", "activeBattle"]) || ndbExists(["wto", "battleLog", "sessionEnd"])) + return false + wtoCloseBattleLife(false) + ndbWrite(["wto", "battleLog", "sessionEnd"], require("mission").get_mission_time()) + return true +} + +// AcesMission::endFinally raises this HUD event with the result for the local player's team on +// every end of the battle (tickets, nuclear strike, empty team), while the mission still exists. +// The end that follows on its own (FadeToDebriefing) raises no script event. +function wtoOnMissionResult(ev) { + let { GO_WIN, GO_FAIL } = require("guiMission") + let result = ev?.resultNum + if (result != GO_WIN && result != GO_FAIL) + return + if (wtoEndBattleRecord()) + require("nestdb").ndbWrite(["wto", "battleLog", "outcome"], result == GO_WIN ? "win" : "lose") +} + function wtoSubscribeBattleAwards() { let { g_hud_event_manager } = require("%scripts/hud/hudEventManager.nut") g_hud_event_manager.subscribe("wtoLocalMpExpEvent", wtoOnLocalMpExpEvent) g_hud_event_manager.subscribe("LocalPlayerAlive", wtoOnLocalPlayerAlive) g_hud_event_manager.subscribe("LocalPlayerDead", wtoOnLocalPlayerDead) g_hud_event_manager.subscribe("HudMessage", wtoOnHudMessage) + g_hud_event_manager.subscribe("MissionResult", wtoOnMissionResult) } -// The battle ends for the local player here: close the open life and keep the end time, which the -// settlement report needs after the mission has been left. +// Leaving the battle from the menu before it has ended. function wtoOnQuitToDebriefing(_p) { - let { ndbExists, ndbWrite } = require("nestdb") - if (!ndbExists(["wto", "activeBattle"])) - return - wtoCloseBattleLife(false) - ndbWrite(["wto", "battleLog", "sessionEnd"], require("mission").get_mission_time()) + wtoEndBattleRecord() } wtoSubscribeBattleAwards() diff --git a/gui-patches/battle/report-local-battle-settlement/replace.nut b/gui-patches/battle/report-local-battle-settlement/replace.nut index d82d8eb..38cce17 100644 --- a/gui-patches/battle/report-local-battle-settlement/replace.nut +++ b/gui-patches/battle/report-local-battle-settlement/replace.nut @@ -4,14 +4,11 @@ // no rewards itself. function buildWtoBattleReport(wtoBattle, res) { let { ndbTryRead } = require("nestdb") - let guiMis = require("guiMission") let battleLog = ndbTryRead(["wto", "battleLog"]) ?? {} - let mStatus = guiMis.get_mission_status() let blk = DataBlock() blk.sessionId = wtoBattle.sessionId - blk.outcome = mStatus == guiMis.MISSION_STATUS_SUCCESS ? "win" - : mStatus == guiMis.MISSION_STATUS_FAIL ? "lose" - : "running" + // Recorded when the battle ended (record-local-battle-awards); none when it was left before. + blk.outcome = battleLog?.outcome ?? "running" blk.localTeam = (res?.localTeam ?? 1).tointeger() blk.teamAScore = (res?.mpTblTeams?[0]?.score ?? 0).tointeger() blk.teamBScore = (res?.mpTblTeams?[1]?.score ?? 0).tointeger()