Add scoring update timeout

This commit is contained in:
hodasemi 2023-01-19 07:39:34 +01:00
parent 5186b96b44
commit eec17e1039
3 changed files with 156 additions and 111 deletions

View file

@ -26,6 +26,8 @@ pub struct Pedals {
throttle: Arc<ProgressBar>, throttle: Arc<ProgressBar>,
_history: Arc<Icon>, _history: Arc<Icon>,
enable: bool,
throttle_samples: HeapRb<f32>, throttle_samples: HeapRb<f32>,
brake_samples: HeapRb<f32>, brake_samples: HeapRb<f32>,
@ -138,6 +140,8 @@ impl Pedals {
throttle, throttle,
_history: history, _history: history,
enable: false,
throttle_samples, throttle_samples,
brake_samples, brake_samples,
@ -202,7 +206,7 @@ impl Pedals {
let command_buffer = let command_buffer =
CommandBuffer::new_primary().build(self.device.clone(), self.queue.clone())?; CommandBuffer::new_primary().build(self.device.clone(), self.queue.clone())?;
{ if self.enable {
let mut recorder = command_buffer.begin(VkCommandBufferBeginInfo::new( let mut recorder = command_buffer.begin(VkCommandBufferBeginInfo::new(
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT
| VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT, | VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
@ -231,7 +235,18 @@ impl Pedals {
impl UiOverlay for Pedals {} impl UiOverlay for Pedals {}
impl DataReceiver for Pedals { impl DataReceiver for Pedals {
fn game_phase_change(&mut self, _phase: GamePhase) -> Result<()> { fn game_phase_change(&mut self, phase: GamePhase) -> Result<()> {
match phase {
GamePhase::None => {
self.enable = false;
self.gui.disable()?;
}
_ => {
self.enable = true;
self.gui.enable()?;
}
}
Ok(()) Ok(())
} }
@ -257,10 +272,7 @@ impl DataReceiver for Pedals {
player_id: Option<i32>, player_id: Option<i32>,
telemetries: &[rF2VehicleTelemetry], telemetries: &[rF2VehicleTelemetry],
) -> Result<()> { ) -> Result<()> {
match player_id { if let Some(id) = player_id {
Some(id) => {
self.gui.enable()?;
if let Some(telemetry) = telemetries.iter().find(|telemetry| telemetry.id == id) { if let Some(telemetry) = telemetries.iter().find(|telemetry| telemetry.id == id) {
let brake = 1.0 - telemetry.unfiltered_brake as f32; let brake = 1.0 - telemetry.unfiltered_brake as f32;
let throttle = 1.0 - telemetry.unfiltered_throttle as f32; let throttle = 1.0 - telemetry.unfiltered_throttle as f32;
@ -274,10 +286,6 @@ impl DataReceiver for Pedals {
self.update_vertex_buffers()?; self.update_vertex_buffers()?;
} }
} }
None => {
self.gui.disable()?;
}
}
Ok(()) Ok(())
} }

View file

@ -73,6 +73,8 @@ pub struct Radar {
car_width: f32, car_width: f32,
car_height: f32, car_height: f32,
enable: bool,
device: Arc<Device>, device: Arc<Device>,
queue: Arc<Mutex<Queue>>, queue: Arc<Mutex<Queue>>,
@ -171,6 +173,8 @@ impl Radar {
car_width, car_width,
car_height, car_height,
enable: false,
device, device,
queue, queue,
@ -216,7 +220,7 @@ impl Radar {
let command_buffer = let command_buffer =
CommandBuffer::new_primary().build(self.device.clone(), self.queue.clone())?; CommandBuffer::new_primary().build(self.device.clone(), self.queue.clone())?;
{ if self.enable {
let mut recorder = command_buffer.begin(VkCommandBufferBeginInfo::new( let mut recorder = command_buffer.begin(VkCommandBufferBeginInfo::new(
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT
| VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT, | VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
@ -268,7 +272,12 @@ impl Radar {
impl UiOverlay for Radar {} impl UiOverlay for Radar {}
impl DataReceiver for Radar { impl DataReceiver for Radar {
fn game_phase_change(&mut self, _phase: GamePhase) -> Result<()> { fn game_phase_change(&mut self, phase: GamePhase) -> Result<()> {
match phase {
GamePhase::None => self.enable = false,
_ => self.enable = true,
}
Ok(()) Ok(())
} }
@ -299,6 +308,7 @@ impl DataReceiver for Radar {
self.cars.clear(); self.cars.clear();
if self.enable {
if let Some(player_id) = player_id { if let Some(player_id) = player_id {
// make sure there are enough cars in buffer // make sure there are enough cars in buffer
if self.car_handles.len() < telemetries.len() { if self.car_handles.len() < telemetries.len() {
@ -339,7 +349,8 @@ impl DataReceiver for Radar {
// check if car is close enough to the players car // check if car is close enough to the players car
if distance < self.config.radar_car_distance { if distance < self.config.radar_car_distance {
let offset = diff.xz() * (self.radar_extent / self.config.radar_car_distance); let offset =
diff.xz() * (self.radar_extent / self.config.radar_car_distance);
let buffered_car = self.car_handles[buffer_car_index].clone(); let buffered_car = self.car_handles[buffer_car_index].clone();
buffer_car_index += 1; buffer_car_index += 1;
@ -360,6 +371,7 @@ impl DataReceiver for Radar {
} }
write_log!(format!("other cars: {:?}", self.cars.len())); write_log!(format!("other cars: {:?}", self.cars.len()));
}
Ok(()) Ok(())
} }

View file

@ -1,7 +1,11 @@
use anyhow::Result; use anyhow::Result;
use rfactor_sm_reader::*; use rfactor_sm_reader::*;
use std::{cell::RefCell, rc::Rc, time::Instant}; use std::{
cell::RefCell,
rc::Rc,
time::{Duration, Instant},
};
use crate::write_log; use crate::write_log;
@ -25,6 +29,8 @@ pub trait DataReceiver {
) -> Result<()>; ) -> Result<()>;
} }
const GAME_PHASE_TIME_OUT: Duration = Duration::from_secs(2);
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum GamePhase { pub enum GamePhase {
TestDay, TestDay,
@ -56,6 +62,7 @@ pub struct RFactorData {
telemetry_reader: TelemetryReader, telemetry_reader: TelemetryReader,
scoring_reader: ScoringReader, scoring_reader: ScoringReader,
last_scoring_read: Duration,
start_time: Instant, start_time: Instant,
player_id: Option<i32>, player_id: Option<i32>,
previous_game_phase: GamePhase, previous_game_phase: GamePhase,
@ -73,6 +80,7 @@ impl RFactorData {
telemetry_reader: TelemetryReader::new(start_time.elapsed().as_secs_f32())?, telemetry_reader: TelemetryReader::new(start_time.elapsed().as_secs_f32())?,
scoring_reader: ScoringReader::new(start_time.elapsed().as_secs_f32())?, scoring_reader: ScoringReader::new(start_time.elapsed().as_secs_f32())?,
last_scoring_read: start_time.elapsed(),
start_time, start_time,
player_id: None, player_id: None,
previous_game_phase: GamePhase::None, previous_game_phase: GamePhase::None,
@ -93,9 +101,10 @@ impl RFactorData {
write_log!(" =================== update RFactorData ==================="); write_log!(" =================== update RFactorData ===================");
// get scoring info // get scoring info
if let Some((scoring_info, vehicle_scorings)) = match self.scoring_reader.vehicle_scoring(self.now()) {
self.scoring_reader.vehicle_scoring(self.now()) Some((scoring_info, vehicle_scorings)) => {
{ self.last_scoring_read = self.start_time.elapsed();
write_log!(format!( write_log!(format!(
"new scoring info: vehicles: {}", "new scoring info: vehicles: {}",
scoring_info.mNumVehicles scoring_info.mNumVehicles
@ -138,6 +147,22 @@ impl RFactorData {
} }
} }
} }
None => {
let now = self.start_time.elapsed();
if now > (self.last_scoring_read + GAME_PHASE_TIME_OUT) {
if self.previous_game_phase != GamePhase::None {
self.previous_game_phase = GamePhase::None;
for receiver in self.receivers.iter() {
receiver
.borrow_mut()
.game_phase_change(self.previous_game_phase)?;
}
}
}
}
}
// check telemetry data // check telemetry data
write_log!("before telemetry update"); write_log!("before telemetry update");