Add offline battle estimates and passive phone sample analysis
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
"""Compare local battle predictions with passively captured settlement requests.
|
||||
|
||||
Input and output are local files. No requests are sent or replayed.
|
||||
"""
|
||||
from collections import Counter
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
from pathlib import Path
|
||||
import statistics
|
||||
|
||||
OUTCOMES = frozenset(("battleService/battleEnd", "taskService/taskCompleteStep"))
|
||||
CLIENT_VARIANTS = ("unknown", "original", "modified")
|
||||
|
||||
|
||||
def observations(events):
|
||||
pending, replies, notes, seen = {}, {}, Counter(), set()
|
||||
for event in events:
|
||||
operation = event.get("operation")
|
||||
if operation not in OUTCOMES:
|
||||
continue
|
||||
key = (operation, str(event.get("request_id") or ""))
|
||||
body = event.get("body")
|
||||
if event.get("kind") == "request" and isinstance(body, dict) and isinstance(body.get("battleData"), dict):
|
||||
if not key[1]:
|
||||
notes["request_without_id"] += 1
|
||||
continue
|
||||
if key in pending:
|
||||
notes["duplicate_request"] += 1
|
||||
continue
|
||||
pending[key] = event
|
||||
elif event.get("kind") == "response" and isinstance(body, dict):
|
||||
replies[key] = event
|
||||
elif event.get("kind") == "error":
|
||||
notes["capture_error"] += 1
|
||||
samples = []
|
||||
fields = ("win", "leftTime", "aliveNum", "apLeft", "killMonster", "killNum", "totalDamage", "deadNum", "bossDamage", "cheatNum")
|
||||
for key, request in pending.items():
|
||||
reply = replies.get(key)
|
||||
if reply is None:
|
||||
notes["settlement_response_missing"] += 1
|
||||
continue
|
||||
body = reply["body"]
|
||||
if type(body.get("resCode")) is not int or body["resCode"] != 0:
|
||||
notes["settlement_not_successful"] += 1
|
||||
continue
|
||||
battle = request["body"]["battleData"]
|
||||
if (battle.get("win") is True and not battle.get("killMonster")
|
||||
and all(battle.get(k, 0) == 0 for k in ("leftTime", "aliveNum", "apLeft", "killNum", "totalDamage", "deadNum", "bossDamage"))):
|
||||
notes["sweep_or_ambiguous_empty_result"] += 1
|
||||
continue
|
||||
ids = battle.get("battleMonIds")
|
||||
if not isinstance(ids, list) or any(type(mid) is not int or mid <= 0 for mid in ids):
|
||||
notes["invalid_roster"] += 1
|
||||
continue
|
||||
position = battle.get("battlePosition")
|
||||
if not isinstance(position, dict):
|
||||
notes["position_missing"] += 1
|
||||
continue
|
||||
identity = (key[0], request["request_id"])
|
||||
if identity in seen:
|
||||
continue
|
||||
seen.add(identity)
|
||||
samples.append({"sample_id": request["request_id"], "operation": key[0], "time": request.get("time"),
|
||||
"capture_origin": request.get("capture_origin", "unspecified"),
|
||||
"position": {k: position.get(k) for k in ("battleType", "stageId", "stageLevel", "taskTypeId", "taskStep")},
|
||||
"roster_instance_ids": ids,
|
||||
"reported": {k: battle.get(k) for k in fields},
|
||||
"response": {k: body.get(k) for k in ("costTi", "resCode")}})
|
||||
for key in replies.keys() - pending.keys():
|
||||
notes["response_without_settlement_request"] += 1
|
||||
return samples, dict(notes)
|
||||
|
||||
|
||||
def compare(estimate, sample, *, client_variant="unknown"):
|
||||
if estimate.get("format") != "myrzx-offline-basic-attack-estimate":
|
||||
raise ValueError("Not a supported estimate report")
|
||||
if client_variant not in CLIENT_VARIANTS:
|
||||
raise ValueError("Unknown client variant")
|
||||
if client_variant == "modified":
|
||||
return {"sample_id": sample["sample_id"], "status": "modified_client_not_calibratable"}
|
||||
position, stage = sample["position"], estimate["stage"]
|
||||
if position.get("battleType") != "stage":
|
||||
return {"sample_id": sample["sample_id"], "status": "task_context_requires_separate_mapping"}
|
||||
if str(position.get("stageId")) != str(stage["id"]) or str(position.get("stageLevel")) != str(stage["difficulty"]):
|
||||
return {"sample_id": sample["sample_id"], "status": "different_stage_or_difficulty"}
|
||||
supplied = [p.get("instance_id") for p in estimate.get("players", [])]
|
||||
roster_matches = supplied == sample["roster_instance_ids"] if supplied and all(mid is not None for mid in supplied) else None
|
||||
if roster_matches is False:
|
||||
return {"sample_id": sample["sample_id"], "status": "different_roster"}
|
||||
reported = sample["reported"]
|
||||
limit, remaining = estimate.get("configured_time_limit_seconds"), reported.get("leftTime")
|
||||
if (type(limit) not in (int, float) or type(remaining) not in (int, float)
|
||||
or not math.isfinite(limit) or not math.isfinite(remaining)
|
||||
or limit <= 0 or not 0 <= remaining <= limit * 1000 or type(reported.get("win")) is not bool):
|
||||
return {"sample_id": sample["sample_id"], "status": "invalid_or_missing_timing"}
|
||||
elapsed = limit - remaining / 1000
|
||||
outcome = "win" if reported["win"] else "loss"
|
||||
prediction = estimate.get("duration_seconds_by_predicted_outcome", {}).get(outcome)
|
||||
status = ("exploratory_client_unverified" if client_variant == "unknown" else
|
||||
"compared" if roster_matches else "exploratory_roster_unverified")
|
||||
result = {"sample_id": sample["sample_id"], "status": status,
|
||||
"observed_outcome": outcome, "observed_clock_elapsed_seconds": elapsed,
|
||||
"roster_matches": roster_matches,
|
||||
"same_attributes_and_resource_version_verified": False,
|
||||
"model_has_samples_for_observed_outcome": prediction is not None}
|
||||
if prediction:
|
||||
delta = prediction["median"] - elapsed
|
||||
result.update(model_median_seconds=prediction["median"], signed_time_error_seconds=delta,
|
||||
absolute_time_error_seconds=abs(delta), relative_time_error=abs(delta) / elapsed if elapsed else None,
|
||||
within_model_p10_p90=prediction["p10"] <= elapsed <= prediction["p90"])
|
||||
return result
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--capture", type=Path, required=True, nargs="+")
|
||||
parser.add_argument("--estimate", type=Path, help="Optional model report; omit to summarize protocol samples only")
|
||||
parser.add_argument("--client-variant", choices=CLIENT_VARIANTS, default="unknown",
|
||||
help="Operator-reported capture client, not independently verified")
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
args = parser.parse_args(argv)
|
||||
try:
|
||||
events = []
|
||||
for file in args.capture:
|
||||
for line in file.read_text(encoding="utf-8").splitlines():
|
||||
if line.strip():
|
||||
event = json.loads(line)
|
||||
if isinstance(event, dict):
|
||||
events.append(event)
|
||||
samples, diagnostics = observations(events)
|
||||
estimate = json.loads(args.estimate.read_text(encoding="utf-8")) if args.estimate else None
|
||||
comparisons = ([compare(estimate, sample, client_variant=args.client_variant) for sample in samples]
|
||||
if estimate else [{"sample_id": sample["sample_id"], "status": "model_not_provided"} for sample in samples])
|
||||
aligned = [row for row in comparisons if row["status"] == "compared"]
|
||||
errors = [row["relative_time_error"] for row in aligned if row.get("relative_time_error") is not None]
|
||||
report = {"format": "myrzx-local-battle-comparison", "version": 1,
|
||||
"capture_client_variant": args.client_variant, "client_variant_source": "operator_report",
|
||||
"accepted_settlement_sample_count": len(samples), "aligned_roster_comparison_count": len(aligned),
|
||||
"median_relative_time_error": statistics.median(errors) if errors else None,
|
||||
"diagnostics": diagnostics, "observations": samples, "comparisons": comparisons,
|
||||
"limitations": ["Captured success validates that captured request, not a future simulated result.",
|
||||
"Modified-client observations are protocol evidence, not original-damage calibration data.",
|
||||
"Matching instance IDs do not verify identical attributes, equipment or hot-update resources.",
|
||||
"Time uses configured battle limit minus reported leftTime in milliseconds, not HTTP wall time.",
|
||||
"Samples and model intervals do not establish server verification rules."]}
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
print(json.dumps({k: report[k] for k in ("accepted_settlement_sample_count", "aligned_roster_comparison_count",
|
||||
"median_relative_time_error", "diagnostics")}, ensure_ascii=True))
|
||||
return 0
|
||||
except (ValueError, TypeError, KeyError, OSError) as error:
|
||||
print(json.dumps({"error": type(error).__name__, "message": str(error)}))
|
||||
return 2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user