feat(match): 添加画面上下镜像、跳过准备和装甲板整局复位
This commit is contained in:
39 files changed
+1094
-54
No files matched your search
@@ -34,8 +34,15 @@ const FALLBACK_INPUT_FPS: f32 = 20.0;
|
||||
type Shared<'a> = State<'a, Arc<AppState>>;
|
||||
|
||||
#[tauri::command]
|
||||
pub fn match_action(state: Shared<'_>, action: cam_core::match_clock::MatchAction) -> Result<(), String> {
|
||||
state.match_action(action)
|
||||
pub async fn match_action(
|
||||
state: Shared<'_>,
|
||||
action: cam_core::match_clock::MatchAction,
|
||||
) -> Result<(), String> {
|
||||
if matches!(action, cam_core::match_clock::MatchAction::Reset) {
|
||||
state.reset_match().await
|
||||
} else {
|
||||
state.match_action(action)
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -208,6 +215,16 @@ pub fn set_mirror(mirror: bool, state: Shared<'_>) -> Result<ConfigView, String>
|
||||
Ok(ConfigView::from(&state.config()))
|
||||
}
|
||||
|
||||
/// 上下镜像,独立保存,不改动左右镜像。
|
||||
#[tauri::command]
|
||||
pub fn set_mirror_vertical(mirror_vertical: bool, state: Shared<'_>) -> Result<ConfigView, String> {
|
||||
state
|
||||
.update_config(move |config| config.mirror_vertical = mirror_vertical)
|
||||
.map_err(|err| format!("{err}"))?;
|
||||
|
||||
Ok(ConfigView::from(&state.config()))
|
||||
}
|
||||
|
||||
/// 保存 HUD 设置(血条、瞄准器、网格、命中反馈)。
|
||||
#[tauri::command]
|
||||
pub fn save_hud(hud: HudConfig, state: Shared<'_>) -> Result<ConfigView, String> {
|
||||
|
||||
@@ -122,21 +122,35 @@ fn spawn_armor(state: Arc<AppState>) {
|
||||
let mut ticks = [0u32, 7];
|
||||
let mut hits = [0usize, 1];
|
||||
let mut dead_for = [0u32, 0];
|
||||
let mut reset_epoch = 0;
|
||||
|
||||
loop {
|
||||
let epoch = state
|
||||
.demo_reset_epoch
|
||||
.load(std::sync::atomic::Ordering::Acquire);
|
||||
if epoch != reset_epoch {
|
||||
reset_epoch = epoch;
|
||||
hp = [100; 2];
|
||||
ticks = [0, 7];
|
||||
hits = [0, 1];
|
||||
dead_for = [0; 2];
|
||||
}
|
||||
let scripted = reset_epoch == 0 || state.demo_damage_enabled();
|
||||
for (slot, (board, team, port)) in boards.iter().enumerate() {
|
||||
ticks[slot] += 1;
|
||||
if scripted {
|
||||
ticks[slot] += 1;
|
||||
}
|
||||
|
||||
if hp[slot] == 0 {
|
||||
// 打空之后停一会儿再重置:让界面上的胜负横幅真的能被看到。
|
||||
dead_for[slot] += 1;
|
||||
if dead_for[slot] >= 10 {
|
||||
if reset_epoch == 0 && dead_for[slot] >= 10 {
|
||||
hp[slot] = 100;
|
||||
dead_for[slot] = 0;
|
||||
ticks[slot] = 0;
|
||||
log::info!("demo: 0x{board:08X} 重置为满血(长按 BOOT 的等价动作)");
|
||||
}
|
||||
} else if ticks[slot] % 15 == 0 {
|
||||
} else if scripted && ticks[slot] % 15 == 0 {
|
||||
// 约 3 秒一次。
|
||||
let damage = DAMAGE_CYCLE[hits[slot] % DAMAGE_CYCLE.len()];
|
||||
hits[slot] += 1;
|
||||
|
||||
@@ -342,6 +342,7 @@ fn run() -> Result<(), Box<dyn Error>> {
|
||||
commands::set_team,
|
||||
commands::set_role,
|
||||
commands::set_mirror,
|
||||
commands::set_mirror_vertical,
|
||||
commands::save_hud,
|
||||
commands::set_team_name,
|
||||
commands::set_referee_slots,
|
||||
|
||||
@@ -59,6 +59,8 @@ pub struct AppState {
|
||||
match_clock: Mutex<MatchClock>,
|
||||
match_socket: Option<std::net::UdpSocket>,
|
||||
match_error: Mutex<Option<String>>,
|
||||
resetting: AtomicBool,
|
||||
pub demo_reset_epoch: AtomicUsize,
|
||||
discovery: Arc<Discovery>,
|
||||
latest: Arc<LatestFrames>,
|
||||
|
||||
@@ -88,11 +90,18 @@ impl AppState {
|
||||
self.started.elapsed().as_millis().min(u64::MAX as u128) as u64
|
||||
}
|
||||
|
||||
pub fn demo_damage_enabled(&self) -> bool {
|
||||
self.match_view().phase == "running"
|
||||
}
|
||||
|
||||
pub fn match_action(&self, action: MatchAction) -> Result<(), String> {
|
||||
// 闸门留在后端,选手端不能通过 IPC 修改权威计时。
|
||||
if self.config().role != Role::Referee {
|
||||
return Err("只有裁判端可以控制比赛计时".into());
|
||||
}
|
||||
if self.resetting.load(Ordering::Acquire) {
|
||||
return Err("正在重置本局,请等待装甲板确认".into());
|
||||
}
|
||||
self.match_clock
|
||||
.lock()
|
||||
.expect("match clock poisoned")
|
||||
@@ -102,6 +111,89 @@ impl AppState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn reset_match(&self) -> Result<(), String> {
|
||||
if self.config().role != Role::Referee {
|
||||
return Err("只有裁判端可以重置比赛".into());
|
||||
}
|
||||
self.resetting
|
||||
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
|
||||
.map_err(|_| "正在重置本局,请等待装甲板确认".to_string())?;
|
||||
let _guard = MatchResetGuard {
|
||||
busy: &self.resetting,
|
||||
armor: &self.armor,
|
||||
};
|
||||
let config = self.config();
|
||||
let health = [self.health_for(0), self.health_for(1)];
|
||||
let participants = [
|
||||
self.assigned_armor(&config, 0),
|
||||
self.assigned_armor(&config, 1),
|
||||
];
|
||||
if !self.demo
|
||||
&& health
|
||||
.iter()
|
||||
.enumerate()
|
||||
.any(|(team, health)| health.available && participants[team].is_none())
|
||||
{
|
||||
return Err("有血量上报但未分配对应装甲板,请先检查两队装甲板槽位再重置".into());
|
||||
}
|
||||
let has_outcome = health
|
||||
.iter()
|
||||
.any(|health| health.available && health.hp == Some(0));
|
||||
let packet = self
|
||||
.match_clock
|
||||
.lock()
|
||||
.expect("match clock poisoned")
|
||||
.begin_reset(has_outcome, self.match_now())?;
|
||||
self.tick_match();
|
||||
self.push_snapshot();
|
||||
|
||||
if self.demo {
|
||||
self.demo_reset_epoch.fetch_add(1, Ordering::Release);
|
||||
for (board, team) in [(DEMO_ARMOR_A, 0), (DEMO_ARMOR_B, 1)] {
|
||||
for index in 0..4 {
|
||||
let report = protocol::ArmorHealth {
|
||||
team,
|
||||
index,
|
||||
hp: 100,
|
||||
}
|
||||
.encode_from(board);
|
||||
self.armor
|
||||
.datagram(&report, "127.0.0.1:5601".parse().unwrap());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if self.match_socket.is_none() {
|
||||
return Err("装甲板控制通道不可用,请检查 UDP 5601 后重启客户端".into());
|
||||
}
|
||||
self.armor.begin_reset(&packet, participants)?;
|
||||
self.tick_match();
|
||||
let deadline = Instant::now() + Duration::from_secs(3);
|
||||
loop {
|
||||
let waiting = self.armor.reset_waiting();
|
||||
if waiting.is_empty() {
|
||||
break;
|
||||
}
|
||||
if Instant::now() >= deadline {
|
||||
let ids = waiting
|
||||
.iter()
|
||||
.map(|id| format!("0x{id:08X}"))
|
||||
.collect::<Vec<_>>()
|
||||
.join("、");
|
||||
return Err(format!("装甲板 {ids} 未确认复位或未上报四路满血;本局保持结束。请检查连接并更新装甲板固件后重试,也可长按 BOOT 复位"));
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||
}
|
||||
}
|
||||
self.armor.finish_reset();
|
||||
self.match_clock
|
||||
.lock()
|
||||
.expect("match clock poisoned")
|
||||
.action(MatchAction::Reset, self.match_now())?;
|
||||
self.tick_match();
|
||||
self.push_snapshot();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn release_match_source(&self) -> Result<(), String> {
|
||||
self.match_clock
|
||||
.lock()
|
||||
@@ -219,6 +311,8 @@ impl AppState {
|
||||
match_clock: Mutex::new(MatchClock::new(role == Role::Referee, session, 0)),
|
||||
match_socket,
|
||||
match_error: Mutex::new(match_error),
|
||||
resetting: AtomicBool::new(false),
|
||||
demo_reset_epoch: AtomicUsize::new(0),
|
||||
discovery,
|
||||
latest,
|
||||
fps: Mutex::new(HashMap::new()),
|
||||
@@ -349,6 +443,9 @@ impl AppState {
|
||||
|
||||
/// 改配置、落盘、重新套用闸门。
|
||||
pub fn update_config(&self, change: impl FnOnce(&mut Config)) -> Result<Config> {
|
||||
if self.resetting.load(Ordering::Acquire) {
|
||||
anyhow::bail!("正在重置本局,请等待装甲板确认后修改配置");
|
||||
}
|
||||
let updated = {
|
||||
let mut config = self.config.lock().expect("config mutex poisoned");
|
||||
change(&mut config);
|
||||
@@ -569,8 +666,10 @@ impl AppState {
|
||||
.armor
|
||||
.send_damage_controls(socket, &packet, participants)
|
||||
.err();
|
||||
let reset_error = self.armor.send_reset_controls(socket, &packet).err();
|
||||
let error = sync_error
|
||||
.or(damage_error)
|
||||
.or(reset_error)
|
||||
.map(|err| format!("比赛状态发送失败:{err}。请检查网络和防火墙"));
|
||||
*self.match_error.lock().expect("match error poisoned") = error;
|
||||
}
|
||||
@@ -690,6 +789,7 @@ impl AppState {
|
||||
role: config.role,
|
||||
team: config.team,
|
||||
mirrored: config.mirror,
|
||||
mirrored_vertical: config.mirror_vertical,
|
||||
demo: self.demo,
|
||||
diagnostics,
|
||||
unlocked: self.is_unlocked(),
|
||||
@@ -781,3 +881,15 @@ pub const DEMO_CAMERA_A: u32 = 0x0DE0_0001;
|
||||
pub const DEMO_CAMERA_B: u32 = 0x0DE0_0002;
|
||||
pub const DEMO_ARMOR_A: u32 = 0x0A20_0001;
|
||||
pub const DEMO_ARMOR_B: u32 = 0x0A20_0002;
|
||||
|
||||
struct MatchResetGuard<'a> {
|
||||
busy: &'a AtomicBool,
|
||||
armor: &'a ArmorHealthStore,
|
||||
}
|
||||
|
||||
impl Drop for MatchResetGuard<'_> {
|
||||
fn drop(&mut self) {
|
||||
self.armor.finish_reset();
|
||||
self.busy.store(false, Ordering::Release);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user