2023-01-18 06:09:44 +00:00
|
|
|
use std::{
|
|
|
|
ffi::{c_char, CStr},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use rfactor_sm_reader::{rF2VehicleTelemetry, VehicleScoringInfoV01};
|
|
|
|
use ui::prelude::*;
|
|
|
|
use utilities::prelude::Color;
|
|
|
|
|
|
|
|
use crate::overlay::{rfactor_data::DataReceiver, UiOverlay};
|
2023-01-18 07:04:47 +00:00
|
|
|
use crate::write_log;
|
2023-01-18 06:09:44 +00:00
|
|
|
|
|
|
|
pub struct LeaderBoard {
|
|
|
|
gui_handler: Arc<GuiHandler>,
|
|
|
|
gui: Arc<GuiBuilder>,
|
|
|
|
main_grid: Arc<Grid>,
|
|
|
|
|
|
|
|
entries: Vec<LeaderBoardEntry>,
|
|
|
|
|
|
|
|
entry_backgrounds: [Color; 2],
|
2023-01-18 07:04:47 +00:00
|
|
|
player_background: Color,
|
2023-01-18 06:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LeaderBoard {
|
|
|
|
const GRID: &str = include_str!("ui_files/leaderboard_grid.xml");
|
|
|
|
const ENTRY: &str = include_str!("ui_files/leaderboard_entry.xml");
|
|
|
|
|
|
|
|
pub fn new(gui_handler: &Arc<GuiHandler>) -> Result<Self> {
|
|
|
|
let gui = GuiBuilder::from_str(gui_handler, Self::GRID)?;
|
|
|
|
|
|
|
|
let main_grid = gui.element("main_grid")?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
gui_handler: gui_handler.clone(),
|
|
|
|
gui,
|
|
|
|
main_grid,
|
|
|
|
|
|
|
|
entries: Vec::new(),
|
|
|
|
|
|
|
|
entry_backgrounds: [Color::try_from("#838383")?, Color::try_from("#545454")?],
|
2023-01-18 07:04:47 +00:00
|
|
|
player_background: Color::try_from("#b4bf26")?,
|
2023-01-18 06:09:44 +00:00
|
|
|
})
|
|
|
|
}
|
2023-01-18 07:04:47 +00:00
|
|
|
|
|
|
|
fn c_char_to_string(c: [c_char; 32usize]) -> String {
|
|
|
|
unsafe { CStr::from_ptr(&c as *const c_char) }
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.to_string()
|
|
|
|
}
|
2023-01-18 06:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UiOverlay for LeaderBoard {}
|
|
|
|
|
|
|
|
impl DataReceiver for LeaderBoard {
|
|
|
|
fn scoring_update(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> {
|
2023-01-18 07:04:47 +00:00
|
|
|
write_log!("=================== leader board: scoring update ===================");
|
|
|
|
|
2023-01-18 06:09:44 +00:00
|
|
|
for vehicle_scoring in vehicle_scorings {
|
2023-01-18 07:04:47 +00:00
|
|
|
let driver_name = Self::c_char_to_string(vehicle_scoring.mDriverName);
|
|
|
|
|
2023-01-18 06:09:44 +00:00
|
|
|
// check driver list
|
|
|
|
match self
|
|
|
|
.entries
|
|
|
|
.iter_mut()
|
2023-01-18 07:04:47 +00:00
|
|
|
.find(|entry| vehicle_scoring.mID == entry.id())
|
2023-01-18 06:09:44 +00:00
|
|
|
{
|
|
|
|
Some(entry) => {
|
2023-01-18 07:04:47 +00:00
|
|
|
if entry.name() != driver_name {
|
|
|
|
entry.change_name(driver_name)?;
|
|
|
|
}
|
|
|
|
|
2023-01-18 06:09:44 +00:00
|
|
|
entry.update_place(vehicle_scoring.mPlace)?;
|
|
|
|
entry.update_time_behind_leader(vehicle_scoring.mTimeBehindLeader)?;
|
|
|
|
entry.update_time_behind_next(vehicle_scoring.mTimeBehindNext)?;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let entry = LeaderBoardEntry::new(
|
|
|
|
&self.gui_handler,
|
|
|
|
vehicle_scoring.mID,
|
2023-01-18 07:04:47 +00:00
|
|
|
driver_name,
|
2023-01-18 06:09:44 +00:00
|
|
|
vehicle_scoring.mPlace,
|
|
|
|
vehicle_scoring.mTimeBehindLeader,
|
|
|
|
vehicle_scoring.mTimeBehindNext,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
self.entries.push(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 07:04:47 +00:00
|
|
|
write_log!("create entries");
|
|
|
|
|
2023-01-18 06:09:44 +00:00
|
|
|
// check if entry count in grid is the same as the gathered entries
|
2023-01-18 07:04:47 +00:00
|
|
|
let force_update = if !self.entries.is_empty()
|
|
|
|
&& self
|
|
|
|
.main_grid
|
|
|
|
.child_at(0, self.entries.len() - 1)?
|
|
|
|
.is_none()
|
2023-01-18 06:09:44 +00:00
|
|
|
{
|
|
|
|
for i in 0..self.entries.len() {
|
|
|
|
self.main_grid.detach(0, i)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
// check if any entry needs resorting
|
|
|
|
if force_update || self.entries.iter().any(|entry| entry.needs_resorting()) {
|
2023-01-18 07:04:47 +00:00
|
|
|
write_log!("leader board update required");
|
|
|
|
|
2023-01-18 06:09:44 +00:00
|
|
|
self.entries
|
|
|
|
.sort_by(|lhs, rhs| lhs.place().cmp(&rhs.place()));
|
|
|
|
|
|
|
|
for (i, entry) in self.entries.iter_mut().enumerate() {
|
|
|
|
entry.resorting_finished();
|
|
|
|
entry.change_background_color(self.entry_backgrounds[i % 2])?;
|
|
|
|
|
|
|
|
self.main_grid.attach(entry.snippet(), 0, i, 1, 1)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.entries.is_empty() {
|
|
|
|
self.gui.disable()?;
|
|
|
|
} else {
|
|
|
|
self.gui.enable()?;
|
|
|
|
}
|
|
|
|
|
2023-01-18 07:04:47 +00:00
|
|
|
write_log!("leader board update finished");
|
|
|
|
|
2023-01-18 06:09:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn telemetry_update(
|
|
|
|
&mut self,
|
2023-01-18 07:04:47 +00:00
|
|
|
player_id: Option<i32>,
|
2023-01-18 06:09:44 +00:00
|
|
|
_telemetries: &[rF2VehicleTelemetry],
|
|
|
|
) -> Result<()> {
|
2023-01-18 07:04:47 +00:00
|
|
|
if let Some(player_id) = player_id {
|
|
|
|
if let Some(entry) = self.entries.iter().find(|entry| entry.id() == player_id) {
|
|
|
|
entry.change_background_color(self.player_background)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 06:09:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct LeaderBoardEntry {
|
|
|
|
id: i32,
|
|
|
|
|
|
|
|
name: String,
|
|
|
|
place: u8,
|
|
|
|
time_behind_leader: f64,
|
|
|
|
time_behind_next: f64,
|
|
|
|
|
|
|
|
snippet: Arc<GuiSnippet>,
|
|
|
|
|
|
|
|
grid: Arc<Grid>,
|
|
|
|
name_label: Arc<Label>,
|
|
|
|
place_label: Arc<Label>,
|
|
|
|
time_behind_label: Arc<Label>,
|
|
|
|
|
|
|
|
place_updated: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LeaderBoardEntry {
|
|
|
|
pub fn new(
|
|
|
|
gui_handler: &Arc<GuiHandler>,
|
|
|
|
id: i32,
|
|
|
|
name: String,
|
|
|
|
place: u8,
|
|
|
|
time_behind_leader: f64,
|
|
|
|
time_behind_next: f64,
|
|
|
|
) -> Result<Self> {
|
|
|
|
let snippet = GuiSnippet::from_str(gui_handler, LeaderBoard::ENTRY)?;
|
|
|
|
|
|
|
|
let background = snippet.element("grid")?;
|
|
|
|
let name_label: Arc<Label> = snippet.element("name")?;
|
|
|
|
let place_label: Arc<Label> = snippet.element("place")?;
|
|
|
|
let time_behind_label: Arc<Label> = snippet.element("time_behind")?;
|
|
|
|
|
|
|
|
name_label.set_text(&name)?;
|
|
|
|
place_label.set_text(place)?;
|
|
|
|
time_behind_label.set_text(format!("{:.3}", time_behind_leader))?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
id,
|
|
|
|
|
|
|
|
name,
|
|
|
|
place,
|
|
|
|
time_behind_leader,
|
|
|
|
time_behind_next,
|
|
|
|
|
|
|
|
snippet,
|
|
|
|
|
|
|
|
grid: background,
|
|
|
|
name_label,
|
|
|
|
place_label,
|
|
|
|
time_behind_label,
|
|
|
|
|
|
|
|
place_updated: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-18 07:04:47 +00:00
|
|
|
pub fn id(&self) -> i32 {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
|
2023-01-18 06:09:44 +00:00
|
|
|
pub fn place(&self) -> u8 {
|
|
|
|
self.place
|
|
|
|
}
|
|
|
|
|
2023-01-18 07:04:47 +00:00
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change_name(&mut self, name: String) -> Result<()> {
|
|
|
|
self.name = name;
|
|
|
|
|
|
|
|
self.name_label.set_text(&self.name)
|
|
|
|
}
|
|
|
|
|
2023-01-18 06:09:44 +00:00
|
|
|
pub fn snippet(&self) -> Arc<GuiSnippet> {
|
|
|
|
self.snippet.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change_background_color(&self, color: Color) -> Result<()> {
|
|
|
|
self.grid.set_background(color)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_place(&mut self, place: u8) -> Result<()> {
|
|
|
|
if self.place != place {
|
|
|
|
self.place_updated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.place = place;
|
|
|
|
|
|
|
|
self.place_label.set_text(self.place)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_time_behind_leader(&mut self, time: f64) -> Result<()> {
|
|
|
|
self.time_behind_leader = time;
|
|
|
|
|
|
|
|
self.time_behind_label
|
|
|
|
.set_text(format!("{:.3}", self.time_behind_leader))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_time_behind_next(&mut self, time: f64) -> Result<()> {
|
|
|
|
self.time_behind_next = time;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn needs_resorting(&self) -> bool {
|
|
|
|
self.place_updated
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resorting_finished(&mut self) {
|
|
|
|
self.place_updated = false;
|
|
|
|
}
|
|
|
|
}
|