feat(m5): client-server sync, offline queue and replay playback

This commit is contained in:
2026-10-24 23:37:13 +02:00
parent 4a9031b29c
commit 33bdc16fbd
22 changed files with 1419 additions and 184 deletions
+21 -11
View File
@@ -106,6 +106,16 @@ impl AppState {
}
}
/// Encodes a 64 bit hash as fixed-width hex (JSON numbers lose precision above 2^53).
fn hex64(value: u64) -> String {
format!("{value:016x}")
}
fn parse_hash(value: &str) -> Result<u64, ApiError> {
u64::from_str_radix(value, 16)
.map_err(|_| ApiError::bad_request("bad_hash", "expected 16 hex chars"))
}
fn hex(bytes: &[u8]) -> String {
let mut out = String::with_capacity(bytes.len() * 2);
for b in bytes {
@@ -288,7 +298,7 @@ struct CreateMatchResponse {
match_id: String,
seed: u64,
turn: u32,
state_hash: u64,
state_hash: String,
}
async fn create_match(
@@ -359,7 +369,7 @@ async fn create_match(
match_id: id,
seed,
turn: 1,
state_hash,
state_hash: hex64(state_hash),
}),
))
}
@@ -371,7 +381,7 @@ struct MatchStateBody {
current: u8,
phase: u8,
hp: [i32; 2],
state_hash: u64,
state_hash: String,
}
async fn get_match(
@@ -392,7 +402,7 @@ async fn get_match(
Phase::Finished => 2,
},
hp: [sim.tanks[0].hp, sim.tanks[1].hp],
state_hash: sim.state_hash(),
state_hash: hex64(sim.state_hash()),
})
};
if let Some(body) = live_body {
@@ -413,13 +423,13 @@ async fn get_match(
current: 0,
phase: 0,
hp: [0, 0],
state_hash: row.state_hash,
state_hash: hex64(row.state_hash),
}))
}
#[derive(Deserialize)]
struct TurnRequest {
expect_hash: u64,
expect_hash: String,
command: sim::replay::CommandDto,
}
@@ -428,7 +438,7 @@ struct TurnResponse {
accepted: bool,
turn: u32,
current: u8,
state_hash: u64,
state_hash: String,
impacts: u32,
}
@@ -447,7 +457,7 @@ async fn submit_turn(
if sim.phase != Phase::Aiming {
return Err(ApiError::conflict("state_conflict", "no turn is open"));
}
if sim.state_hash() != body.expect_hash {
if sim.state_hash() != parse_hash(&body.expect_hash)? {
return Err(ApiError::conflict(
"state_conflict",
"expect_hash does not match",
@@ -483,7 +493,7 @@ async fn submit_turn(
accepted: true,
turn,
current,
state_hash,
state_hash: hex64(state_hash),
impacts,
}))
}
@@ -503,7 +513,7 @@ struct Rewards {
#[derive(Serialize)]
struct ValidateResponse {
valid: bool,
final_hash: u64,
final_hash: String,
rewards: Rewards,
}
@@ -531,7 +541,7 @@ async fn validate_replay(
})?;
Ok(Json(ValidateResponse {
valid: true,
final_hash: hash,
final_hash: hex64(hash),
rewards: Rewards { scrap: 120, xp: 35 },
}))
}
+7 -4
View File
@@ -99,12 +99,12 @@ async fn create_match_and_play_a_turn() {
let (status, body) = call(&app, req).await;
assert_eq!(status, StatusCode::CREATED, "body: {body}");
let match_id = body["match_id"].as_str().unwrap().to_string();
let state_hash = body["state_hash"].as_u64().unwrap();
let state_hash = body["state_hash"].as_str().unwrap().to_string();
assert!(state.has_live_match(&match_id));
// Wrong expect_hash must be rejected.
let turn = json!({
"expect_hash": state_hash ^ 0xFF,
"expect_hash": "ffffffffffffffff",
"command": {"kind": "fire", "angle": 45000, "power": 65536, "w": 1}
});
let req = Request::builder()
@@ -134,7 +134,7 @@ async fn create_match_and_play_a_turn() {
assert_eq!(status, StatusCode::OK, "body: {body}");
assert_eq!(body["accepted"], true);
assert_eq!(body["turn"], 2);
assert!(body["state_hash"].as_u64().unwrap() != state_hash);
assert_ne!(body["state_hash"].as_str().unwrap(), state_hash);
}
fn build_replay(seed: u64) -> ReplayV1 {
@@ -175,7 +175,10 @@ async fn valid_replay_is_accepted() {
let (status, body) = call(&app, req).await;
assert_eq!(status, StatusCode::OK, "body: {body}");
assert_eq!(body["valid"], true);
assert_eq!(body["final_hash"].as_u64().unwrap(), replay.final_hash);
assert_eq!(
body["final_hash"].as_str().unwrap(),
format!("{:016x}", replay.final_hash)
);
}
#[tokio::test]