feat(m5): client-server sync, offline queue and replay playback
This commit is contained in:
+21
-11
@@ -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 },
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user