Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make hide time in last 15 seconds optional #189

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions refbox/src/app/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ pub enum BoolGameParameter {
RefAlertEnabled,
AutoSoundStartPlay,
AutoSoundStopPlay,
HideTime,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
26 changes: 25 additions & 1 deletion refbox/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ impl Application for RefBoxApp {

let tm = Arc::new(Mutex::new(tm));

let update_sender = UpdateSender::new(serial_ports, binary_port, json_port);
let update_sender =
UpdateSender::new(serial_ports, binary_port, json_port, config.hide_time);

let sound =
SoundController::new(config.sound.clone(), update_sender.get_trigger_flash_fn());
Expand Down Expand Up @@ -912,6 +913,7 @@ impl Application for RefBoxApp {
games: self.games.clone(),
sound: self.config.sound.clone(),
mode: self.config.mode,
hide_time: self.config.hide_time,
};

self.edited_settings = Some(edited_settings);
Expand Down Expand Up @@ -1008,6 +1010,13 @@ impl Application for RefBoxApp {
self.sound.update_settings(self.config.sound.clone());
self.config.mode = edited_settings.mode;

if self.config.hide_time != edited_settings.hide_time {
self.config.hide_time = edited_settings.hide_time;
self.update_sender
.set_hide_time(self.config.hide_time)
.unwrap();
}

confy::store(APP_NAME, None, &self.config).unwrap();
AppState::MainPage
}
Expand All @@ -1025,6 +1034,13 @@ impl Application for RefBoxApp {
self.sound.update_settings(self.config.sound.clone());
self.config.mode = edited_settings.mode;

if self.config.hide_time != edited_settings.hide_time {
self.config.hide_time = edited_settings.hide_time;
self.update_sender
.set_hide_time(self.config.hide_time)
.unwrap();
}

confy::store(APP_NAME, None, &self.config).unwrap();

let next_game_info = if edited_settings.using_uwhscores {
Expand Down Expand Up @@ -1067,6 +1083,13 @@ impl Application for RefBoxApp {
self.sound.update_settings(self.config.sound.clone());
self.config.mode = edited_settings.mode;

if self.config.hide_time != edited_settings.hide_time {
self.config.hide_time = edited_settings.hide_time;
self.update_sender
.set_hide_time(self.config.hide_time)
.unwrap();
}

confy::store(APP_NAME, None, &self.config).unwrap();
AppState::MainPage
}
Expand Down Expand Up @@ -1246,6 +1269,7 @@ impl Application for RefBoxApp {
BoolGameParameter::AutoSoundStopPlay => {
edited_settings.sound.auto_sound_stop_play ^= true
}
BoolGameParameter::HideTime => edited_settings.hide_time ^= true,
}
}
Message::CycleParameter(param) => {
Expand Down
73 changes: 52 additions & 21 deletions refbox/src/app/update_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,20 @@ pub struct UpdateSender {
}

impl UpdateSender {
pub fn new(initial: Vec<SerialPortBuilder>, binary_port: u16, json_port: u16) -> Self {
pub fn new(
initial: Vec<SerialPortBuilder>,
binary_port: u16,
json_port: u16,
hide_time: bool,
) -> Self {
let (tx, rx) = mpsc::channel(8);

let initial = initial
.into_iter()
.map(|builder| builder.open_native_async().unwrap())
.collect();

let server_join = task::spawn(Server::new(rx, initial).run_loop());
let server_join = task::spawn(Server::new(rx, initial, hide_time).run_loop());

let listener_join = task::spawn(listener_loop(tx.clone(), binary_port, json_port));

Expand Down Expand Up @@ -78,6 +83,20 @@ impl UpdateSender {
let tx = self.tx.clone();
move || tx.try_send(ServerMessage::TriggerFlash)
}

pub fn set_hide_time(&self, hide_time: bool) -> Result<(), TrySendError<bool>> {
self.tx
.try_send(ServerMessage::SetHideTime(hide_time))
.map_err(|e| match e {
TrySendError::Full(ServerMessage::SetHideTime(hide_time)) => {
TrySendError::Full(hide_time)
}
TrySendError::Closed(ServerMessage::SetHideTime(hide_time)) => {
TrySendError::Closed(hide_time)
}
_ => unreachable!(),
})
}
}

impl Drop for UpdateSender {
Expand Down Expand Up @@ -267,6 +286,7 @@ pub enum ServerMessage {
NewSnapshot(GameSnapshot, bool),
TriggerFlash,
Stop,
SetHideTime(bool),
}

#[derive(Debug)]
Expand All @@ -281,10 +301,15 @@ struct Server {
flash: bool,
binary: Vec<u8>,
json: Vec<u8>,
hide_time: bool,
}

impl Server {
pub fn new(rx: mpsc::Receiver<ServerMessage>, initial: Vec<SerialStream>) -> Self {
pub fn new(
rx: mpsc::Receiver<ServerMessage>,
initial: Vec<SerialStream>,
hide_time: bool,
) -> Self {
let mut server = Server {
next_id: 0,
senders: HashMap::new(),
Expand All @@ -296,6 +321,7 @@ impl Server {
flash: false,
binary: Vec::new(),
json: Vec::new(),
hide_time,
};

for stream in initial {
Expand Down Expand Up @@ -355,25 +381,27 @@ impl Server {

self.snapshot = new_snapshot.into();

match self.snapshot.current_period {
GamePeriod::BetweenGames
| GamePeriod::HalfTime
| GamePeriod::OvertimeHalfTime
| GamePeriod::PreOvertime => {
if self.snapshot.secs_in_period < 15 {
self.snapshot.secs_in_period = next_time;
};
}
GamePeriod::PreSuddenDeath => {
if self.snapshot.secs_in_period < 15 {
self.snapshot.secs_in_period = 0;
if self.hide_time {
match self.snapshot.current_period {
GamePeriod::BetweenGames
| GamePeriod::HalfTime
| GamePeriod::OvertimeHalfTime
| GamePeriod::PreOvertime => {
if self.snapshot.secs_in_period < 15 {
self.snapshot.secs_in_period = next_time;
};
}
GamePeriod::PreSuddenDeath => {
if self.snapshot.secs_in_period < 15 {
self.snapshot.secs_in_period = 0;
}
}
GamePeriod::FirstHalf
| GamePeriod::OvertimeFirstHalf
| GamePeriod::OvertimeSecondHalf
| GamePeriod::SecondHalf
| GamePeriod::SuddenDeath => {}
}
GamePeriod::FirstHalf
| GamePeriod::OvertimeFirstHalf
| GamePeriod::OvertimeSecondHalf
| GamePeriod::SecondHalf
| GamePeriod::SuddenDeath => {}
}

self.encode_flash();
Expand Down Expand Up @@ -457,6 +485,9 @@ impl Server {
Some(ServerMessage::Stop) => {
break;
}
Some(ServerMessage::SetHideTime(hide_time)) => {
self.hide_time = hide_time
}
None => {
break;
}
Expand Down Expand Up @@ -581,7 +612,7 @@ mod test {

#[tokio::test]
async fn test_update_sender() {
let update_sender = UpdateSender::new(vec![], BINARY_PORT, JSON_PORT);
let update_sender = UpdateSender::new(vec![], BINARY_PORT, JSON_PORT, false);

let mut binary_conn;
let mut fail_count = 0;
Expand Down
25 changes: 19 additions & 6 deletions refbox/src/app/view_builders/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub(in super::super) struct EditableSettings {
pub games: Option<BTreeMap<u32, GameInfo>>,
pub sound: SoundSettings,
pub mode: Mode,
pub hide_time: bool,
}

pub(in super::super) trait Cyclable
Expand Down Expand Up @@ -194,12 +195,24 @@ fn make_main_config_page<'a>(
)
.style(style::Button::LightGray),
)
.push(make_value_button(
"MODE",
settings.mode.to_string().to_uppercase(),
(true, true),
Some(Message::CycleParameter(CyclingParameter::Mode)),
))
.push(
row()
.spacing(SPACING)
.width(Length::Fill)
.height(Length::Fill)
.push(make_value_button(
"MODE",
settings.mode.to_string().to_uppercase(),
(true, true),
Some(Message::CycleParameter(CyclingParameter::Mode)),
))
.push(make_value_button(
"HIDE TIME FOR\nLAST 15 SECONDS",
bool_string(settings.hide_time),
(false, true),
Some(Message::ToggleBoolParameter(BoolGameParameter::HideTime)),
)),
)
.push(
row()
.spacing(SPACING)
Expand Down
1 change: 1 addition & 0 deletions refbox/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl Default for UwhScores {
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Config {
pub mode: Mode,
pub hide_time: bool,
pub game: Game,
pub hardware: Hardware,
pub uwhscores: UwhScores,
Expand Down