feat(match): 同步比赛计时与装甲板扣血许可
- feat(client): 增加裁判比赛计时、技术暂停与选手端局域网同步 - feat(armor): 红蓝共用通用固件,按客户端选队并显示网络连接状态 - feat(armor): 仅在对局进行中允许扣血,其他阶段保留受击闪烁 - feat(client): 将计时置于血条中间并增加开赛前五秒全屏动画 - fix(client): 修复连续血条、对手镜像和图传断连灰度遮罩 - fix(client): 统一界面中文字体并整理各页面文案 - fix(maintenance): 备份迁移装甲板分区,恢复 SDK 构建与固件打包 - refactor(repo): 按 armor、vt 和 client 整理工程与脚本,移除旧参考实现 - test: 补充线路、受击许可、回环 UDP、维护和 HUD 验证 - docs: 同步构建、协议、比赛控制和硬件验证边界 验证:前端构建与 19 项相关测试通过;核心 91 项单元测试与 2 项回环 UDP 集成测试通过;装甲板规则 25 项、线路 26 项、Tauri 51 项测试通过;独立探测器 5 项与目录回归 4 项通过。完成 C3 两组阈值实际编译、导出与 ZIP 校验,检查 900×600 / 1280×720 页面、倒计时和随包中文字体。 未执行烧录、板上运行、跨电脑广播或硬件稳定性验证。
This commit is contained in:
581 files changed
+6596
-7683
No files matched your search
@@ -0,0 +1,425 @@
|
||||
//! 裁判计时规则与选手端快照跟随。所有时间来自调用方的单调时钟。
|
||||
//! UDP 会话锁定到首次收到的来源;掉线不自动换裁判,需显式重新接收。
|
||||
use protocol::MatchState;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::net::Ipv4Addr;
|
||||
|
||||
pub const SYNC_LEASE_MS: u64 = 1500;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "camelCase")]
|
||||
pub enum MatchAction {
|
||||
Prepare,
|
||||
Countdown {
|
||||
seconds: u32,
|
||||
},
|
||||
PausePlayer {
|
||||
team: u8,
|
||||
reason: String,
|
||||
approved: bool,
|
||||
},
|
||||
PauseOfficial {
|
||||
reason: String,
|
||||
},
|
||||
Resume,
|
||||
End,
|
||||
Reset,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct MatchView {
|
||||
pub phase: &'static str,
|
||||
pub resume_phase: &'static str,
|
||||
pub pause_kind: Option<&'static str>,
|
||||
pub applicant: Option<u8>,
|
||||
pub reason: String,
|
||||
pub remaining_ms: u32,
|
||||
pub elapsed_ms: u32,
|
||||
pub pause_remaining_ms: Option<u32>,
|
||||
pub pause_elapsed_ms: u32,
|
||||
pub connected: bool,
|
||||
/// 前端最多可从这份快照继续推算多久;超过后冻结并显示断连。
|
||||
pub fresh_for_ms: u64,
|
||||
pub source: Option<String>,
|
||||
pub sync_error: Option<String>,
|
||||
}
|
||||
|
||||
pub struct MatchClock {
|
||||
authority: bool,
|
||||
local_session: u64,
|
||||
state: MatchState,
|
||||
at: u64,
|
||||
source: Option<(Ipv4Addr, u64)>,
|
||||
received_at: Option<u64>,
|
||||
}
|
||||
|
||||
fn idle(session: u64) -> MatchState {
|
||||
MatchState {
|
||||
session: session.max(1),
|
||||
sequence: 0,
|
||||
phase: 0,
|
||||
resume_phase: 0,
|
||||
pause_kind: 0,
|
||||
applicant: 255,
|
||||
remaining_ms: 0,
|
||||
elapsed_ms: 0,
|
||||
pause_remaining_ms: u32::MAX,
|
||||
pause_elapsed_ms: 0,
|
||||
reason: String::new(),
|
||||
}
|
||||
}
|
||||
fn phase(n: u8) -> &'static str {
|
||||
[
|
||||
"idle",
|
||||
"preparation",
|
||||
"ready",
|
||||
"countdown",
|
||||
"running",
|
||||
"paused",
|
||||
"finished",
|
||||
]
|
||||
.get(n as usize)
|
||||
.copied()
|
||||
.unwrap_or("idle")
|
||||
}
|
||||
fn clear_pause(s: &mut MatchState) {
|
||||
s.resume_phase = 0;
|
||||
s.pause_kind = 0;
|
||||
s.applicant = 255;
|
||||
s.reason.clear();
|
||||
s.pause_remaining_ms = u32::MAX;
|
||||
s.pause_elapsed_ms = 0;
|
||||
}
|
||||
fn project(s: &mut MatchState, delta: u64) {
|
||||
let delta = delta.min(u32::MAX as u64) as u32;
|
||||
if matches!(s.phase, 1 | 3 | 4) {
|
||||
let spent = delta.min(s.remaining_ms);
|
||||
s.remaining_ms -= spent;
|
||||
if s.phase == 4 {
|
||||
s.elapsed_ms = (s.elapsed_ms + spent).min(180_000);
|
||||
}
|
||||
} else if s.phase == 5 {
|
||||
s.pause_elapsed_ms = s.pause_elapsed_ms.saturating_add(delta);
|
||||
if s.pause_kind == 1 {
|
||||
s.pause_remaining_ms = s.pause_remaining_ms.saturating_sub(delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MatchClock {
|
||||
pub fn new(authority: bool, session: u64, now: u64) -> Self {
|
||||
Self {
|
||||
authority,
|
||||
local_session: session.max(1),
|
||||
state: idle(session),
|
||||
at: now,
|
||||
source: None,
|
||||
received_at: None,
|
||||
}
|
||||
}
|
||||
pub fn set_authority(&mut self, authority: bool, now: u64) {
|
||||
if self.authority != authority {
|
||||
self.authority = authority;
|
||||
// 新的裁判会话,不能让旧的延迟包恢复上一局。
|
||||
self.local_session = self.local_session.wrapping_add(1).max(1);
|
||||
self.state = idle(self.local_session);
|
||||
self.at = now;
|
||||
self.source = None;
|
||||
self.received_at = None;
|
||||
}
|
||||
}
|
||||
fn advance(&mut self, now: u64) {
|
||||
let delta = now.saturating_sub(self.at);
|
||||
self.at = now;
|
||||
if self.state.phase == 3 && delta >= self.state.remaining_ms as u64 {
|
||||
let rest = delta - self.state.remaining_ms as u64;
|
||||
self.state.phase = 4;
|
||||
self.state.remaining_ms = 180_000;
|
||||
self.state.elapsed_ms = 0;
|
||||
project(&mut self.state, rest);
|
||||
} else {
|
||||
project(&mut self.state, delta);
|
||||
}
|
||||
if self.state.remaining_ms == 0 {
|
||||
if self.state.phase == 1 {
|
||||
self.state.phase = 2;
|
||||
} else if self.state.phase == 4 {
|
||||
self.state.phase = 6;
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn action(&mut self, action: MatchAction, now: u64) -> Result<(), String> {
|
||||
if !self.authority {
|
||||
return Err("只有裁判端可以控制比赛计时".into());
|
||||
}
|
||||
self.advance(now);
|
||||
match action {
|
||||
MatchAction::Prepare if matches!(self.state.phase, 0 | 6) => {
|
||||
let seq = self.state.sequence;
|
||||
self.state = idle(self.local_session);
|
||||
self.state.sequence = seq;
|
||||
self.state.phase = 1;
|
||||
self.state.remaining_ms = 180_000;
|
||||
}
|
||||
MatchAction::Countdown { seconds }
|
||||
if self.state.phase == 2 && (1..=60).contains(&seconds) =>
|
||||
{
|
||||
self.state.phase = 3;
|
||||
self.state.remaining_ms = seconds * 1000;
|
||||
}
|
||||
MatchAction::PausePlayer {
|
||||
team,
|
||||
reason,
|
||||
approved,
|
||||
} => {
|
||||
if !approved {
|
||||
return Err("选手技术暂停须经主裁判批准".into());
|
||||
}
|
||||
if team > 1 {
|
||||
return Err("请选择申请暂停的队伍".into());
|
||||
}
|
||||
self.pause(1, team, reason)?;
|
||||
}
|
||||
MatchAction::PauseOfficial { reason } => self.pause(2, 255, reason)?,
|
||||
MatchAction::Resume if self.state.phase == 5 => {
|
||||
self.state.phase = self.state.resume_phase;
|
||||
clear_pause(&mut self.state);
|
||||
}
|
||||
MatchAction::End if (1..=5).contains(&self.state.phase) => {
|
||||
self.state.phase = 6;
|
||||
clear_pause(&mut self.state);
|
||||
}
|
||||
MatchAction::Reset if matches!(self.state.phase, 0 | 6) => {
|
||||
let seq = self.state.sequence;
|
||||
self.state = idle(self.local_session);
|
||||
self.state.sequence = seq;
|
||||
}
|
||||
_ => {
|
||||
return Err(
|
||||
"当前比赛阶段不允许此操作;准备结束后才能开始开赛倒计时(1–60 秒)".into(),
|
||||
)
|
||||
}
|
||||
}
|
||||
self.state.sequence = self.state.sequence.saturating_add(1);
|
||||
Ok(())
|
||||
}
|
||||
fn pause(&mut self, kind: u8, team: u8, reason: String) -> Result<(), String> {
|
||||
if !(1..=4).contains(&self.state.phase) {
|
||||
return Err("当前阶段不能申请技术暂停".into());
|
||||
}
|
||||
let reason = reason.trim();
|
||||
if reason.is_empty() || reason.len() > 192 || reason.chars().any(char::is_control) {
|
||||
return Err("请填写暂停原因(最多 192 个 UTF-8 字节,不含换行)".into());
|
||||
}
|
||||
self.state.resume_phase = self.state.phase;
|
||||
self.state.phase = 5;
|
||||
self.state.pause_kind = kind;
|
||||
self.state.applicant = team;
|
||||
self.state.reason = reason.into();
|
||||
self.state.pause_elapsed_ms = 0;
|
||||
self.state.pause_remaining_ms = if kind == 1 { 300_000 } else { u32::MAX };
|
||||
Ok(())
|
||||
}
|
||||
pub fn broadcast(&mut self, now: u64) -> Option<MatchState> {
|
||||
if !self.authority {
|
||||
return None;
|
||||
}
|
||||
self.advance(now);
|
||||
self.state.sequence = self.state.sequence.saturating_add(1);
|
||||
Some(self.state.clone())
|
||||
}
|
||||
pub fn receive(&mut self, packet: MatchState, ip: Ipv4Addr, now: u64) -> bool {
|
||||
if self.authority || !packet.valid() {
|
||||
return false;
|
||||
}
|
||||
if let Some(source) = self.source {
|
||||
if source != (ip, packet.session) || packet.sequence <= self.state.sequence {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
self.source = Some((ip, packet.session));
|
||||
self.state = packet;
|
||||
self.received_at = Some(now);
|
||||
true
|
||||
}
|
||||
/// 明确的人为操作才允许切换裁判会话(例如裁判应用重启)。
|
||||
pub fn release_source(&mut self) -> Result<(), String> {
|
||||
if self.authority {
|
||||
return Err("裁判端不接收其他裁判的计时".into());
|
||||
}
|
||||
self.source = None;
|
||||
self.received_at = None;
|
||||
self.state = idle(self.local_session);
|
||||
Ok(())
|
||||
}
|
||||
pub fn view(&mut self, now: u64) -> MatchView {
|
||||
if self.authority {
|
||||
self.advance(now);
|
||||
}
|
||||
let age = self.received_at.map(|at| now.saturating_sub(at));
|
||||
let connected = self.authority || age.is_some_and(|age| age < SYNC_LEASE_MS);
|
||||
let mut s = self.state.clone();
|
||||
if !self.authority {
|
||||
project(&mut s, age.unwrap_or(0).min(SYNC_LEASE_MS));
|
||||
}
|
||||
MatchView {
|
||||
phase: phase(s.phase),
|
||||
resume_phase: phase(s.resume_phase),
|
||||
pause_kind: match s.pause_kind {
|
||||
1 => Some("player"),
|
||||
2 => Some("official"),
|
||||
_ => None,
|
||||
},
|
||||
applicant: (s.applicant <= 1).then_some(s.applicant),
|
||||
reason: s.reason,
|
||||
remaining_ms: s.remaining_ms,
|
||||
elapsed_ms: s.elapsed_ms,
|
||||
pause_remaining_ms: (s.pause_remaining_ms != u32::MAX).then_some(s.pause_remaining_ms),
|
||||
pause_elapsed_ms: s.pause_elapsed_ms,
|
||||
connected,
|
||||
fresh_for_ms: if self.authority {
|
||||
SYNC_LEASE_MS
|
||||
} else {
|
||||
SYNC_LEASE_MS.saturating_sub(age.unwrap_or(SYNC_LEASE_MS))
|
||||
},
|
||||
source: self.source.map(|(ip, _)| ip.to_string()),
|
||||
sync_error: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
fn ready(c: &mut MatchClock) {
|
||||
c.action(MatchAction::Prepare, 0).unwrap();
|
||||
assert_eq!(c.view(180_000).phase, "ready");
|
||||
}
|
||||
#[test]
|
||||
fn preparation_waits_for_referee_then_countdown_carries_excess_time() {
|
||||
let mut c = MatchClock::new(true, 1, 0);
|
||||
ready(&mut c);
|
||||
assert_eq!(c.view(240_000).phase, "ready");
|
||||
assert!(c
|
||||
.action(MatchAction::Countdown { seconds: 0 }, 240_000)
|
||||
.is_err());
|
||||
c.action(MatchAction::Countdown { seconds: 5 }, 240_000)
|
||||
.unwrap();
|
||||
let v = c.view(247_450);
|
||||
assert_eq!(
|
||||
(v.phase, v.remaining_ms, v.elapsed_ms),
|
||||
("running", 177_550, 2450)
|
||||
);
|
||||
let v = c.view(500_000);
|
||||
assert_eq!((v.phase, v.elapsed_ms), ("finished", 180_000));
|
||||
}
|
||||
#[test]
|
||||
fn pause_approval_freezes_time_and_expiry_never_resumes() {
|
||||
let mut c = MatchClock::new(true, 1, 0);
|
||||
c.action(MatchAction::Prepare, 0).unwrap();
|
||||
assert!(c
|
||||
.action(
|
||||
MatchAction::PausePlayer {
|
||||
team: 0,
|
||||
reason: "故障".into(),
|
||||
approved: false
|
||||
},
|
||||
1000
|
||||
)
|
||||
.is_err());
|
||||
c.action(
|
||||
MatchAction::PausePlayer {
|
||||
team: 1,
|
||||
reason: "更换电池".into(),
|
||||
approved: true,
|
||||
},
|
||||
1000,
|
||||
)
|
||||
.unwrap();
|
||||
let v = c.view(401_000);
|
||||
assert_eq!(
|
||||
(v.phase, v.remaining_ms, v.pause_remaining_ms),
|
||||
("paused", 179_000, Some(0))
|
||||
);
|
||||
c.action(MatchAction::Resume, 401_000).unwrap();
|
||||
assert_eq!(c.view(402_000).remaining_ms, 178_000);
|
||||
c.action(
|
||||
MatchAction::PauseOfficial {
|
||||
reason: "场地检查".into(),
|
||||
},
|
||||
402_000,
|
||||
)
|
||||
.unwrap();
|
||||
let v = c.view(802_000);
|
||||
assert_eq!(
|
||||
(v.pause_remaining_ms, v.pause_elapsed_ms, v.remaining_ms),
|
||||
(None, 400_000, 178_000)
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn pause_preserves_countdown_and_running_and_rejects_invalid_transitions() {
|
||||
for (phase, at) in [(3, 181_000), (4, 187_000)] {
|
||||
let mut c = MatchClock::new(true, 1, 0);
|
||||
ready(&mut c);
|
||||
c.action(MatchAction::Countdown { seconds: 5 }, 180_000)
|
||||
.unwrap();
|
||||
assert!(c
|
||||
.action(
|
||||
MatchAction::PauseOfficial {
|
||||
reason: "\n".into()
|
||||
},
|
||||
at
|
||||
)
|
||||
.is_err());
|
||||
c.action(
|
||||
MatchAction::PauseOfficial {
|
||||
reason: "检查".into(),
|
||||
},
|
||||
at,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(c.state.resume_phase, phase);
|
||||
let before = c.view(at).remaining_ms;
|
||||
assert!(c.action(MatchAction::Prepare, at).is_err());
|
||||
c.action(MatchAction::Resume, at + 900_000).unwrap();
|
||||
assert_eq!(c.view(at + 900_000).remaining_ms, before);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn network_follower_locks_session_rejects_reorder_freezes_and_rejoins() {
|
||||
let mut sender = MatchClock::new(true, 44, 0);
|
||||
sender.action(MatchAction::Prepare, 0).unwrap();
|
||||
let mut follower = MatchClock::new(false, 2, 0);
|
||||
let ip = Ipv4Addr::LOCALHOST;
|
||||
let p = sender.broadcast(1000).unwrap();
|
||||
assert!(follower.receive(p.clone(), ip, 1000));
|
||||
assert!(!follower.receive(p.clone(), ip, 1100));
|
||||
assert!(!follower.receive(
|
||||
sender.broadcast(1200).unwrap(),
|
||||
Ipv4Addr::new(1, 2, 3, 4),
|
||||
1200
|
||||
));
|
||||
assert!(!follower.view(2500).connected);
|
||||
let frozen = follower.view(2500).remaining_ms;
|
||||
assert_eq!(follower.view(99_000).remaining_ms, frozen);
|
||||
assert!(follower.receive(sender.broadcast(100_000).unwrap(), ip, 100_000));
|
||||
assert!(follower.view(100_000).connected);
|
||||
let mut reboot = MatchClock::new(true, 99, 0);
|
||||
let p = reboot.broadcast(0).unwrap();
|
||||
assert!(!follower.receive(p.clone(), ip, 101_000));
|
||||
follower.release_source().unwrap();
|
||||
assert!(follower.receive(p, ip, 102_000));
|
||||
assert!(follower.action(MatchAction::Prepare, 102_000).is_err());
|
||||
}
|
||||
#[test]
|
||||
fn role_change_starts_a_new_session_and_never_broadcasts_as_player() {
|
||||
let mut c = MatchClock::new(true, 1, 0);
|
||||
c.action(MatchAction::Prepare, 0).unwrap();
|
||||
c.set_authority(false, 1000);
|
||||
assert!(c.broadcast(1000).is_none());
|
||||
assert_eq!(c.view(1000).phase, "idle");
|
||||
c.set_authority(true, 2000);
|
||||
assert_ne!(c.broadcast(2000).unwrap().session, 1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user