local UI = require("urhox-libs/UI") local Engine = require("Game/Engine") local Clock = require("Game/Clock") local Save = require("Game/Save") local Auto = require("Game/Auto") local View = require("Game/UI/View") ---@class BiaojuApp ---@field engine table ---@field state any ---@field selected table ---@field progressWidgets table ---@field eventsById table local App = {} App.__index=App ---@return BiaojuApp function App.new() local self=setmetatable({},App) self:Init() return self end function App:Init() self.mode=Save.LoadMode() self.page="dispatch" self.codexTab="roster" self.rankMetric="prestige" self.detailTab="overview" self.recordLimit=30 self.autoMode="稳健" self.autoOn=false self.cityPicker=false self.moreBusiness=false self.showLedger=false self.importCode="" self.tickAccumulator=0.0 self.saveAccumulator=0.0 self.messageTime=0.0 self.message="" self.dialog=nil self.loadError=nil self.root=nil self.content=nil self.dialogContent=nil self.toastPanel=nil self.toastLabel=nil self.pendingScroll=nil self.progressWidgets={} self.eventsById={} self.selected={} self.selectedBiz="civil" self.dirty=true self.width=graphics:GetWidth() self.height=graphics:GetHeight() self:LoadMode(self.mode) end ---@param mode string function App:LoadMode(mode) self.mode=mode self.tickAccumulator=0.0 self.engine=Engine.new({cityTrial=mode=="city",now=Clock.Now}) local state,reason,isNew=Save.Load(self.engine) self.state=state self.loadError=reason self.selected={} self.eventsById={} for _,event in ipairs(self.engine.data.EVENTS) do self.eventsById[event.id]=event end if state then for i=1,math.min(#state.escorts,2) do self.selected[state.escorts[i].id]=true end local business=self.engine.bestBusiness(state) self.selectedBiz=business and business.id or "civil" self.cityPicker=false if isNew then self:Persist() end end self:Invalidate() print("[Biaoju] mode="..mode.." loaded="..tostring(state~=nil).." new="..tostring(isNew)) end function App:Invalidate() self.dirty=true end ---@param message string function App:Toast(message) self.message=message self.messageTime=math.min(6,2+#message/35) if self.toastLabel then self.toastLabel:SetText(message) end if self.toastPanel then self.toastPanel:SetVisible(true) end end ---@return boolean function App:Persist() if not self.state then return false end local ok,reason=Save.Write(self.engine,self.state) if not ok then self:Toast(reason or "保存失败,请导出存档留存进度") print("[Biaoju] save failed: "..tostring(reason)) end return ok end ---@param rule string ---@param ... any ---@return any function App:Do(rule,...) if not self.state then return nil end local ok,result=pcall(self.engine[rule],self.state,...) if not ok then print("[Biaoju] rule="..rule.." error="..tostring(result)) self:Toast("操作未完成,请稍后重试") return nil end if type(result)=="table" and result.msg then self:Toast(result.msg) end if type(result)~="table" or result.ok~=false then self:Persist() print("[Biaoju] rule="..rule.." silver="..self.state.silver.." runs="..#self.state.runs) end self:Invalidate() return result end ---@return string[] function App:SelectedIds() local ids={} if not self.state then return ids end local busy=self.engine.occupied(self.state) for _,escort in ipairs(self.state.escorts) do if self.selected[escort.id] and not self.engine.support.call(busy,"has",escort.id) then ids[#ids+1]=escort.id end end return ids end ---@param page string function App:SetPage(page) self.page=page self.content=nil if self.state then local names={dispatch=1,escort=2,facility=3,codex=4,city=5,rank=6} self.engine.track(self.state,"page_view",names[page]) end self:Invalidate() end ---@param kind string ---@param data? table function App:Open(kind,data) self.dialog=data or {} self.dialog.kind=kind self.dialogContent=nil self:Invalidate() end function App:CloseDialog() self.dialog=nil self.dialogContent=nil self:Invalidate() end ---@return any function App:CurrentDialog() if not self.state then if self.dialog and (self.dialog.kind=="import" or self.dialog.kind=="reset") then return self.dialog end return {kind="recovery",blocking=true} end if self.state.offline then return {kind="offline",blocking=true} end for _,run in ipairs(self.state.runs) do if run.decisionNotice then return {kind="outcome",run=run,blocking=true} end if run.battleNotice and not self.autoOn then return {kind="battle-outcome",run=run,blocking=true} end if run.pendingEvent and not self.autoOn then return {kind="choice",run=run,blocking=true} end end if not self.autoOn then for _,run in ipairs(self.state.runs) do if run.done then return {kind="settlement",blocking=true} end end end return self.dialog end ---@param run any ---@param option any function App:Choose(run,option) if not run.pendingEvent or run.done then return end local found=false for _,opt in ipairs(run.pendingEvent.opts) do if opt==option then found=true break end end if not found then return end self:Do("applyChoice",run,option) end function App:Claim() local result=self.engine.commitAll(self.state,false) if not result then return end self:Persist() self:Open("receipt",{result=result}) print("[Biaoju] claimed="..result.silver.." prestige="..result.prestige) end function App:ClaimOffline() if not self.state or not self.state.offline then return end local silver=self.state.offline.silver self.state.silver=self.state.silver+silver self.state.stats.silverEarned=self.state.stats.silverEarned+silver self.state.offline=nil self:Persist() self:Toast("离线镖银已入账 · +"..self.engine.fmtNum(silver).." 两") self:Invalidate() end function App:RefreshRecruits() local cost=self.engine.refreshCost(self.state) if self.state.silver0 then return end self.state.cityJourney.target=nil self.cityPicker=false self:Persist() self:Invalidate() end ---@param mode string function App:SwitchMode(mode) if mode==self.mode then self:CloseDialog() return end if self.state and not self:Persist() then return end self.dialog=nil self.autoOn=false self.content=nil self:LoadMode(mode) Save.RememberMode(mode) end ---@param code string function App:ImportSave(code) local state,reason=Save.Import(self.engine,code) if not state then self:Toast(reason or "存档码无效") return end local ok,writeReason=Save.Write(self.engine,state) if not ok then self:Toast(writeReason or "导入未保存") return end self.dialog=nil self:LoadMode(self.mode) self:Toast("存档导入成功") end function App:RestoreBackup() local state,reason=Save.RestoreBackup(self.engine) if not state then self:Toast(reason or "备份不可用") return end local ok,writeReason=Save.Write(self.engine,state) if not ok then self:Toast(writeReason or "恢复未保存") return end self.dialog=nil self:LoadMode(self.mode) end function App:Reset() local state=self.engine.initState() local ok,reason=Save.Write(self.engine,state) if not ok then self:Toast(reason or "重新开始未保存") return end self.dialog=nil self:LoadMode(self.mode) end function App:ToggleAuto() for _,run in ipairs(self.state.runs) do if run.journeyDecision then self:Toast("这趟商路由你决定,领完镖银后再托管") return end end self.autoOn=not self.autoOn self:CloseDialog() end ---@param dt number function App:Update(dt) if self.pendingScroll then UI.Layout() if self.content then self.content:UpdateContentSize() self.content:SetScroll(self.pendingScroll.x,self.pendingScroll.y) end if self.dialogContent then self.dialogContent:SetScroll(self.pendingScroll.modalX,self.pendingScroll.modalY) end self.pendingScroll=nil end self.messageTime=math.max(0,self.messageTime-dt) if self.messageTime==0 and self.message~="" then self.message="" if self.toastPanel then self.toastPanel:SetVisible(false) end end self.saveAccumulator=self.saveAccumulator+dt -- Every modal pauses all journeys and the steward. Reading time is discarded. local paused=not self.state or self:CurrentDialog()~=nil if paused then self.tickAccumulator=0 else self.tickAccumulator=self.tickAccumulator+dt end if not paused and self.tickAccumulator>=0.2 then local result=self.engine.tick(self.state,self.tickAccumulator) if result then self:Persist() self:Invalidate() end if result and result.legs then local total=0 for _,leg in ipairs(result.legs) do total=total+leg.silver end self:Toast("镖队抵驿 · +"..self.engine.fmtNum(total).." 两") end if self.autoOn and Auto.Step(self) then self:Persist() self:Invalidate() end end if paused or self.tickAccumulator>=0.2 or self:CurrentDialog() then self.tickAccumulator=0 end if self.saveAccumulator>=5 then if self.state then self:Persist() end self.saveAccumulator=0 end if self.width~=graphics:GetWidth() or self.height~=graphics:GetHeight() then self.width,self.height=graphics:GetWidth(),graphics:GetHeight() self:Invalidate() end if self.dirty then self.dirty=false View.Render(self) end if self.facilityStatusUpdate then self.facilityStatusUpdate() end if self.state then for _,run in ipairs(self.state.runs) do local widgets=self.progressWidgets[run.id] if widgets then if widgets.update then widgets.update(run) else widgets.label:SetText(run.done and "已抵达,等待交割" or run.paused and "途中有事待你决定" or "镖队正在赶路 · 约剩 "..math.max(0,math.ceil(run.duration*run.effects.time-run.elapsed)).." 秒") widgets.bar:SetValue(math.min(1,run.elapsed/(run.duration*run.effects.time))) end end end end end function App:Shutdown() if self.state then self:Persist() end end return App