Add scoring update timeout
This commit is contained in:
parent
5186b96b44
commit
eec17e1039
3 changed files with 156 additions and 111 deletions
|
@ -26,6 +26,8 @@ pub struct Pedals {
|
|||
throttle: Arc<ProgressBar>,
|
||||
_history: Arc<Icon>,
|
||||
|
||||
enable: bool,
|
||||
|
||||
throttle_samples: HeapRb<f32>,
|
||||
brake_samples: HeapRb<f32>,
|
||||
|
||||
|
@ -138,6 +140,8 @@ impl Pedals {
|
|||
throttle,
|
||||
_history: history,
|
||||
|
||||
enable: false,
|
||||
|
||||
throttle_samples,
|
||||
brake_samples,
|
||||
|
||||
|
@ -202,7 +206,7 @@ impl Pedals {
|
|||
let command_buffer =
|
||||
CommandBuffer::new_primary().build(self.device.clone(), self.queue.clone())?;
|
||||
|
||||
{
|
||||
if self.enable {
|
||||
let mut recorder = command_buffer.begin(VkCommandBufferBeginInfo::new(
|
||||
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT
|
||||
| VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
|
||||
|
@ -231,7 +235,18 @@ impl Pedals {
|
|||
impl UiOverlay 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(())
|
||||
}
|
||||
|
||||
|
@ -257,10 +272,7 @@ impl DataReceiver for Pedals {
|
|||
player_id: Option<i32>,
|
||||
telemetries: &[rF2VehicleTelemetry],
|
||||
) -> Result<()> {
|
||||
match player_id {
|
||||
Some(id) => {
|
||||
self.gui.enable()?;
|
||||
|
||||
if let Some(id) = player_id {
|
||||
if let Some(telemetry) = telemetries.iter().find(|telemetry| telemetry.id == id) {
|
||||
let brake = 1.0 - telemetry.unfiltered_brake as f32;
|
||||
let throttle = 1.0 - telemetry.unfiltered_throttle as f32;
|
||||
|
@ -274,10 +286,6 @@ impl DataReceiver for Pedals {
|
|||
self.update_vertex_buffers()?;
|
||||
}
|
||||
}
|
||||
None => {
|
||||
self.gui.disable()?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -73,6 +73,8 @@ pub struct Radar {
|
|||
car_width: f32,
|
||||
car_height: f32,
|
||||
|
||||
enable: bool,
|
||||
|
||||
device: Arc<Device>,
|
||||
queue: Arc<Mutex<Queue>>,
|
||||
|
||||
|
@ -171,6 +173,8 @@ impl Radar {
|
|||
car_width,
|
||||
car_height,
|
||||
|
||||
enable: false,
|
||||
|
||||
device,
|
||||
queue,
|
||||
|
||||
|
@ -216,7 +220,7 @@ impl Radar {
|
|||
let command_buffer =
|
||||
CommandBuffer::new_primary().build(self.device.clone(), self.queue.clone())?;
|
||||
|
||||
{
|
||||
if self.enable {
|
||||
let mut recorder = command_buffer.begin(VkCommandBufferBeginInfo::new(
|
||||
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT
|
||||
| VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
|
||||
|
@ -268,7 +272,12 @@ impl Radar {
|
|||
impl UiOverlay 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(())
|
||||
}
|
||||
|
||||
|
@ -299,6 +308,7 @@ impl DataReceiver for Radar {
|
|||
|
||||
self.cars.clear();
|
||||
|
||||
if self.enable {
|
||||
if let Some(player_id) = player_id {
|
||||
// make sure there are enough cars in buffer
|
||||
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
|
||||
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();
|
||||
buffer_car_index += 1;
|
||||
|
@ -360,6 +371,7 @@ impl DataReceiver for Radar {
|
|||
}
|
||||
|
||||
write_log!(format!("other cars: {:?}", self.cars.len()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
use anyhow::Result;
|
||||
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;
|
||||
|
||||
|
@ -25,6 +29,8 @@ pub trait DataReceiver {
|
|||
) -> Result<()>;
|
||||
}
|
||||
|
||||
const GAME_PHASE_TIME_OUT: Duration = Duration::from_secs(2);
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum GamePhase {
|
||||
TestDay,
|
||||
|
@ -56,6 +62,7 @@ pub struct RFactorData {
|
|||
telemetry_reader: TelemetryReader,
|
||||
scoring_reader: ScoringReader,
|
||||
|
||||
last_scoring_read: Duration,
|
||||
start_time: Instant,
|
||||
player_id: Option<i32>,
|
||||
previous_game_phase: GamePhase,
|
||||
|
@ -73,6 +80,7 @@ impl RFactorData {
|
|||
telemetry_reader: TelemetryReader::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,
|
||||
player_id: None,
|
||||
previous_game_phase: GamePhase::None,
|
||||
|
@ -93,9 +101,10 @@ impl RFactorData {
|
|||
write_log!(" =================== update RFactorData ===================");
|
||||
|
||||
// get scoring info
|
||||
if let Some((scoring_info, vehicle_scorings)) =
|
||||
self.scoring_reader.vehicle_scoring(self.now())
|
||||
{
|
||||
match self.scoring_reader.vehicle_scoring(self.now()) {
|
||||
Some((scoring_info, vehicle_scorings)) => {
|
||||
self.last_scoring_read = self.start_time.elapsed();
|
||||
|
||||
write_log!(format!(
|
||||
"new scoring info: vehicles: {}",
|
||||
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
|
||||
write_log!("before telemetry update");
|
||||
|
|
Loading…
Reference in a new issue