feat(match): 添加画面上下镜像、跳过准备和装甲板整局复位

This commit is contained in:
sloray committed 2026-10-31 09:27:26 +08:00
1 parent 235fb1c73b
commit 338efa5c2e
39 files changed
+1094 -54

No files matched your search

+22
View File
@@ -188,6 +188,28 @@ fn main() -> Result<()> {
damage.accept(command, from, now);
}
}
Ok(Some((net::ControlCommand::Reset(mut command), from))) => {
if command.board_id == sender_id {
if let Some(apply) = damage.accept_reset(command, from, now) {
if apply {
armor.set_damage_enabled(false);
armor.reset();
warn!(
"referee reset: hp={} reset_id={}",
armor.hp(),
command.reset_id
);
}
command.acknowledged = true;
if let Some(bytes) = command.encode() {
if let Err(err) = reporter.send_to(&bytes, from) {
warn!("reset acknowledgement failed: {err:#}");
}
}
force_report = true;
}
}
}
Ok(None) => break,
Err(err) => {
warn!("client control receive failed: {err:#}");
+9 -2
View File
@@ -21,11 +21,12 @@ use anyhow::{anyhow, Result};
use esp_idf_svc::sys as ffi;
use log::{info, warn};
use armor_protocol::{ArmorControl, ArmorDamageControl, ARMOR_PORT, BROADCAST_ADDR};
use armor_protocol::{ArmorControl, ArmorDamageControl, ArmorReset, ARMOR_PORT, BROADCAST_ADDR};
pub enum ControlCommand {
Team(ArmorControl),
Damage(ArmorDamageControl),
Reset(ArmorReset),
}
/// 把 Rust 的 socket 地址转成 lwIP 期望的 C 结构。
@@ -98,6 +99,8 @@ impl Reporter {
ControlCommand::Team(control)
} else if let Some(control) = ArmorDamageControl::decode(bytes) {
ControlCommand::Damage(control)
} else if let Some(control) = ArmorReset::decode(bytes) {
ControlCommand::Reset(control)
} else {
return Ok(None);
};
@@ -148,7 +151,11 @@ impl Reporter {
/// 把一个数据报广播到裁判终端。
pub fn broadcast(&self, payload: &[u8]) -> Result<()> {
let addr = to_sockaddr(SocketAddrV4::new(BROADCAST_ADDR, ARMOR_PORT));
self.send_to(payload, SocketAddrV4::new(BROADCAST_ADDR, ARMOR_PORT))
}
pub fn send_to(&self, payload: &[u8], destination: SocketAddrV4) -> Result<()> {
let addr = to_sockaddr(destination);
// SAFETY: 缓冲区与地址在调用期间都有效;sendto 复制数据而不是持有指针。
let rc = unsafe {
+124 -1
View File
@@ -37,7 +37,7 @@
use std::time::Duration;
pub use protocol::{id_from_mac, ArmorHealth, MsgType, ARMOR_PORT, BROADCAST_ADDR};
pub use protocol::{ArmorControl, ArmorDamageControl, ARMOR_UNASSIGNED};
pub use protocol::{ArmorControl, ArmorDamageControl, ArmorReset, ARMOR_UNASSIGNED};
pub const CLIENT_LEASE: Duration = Duration::from_secs(2);
pub const DAMAGE_LEASE: Duration = Duration::from_secs(1);
@@ -46,6 +46,7 @@ pub const DAMAGE_LEASE: Duration = Duration::from_secs(1);
#[derive(Debug, Default)]
pub struct DamageLink {
source: Option<(std::net::SocketAddrV4, u64, u64, Duration, bool)>,
last_reset: Option<(std::net::SocketAddrV4, u64, u64)>,
}
impl DamageLink {
pub fn disconnect(&mut self) {
@@ -83,6 +84,40 @@ impl DamageLink {
self.source
.is_some_and(|(_, _, _, at, enabled)| enabled && now.saturating_sub(at) < DAMAGE_LEASE)
}
/// Some(true) 执行一次复位;Some(false) 只重发回执;None 拒绝。
/// 必须先收到同源、同序号的禁止扣血许可,延迟到达的旧复位不能影响后续阶段。
pub fn accept_reset(
&mut self,
command: ArmorReset,
from: std::net::SocketAddrV4,
now: Duration,
) -> Option<bool> {
let (addr, session, sequence, at, enabled) = self.source?;
if command.acknowledged
|| enabled
|| addr != from
|| session != command.session
|| sequence != command.sequence
|| command.reset_id == 0
|| command.reset_id > sequence
|| now.saturating_sub(at) >= DAMAGE_LEASE
{
return None;
}
if let Some((last_addr, last_session, reset_id)) = self.last_reset {
if last_addr == from && last_session == session {
if command.reset_id < reset_id {
return None;
}
if command.reset_id == reset_id {
return Some(false);
}
}
}
self.last_reset = Some((from, session, command.reset_id));
Some(true)
}
}
/// 只接受当前持有者续约;其他客户端需等租约到期,避免裁判与操作手交替改色。
@@ -734,6 +769,94 @@ mod tests {
assert!(!link.enabled(ms(2003)));
}
#[test]
fn remote_reset_requires_current_disabled_permission_and_only_applies_once() {
let mut link = DamageLink::default();
let from = "192.168.1.2:5601".parse().unwrap();
let other = "192.168.1.3:5601".parse().unwrap();
let mut permit = ArmorDamageControl {
board_id: 9,
session: 11,
sequence: 10,
enabled: false,
};
let mut reset = ArmorReset {
board_id: 9,
session: 11,
sequence: 10,
reset_id: 10,
acknowledged: false,
};
assert_eq!(link.accept_reset(reset, from, ms(0)), None);
assert!(link.accept(permit, from, ms(0)));
assert_eq!(link.accept_reset(reset, other, ms(1)), None);
assert_eq!(
link.accept_reset(
ArmorReset {
session: 12,
..reset
},
from,
ms(1)
),
None
);
assert_eq!(
link.accept_reset(
ArmorReset {
acknowledged: true,
..reset
},
from,
ms(1)
),
None
);
assert_eq!(link.accept_reset(reset, from, ms(1)), Some(true));
assert_eq!(link.accept_reset(reset, from, ms(2)), Some(false));
let mut a = armor();
a.on_sample(Plate::Front, 2500, ms(0)).unwrap();
assert_eq!(a.hp(), 97);
a.set_damage_enabled(false);
a.reset();
assert_eq!((a.hp(), a.hits(), a.plate_hits(Plate::Front)), (100, 0, 0));
assert!(!a.damage_enabled());
assert_eq!(a.color(), Rgb::for_team(0));
// 重试更新许可序号,但保留复位 id;丢回执也不能重复补血。
permit.sequence += 1;
assert!(link.accept(permit, from, ms(200)));
assert_eq!(link.accept_reset(reset, from, ms(201)), None);
reset.sequence = permit.sequence;
assert_eq!(link.accept_reset(reset, from, ms(201)), Some(false));
assert_eq!(link.accept_reset(reset, from, ms(1200)), None);
permit.sequence += 1;
permit.enabled = true;
assert!(link.accept(permit, from, ms(1201)));
reset.sequence = permit.sequence;
assert_eq!(link.accept_reset(reset, from, ms(1202)), None);
permit.sequence += 1;
permit.enabled = false;
assert!(link.accept(permit, from, ms(1203)));
reset.sequence = permit.sequence;
reset.reset_id = permit.sequence;
assert_eq!(link.accept_reset(reset, from, ms(1204)), Some(true));
assert_eq!(
link.accept_reset(
ArmorReset {
reset_id: 10,
..reset
},
from,
ms(1205)
),
None
);
link.disconnect();
assert_eq!(link.accept_reset(reset, from, ms(1206)), None);
}
// ---------------------------------------------------------------- 血量 --
#[test]
+19
View File
@@ -39,6 +39,7 @@ MSG_ARMOR_HEALTH = 5 # MsgType::ArmorHealth
MSG_ARMOR_CONTROL = 6
MSG_MATCH_STATE = 7 # 比赛计时广播;装甲板探测器忽略
MSG_ARMOR_DAMAGE_CONTROL = 8 # 扣血许可;装甲板探测器忽略
MSG_ARMOR_RESET = 9 # 复位与回执;探测器只解析,不发送
PROTOCOL_VERSION = 1
TEAM_NAMES = {0: "队伍一", 1: "队伍二", 255: "待分配队伍"}
@@ -66,6 +67,24 @@ def decode_damage_control(data):
return (board, session, sequence, bool(data[47])) if board and session and sequence else None
def decode_reset(data):
"""独立核对复位及回执的 56 字节格式,不向设备发送复位。"""
if len(data) != HEADER + 29 or int.from_bytes(data[:4], "little") != MAGIC:
return None
if data[4] != MSG_ARMOR_RESET or int.from_bytes(data[5:7], "little") != PROTOCOL_VERSION or data[-1] not in (0, 1):
return None
board = int.from_bytes(data[27:31], "little")
session = int.from_bytes(data[31:39], "little")
sequence = int.from_bytes(data[39:47], "little")
reset_id = int.from_bytes(data[47:55], "little")
acknowledged = bool(data[55])
if not board or not session or not 0 < reset_id <= sequence:
return None
if int.from_bytes(data[7:11], "little") != (board if acknowledged else 0):
return None
return board, session, sequence, reset_id, acknowledged
def decode(data):
"""解析一个数据报,返回 (team, index, hp, sender_id, version) 或 None。"""
if len(data) < HEADER + 4:
+8
View File
@@ -24,5 +24,13 @@ class WireTests(unittest.TestCase):
self.assertIsNone(probe.decode(packet))
for invalid in (packet[:-1], packet + b'\x00', packet[:-1] + b'\x02', packet[:31] + bytes(8) + packet[39:]):
self.assertIsNone(probe.decode_damage_control(invalid))
def test_reset_golden_and_ack_identity(self):
packet = bytes.fromhex("3143564d090100" + "00" * 20 + "78563412" + "0807060504030201" + "0300000000000000" + "0200000000000000" + "00")
self.assertEqual(probe.decode_reset(packet), (0x12345678, 0x0102030405060708, 3, 2, False))
ack = packet[:7] + bytes.fromhex("78563412") + packet[11:-1] + b'\x01'
self.assertEqual(probe.decode_reset(ack)[-1], True)
self.assertIsNone(probe.decode(packet))
for invalid in (packet[:-1], packet + b'\x00', packet[:-1] + b'\x01', ack[:-1] + b'\x00', packet[:47] + bytes(8) + packet[55:]):
self.assertIsNone(probe.decode_reset(invalid))
if __name__ == "__main__": unittest.main()