221 lines
9.1 KiB
GDScript
221 lines
9.1 KiB
GDScript
class_name P98BossService
|
|
extends "res://p9_8/round_service.gd"
|
|
|
|
const BossState = preload("res://p9_8/boss_state.gd")
|
|
|
|
const CLEAR_ID := "CLEAR-RG01-C01"
|
|
const RENOWN_GRANT_ID := "RENOWN-RG01-C01"
|
|
const RECORD_ID := "RECORD-RG01-C01"
|
|
const RENOWN_REWARD := 3
|
|
|
|
|
|
func commit_boss_plan(operation_id: String, plan: String) -> Dictionary:
|
|
var intent := {"boss_hash": String(state.boss_muster.get("boss_hash", "")), "plan": plan, "deployed_recruit_ids": state.deployed_recruit_ids.duplicate()}
|
|
var prior := _prior_operation(operation_id, "commit_boss_plan", intent)
|
|
if prior.known:
|
|
return prior.result
|
|
if state.contract_phase != ContractState.ContractPhase.BOSS_MUSTER or state.boss_muster.is_empty():
|
|
return _reject("boss_muster_required")
|
|
if state.recovery_required:
|
|
return _reject("recovery_required")
|
|
if plan not in ["rotate", "brace"]:
|
|
return _reject("boss_plan_invalid")
|
|
var legal := false
|
|
for row in state.boss_muster.get("plans", []):
|
|
if String(row.get("id", "")) == plan:
|
|
legal = true
|
|
if not legal or state.deployed_recruit_ids.is_empty():
|
|
return _reject("boss_plan_unavailable")
|
|
var checkpoint: Dictionary = state.snapshot()
|
|
state.selected_boss_plan = plan
|
|
state.phase = P97State.Phase.ENCOUNTER
|
|
state.contract_phase = ContractState.ContractPhase.BOSS_ENCOUNTER
|
|
state.battle_checkpoint = checkpoint
|
|
var receipt := {
|
|
"index": state.boss_plan_receipts.size(), "operation_id": operation_id,
|
|
"boss_id": "BO-R1-01", "boss_hash": String(state.boss_muster.boss_hash),
|
|
"plan": plan, "deployed_recruit_ids": state.deployed_recruit_ids.duplicate(),
|
|
}
|
|
state.boss_plan_receipts.append(receipt)
|
|
return _commit_operation(operation_id, "commit_boss_plan", intent, "boss_plan_committed", receipt)
|
|
|
|
|
|
func settle_boss(operation_id: String, boss_snapshot: Dictionary) -> Dictionary:
|
|
var intent := {
|
|
"boss_id": String(boss_snapshot.get("boss_id", "")),
|
|
"plan": String(boss_snapshot.get("plan", "")),
|
|
"revision": int(boss_snapshot.get("revision", -1)),
|
|
"tick": int(boss_snapshot.get("tick", -1)),
|
|
"outcome": String(boss_snapshot.get("outcome", "")),
|
|
"boss_hp": int(boss_snapshot.get("boss_hp", -1)),
|
|
"member_hp_hash": JSON.stringify(boss_snapshot.get("member_hp", {})).sha256_text(),
|
|
}
|
|
var prior := _prior_operation(operation_id, "settle_boss", intent)
|
|
if prior.known:
|
|
return prior.result
|
|
var validation := _validate_boss_snapshot(boss_snapshot)
|
|
if not validation.ok:
|
|
return _reject(String(validation.code))
|
|
var outcome := String(boss_snapshot.outcome)
|
|
var injured: Array[String] = []
|
|
for recruit_id in state.deployed_recruit_ids:
|
|
var hp := int(boss_snapshot.member_hp[recruit_id])
|
|
state.member_condition[recruit_id] = hp
|
|
if hp <= 0:
|
|
injured.append(recruit_id)
|
|
if outcome == "victory":
|
|
state.pending_boss_result = {
|
|
"id": "P98-BOSS-RESULT-BO-R1-01",
|
|
"outcome": "victory", "boss_id": "BO-R1-01",
|
|
"boss_hash": String(state.boss_muster.boss_hash),
|
|
"plan": state.selected_boss_plan, "tick": int(boss_snapshot.tick),
|
|
"clear_id": CLEAR_ID, "renown_grant_id": RENOWN_GRANT_ID,
|
|
"renown": RENOWN_REWARD,
|
|
}
|
|
state.phase = P97State.Phase.MARKET
|
|
state.contract_phase = ContractState.ContractPhase.CONTRACT_RESULT
|
|
_set_recovery("", [], 0, "")
|
|
else:
|
|
state.pending_boss_result.clear()
|
|
state.selected_boss_plan = ""
|
|
state.phase = P97State.Phase.MARKET
|
|
state.contract_phase = ContractState.ContractPhase.BOSS_MUSTER
|
|
_set_recovery("boss_defeat", injured, int(content.bosses["BO-R1-01"].recovery_cost), "")
|
|
state.battle_checkpoint.clear()
|
|
var receipt := {
|
|
"index": state.boss_result_receipts.size(), "operation_id": operation_id,
|
|
"boss_id": "BO-R1-01", "boss_hash": String(state.boss_muster.boss_hash),
|
|
"plan": String(boss_snapshot.plan), "outcome": outcome,
|
|
"tick": int(boss_snapshot.tick), "boss_hp": int(boss_snapshot.boss_hp),
|
|
"member_hp": Dictionary(boss_snapshot.member_hp).duplicate(true),
|
|
"injured_recruit_ids": injured.duplicate(),
|
|
"recovery_cost": int(content.bosses["BO-R1-01"].recovery_cost) if outcome == "defeat" else 0,
|
|
"clear_id": CLEAR_ID if outcome == "victory" else "",
|
|
}
|
|
state.boss_result_receipts.append(receipt)
|
|
return _commit_operation(operation_id, "settle_boss", intent, "boss_settled", receipt)
|
|
|
|
|
|
func close_contract(operation_id: String) -> Dictionary:
|
|
var intent := {"clear_id": CLEAR_ID, "renown_grant_id": RENOWN_GRANT_ID}
|
|
var prior := _prior_operation(operation_id, "close_contract", intent)
|
|
if prior.known:
|
|
return prior.result
|
|
if state.contract_phase != ContractState.ContractPhase.CONTRACT_RESULT or String(state.pending_boss_result.get("outcome", "")) != "victory":
|
|
return _reject("pending_boss_clear_required")
|
|
if CLEAR_ID in state.contract_clear_ids or RENOWN_GRANT_ID in state.discovery_ids:
|
|
return _reject("boss_clear_already_applied")
|
|
var company_record := {
|
|
"id": RECORD_ID,
|
|
"clear_id": CLEAR_ID,
|
|
"boss_id": "BO-R1-01",
|
|
"boss_plan": String(state.pending_boss_result.plan),
|
|
"artifact_id": state.selected_artifact_id,
|
|
"company": _company_record_rows(),
|
|
"rounds_cleared": state.cleared_rounds,
|
|
"routes_completed": state.cleared_rounds,
|
|
"recorded_route_ids": state.route_history.duplicate(),
|
|
}
|
|
state.persistent_renown += RENOWN_REWARD
|
|
state.contract_clear_ids.append(CLEAR_ID)
|
|
for discovery_id in [RENOWN_GRANT_ID, "BOSS-BO-R1-01", "ARTIFACT-%s" % state.selected_artifact_id]:
|
|
if discovery_id not in state.discovery_ids:
|
|
state.discovery_ids.append(discovery_id)
|
|
for receipt in state.promotion_receipts:
|
|
var discovery_id := "PROMOTION-%s" % String(receipt.promotion_id)
|
|
if discovery_id not in state.discovery_ids:
|
|
state.discovery_ids.append(discovery_id)
|
|
state.company_records.append(company_record)
|
|
var receipt := {
|
|
"index": state.close_receipts.size(), "operation_id": operation_id,
|
|
"clear_id": CLEAR_ID, "renown_grant_id": RENOWN_GRANT_ID,
|
|
"renown": RENOWN_REWARD, "renown_after": state.persistent_renown,
|
|
"record_id": RECORD_ID, "discoveries": state.discovery_ids.duplicate(),
|
|
}
|
|
_clear_run_only_state()
|
|
state.close_receipts.append(receipt)
|
|
state.operation_receipts.clear()
|
|
return _commit_operation(operation_id, "close_contract", intent, "contract_closed", receipt)
|
|
|
|
|
|
func _validate_boss_snapshot(raw: Dictionary) -> Dictionary:
|
|
if state.contract_phase != ContractState.ContractPhase.BOSS_ENCOUNTER or state.selected_boss_plan.is_empty():
|
|
return {"ok": false, "code": "boss_encounter_required"}
|
|
var candidate = BossState.new()
|
|
if not candidate.restore_snapshot(raw):
|
|
return {"ok": false, "code": "boss_snapshot_invalid"}
|
|
if not candidate.terminal or candidate.outcome not in ["victory", "defeat"]:
|
|
return {"ok": false, "code": "boss_not_settleable"}
|
|
if candidate.boss_id != "BO-R1-01" or candidate.plan != state.selected_boss_plan:
|
|
return {"ok": false, "code": "boss_snapshot_mismatch"}
|
|
if not _same_members(candidate.member_ids, state.deployed_recruit_ids):
|
|
return {"ok": false, "code": "boss_company_mismatch"}
|
|
return {"ok": true}
|
|
|
|
|
|
func _company_record_rows() -> Array[Dictionary]:
|
|
var rows: Array[Dictionary] = []
|
|
for recruit_id in state.company:
|
|
var record: Dictionary = state.recruit_records[recruit_id]
|
|
var equipment_instance_id := String(state.equipped_by_recruit.get(recruit_id, ""))
|
|
var equipment_catalog_id := String(state.inventory.get(equipment_instance_id, {}).get("catalog_id", ""))
|
|
rows.append({
|
|
"recruit_id": recruit_id, "name_zh": String(record.get("name_zh", recruit_id)),
|
|
"name_en": String(record.get("name_en", recruit_id)),
|
|
"profession_id": String(record.profession_id),
|
|
"level": int(record.level), "xp": int(record.xp),
|
|
"survival_battles": int(record.get("survival_battles", 0)),
|
|
"final_hp": int(state.member_condition.get(recruit_id, 0)),
|
|
"equipment_instance_id": equipment_instance_id,
|
|
"equipment_catalog_id": equipment_catalog_id,
|
|
})
|
|
return rows
|
|
|
|
|
|
func _clear_run_only_state() -> void:
|
|
state.phase = P97State.Phase.MARKET
|
|
state.contract_phase = ContractState.ContractPhase.RUN_CLOSED
|
|
state.run_closed = true
|
|
state.coin = 0
|
|
state.company.clear()
|
|
state.deployed_recruit_ids.clear()
|
|
state.reserve_recruit_ids.clear()
|
|
state.recruit_records.clear()
|
|
state.member_condition.clear()
|
|
state.inventory.clear()
|
|
state.equipped_by_recruit.clear()
|
|
state.market_board.clear()
|
|
state.available_recruit_offer_ids.clear()
|
|
state.available_equipment_offer_ids.clear()
|
|
state.available_battle_offer_ids.clear()
|
|
state.focused_recruit_offer = ""
|
|
state.focused_equipment_offer = ""
|
|
state.focused_route_offer = ""
|
|
state.pending_dismissal.clear()
|
|
state.selected_route = ""
|
|
state.route_history.clear()
|
|
state.hire_receipts.clear()
|
|
state.purchase_receipts.clear()
|
|
state.equip_receipts.clear()
|
|
state.dismissal_receipts.clear()
|
|
state.deployment_receipts.clear()
|
|
state.route_receipts.clear()
|
|
state.battle_settlement_receipts.clear()
|
|
state.recovery_receipts.clear()
|
|
state.recruits_hired_total = 0
|
|
state.recruits_dismissed_total = 0
|
|
state.relay_restored = false
|
|
state.battle_checkpoint.clear()
|
|
_set_recovery("", [], 0, "")
|
|
state.continuation_locked = false
|
|
state.artifact_draft.clear()
|
|
state.selected_artifact_id = ""
|
|
state.artifact_receipts.clear()
|
|
state.promotion_draft.clear()
|
|
state.promotion_receipts.clear()
|
|
state.boss_muster.clear()
|
|
state.selected_boss_plan = ""
|
|
state.boss_plan_receipts.clear()
|
|
state.pending_boss_result.clear()
|
|
state.boss_result_receipts.clear()
|