feat(client): 锁定终局结果并接入 Windows 签名构建
- fix(match): 首次归零结束对局并撤销双方扣血许可,裁判结果同步后不再改判 - fix(hud): 蓝方低血量始终保留队伍颜色 - build(windows): 校验代码签名身份和时间戳,明确标注未签名测试包 - docs(version): 说明发布标签基线与语义版本迭代
This commit is contained in:
26 files changed
+823
-229
No files matched your search
@@ -113,6 +113,7 @@ pub struct ArmorReport {
|
||||
/// 在接收线程里被调用,所以必须是非阻塞的。Tauri 层在这里做掉血检测并把结果推给界面。
|
||||
pub type ArmorSink = Arc<dyn Fn(ArmorReport) + Send + Sync>;
|
||||
pub type MatchSink = Arc<dyn Fn(protocol::MatchState, Ipv4Addr) + Send + Sync>;
|
||||
pub type MatchResultSink = Arc<dyn Fn(protocol::MatchResult, Ipv4Addr) + Send + Sync>;
|
||||
|
||||
/// 已收到的最新血量。
|
||||
#[derive(Debug, Default)]
|
||||
@@ -199,6 +200,7 @@ pub struct ArmorHealthStore {
|
||||
sink: Mutex<Option<ArmorSink>>,
|
||||
assignments: Mutex<Vec<(u32, u8)>>,
|
||||
match_sink: Mutex<Option<MatchSink>>,
|
||||
match_result_sink: Mutex<Option<MatchResultSink>>,
|
||||
damage_participants: Mutex<HashSet<u32>>,
|
||||
reset: Mutex<Option<crate::armor_reset::ArmorResetTracker>>,
|
||||
}
|
||||
@@ -217,6 +219,7 @@ impl ArmorHealthStore {
|
||||
sink: Mutex::new(None),
|
||||
assignments: Mutex::new(Vec::new()),
|
||||
match_sink: Mutex::new(None),
|
||||
match_result_sink: Mutex::new(None),
|
||||
damage_participants: Mutex::new(HashSet::new()),
|
||||
reset: Mutex::new(None),
|
||||
}
|
||||
@@ -284,6 +287,17 @@ impl ArmorHealthStore {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if let Some(packet) = protocol::MatchResult::decode(bytes) {
|
||||
if let Some(sink) = self
|
||||
.match_result_sink
|
||||
.lock()
|
||||
.expect("match result sink poisoned")
|
||||
.clone()
|
||||
{
|
||||
sink(packet, *addr.ip());
|
||||
}
|
||||
return;
|
||||
}
|
||||
if let Some(packet) = protocol::ArmorReset::decode(bytes) {
|
||||
if let Some(reset) = self.reset.lock().expect("armor reset poisoned").as_mut() {
|
||||
reset.acknowledge(packet, addr);
|
||||
@@ -404,6 +418,12 @@ impl ArmorHealthStore {
|
||||
pub fn set_match_sink(&self, sink: MatchSink) {
|
||||
*self.match_sink.lock().expect("match sink poisoned") = Some(sink);
|
||||
}
|
||||
pub fn set_match_result_sink(&self, sink: MatchResultSink) {
|
||||
*self
|
||||
.match_result_sink
|
||||
.lock()
|
||||
.expect("match result sink poisoned") = Some(sink);
|
||||
}
|
||||
|
||||
pub fn set_sink(&self, sink: ArmorSink) {
|
||||
*self.sink.lock().expect("armor sink mutex poisoned") = Some(sink);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! 裁判计时规则与选手端快照跟随。所有时间来自调用方的单调时钟。
|
||||
//! UDP 会话锁定到首次收到的来源;掉线不自动换裁判,需显式重新接收。
|
||||
use crate::snapshot::{resolve_outcomes, MatchOutcome, TeamHealthDto};
|
||||
use protocol::MatchState;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::net::Ipv4Addr;
|
||||
@@ -53,6 +54,7 @@ pub struct MatchClock {
|
||||
at: u64,
|
||||
source: Option<(Ipv4Addr, u64)>,
|
||||
received_at: Option<u64>,
|
||||
result: Option<protocol::MatchResult>,
|
||||
}
|
||||
|
||||
fn idle(session: u64) -> MatchState {
|
||||
@@ -117,6 +119,7 @@ impl MatchClock {
|
||||
at: now,
|
||||
source: None,
|
||||
received_at: None,
|
||||
result: None,
|
||||
}
|
||||
}
|
||||
pub fn set_authority(&mut self, authority: bool, now: u64) {
|
||||
@@ -128,6 +131,7 @@ impl MatchClock {
|
||||
self.at = now;
|
||||
self.source = None;
|
||||
self.received_at = None;
|
||||
self.result = None;
|
||||
}
|
||||
}
|
||||
fn advance(&mut self, now: u64) {
|
||||
@@ -156,7 +160,7 @@ impl MatchClock {
|
||||
}
|
||||
self.advance(now);
|
||||
match action {
|
||||
MatchAction::Prepare if matches!(self.state.phase, 0 | 6) => {
|
||||
MatchAction::Prepare if matches!(self.state.phase, 0 | 6) && self.result.is_none() => {
|
||||
let seq = self.state.sequence;
|
||||
self.state = idle(self.local_session);
|
||||
self.state.sequence = seq;
|
||||
@@ -199,6 +203,7 @@ impl MatchClock {
|
||||
let seq = self.state.sequence;
|
||||
self.state = idle(self.local_session);
|
||||
self.state.sequence = seq;
|
||||
self.result = None;
|
||||
}
|
||||
_ => {
|
||||
return Err(
|
||||
@@ -223,6 +228,109 @@ impl MatchClock {
|
||||
self.state.sequence = self.state.sequence.saturating_add(1);
|
||||
Ok(self.state.clone())
|
||||
}
|
||||
|
||||
/// 首次归零结束正在进行的对局;后续上报不得重新结算。
|
||||
pub fn finish_on_health(&mut self, hp: [Option<u16>; 2], now: u64) -> bool {
|
||||
if !self.authority || self.result.is_some() {
|
||||
return false;
|
||||
}
|
||||
self.advance(now);
|
||||
let playing =
|
||||
self.state.phase == 4 || (self.state.phase == 5 && self.state.resume_phase == 4);
|
||||
if !playing || !hp.contains(&Some(0)) {
|
||||
return false;
|
||||
}
|
||||
let outcomes = match hp {
|
||||
[Some(0), Some(0)] => [3, 3],
|
||||
[Some(0), Some(hp)] if hp > 0 => [2, 1],
|
||||
[Some(hp), Some(0)] if hp > 0 => [1, 2],
|
||||
[Some(0), _] => [2, 0],
|
||||
_ => [0, 2],
|
||||
};
|
||||
self.state.phase = 6;
|
||||
clear_pause(&mut self.state);
|
||||
self.state.sequence = self.state.sequence.saturating_add(1);
|
||||
self.result = Some(protocol::MatchResult {
|
||||
session: self.state.session,
|
||||
sequence: self.state.sequence,
|
||||
result_id: self.state.sequence,
|
||||
remaining_ms: self.state.remaining_ms,
|
||||
elapsed_ms: self.state.elapsed_ms,
|
||||
outcomes,
|
||||
});
|
||||
true
|
||||
}
|
||||
|
||||
pub fn result_packet(&self) -> Option<protocol::MatchResult> {
|
||||
if !self.authority || self.state.phase != 6 {
|
||||
return None;
|
||||
}
|
||||
self.result.map(|result| protocol::MatchResult {
|
||||
sequence: self.state.sequence,
|
||||
..result
|
||||
})
|
||||
}
|
||||
|
||||
/// 终局只投影锁定结论,不篡改装甲板的真实血量。正式选手端以裁判结论为准。
|
||||
pub fn apply_outcomes(&self, health: &mut [TeamHealthDto]) {
|
||||
for entry in health.iter_mut() {
|
||||
entry.outcome = None;
|
||||
}
|
||||
if let Some(result) = self.result {
|
||||
for entry in health {
|
||||
entry.outcome = match result.outcomes.get(usize::from(entry.team)) {
|
||||
Some(1) => Some(MatchOutcome::Victory),
|
||||
Some(2) => Some(MatchOutcome::Defeat),
|
||||
Some(3) => Some(MatchOutcome::Draw),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
} else if (self.authority && self.state.phase != 6)
|
||||
|| (!self.authority && self.source.is_none())
|
||||
{
|
||||
resolve_outcomes(health);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn receive_result(
|
||||
&mut self,
|
||||
result: protocol::MatchResult,
|
||||
ip: Ipv4Addr,
|
||||
now: u64,
|
||||
) -> bool {
|
||||
if self.authority || !result.valid() {
|
||||
return false;
|
||||
}
|
||||
if let Some(previous) = self.result {
|
||||
if previous.session == result.session
|
||||
&& (result.result_id < previous.result_id
|
||||
|| (result.result_id == previous.result_id
|
||||
&& (result.outcomes != previous.outcomes
|
||||
|| result.remaining_ms != previous.remaining_ms
|
||||
|| result.elapsed_ms != previous.elapsed_ms)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
let same_snapshot = self.source == Some((ip, result.session))
|
||||
&& self.state.sequence == result.sequence
|
||||
&& self.state.phase == 6
|
||||
&& self.state.remaining_ms == result.remaining_ms
|
||||
&& self.state.elapsed_ms == result.elapsed_ms;
|
||||
if !same_snapshot {
|
||||
let mut state = idle(result.session);
|
||||
state.sequence = result.sequence;
|
||||
state.phase = 6;
|
||||
state.remaining_ms = result.remaining_ms;
|
||||
state.elapsed_ms = result.elapsed_ms;
|
||||
if !self.receive(state, ip, now) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
let changed = self.result != Some(result);
|
||||
self.result = Some(result);
|
||||
changed
|
||||
}
|
||||
fn pause(&mut self, kind: u8, team: u8, reason: String) -> Result<(), String> {
|
||||
if !(1..=4).contains(&self.state.phase) {
|
||||
return Err("当前阶段不能申请技术暂停".into());
|
||||
@@ -258,6 +366,9 @@ impl MatchClock {
|
||||
}
|
||||
}
|
||||
self.source = Some((ip, packet.session));
|
||||
if packet.phase != 6 {
|
||||
self.result = None;
|
||||
}
|
||||
self.state = packet;
|
||||
self.received_at = Some(now);
|
||||
true
|
||||
@@ -270,6 +381,7 @@ impl MatchClock {
|
||||
self.source = None;
|
||||
self.received_at = None;
|
||||
self.state = idle(self.local_session);
|
||||
self.result = None;
|
||||
Ok(())
|
||||
}
|
||||
pub fn view(&mut self, now: u64) -> MatchView {
|
||||
@@ -315,6 +427,99 @@ mod tests {
|
||||
c.action(MatchAction::Prepare, 0).unwrap();
|
||||
assert_eq!(c.view(180_000).phase, "ready");
|
||||
}
|
||||
fn running(c: &mut MatchClock) {
|
||||
c.action(MatchAction::Prepare, 0).unwrap();
|
||||
c.action(MatchAction::SkipPreparation, 100).unwrap();
|
||||
c.action(MatchAction::Countdown { seconds: 1 }, 100)
|
||||
.unwrap();
|
||||
assert_eq!(c.view(1100).phase, "running");
|
||||
}
|
||||
#[test]
|
||||
fn first_knockout_stops_time_and_locks_result_through_late_health_and_reset_attempts() {
|
||||
let mut c = MatchClock::new(true, 44, 0);
|
||||
assert!(!c.finish_on_health([Some(0), Some(10)], 0));
|
||||
running(&mut c);
|
||||
assert!(c.finish_on_health([Some(0), Some(10)], 2100));
|
||||
let result = c.result_packet().unwrap();
|
||||
assert_eq!(result.outcomes, [2, 1]);
|
||||
assert_eq!(
|
||||
(c.view(10_000).phase, c.view(10_000).elapsed_ms),
|
||||
("finished", 1000)
|
||||
);
|
||||
assert!(!c.finish_on_health([Some(0), Some(0)], 10_000));
|
||||
assert!(!c.finish_on_health([Some(100), Some(0)], 10_000));
|
||||
let names = ["红队".into(), "蓝队".into()];
|
||||
let mut health = [
|
||||
TeamHealthDto::present(0, Some(1), [0; 4], 25, &names),
|
||||
TeamHealthDto::present(1, Some(2), [0; 4], 25, &names),
|
||||
];
|
||||
c.apply_outcomes(&mut health);
|
||||
assert_eq!(health[0].outcome, Some(MatchOutcome::Defeat));
|
||||
assert_eq!(health[1].outcome, Some(MatchOutcome::Victory));
|
||||
assert_eq!(health[1].hp, Some(0), "结论锁定不篡改真实上报");
|
||||
assert!(c.action(MatchAction::Prepare, 10_000).is_err());
|
||||
c.begin_reset(true, 10_000).unwrap();
|
||||
assert_eq!(c.result_packet().unwrap().result_id, result.result_id);
|
||||
c.action(MatchAction::Reset, 10_000).unwrap();
|
||||
assert!(c.result_packet().is_none());
|
||||
c.action(MatchAction::Prepare, 10_000).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn knockout_handles_paused_play_draw_and_missing_opponent_without_inventing_a_win() {
|
||||
for (hp, expected) in [
|
||||
([Some(0), Some(0)], [3, 3]),
|
||||
([Some(0), None], [2, 0]),
|
||||
([None, Some(0)], [0, 2]),
|
||||
] {
|
||||
let mut c = MatchClock::new(true, 44, 0);
|
||||
running(&mut c);
|
||||
c.action(
|
||||
MatchAction::PauseOfficial {
|
||||
reason: "检查".into(),
|
||||
},
|
||||
2100,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(c.finish_on_health(hp, 2200));
|
||||
assert_eq!(c.result_packet().unwrap().outcomes, expected);
|
||||
}
|
||||
assert!(!MatchClock::new(false, 2, 0).finish_on_health([Some(0), Some(10)], 0));
|
||||
}
|
||||
#[test]
|
||||
fn result_sync_is_self_contained_order_independent_and_cannot_revive_after_reset() {
|
||||
let mut referee = MatchClock::new(true, 44, 0);
|
||||
running(&mut referee);
|
||||
referee.finish_on_health([Some(10), Some(0)], 2100);
|
||||
let state = referee.broadcast(2100).unwrap();
|
||||
let result = referee.result_packet().unwrap();
|
||||
for result_first in [false, true] {
|
||||
let mut player = MatchClock::new(false, 2, 0);
|
||||
if result_first {
|
||||
assert!(player.receive_result(result, Ipv4Addr::LOCALHOST, 0));
|
||||
assert!(!player.receive(state.clone(), Ipv4Addr::LOCALHOST, 1));
|
||||
} else {
|
||||
assert!(player.receive(state.clone(), Ipv4Addr::LOCALHOST, 0));
|
||||
assert!(player.receive_result(result, Ipv4Addr::LOCALHOST, 1));
|
||||
}
|
||||
assert_eq!(player.view(1).phase, "finished");
|
||||
assert!(!player.receive_result(result, Ipv4Addr::LOCALHOST, 1400));
|
||||
assert!(!player.view(1500).connected, "重复回执不能续约");
|
||||
assert!(!player.receive_result(
|
||||
protocol::MatchResult {
|
||||
outcomes: [2, 1],
|
||||
..result
|
||||
},
|
||||
Ipv4Addr::LOCALHOST,
|
||||
1501
|
||||
));
|
||||
assert!(!player.receive_result(result, Ipv4Addr::new(1, 2, 3, 4), 1501));
|
||||
referee.action(MatchAction::Reset, 2200).unwrap();
|
||||
let reset = referee.broadcast(2200).unwrap();
|
||||
assert!(player.receive(reset, Ipv4Addr::LOCALHOST, 1600));
|
||||
assert!(!player.receive_result(result, Ipv4Addr::LOCALHOST, 1601));
|
||||
assert!(player.result.is_none());
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn preparation_waits_for_referee_then_countdown_carries_excess_time() {
|
||||
let mut c = MatchClock::new(true, 1, 0);
|
||||
|
||||
@@ -94,3 +94,102 @@ fn udp_snapshot_routes_to_follower_without_polluting_armor_health() {
|
||||
store.datagram(&packet, from);
|
||||
assert_eq!(follower.lock().unwrap().view(1000).remaining_ms, 160_000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn knockout_revokes_both_boards_and_result_survives_late_winner_death() {
|
||||
let tx = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
|
||||
let store = ArmorHealthStore::new();
|
||||
let boards: Vec<_> = (1..=2)
|
||||
.map(|id| {
|
||||
let socket = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
|
||||
socket
|
||||
.set_read_timeout(Some(Duration::from_secs(2)))
|
||||
.unwrap();
|
||||
store.datagram(
|
||||
&protocol::ArmorHealth {
|
||||
team: id - 1,
|
||||
index: 0,
|
||||
hp: 100,
|
||||
}
|
||||
.encode_from(u32::from(id)),
|
||||
socket.local_addr().unwrap(),
|
||||
);
|
||||
socket
|
||||
})
|
||||
.collect();
|
||||
let mut clock = MatchClock::new(true, 80, 0);
|
||||
clock.action(MatchAction::Prepare, 0).unwrap();
|
||||
clock.action(MatchAction::SkipPreparation, 100).unwrap();
|
||||
clock
|
||||
.action(MatchAction::Countdown { seconds: 1 }, 100)
|
||||
.unwrap();
|
||||
let state = clock.broadcast(1100).unwrap();
|
||||
let mut buf = [0; 512];
|
||||
store
|
||||
.send_damage_controls(&tx, &state, [Some(1), Some(2)])
|
||||
.unwrap();
|
||||
for board in &boards {
|
||||
let (n, _) = board.recv_from(&mut buf).unwrap();
|
||||
assert!(
|
||||
protocol::ArmorDamageControl::decode(&buf[..n])
|
||||
.unwrap()
|
||||
.enabled
|
||||
);
|
||||
}
|
||||
assert!(clock.finish_on_health([Some(0), Some(10)], 2100));
|
||||
let state = clock.broadcast(2100).unwrap();
|
||||
store
|
||||
.send_damage_controls(&tx, &state, [Some(1), Some(2)])
|
||||
.unwrap();
|
||||
for board in &boards {
|
||||
let (n, _) = board.recv_from(&mut buf).unwrap();
|
||||
assert!(
|
||||
!protocol::ArmorDamageControl::decode(&buf[..n])
|
||||
.unwrap()
|
||||
.enabled
|
||||
);
|
||||
}
|
||||
assert!(!clock.finish_on_health([Some(0), Some(0)], 2200));
|
||||
let follower = Arc::new(Mutex::new(MatchClock::new(false, 2, 0)));
|
||||
let reader = ArmorHealthStore::new();
|
||||
let follow = follower.clone();
|
||||
reader.set_match_result_sink(Arc::new(move |result, ip| {
|
||||
follow.lock().unwrap().receive_result(result, ip, 2100);
|
||||
}));
|
||||
let rx = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
|
||||
rx.set_read_timeout(Some(Duration::from_secs(2))).unwrap();
|
||||
tx.send_to(
|
||||
&clock.result_packet().unwrap().encode().unwrap(),
|
||||
rx.local_addr().unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let (n, from) = rx.recv_from(&mut buf).unwrap();
|
||||
reader.datagram(&buf[..n], from);
|
||||
assert_eq!(follower.lock().unwrap().view(2100).phase, "finished");
|
||||
assert!(reader.boards().is_empty());
|
||||
for (index, board) in boards.iter().enumerate() {
|
||||
reader.datagram(
|
||||
&protocol::ArmorHealth {
|
||||
team: index as u8,
|
||||
index: 0,
|
||||
hp: 0,
|
||||
}
|
||||
.encode_from(index as u32 + 1),
|
||||
board.local_addr().unwrap(),
|
||||
);
|
||||
}
|
||||
let names = ["红队".into(), "蓝队".into()];
|
||||
let mut health = [
|
||||
cam_core::snapshot::TeamHealthDto::present(0, Some(1), [0; 4], 25, &names),
|
||||
cam_core::snapshot::TeamHealthDto::present(1, Some(2), [0; 4], 25, &names),
|
||||
];
|
||||
follower.lock().unwrap().apply_outcomes(&mut health);
|
||||
assert_eq!(
|
||||
health[0].outcome,
|
||||
Some(cam_core::snapshot::MatchOutcome::Defeat)
|
||||
);
|
||||
assert_eq!(
|
||||
health[1].outcome,
|
||||
Some(cam_core::snapshot::MatchOutcome::Victory)
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user