Skip to content

Commit

Permalink
Ok it works
Browse files Browse the repository at this point in the history
  • Loading branch information
kkevlar committed Jun 14, 2024
1 parent cef624c commit ddeb559
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
2 changes: 1 addition & 1 deletion command_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "command_sever"
name = "command_server"
version = "0.1.0"
edition = "2021"

Expand Down
11 changes: 7 additions & 4 deletions mjoy_core/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::fs::File;
use std::io::{BufRead, BufReader};
use std::time::{Duration, Instant};

#[derive(Copy, Clone)]
#[derive(Clone)]
pub enum UpdateState {
Done,
Binding,
Binding(Option<String>),
}

pub struct Binder {
Expand All @@ -23,7 +23,7 @@ impl Binder {
bindings_filepath,
bindings_to_make: None,
next_binding_allowed_time: None,
cached_state: UpdateState::Binding,
cached_state: UpdateState::Binding(None),
next_print_time: Instant::now(),
}
}
Expand Down Expand Up @@ -58,6 +58,9 @@ impl Binder {
tracing::info!("I'm trying to bind {}", candidate_binding);
self.next_print_time += Duration::from_secs(1);
}

self.cached_state = UpdateState::Binding(Some(candidate_binding.to_owned()));

match self.perform_candidate_binding(
&candidate_binding,
gilrs,
Expand All @@ -80,7 +83,7 @@ impl Binder {
}
}

self.cached_state
self.cached_state.clone()
}

pub fn perform_candidate_binding(
Expand Down
1 change: 1 addition & 0 deletions mjoy_core/src/joypaths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct NamedPath {
pub common_name: Option<String>,
}

#[derive(Debug)]
pub struct EventPathLookup(pub HashMap<String, String>);

#[derive(Debug)]
Expand Down
38 changes: 24 additions & 14 deletions mjoy_core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod outjoy;
mod team_select;

use clap::Parser;
use command_server;
use mjoy_gui::gui::feedback_info::FeedbackInfo;
use rand;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -63,6 +64,9 @@ fn main() {
.expect("Failed to parse config file");
dbg!(&config);

let (tx, rx) = std::sync::mpsc::channel();
let server_handle = std::thread::spawn(|| command_server::field_commands_forever(tx));

// Read configuration file .json file
let mut mpl = joypaths::MinimalPathLookup::read_from_disk(&config.controller_bindings_file);
mpl.add_missing_paths_for_joys(&config);
Expand Down Expand Up @@ -121,7 +125,6 @@ fn main() {
dbg!(&frozen);

// Check frozen
let mut total_player_count = 0;
let mut missing_players = Vec::new();
for team in frozen.teams.iter() {
for player in team.players.iter() {
Expand All @@ -137,7 +140,6 @@ fn main() {
if fail {
missing_players.push(player.clone());
}
total_player_count += 1;
}
}

Expand Down Expand Up @@ -194,7 +196,7 @@ fn main() {
let mut gui_render_time = std::time::Instant::now();
let mut game_state: GameState = GameState::TeamSelect;
let mut binder = crate::bindings::Binder::new(config.binding_names_file.clone());
let mut team_select_time: Option<std::time::Instant> = None;
let mut candidate = None;
loop {
let event = gilrs.next_event();

Expand All @@ -203,8 +205,9 @@ fn main() {
event: gilrs::EventType::Connected | gilrs::EventType::Disconnected,
..
}) => {
event_path_lookup = joypaths::EventPathLookup::repath(&config);
mpl.add_missing_paths_for_joys(&config);
event_path_lookup = joypaths::EventPathLookup::repath(&config);
dbg!((&mpl, &event_path_lookup));
continue;
}
_ => {}
Expand All @@ -215,14 +218,21 @@ fn main() {
continue;
}

if let Some(team_select_time) = team_select_time {
if !team_select_time.elapsed().is_zero() {
game_state = GameState::GameActive;
use command_server::Command;
match rx.try_recv() {
Ok(Command::Setup) => {
binder = crate::bindings::Binder::new(config.binding_names_file.clone());
game_state = GameState::Binding
}
Ok(Command::Teams(_)) => game_state = GameState::TeamSelect,
Ok(Command::Start) => game_state = GameState::GameActive,
Err(_) => (),
}

match game_state {
GameState::GameActive => {
candidate = None;

let TopContext {
mut fbinfo,
all_joys,
Expand Down Expand Up @@ -266,15 +276,15 @@ fn main() {
mpl.write_to_disk(&config.controller_bindings_file);
use bindings::UpdateState;
match result {
UpdateState::Done => game_state = GameState::TeamSelect,
_ => (),
UpdateState::Done => {
candidate = None;
game_state = GameState::TeamSelect
}
UpdateState::Binding(cand) => candidate = cand,
}
}
GameState::TeamSelect => {
if team_select_time.is_none() {
team_select_time =
Some(std::time::Instant::now() + std::time::Duration::from_secs(10));
}
candidate = None;

let changed = team_select::mutate_team_selection(
&mut frozen,
Expand All @@ -300,7 +310,7 @@ fn main() {
{
gui_render_time = std::time::Instant::now() + std::time::Duration::from_millis(50);
if let Some(tc) = top_context.borrow().as_ref() {
ui.render(&tc.fbinfo, game_state == GameState::GameActive);
ui.render(&tc.fbinfo, game_state == GameState::GameActive, candidate);
} else {
tracing::error!("No update");
}
Expand Down
17 changes: 16 additions & 1 deletion mjoy_gui/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,22 @@ impl Ui {
ui
}

pub fn render(&mut self, feedback: &FeedbackInfo, show_logos: bool) {
pub fn render(
&mut self,
feedback: &FeedbackInfo,
show_logos: bool,
bonus_name: Option<String>,
) {
if let Some(name) = bonus_name {
self.window.draw_text(
&name,
&kiss3d::nalgebra::Point2::new(20.0, 20.0),
60.0,
&self.font,
&kiss3d::nalgebra::Point3::new(1.0, 1.0, 1.0),
);
}

if show_logos && !self.did_gui_on {
manipulate_emulator::resize::resize_and_focus_matching(
&regex::Regex::new("Dolphin.*FPS").unwrap(),
Expand Down

0 comments on commit ddeb559

Please sign in to comment.