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
@@ -6,4 +6,4 @@ edition = "2021"
|
||||
# 只依赖错误检查用的东西:规则层是纯计算(血量、去抖、按键状态机),
|
||||
# 这样它能在 PC 上跑单元测试,不需要 ESP32-C3 硬件。
|
||||
[dependencies]
|
||||
protocol = { path = "../../protocol" }
|
||||
protocol = { path = "../../vt/protocol" }
|
||||
+217
-10
@@ -24,7 +24,8 @@
|
||||
//!
|
||||
//! # 规则(已与本项目裁判端对齐)
|
||||
//!
|
||||
//! - 整车一条血量:初始 100,每次受击 −3,减到 0 为止**不变负**,血量 0 时四路灯带
|
||||
//! - 整车一条血量:初始 100。仅在裁判许可有效时,每次受击 −3;其他时候受击仍闪灯。
|
||||
//! 血量减到 0 为止**不变负**,血量 0 时四路灯带
|
||||
//! 全黑("死了全黑")。
|
||||
//! - 受击时**只有被打的那一路**灯带全黑 50 ms,其余三路不动;50 ms 同时是同一板位的
|
||||
//! 重复判定冷却。
|
||||
@@ -35,7 +36,97 @@
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
pub use protocol::{id_from_mac, ArmorHealth, ARMOR_PORT, BROADCAST_ADDR, MsgType};
|
||||
pub use protocol::{id_from_mac, ArmorHealth, MsgType, ARMOR_PORT, BROADCAST_ADDR};
|
||||
pub use protocol::{ArmorControl, ArmorDamageControl, ARMOR_UNASSIGNED};
|
||||
|
||||
pub const CLIENT_LEASE: Duration = Duration::from_secs(2);
|
||||
pub const DAMAGE_LEASE: Duration = Duration::from_secs(1);
|
||||
|
||||
/// 默认禁止扣血。裁判每 200 ms 续约,1 秒没有新许可就停止扣血。
|
||||
#[derive(Debug, Default)]
|
||||
pub struct DamageLink {
|
||||
source: Option<(std::net::SocketAddrV4, u64, u64, Duration, bool)>,
|
||||
}
|
||||
impl DamageLink {
|
||||
pub fn disconnect(&mut self) {
|
||||
self.source = None;
|
||||
}
|
||||
pub fn accept(
|
||||
&mut self,
|
||||
command: ArmorDamageControl,
|
||||
from: std::net::SocketAddrV4,
|
||||
now: Duration,
|
||||
) -> bool {
|
||||
if command.session == 0 || command.sequence == 0 {
|
||||
return false;
|
||||
}
|
||||
if let Some((addr, session, seq, at, _)) = self.source {
|
||||
if addr == from && session == command.session {
|
||||
// 过期、重复包不能续约或重新打开许可。
|
||||
if command.sequence <= seq {
|
||||
return false;
|
||||
}
|
||||
} else if now.saturating_sub(at) < DAMAGE_LEASE {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
self.source = Some((
|
||||
from,
|
||||
command.session,
|
||||
command.sequence,
|
||||
now,
|
||||
command.enabled,
|
||||
));
|
||||
true
|
||||
}
|
||||
pub fn enabled(&self, now: Duration) -> bool {
|
||||
self.source
|
||||
.is_some_and(|(_, _, _, at, enabled)| enabled && now.saturating_sub(at) < DAMAGE_LEASE)
|
||||
}
|
||||
}
|
||||
|
||||
/// 只接受当前持有者续约;其他客户端需等租约到期,避免裁判与操作手交替改色。
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ClientLink {
|
||||
owner: Option<(std::net::SocketAddrV4, Duration)>,
|
||||
}
|
||||
|
||||
impl ClientLink {
|
||||
pub fn disconnect(&mut self) {
|
||||
self.owner = None;
|
||||
}
|
||||
|
||||
pub fn connected(&self, now: Duration) -> bool {
|
||||
self.owner
|
||||
.is_some_and(|(_, at)| now.saturating_sub(at) < CLIENT_LEASE)
|
||||
}
|
||||
|
||||
pub fn accept(&mut self, from: std::net::SocketAddrV4, now: Duration) -> bool {
|
||||
if self.connected(now) && self.owner.is_some_and(|(owner, _)| owner != from) {
|
||||
return false;
|
||||
}
|
||||
self.owner = Some((from, now));
|
||||
true
|
||||
}
|
||||
|
||||
/// 状态灯优先:断网黄闪,等待客户端绿闪,连接后沿用受击/死亡输出。
|
||||
pub fn strips(&self, armor: &Armor, wifi: bool, now: Duration) -> [Strip; 4] {
|
||||
if wifi && self.connected(now) {
|
||||
return armor.strips(now);
|
||||
}
|
||||
let color = if wifi {
|
||||
Rgb(0, 255, 0)
|
||||
} else {
|
||||
Rgb(255, 255, 0)
|
||||
};
|
||||
let strip = if now.as_millis() / 500 % 2 == 0 {
|
||||
Strip::Color(color)
|
||||
} else {
|
||||
Strip::Black
|
||||
};
|
||||
[strip; 4]
|
||||
}
|
||||
}
|
||||
|
||||
/// 队伍一。
|
||||
pub const TEAM_1: u8 = 0;
|
||||
@@ -224,6 +315,7 @@ pub struct Armor {
|
||||
hits: u32,
|
||||
plate_hits: [u32; 4],
|
||||
last_hit: [Option<Duration>; 4],
|
||||
damage_enabled: bool,
|
||||
}
|
||||
|
||||
impl Armor {
|
||||
@@ -237,6 +329,7 @@ impl Armor {
|
||||
hits: 0,
|
||||
plate_hits: [0; 4],
|
||||
last_hit: [None; 4],
|
||||
damage_enabled: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,9 +380,19 @@ impl Armor {
|
||||
self.color = color;
|
||||
}
|
||||
|
||||
/// 只改变扣血许可,不重置血量、冷却或闪烁。
|
||||
pub fn set_damage_enabled(&mut self, enabled: bool) {
|
||||
self.damage_enabled = enabled;
|
||||
}
|
||||
|
||||
pub fn damage_enabled(&self) -> bool {
|
||||
self.damage_enabled
|
||||
}
|
||||
|
||||
/// 喂一个 ADC 采样值。
|
||||
///
|
||||
/// 只有"过阈值 + 该板不在冷却期内"才计入受击并扣血;其余返回 `None`。
|
||||
/// 只有"过阈值 + 该板不在冷却期内"才计入受击;其余返回 `None`。
|
||||
/// 有扣血许可时减少血量,禁止时仍记录受击并触发闪烁。
|
||||
/// 一次击打在 40 kHz 采样下会连续多个点过阈值,冷却窗口就是把这些点算成一次。
|
||||
pub fn on_sample(&mut self, plate: Plate, raw: u16, now: Duration) -> Option<HitEvent> {
|
||||
if raw <= self.rules.threshold {
|
||||
@@ -309,7 +412,9 @@ impl Armor {
|
||||
self.plate_hits[slot] = self.plate_hits[slot].saturating_add(1);
|
||||
|
||||
let before = self.hp;
|
||||
self.hp = self.hp.saturating_sub(self.rules.damage_per_hit);
|
||||
if self.damage_enabled {
|
||||
self.hp = self.hp.saturating_sub(self.rules.damage_per_hit);
|
||||
}
|
||||
|
||||
Some(HitEvent {
|
||||
plate,
|
||||
@@ -532,6 +637,38 @@ pub fn parse_team(value: &str) -> Option<u8> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn connection_lights_follow_wifi_and_expiring_client_lease() {
|
||||
let mut link = ClientLink::default();
|
||||
let mut armor = Armor::new(Rules::default(), Rgb::RED);
|
||||
armor.set_damage_enabled(true);
|
||||
let owner = "192.168.1.2:5601".parse().unwrap();
|
||||
let other = "192.168.1.3:5601".parse().unwrap();
|
||||
let at = Duration::from_millis;
|
||||
assert_eq!(
|
||||
link.strips(&armor, false, at(0)),
|
||||
[Strip::Color(Rgb(255, 255, 0)); 4]
|
||||
);
|
||||
assert_eq!(link.strips(&armor, false, at(500)), [Strip::Black; 4]);
|
||||
assert_eq!(
|
||||
link.strips(&armor, true, at(1000)),
|
||||
[Strip::Color(Rgb(0, 255, 0)); 4]
|
||||
);
|
||||
assert!(link.accept(owner, at(1000)));
|
||||
assert!(!link.accept(other, at(1500)));
|
||||
armor.set_color(Rgb::BLUE);
|
||||
assert_eq!(
|
||||
link.strips(&armor, true, at(1500)),
|
||||
[Strip::Color(Rgb::BLUE); 4]
|
||||
);
|
||||
armor.on_sample(Plate::Front, 3000, at(1501));
|
||||
assert_eq!(link.strips(&armor, true, at(1520))[0], Strip::Black);
|
||||
assert_eq!(armor.hp(), 97, "换队和连接不得重置血量");
|
||||
assert!(!link.connected(at(3000)));
|
||||
assert!(link.accept(other, at(3000)));
|
||||
link.disconnect();
|
||||
assert!(!link.connected(at(3001)));
|
||||
}
|
||||
use super::*;
|
||||
|
||||
fn ms(v: u64) -> Duration {
|
||||
@@ -539,7 +676,62 @@ mod tests {
|
||||
}
|
||||
|
||||
fn armor() -> Armor {
|
||||
Armor::new(Rules::default(), Rgb::for_team(TEAM_1))
|
||||
let mut armor = Armor::new(Rules::default(), Rgb::for_team(TEAM_1));
|
||||
armor.set_damage_enabled(true);
|
||||
armor
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disabled_damage_still_detects_hits_and_flashes_without_changing_health() {
|
||||
let mut a = Armor::new(Rules::default(), Rgb::RED);
|
||||
assert!(!a.damage_enabled());
|
||||
assert!(a.on_sample(Plate::Front, 3000, ms(0)).is_some());
|
||||
assert_eq!(a.hp(), 100);
|
||||
assert_eq!(a.strip(Plate::Front, ms(25)), Strip::Black);
|
||||
assert_eq!(a.strip(Plate::Front, ms(50)), Strip::Color(Rgb::RED));
|
||||
assert!(a.on_sample(Plate::Front, 3000, ms(49)).is_none());
|
||||
a.set_damage_enabled(true);
|
||||
a.on_sample(Plate::Front, 3000, ms(50));
|
||||
assert_eq!(a.hp(), 97);
|
||||
a.set_damage_enabled(false);
|
||||
a.on_sample(Plate::Front, 3000, ms(100));
|
||||
assert_eq!(a.hp(), 97);
|
||||
assert_eq!(a.strip(Plate::Front, ms(125)), Strip::Black);
|
||||
assert_eq!(a.hits(), 3);
|
||||
a.reset();
|
||||
assert!(!a.damage_enabled(), "复位血量不能自行取得扣血许可");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn damage_lease_expires_and_rejects_duplicates_old_packets_and_other_referees() {
|
||||
let mut link = DamageLink::default();
|
||||
let addr = "192.168.1.2:5601".parse().unwrap();
|
||||
let other = "192.168.1.3:5601".parse().unwrap();
|
||||
let mut command = ArmorDamageControl {
|
||||
board_id: 9,
|
||||
session: 11,
|
||||
sequence: 1,
|
||||
enabled: true,
|
||||
};
|
||||
assert!(!link.enabled(ms(0)));
|
||||
assert!(link.accept(command, addr, ms(0)));
|
||||
assert!(!link.accept(command, addr, ms(900)));
|
||||
assert!(link.enabled(ms(999)));
|
||||
assert!(!link.enabled(ms(1000)));
|
||||
assert!(!link.accept(command, addr, ms(1001)));
|
||||
command.sequence = 2;
|
||||
assert!(link.accept(command, addr, ms(1001)));
|
||||
command.sequence = 3;
|
||||
assert!(!link.accept(command, other, ms(1002)));
|
||||
command.enabled = false;
|
||||
assert!(link.accept(command, addr, ms(1002)));
|
||||
assert!(!link.enabled(ms(1003)));
|
||||
command.session = 22;
|
||||
command.sequence = 1;
|
||||
assert!(!link.accept(command, addr, ms(1004)));
|
||||
assert!(link.accept(command, addr, ms(2002)));
|
||||
link.disconnect();
|
||||
assert!(!link.enabled(ms(2003)));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 血量 --
|
||||
@@ -560,7 +752,8 @@ mod tests {
|
||||
// 100 / 3 = 33.33,所以第 34 击才归零。
|
||||
for i in 0..34 {
|
||||
let at = ms(i * 1000);
|
||||
a.on_sample(Plate::Front, 2500, at).expect("hit should count");
|
||||
a.on_sample(Plate::Front, 2500, at)
|
||||
.expect("hit should count");
|
||||
hits += 1;
|
||||
}
|
||||
|
||||
@@ -580,7 +773,10 @@ mod tests {
|
||||
#[test]
|
||||
fn sample_below_threshold_is_not_a_hit() {
|
||||
let mut a = armor();
|
||||
assert!(a.on_sample(Plate::Front, 2000, ms(0)).is_none(), "阈值是严格大于");
|
||||
assert!(
|
||||
a.on_sample(Plate::Front, 2000, ms(0)).is_none(),
|
||||
"阈值是严格大于"
|
||||
);
|
||||
assert!(a.on_sample(Plate::Front, 1999, ms(0)).is_none());
|
||||
assert!(a.on_sample(Plate::Front, 2001, ms(0)).is_some());
|
||||
assert_eq!(a.hp(), 97);
|
||||
@@ -624,7 +820,10 @@ mod tests {
|
||||
|
||||
a.on_sample(Plate::Left, 2600, ms(0));
|
||||
assert!(a.on_sample(Plate::Left, 2600, ms(49)).is_none());
|
||||
assert!(a.on_sample(Plate::Left, 2600, ms(50)).is_some(), "50 ms 是边界,算新受击");
|
||||
assert!(
|
||||
a.on_sample(Plate::Left, 2600, ms(50)).is_some(),
|
||||
"50 ms 是边界,算新受击"
|
||||
);
|
||||
assert_eq!(a.plate_hits(Plate::Left), 2);
|
||||
assert_eq!(a.hp(), 94);
|
||||
}
|
||||
@@ -790,11 +989,19 @@ mod tests {
|
||||
let mut b = Button::new(ms(20), ms(500), ms(500));
|
||||
|
||||
assert_eq!(b.tick(true, ms(0)), ButtonEvent::None);
|
||||
assert_eq!(b.tick(false, ms(5)), ButtonEvent::None, "5 ms 的毛刺不算按下");
|
||||
assert_eq!(
|
||||
b.tick(false, ms(5)),
|
||||
ButtonEvent::None,
|
||||
"5 ms 的毛刺不算按下"
|
||||
);
|
||||
assert_eq!(b.tick(true, ms(10)), ButtonEvent::None);
|
||||
// 从 10 ms 起连续按下到 30 ms 才稳定。
|
||||
assert_eq!(b.tick(true, ms(29)), ButtonEvent::None);
|
||||
assert_eq!(b.tick(true, ms(30)), ButtonEvent::None, "按下本身不产生事件");
|
||||
assert_eq!(
|
||||
b.tick(true, ms(30)),
|
||||
ButtonEvent::None,
|
||||
"按下本身不产生事件"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user