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

+84
View File
@@ -10,6 +10,7 @@ pub const SYNC_LEASE_MS: u64 = 1500;
#[serde(tag = "type", rename_all = "camelCase")]
pub enum MatchAction {
Prepare,
SkipPreparation,
Countdown {
seconds: u32,
},
@@ -162,6 +163,10 @@ impl MatchClock {
self.state.phase = 1;
self.state.remaining_ms = 180_000;
}
MatchAction::SkipPreparation if matches!(self.state.phase, 1 | 2) => {
self.state.phase = 2;
self.state.remaining_ms = 0;
}
MatchAction::Countdown { seconds }
if self.state.phase == 2 && (1..=60).contains(&seconds) =>
{
@@ -204,6 +209,20 @@ impl MatchClock {
self.state.sequence = self.state.sequence.saturating_add(1);
Ok(())
}
/// 装甲板复位期间保持结束状态,先停止扣血;确认满血后再 Reset 到未开始。
pub fn begin_reset(&mut self, has_outcome: bool, now: u64) -> Result<MatchState, String> {
if !self.authority {
return Err("只有裁判端可以重置比赛".into());
}
self.advance(now);
if !matches!(self.state.phase, 0 | 6) && !has_outcome {
return Err("比赛尚未结束,请先结束本局再重置".into());
}
self.state.phase = 6;
clear_pause(&mut self.state);
self.state.sequence = self.state.sequence.saturating_add(1);
Ok(self.state.clone())
}
fn pause(&mut self, kind: u8, team: u8, reason: String) -> Result<(), String> {
if !(1..=4).contains(&self.state.phase) {
return Err("当前阶段不能申请技术暂停".into());
@@ -315,6 +334,71 @@ mod tests {
assert_eq!((v.phase, v.elapsed_ms), ("finished", 180_000));
}
#[test]
fn skip_preparation_waits_for_confirmation_and_syncs_without_changing_session() {
let mut c = MatchClock::new(true, 44, 0);
assert!(c.action(MatchAction::SkipPreparation, 0).is_err());
c.action(MatchAction::Prepare, 0).unwrap();
c.action(MatchAction::SkipPreparation, 1000).unwrap();
let packet = c.broadcast(1000).unwrap();
assert_eq!(
(packet.phase, packet.remaining_ms, packet.elapsed_ms),
(2, 0, 0)
);
let mut follower = MatchClock::new(false, 2, 0);
assert!(follower.receive(packet.clone(), Ipv4Addr::LOCALHOST, 1000));
assert_eq!(follower.view(1000).phase, "ready");
assert!(follower.action(MatchAction::SkipPreparation, 1000).is_err());
assert_eq!(c.view(200_000).phase, "ready");
// 重复操作、准备刚好自然结束时均保持待开赛。
c.action(MatchAction::SkipPreparation, 200_000).unwrap();
c.action(MatchAction::Countdown { seconds: 5 }, 200_000)
.unwrap();
assert!(c.action(MatchAction::SkipPreparation, 201_000).is_err());
assert_eq!(c.view(205_000).phase, "running");
assert!(c.action(MatchAction::SkipPreparation, 205_000).is_err());
let later = c.broadcast(205_000).unwrap();
assert_eq!(later.session, packet.session);
assert!(later.sequence > packet.sequence);
assert!(follower.receive(later, Ipv4Addr::LOCALHOST, 205_000));
assert!(!follower.receive(packet, Ipv4Addr::LOCALHOST, 205_000));
assert_eq!(follower.view(205_000).phase, "running");
}
#[test]
fn reset_after_a_verdict_stops_the_match_then_clears_all_round_state() {
let mut c = MatchClock::new(true, 44, 0);
ready(&mut c);
c.action(MatchAction::Countdown { seconds: 1 }, 180_000)
.unwrap();
assert!(c.begin_reset(false, 185_000).is_err());
c.action(
MatchAction::PauseOfficial {
reason: "检查".into(),
},
185_000,
)
.unwrap();
let stopped = c.begin_reset(true, 186_000).unwrap();
assert_eq!((stopped.phase, stopped.pause_kind), (6, 0));
assert_eq!(c.view(190_000).elapsed_ms, 4000);
c.action(MatchAction::Reset, 190_000).unwrap();
let state = c.broadcast(190_000).unwrap();
assert_eq!(
(
state.phase,
state.remaining_ms,
state.elapsed_ms,
state.pause_elapsed_ms
),
(0, 0, 0, 0)
);
assert_eq!(state.resume_phase, 0);
assert!(state.reason.is_empty());
assert_eq!(state.session, stopped.session);
assert!(state.sequence > stopped.sequence);
assert!(state.valid());
assert!(MatchClock::new(false, 1, 0).begin_reset(true, 0).is_err());
}
#[test]
fn pause_approval_freezes_time_and_expiry_never_resumes() {
let mut c = MatchClock::new(true, 1, 0);
c.action(MatchAction::Prepare, 0).unwrap();