//! 实际回环 UDP + 与生产相同的 5601 消息分流;不代替跨电脑广播验证。 use cam_core::armor::ArmorHealthStore; use cam_core::match_clock::{MatchAction, MatchClock}; use cam_core::ArmorSource; use std::net::{Ipv4Addr, UdpSocket}; use std::sync::{Arc, Mutex}; use std::time::Duration; #[test] fn referee_damage_permission_follows_all_phases_and_releases_old_slots() { let tx = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let board = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); board .set_read_timeout(Some(Duration::from_secs(2))) .unwrap(); let store = ArmorHealthStore::new(); let report = protocol::ArmorHealth { team: 0, index: 0, hp: 100, } .encode_from(17); store.datagram(&report, board.local_addr().unwrap()); let mut clock = MatchClock::new(true, 80, 0); let receive = |clock: &mut MatchClock, at, slots, enabled| { let state = clock.broadcast(at).unwrap(); store.send_damage_controls(&tx, &state, slots).unwrap(); let mut bytes = [0; 128]; let (n, _) = board.recv_from(&mut bytes).unwrap(); let permit = protocol::ArmorDamageControl::decode(&bytes[..n]).unwrap(); assert_eq!( (permit.board_id, permit.enabled, permit.sequence), (17, enabled, state.sequence) ); }; let slots = [Some(17), None]; receive(&mut clock, 0, slots, false); clock.action(MatchAction::Prepare, 0).unwrap(); receive(&mut clock, 1000, slots, false); receive(&mut clock, 180_000, slots, false); clock .action(MatchAction::Countdown { seconds: 5 }, 180_000) .unwrap(); receive(&mut clock, 184_999, slots, false); receive(&mut clock, 185_000, slots, true); clock .action( MatchAction::PauseOfficial { reason: "检查".into(), }, 186_000, ) .unwrap(); receive(&mut clock, 186_000, slots, false); clock.action(MatchAction::Resume, 187_000).unwrap(); receive(&mut clock, 187_000, slots, true); receive(&mut clock, 187_001, [None, None], false); receive(&mut clock, 187_002, slots, true); receive(&mut clock, 187_003, [Some(17), Some(17)], false); receive(&mut clock, 187_004, slots, true); clock.action(MatchAction::End, 188_000).unwrap(); receive(&mut clock, 188_000, slots, false); } #[test] fn udp_snapshot_routes_to_follower_without_polluting_armor_health() { let tx = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let rx = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); rx.set_read_timeout(Some(Duration::from_secs(2))).unwrap(); let follower = Arc::new(Mutex::new(MatchClock::new(false, 2, 0))); let store = ArmorHealthStore::new(); let sink = follower.clone(); store.set_match_sink(Arc::new(move |packet, ip| { sink.lock().unwrap().receive(packet, ip, 1000); })); let mut referee = MatchClock::new(true, 1, 0); referee.action(MatchAction::Prepare, 0).unwrap(); let packet = referee.broadcast(1000).unwrap().encode().unwrap(); tx.send_to(&packet, rx.local_addr().unwrap()).unwrap(); let mut buf = [0; 512]; let (n, from) = rx.recv_from(&mut buf).unwrap(); store.datagram(&buf[..n], from); let v = follower.lock().unwrap().view(1000); assert!(v.connected); assert_eq!(v.phase, "preparation"); assert_eq!(v.remaining_ms, 179_000); assert!(store.boards().is_empty()); assert!(store.health(0).is_none()); // 一次丢包不丢状态,下一份完整快照直接校正;重复包不能回滚。 let later = referee.broadcast(20_000).unwrap().encode().unwrap(); tx.send_to(&later, rx.local_addr().unwrap()).unwrap(); let (n, from) = rx.recv_from(&mut buf).unwrap(); store.datagram(&buf[..n], from); store.datagram(&packet, from); assert_eq!(follower.lock().unwrap().view(1000).remaining_ms, 160_000); } #[test] fn knockout_revokes_both_boards_and_result_survives_late_winner_death() { let tx = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let store = ArmorHealthStore::new(); let boards: Vec<_> = (1..=2) .map(|id| { let socket = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); socket .set_read_timeout(Some(Duration::from_secs(2))) .unwrap(); store.datagram( &protocol::ArmorHealth { team: id - 1, index: 0, hp: 100, } .encode_from(u32::from(id)), socket.local_addr().unwrap(), ); socket }) .collect(); let mut clock = MatchClock::new(true, 80, 0); clock.action(MatchAction::Prepare, 0).unwrap(); clock.action(MatchAction::SkipPreparation, 100).unwrap(); clock .action(MatchAction::Countdown { seconds: 1 }, 100) .unwrap(); let state = clock.broadcast(1100).unwrap(); let mut buf = [0; 512]; store .send_damage_controls(&tx, &state, [Some(1), Some(2)]) .unwrap(); for board in &boards { let (n, _) = board.recv_from(&mut buf).unwrap(); assert!( protocol::ArmorDamageControl::decode(&buf[..n]) .unwrap() .enabled ); } assert!(clock.finish_on_health([Some(0), Some(10)], 2100)); let state = clock.broadcast(2100).unwrap(); store .send_damage_controls(&tx, &state, [Some(1), Some(2)]) .unwrap(); for board in &boards { let (n, _) = board.recv_from(&mut buf).unwrap(); assert!( !protocol::ArmorDamageControl::decode(&buf[..n]) .unwrap() .enabled ); } assert!(!clock.finish_on_health([Some(0), Some(0)], 2200)); let follower = Arc::new(Mutex::new(MatchClock::new(false, 2, 0))); let reader = ArmorHealthStore::new(); let follow = follower.clone(); reader.set_match_result_sink(Arc::new(move |result, ip| { follow.lock().unwrap().receive_result(result, ip, 2100); })); let rx = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); rx.set_read_timeout(Some(Duration::from_secs(2))).unwrap(); tx.send_to( &clock.result_packet().unwrap().encode().unwrap(), rx.local_addr().unwrap(), ) .unwrap(); let (n, from) = rx.recv_from(&mut buf).unwrap(); reader.datagram(&buf[..n], from); assert_eq!(follower.lock().unwrap().view(2100).phase, "finished"); assert!(reader.boards().is_empty()); for (index, board) in boards.iter().enumerate() { reader.datagram( &protocol::ArmorHealth { team: index as u8, index: 0, hp: 0, } .encode_from(index as u32 + 1), board.local_addr().unwrap(), ); } let names = ["红队".into(), "蓝队".into()]; let mut health = [ cam_core::snapshot::TeamHealthDto::present(0, Some(1), [0; 4], 25, &names), cam_core::snapshot::TeamHealthDto::present(1, Some(2), [0; 4], 25, &names), ]; follower.lock().unwrap().apply_outcomes(&mut health); assert_eq!( health[0].outcome, Some(cam_core::snapshot::MatchOutcome::Defeat) ); assert_eq!( health[1].outcome, Some(cam_core::snapshot::MatchOutcome::Victory) ); }