Files
WT-Offline/gui-patches/battle/record-local-battle-awards/replace.nut
T
N0VAN0M4LY 8febf2d94e Judge LocalMP battle awards in the local service
The battle awards (streaks) of unlocks.blk are now judged by the local
service from the battle's facts: the player's combat events, each life
and whether it ended in death, the HUD kill log and the final
scoreboard. The record patch also sends each fact live over the
matching connection (wto_battle.fact); the awards it completes come
back and are shown with the stock award banner and kill log entry.

After the battle the awards become EULT_NEW_STREAK logs, which the
stock debriefing lists on its left, and their Silver Lions are paid and
shown in the Unlocks row. Modes whose facts are not reported, and
best-player awards limited by battle rating, are never given.
2026-11-28 08:32:35 +08:00

179 lines
7.1 KiB
Plaintext

// WTOffline: record the local player's lives and combat events in a LocalMP battle and show the
// reward banner. A LocalMP host has no battle server to price events: the native bridge raises the
// HUD event "wtoLocalMpExpEvent" with the facts, the banner amount comes from the parameters the
// local service sent at launch (nestdb wto.activeBattle.awards, battle-economy.ts BannerParams), and
// the record (nestdb wto.battleLog) is settled by the local service after the battle.
// Each fact is also sent to the local service as it happens (matching request wto_battle.fact);
// the battle awards (streaks) it completed come back and are shown with the stock award banner,
// as the battle server's award messages are online.
function wtoShowLiveStreaks(resp) {
let streaks = resp?.streaks ?? []
if (streaks.len() == 0)
return
let { add_streak_message, getLocForStreak } = require("%scripts/streaks.nut")
let { SNT_MY_STREAK_HEADER } = require("%globalScripts/battleMetaConsts.nut")
let { HUD_MSG_STREAK } = require("hudMessages")
let { g_hud_event_manager } = require("%scripts/hud/hudEventManager.nut")
let localId = require("mission").get_local_mplayer()?.id ?? -1
foreach (streak in streaks) {
let stageParam = streak?.stageParam ?? 0
add_streak_message({
header = getLocForStreak(SNT_MY_STREAK_HEADER, streak.id, stageParam)
wp = streak?.wp ?? 0
exp = streak?.exp ?? 0
id = streak.id
})
g_hud_event_manager.onHudEvent("HudMessage", { type = HUD_MSG_STREAK, unlockId = streak.id, stage = stageParam, playerId = localId })
}
}
function wtoSendLiveFact(kind, fact) {
let battle = require("nestdb").ndbTryRead(["wto", "activeBattle"])
if (battle == null)
return
let { matchingApiFunc } = require("%scripts/matching/api.nut")
matchingApiFunc("wto_battle.fact", wtoShowLiveStreaks, fact.__merge({
sessionId = battle.sessionId
kind
localPlayerId = require("mission").get_local_mplayer()?.id ?? -1
localTeam = require("mission").get_mp_local_team()
}))
}
function wtoAppendBattleLog(key, entry) {
let { ndbTryRead, ndbWrite } = require("nestdb")
let list = (ndbTryRead(["wto", "battleLog", key]) ?? []).map(@(v) v)
list.append(entry)
ndbWrite(["wto", "battleLog", key], list)
}
function wtoBannerReward(awards, unit, ev) {
let code = ev?.messageCode ?? -1
let isCapture = code == EXP_EVENT_CAPTURE_ZONE
let expClass = ev?.expClass ?? ""
let group = isCapture ? "own" : (awards?.groups?[expClass] ?? "ground")
let base = awards?.vehicles?[unit]?[$"{code}:{group}"]
if (base == null)
return null
let ai = (isCapture || (ev?.victimIsPlayer ?? 0) != 0) ? 1.0 : awards.aiKillK
let slClass = isCapture ? 1.0 : (awards?.slClass?[expClass] ?? 1.0)
let rpClass = isCapture ? 1.0 : (awards?.rpClass?[expClass] ?? 1.0)
if ((ev?.friendly ?? 0) != 0)
return { wp = -(base[0] * slClass * ai * awards.friendlyKillPenaltyMul + 0.5).tointeger(), exp = 0 }
let count = code == EXP_EVENT_UNIT_DAMAGE ? (ev?.damage ?? 0) : 1
return {
wp = (base[0] * slClass * ai * count + 0.5).tointeger()
exp = (base[1] * rpClass * ai * count + 0.5).tointeger()
}
}
function wtoOnLocalMpExpEvent(ev) {
let { ndbTryRead } = require("nestdb")
let battle = ndbTryRead(["wto", "activeBattle"])
if (battle == null)
return
let wtoMission = require("mission")
let unit = ndbTryRead(["wto", "battleLog", "openLife"])?.unit ?? require("unit").get_player_unit_name()
let entry = {
time = wtoMission.get_mission_time()
unit
code = ev?.messageCode ?? -1
expClass = ev?.expClass ?? ""
victim = ev?.victim ?? ""
victimIsPlayer = (ev?.victimIsPlayer ?? 0) != 0
friendly = (ev?.friendly ?? 0) != 0
modifier = ev?.messageModifier ?? ""
damage = ev?.damage ?? 0
}
wtoAppendBattleLog("events", entry)
wtoSendLiveFact("event", entry)
let reward = wtoBannerReward(battle?.awards, unit, ev)
if (reward == null)
return
let { g_hud_event_manager } = require("%scripts/hud/hudEventManager.nut")
g_hud_event_manager.onHudEvent("InBattleReward", {
messageCode = ev?.messageCode ?? -1
warpoints = reward.wp
experience = reward.exp
counter = 1
expClass = ev?.expClass ?? ""
messageModifier = ev?.messageModifier ?? ""
})
}
function wtoCloseBattleLife(died) {
let { ndbTryRead, ndbDelete } = require("nestdb")
let life = ndbTryRead(["wto", "battleLog", "openLife"])
if (life == null)
return
ndbDelete(["wto", "battleLog", "openLife"])
let closed = life.__merge({ end = require("mission").get_mission_time(), died })
wtoAppendBattleLog("lives", closed)
wtoSendLiveFact("life", closed)
}
function wtoOnLocalPlayerAlive(_ev) {
let { ndbExists, ndbWrite } = require("nestdb")
if (!ndbExists(["wto", "activeBattle"]))
return
wtoCloseBattleLife(false)
let unit = require("unit").get_player_unit_name()
if ((unit ?? "") != "")
ndbWrite(["wto", "battleLog", "openLife"], { unit, start = require("mission").get_mission_time() })
}
function wtoOnLocalPlayerDead(_ev) {
if (require("nestdb").ndbExists(["wto", "activeBattle"]))
wtoCloseBattleLife(true)
}
// Every kill of the HUD kill log, for the awards that depend on who killed whom (first kill,
// avenging). A message without player ids is about the local player (scripts/hud/hudBattleLog.nut).
function wtoOnHudMessage(msg) {
let { HUD_MSG_MULTIPLAYER_DMG } = require("hudMessages")
if (msg?.type != HUD_MSG_MULTIPLAYER_DMG || !(msg?.isKill ?? true))
return
if (!require("nestdb").ndbExists(["wto", "activeBattle"]))
return
let wtoMission = require("mission")
let localId = wtoMission.get_local_mplayer()?.id ?? -1
let localTeam = wtoMission.get_mp_local_team()
let killer = msg?.playerId ?? localId
let victim = msg?.victimPlayerId ?? localId
let kill = {
time = wtoMission.get_mission_time()
killer
killerTeam = msg?.team ?? (killer == localId ? localTeam : 0)
victim
victimTeam = msg?.victimTeam ?? (victim == localId ? localTeam : 0)
}
wtoAppendBattleLog("feed", kill)
wtoSendLiveFact("kill", kill)
}
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)
}
// 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.
function wtoOnQuitToDebriefing(_p) {
let { ndbExists, ndbWrite } = require("nestdb")
if (!ndbExists(["wto", "activeBattle"]))
return
wtoCloseBattleLife(false)
ndbWrite(["wto", "battleLog", "sessionEnd"], require("mission").get_mission_time())
}
wtoSubscribeBattleAwards()
require("%sqStdLibs/helpers/subscriptions.nut").add_event_listener("HudEventManagerReset", @(_p) wtoSubscribeBattleAwards())
require("eventbus").eventbus_subscribe("onQuitToDebriefing", wtoOnQuitToDebriefing)
return {
g_hud_reward_message
}