feat(control): 添加选手 FPS 键鼠操作与 C3 用户 UART 输出
This commit is contained in:
43 files changed
+2389
-77
No files matched your search
@@ -30,6 +30,7 @@ pub mod armor;
|
||||
pub mod armor_reset;
|
||||
pub mod config;
|
||||
pub mod match_clock;
|
||||
pub mod operator;
|
||||
pub mod password;
|
||||
pub mod receiver;
|
||||
pub mod slots;
|
||||
|
||||
@@ -0,0 +1,383 @@
|
||||
//! 选手操控会话。输入只保留最新值,失焦、失联或比赛条件变化后必须显式重新开启。
|
||||
use crate::config::Role;
|
||||
use protocol::OperatorControl;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const INPUT_LEASE_MS: u64 = 150;
|
||||
const STOP_PACKETS: u8 = 5;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum OperatorMode {
|
||||
Match,
|
||||
Debug,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct OperatorInput {
|
||||
pub capture_id: u32,
|
||||
pub sequence: u32,
|
||||
pub active: bool,
|
||||
pub forward: i16,
|
||||
pub strafe: i16,
|
||||
pub yaw: i16,
|
||||
pub pitch: i16,
|
||||
pub fire: bool,
|
||||
}
|
||||
|
||||
pub struct OperatorContext {
|
||||
pub role: Role,
|
||||
pub role_chosen: bool,
|
||||
pub board_id: Option<u32>,
|
||||
pub board_ready: bool,
|
||||
pub hp: Option<u16>,
|
||||
pub phase: &'static str,
|
||||
pub connected: bool,
|
||||
pub has_source: bool,
|
||||
pub focused: bool,
|
||||
pub demo: bool,
|
||||
}
|
||||
|
||||
impl OperatorContext {
|
||||
pub fn check(&self, mode: OperatorMode) -> Result<u32, &'static str> {
|
||||
if self.demo {
|
||||
return Err("演示模式不发送真实操控信号");
|
||||
}
|
||||
if self.role != Role::Team || !self.role_chosen {
|
||||
return Err("请先选择选手端身份");
|
||||
}
|
||||
if !self.focused {
|
||||
return Err("窗口失焦,操作已停止");
|
||||
}
|
||||
let board = self
|
||||
.board_id
|
||||
.filter(|id| *id != 0)
|
||||
.ok_or("请先绑定自己的装甲板");
|
||||
let board = board?;
|
||||
if !self.board_ready || self.hp.is_none() {
|
||||
return Err("绑定的装甲板离线或尚未确认队伍");
|
||||
}
|
||||
if self.hp == Some(0) {
|
||||
return Err("装甲板血量为零,请先重置本局");
|
||||
}
|
||||
match mode {
|
||||
OperatorMode::Match if self.phase != "running" || !self.connected => {
|
||||
Err("比赛未进行或裁判已断连,操作已停止")
|
||||
}
|
||||
OperatorMode::Debug if !matches!(self.phase, "idle" | "preparation" | "ready") => {
|
||||
Err("调试仅在未开赛时可用,操作已停止")
|
||||
}
|
||||
OperatorMode::Debug if self.has_source && !self.connected => {
|
||||
Err("裁判已断连,请恢复同步后再调试")
|
||||
}
|
||||
_ => Ok(board),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct OperatorView {
|
||||
pub active: bool,
|
||||
pub capture_id: Option<u32>,
|
||||
pub mode: Option<OperatorMode>,
|
||||
pub reason: String,
|
||||
}
|
||||
|
||||
struct Capture {
|
||||
id: u32,
|
||||
mode: OperatorMode,
|
||||
packet: OperatorControl,
|
||||
input_sequence: u32,
|
||||
last_input: u64,
|
||||
stops_left: u8,
|
||||
}
|
||||
|
||||
pub struct OperatorState {
|
||||
next_id: u32,
|
||||
next_session: u64,
|
||||
capture: Option<Capture>,
|
||||
reason: String,
|
||||
}
|
||||
|
||||
impl OperatorState {
|
||||
pub fn new(session_seed: u64) -> Self {
|
||||
Self {
|
||||
next_id: 0,
|
||||
next_session: session_seed.max(1),
|
||||
capture: None,
|
||||
reason: "尚未开启操作".into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start(
|
||||
&mut self,
|
||||
mode: OperatorMode,
|
||||
context: &OperatorContext,
|
||||
now: u64,
|
||||
) -> Result<u32, String> {
|
||||
let board_id = context.check(mode).map_err(str::to_owned)?;
|
||||
if self.capture.as_ref().is_some_and(|c| c.packet.enabled) {
|
||||
return Err("操作已开启,请先退出当前操作".into());
|
||||
}
|
||||
if self.capture.as_ref().is_some_and(|c| c.stops_left > 0) {
|
||||
return Err("正在停止上一操作会话,请稍后重新点击".into());
|
||||
}
|
||||
self.next_id = self.next_id.wrapping_add(1).max(1);
|
||||
self.next_session = self.next_session.wrapping_add(1).max(1);
|
||||
self.capture = Some(Capture {
|
||||
id: self.next_id,
|
||||
mode,
|
||||
packet: OperatorControl {
|
||||
board_id,
|
||||
session: self.next_session,
|
||||
sequence: 0,
|
||||
mode: if mode == OperatorMode::Debug { 1 } else { 0 },
|
||||
enabled: true,
|
||||
channels: [0; 4],
|
||||
fire: false,
|
||||
},
|
||||
input_sequence: 0,
|
||||
last_input: now,
|
||||
stops_left: 0,
|
||||
});
|
||||
self.reason.clear();
|
||||
Ok(self.next_id)
|
||||
}
|
||||
|
||||
pub fn stop(&mut self, reason: &str) {
|
||||
if let Some(c) = &mut self.capture {
|
||||
if c.packet.enabled {
|
||||
c.stops_left = STOP_PACKETS;
|
||||
}
|
||||
c.packet.enabled = false;
|
||||
c.packet.channels = [0; 4];
|
||||
c.packet.fire = false;
|
||||
}
|
||||
self.reason = reason.into();
|
||||
}
|
||||
|
||||
fn check(&mut self, context: &OperatorContext, now: u64) {
|
||||
let Some(c) = &self.capture else {
|
||||
return;
|
||||
};
|
||||
if !c.packet.enabled {
|
||||
return;
|
||||
}
|
||||
let reason = if now.saturating_sub(c.last_input) >= INPUT_LEASE_MS {
|
||||
Some("操作输入超时,请重新点击开启")
|
||||
} else {
|
||||
match context.check(c.mode) {
|
||||
Ok(board) if board != c.packet.board_id => Some("装甲板绑定已改变,请重新开启操作"),
|
||||
Ok(_) => None,
|
||||
Err(reason) => Some(reason),
|
||||
}
|
||||
};
|
||||
if let Some(reason) = reason {
|
||||
self.stop(reason);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn input(
|
||||
&mut self,
|
||||
input: OperatorInput,
|
||||
context: &OperatorContext,
|
||||
now: u64,
|
||||
) -> Result<(), String> {
|
||||
// 旧捕获的迟到释放/输入不能终止或续约新捕获。
|
||||
if self.capture.as_ref().map(|c| c.id) != Some(input.capture_id) {
|
||||
return Err("操作会话已更换,请重新开启".into());
|
||||
}
|
||||
if !input.active {
|
||||
self.stop("操作已退出");
|
||||
return Ok(());
|
||||
}
|
||||
self.check(context, now);
|
||||
let c = self.capture.as_mut().unwrap();
|
||||
if !c.packet.enabled {
|
||||
return Err(self.reason.clone());
|
||||
}
|
||||
if input.sequence == 0 || input.sequence <= c.input_sequence {
|
||||
return Err("已忽略过期的操作输入".into());
|
||||
}
|
||||
let mut channels = [input.forward, input.strafe, input.yaw, input.pitch];
|
||||
if channels.iter().any(|v| !(-1000..=1000).contains(v)) {
|
||||
self.stop("操作通道超出范围,已停止");
|
||||
return Err(self.reason.clone());
|
||||
}
|
||||
// 即使绕过前端,斜向平移也不能超过单位速度圆。
|
||||
let length = ((channels[0] as f64).powi(2) + (channels[1] as f64).powi(2)).sqrt();
|
||||
if length > 1000.0 {
|
||||
channels[0] = (channels[0] as f64 * 1000.0 / length).round() as i16;
|
||||
channels[1] = (channels[1] as f64 * 1000.0 / length).round() as i16;
|
||||
}
|
||||
c.input_sequence = input.sequence;
|
||||
c.last_input = now;
|
||||
c.packet.channels = channels;
|
||||
c.packet.fire = input.fire;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 由独立 20 ms 任务调用;不重放历史输入,不在定时器停顿后补发。
|
||||
pub fn packet(&mut self, context: &OperatorContext, now: u64) -> Option<OperatorControl> {
|
||||
self.check(context, now);
|
||||
let c = self.capture.as_mut()?;
|
||||
if !c.packet.enabled {
|
||||
if c.stops_left == 0 {
|
||||
return None;
|
||||
}
|
||||
c.stops_left -= 1;
|
||||
}
|
||||
c.packet.sequence = c.packet.sequence.checked_add(1)?;
|
||||
Some(c.packet)
|
||||
}
|
||||
|
||||
pub fn view(&self) -> OperatorView {
|
||||
OperatorView {
|
||||
active: self.capture.as_ref().is_some_and(|c| c.packet.enabled),
|
||||
capture_id: self.capture.as_ref().map(|c| c.id),
|
||||
mode: self.capture.as_ref().map(|c| c.mode),
|
||||
reason: self.reason.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
fn context() -> OperatorContext {
|
||||
OperatorContext {
|
||||
role: Role::Team,
|
||||
role_chosen: true,
|
||||
board_id: Some(7),
|
||||
board_ready: true,
|
||||
hp: Some(100),
|
||||
phase: "running",
|
||||
connected: true,
|
||||
has_source: true,
|
||||
focused: true,
|
||||
demo: false,
|
||||
}
|
||||
}
|
||||
fn input(id: u32, sequence: u32) -> OperatorInput {
|
||||
OperatorInput {
|
||||
capture_id: id,
|
||||
sequence,
|
||||
active: true,
|
||||
forward: 1000,
|
||||
strafe: 1000,
|
||||
yaw: -900,
|
||||
pitch: 400,
|
||||
fire: true,
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn only_explicit_bound_team_can_start_and_debug_cannot_bypass_match() {
|
||||
let mut s = OperatorState::new(8);
|
||||
let mut c = context();
|
||||
assert!(s.start(OperatorMode::Debug, &c, 0).is_err());
|
||||
c.phase = "idle";
|
||||
c.has_source = false;
|
||||
c.connected = false;
|
||||
assert!(s.start(OperatorMode::Match, &c, 0).is_err());
|
||||
for change in 0..6 {
|
||||
let mut bad = context();
|
||||
match change {
|
||||
0 => bad.role = Role::Referee,
|
||||
1 => bad.board_id = None,
|
||||
2 => bad.board_ready = false,
|
||||
3 => bad.hp = Some(0),
|
||||
4 => bad.demo = true,
|
||||
_ => bad.focused = false,
|
||||
}
|
||||
assert!(s.start(OperatorMode::Match, &bad, 0).is_err());
|
||||
}
|
||||
assert!(s.start(OperatorMode::Debug, &c, 0).is_ok());
|
||||
c.has_source = true;
|
||||
assert!(!s.packet(&c, 20).unwrap().enabled);
|
||||
}
|
||||
#[test]
|
||||
fn stale_inputs_never_rearm_and_stops_are_bounded_neutral() {
|
||||
let mut s = OperatorState::new(8);
|
||||
let c = context();
|
||||
let id = s.start(OperatorMode::Match, &c, 0).unwrap();
|
||||
s.input(input(id, 1), &c, 10).unwrap();
|
||||
let p = s.packet(&c, 20).unwrap();
|
||||
assert_eq!(p.channels, [707, 707, -900, 400]);
|
||||
assert!(p.fire);
|
||||
assert!(s.input(input(id, 1), &c, 100).is_err());
|
||||
let p = s.packet(&c, 160).unwrap();
|
||||
assert!(!p.enabled);
|
||||
assert_eq!(p.channels, [0; 4]);
|
||||
assert!(!p.fire);
|
||||
assert!(s.input(input(id, 2), &c, 161).is_err());
|
||||
for n in 1..STOP_PACKETS {
|
||||
assert!(s.packet(&c, 160 + n as u64 * 20).is_some());
|
||||
}
|
||||
assert!(s.packet(&c, 300).is_none());
|
||||
let new_id = s.start(OperatorMode::Match, &c, 310).unwrap();
|
||||
assert_ne!(new_id, id);
|
||||
let mut old_stop = input(id, 4);
|
||||
old_stop.active = false;
|
||||
assert!(s.input(old_stop, &c, 320).is_err());
|
||||
assert!(s.view().active);
|
||||
}
|
||||
#[test]
|
||||
fn pause_binding_change_loss_and_invalid_values_end_the_capture() {
|
||||
for change in 0..6 {
|
||||
let mut s = OperatorState::new(8);
|
||||
let mut c = context();
|
||||
let id = s.start(OperatorMode::Match, &c, 0).unwrap();
|
||||
match change {
|
||||
0 => c.phase = "paused",
|
||||
1 => c.phase = "finished",
|
||||
2 => c.connected = false,
|
||||
3 => c.board_id = Some(9),
|
||||
4 => c.focused = false,
|
||||
_ => c.hp = Some(0),
|
||||
}
|
||||
assert!(!s.packet(&c, 20).unwrap().enabled);
|
||||
assert!(s.input(input(id, 1), &context(), 30).is_err());
|
||||
}
|
||||
let mut s = OperatorState::new(8);
|
||||
let c = context();
|
||||
let id = s.start(OperatorMode::Match, &c, 0).unwrap();
|
||||
let mut bad = input(id, 1);
|
||||
bad.yaw = 1001;
|
||||
assert!(s.input(bad, &c, 10).is_err());
|
||||
assert!(!s.view().active);
|
||||
}
|
||||
#[test]
|
||||
fn debug_stops_at_countdown_and_does_not_return_at_idle() {
|
||||
let mut s = OperatorState::new(8);
|
||||
let mut c = context();
|
||||
c.phase = "preparation";
|
||||
let id = s.start(OperatorMode::Debug, &c, 0).unwrap();
|
||||
s.input(input(id, 1), &c, 10).unwrap();
|
||||
assert_eq!(s.packet(&c, 20).unwrap().mode, 1);
|
||||
c.phase = "countdown";
|
||||
assert!(!s.packet(&c, 40).unwrap().enabled);
|
||||
c.phase = "idle";
|
||||
assert!(s.input(input(id, 2), &c, 50).is_err());
|
||||
}
|
||||
#[test]
|
||||
fn rebinding_cannot_discard_stop_retries_for_the_previous_board() {
|
||||
let mut s = OperatorState::new(8);
|
||||
let mut c = context();
|
||||
let id = s.start(OperatorMode::Match, &c, 0).unwrap();
|
||||
s.input(input(id, 1), &c, 10).unwrap();
|
||||
s.stop("已退出");
|
||||
c.board_id = Some(9);
|
||||
assert!(s.start(OperatorMode::Match, &c, 20).is_err());
|
||||
for n in 0..STOP_PACKETS {
|
||||
let old = s.packet(&c, 20 + n as u64 * 20).unwrap();
|
||||
assert_eq!(old.board_id, 7);
|
||||
assert!(!old.enabled);
|
||||
assert_eq!(old.channels, [0; 4]);
|
||||
assert!(!old.fire);
|
||||
}
|
||||
s.start(OperatorMode::Match, &c, 140).unwrap();
|
||||
assert_eq!(s.packet(&c, 150).unwrap().board_id, 9);
|
||||
}
|
||||
}
|
||||
@@ -385,6 +385,7 @@ pub struct SlotsDto {
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Snapshot {
|
||||
pub operator_control: crate::operator::OperatorView,
|
||||
pub match_clock: crate::match_clock::MatchView,
|
||||
pub role: Role,
|
||||
pub team: u8,
|
||||
|
||||
Reference in new issue
Block a user