1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use std::io::{self, Write};
use crossterm::{
cursor::MoveTo,
event::{
self, Event, KeyCode, KeyEvent,
KeyEventKind::{Press, Repeat},
KeyModifiers,
},
style::{Print, PrintStyledContent, Stylize},
terminal::{Clear, ClearType},
QueueableCommand,
};
use falling_tetromino_engine::Stat;
use crate::{
application::{
Application, CompressedInputHistory, GameRestorationData, Menu, MenuUpdate, ScoresEntry,
ScoresSorting,
},
fmt_helpers::fmt_duration,
};
impl<T: Write> Application<T> {
#[allow(clippy::len_zero)]
pub(in crate::application) fn run_menu_scores_and_replays(&mut self) -> io::Result<MenuUpdate> {
const CAMERA_SIZE: usize = 13;
const CAMERA_MARGIN: usize = 3;
let mut cursor_pos = 0usize;
let mut camera_pos = 0usize;
loop {
let w_main = Self::W_MAIN.into();
let (x_main, y_main) = Self::fetch_main_xy();
let y_selection = Self::H_MAIN / 5;
self.term
.queue(Clear(ClearType::All))?
.queue(MoveTo(x_main, y_main + y_selection))?
.queue(Print(format!("{:^w_main$}", "* Scores and Replays *")))?
.queue(MoveTo(x_main, y_main + y_selection + 2))?
.queue(Print(format!("{:^w_main$}", "──────────────────────────")))?;
let fmt_comparison_stat = |p: &ScoresEntry| match p.game_meta_data.comparison_stat.0 {
Stat::TimeElapsed(_) => format!("time: {}", fmt_duration(p.time_elapsed)),
Stat::PiecesLocked(_) => format!("pieces: {}", p.pieces_locked.iter().sum::<u32>()),
Stat::LinesCleared(_) => format!("lines: {}", p.lineclears),
Stat::PointsScored(_) => format!("score: {}", p.points_scored),
};
let fmt_past_game = |(entry, opt_rep): &(
ScoresEntry,
Option<GameRestorationData<CompressedInputHistory>>,
)| {
format!(
"{} {} | {}{}{}",
entry.game_meta_data.datetime,
entry.game_meta_data.title,
if entry.result.is_ok() { "" } else { "unf." },
fmt_comparison_stat(entry),
if opt_rep.is_some() { " | RP" } else { "" }
)
};
match self.scores_and_replays.sorting {
ScoresSorting::Chronological => self.sort_past_games_chronologically(),
ScoresSorting::Semantic => self.sort_past_games_semantically(),
};
for (i, entry) in self
.scores_and_replays
.entries
.iter()
.skip(camera_pos)
.take(CAMERA_SIZE)
.map(fmt_past_game)
.enumerate()
{
self.term
.queue(MoveTo(
x_main,
y_main + y_selection + 4 + u16::try_from(i).unwrap(),
))?
.queue(Print(format!(
"{:<w_main$}",
if cursor_pos == camera_pos + i {
format!(">{}", entry)
} else {
entry
}
)))?;
}
let entries_left = self
.scores_and_replays
.entries
.len()
.saturating_sub(camera_pos + CAMERA_SIZE);
self.term
.queue(MoveTo(
x_main,
y_main + y_selection + 4 + u16::try_from(CAMERA_SIZE).unwrap(),
))?
.queue(Print(format!(
"{:^w_main$}",
format!(
"{}{}",
if entries_left > 0 {
format!("... +{entries_left} more ")
} else {
"".to_owned()
},
format!("({:?} order [←|→])", self.scores_and_replays.sorting)
)
)))?;
self.term
.queue(MoveTo(
x_main,
y_main + y_selection + 4 + u16::try_from(CAMERA_SIZE).unwrap() + 1,
))?
.queue(PrintStyledContent(
format!(
"{:^w_main$}",
format!("(Controls: [↓|↑]=scroll [Del]=delete [Enter]=replay)")
)
.italic(),
))?;
self.term.flush()?;
// Wait for new input.
match event::read()? {
// Quit menu.
Event::Key(KeyEvent {
code: KeyCode::Char('c' | 'C'),
modifiers: KeyModifiers::CONTROL,
kind: Press | Repeat,
state: _,
}) => break Ok(MenuUpdate::Push(Menu::Quit)),
Event::Key(KeyEvent {
code:
KeyCode::Esc
| KeyCode::Char('q' | 'Q')
| KeyCode::Backspace
| KeyCode::Char('b' | 'B'),
kind: Press,
..
}) => break Ok(MenuUpdate::Pop),
// Move selector up.
Event::Key(KeyEvent {
code: KeyCode::Up | KeyCode::Char('k' | 'K'),
kind: kind @ (Press | Repeat),
..
}) if self.scores_and_replays.entries.len() > 0 => {
// We allow wrapping cursor pos, but only on manual presses (if detectable).
if 0 < cursor_pos || kind == Press {
// Cursor pos possibly wraps back down.
cursor_pos += self.scores_and_replays.entries.len() - 1;
cursor_pos %= self.scores_and_replays.entries.len();
// If it does, then manually reset camera to bottom of scoreboard.
if cursor_pos == self.scores_and_replays.entries.len() - 1 {
camera_pos = self
.scores_and_replays
.entries
.len()
.saturating_sub(CAMERA_SIZE);
// Otherwise cursor just moved normally, and we may have to adapt camera (unless it hit scoreboard end).
} else if 0 < camera_pos && cursor_pos < camera_pos + CAMERA_MARGIN {
camera_pos -= 1;
}
}
}
// Move selector down.
Event::Key(KeyEvent {
code: KeyCode::Down | KeyCode::Char('j' | 'J'),
kind: kind @ (Press | Repeat),
..
}) if self.scores_and_replays.entries.len() > 0 => {
// We allow wrapping cursor pos, but only on manual presses (if detectable).
if cursor_pos < self.scores_and_replays.entries.len() - 1 || kind == Press {
// Cursor pos possibly wraps back up.
cursor_pos += 1;
cursor_pos %= self.scores_and_replays.entries.len();
// If it does, then manually reset camera to bottom of scoreboard.
if cursor_pos == 0 {
camera_pos = 0;
// Otherwise cursor just moved normally, and we may have to adapt camera (unless it hit scoreboard end).
} else if camera_pos + CAMERA_SIZE - CAMERA_MARGIN <= cursor_pos
&& camera_pos
< self
.scores_and_replays
.entries
.len()
.saturating_sub(CAMERA_SIZE)
{
camera_pos += 1;
}
}
}
Event::Key(KeyEvent {
code: KeyCode::Left | KeyCode::Char('h' | 'H'),
kind: Press | Repeat,
..
}) => {
self.scores_and_replays.sorting = match self.scores_and_replays.sorting {
ScoresSorting::Chronological => ScoresSorting::Semantic,
ScoresSorting::Semantic => ScoresSorting::Chronological,
};
}
Event::Key(KeyEvent {
code: KeyCode::Right | KeyCode::Char('l' | 'L'),
kind: Press | Repeat,
..
}) => {
self.scores_and_replays.sorting = match self.scores_and_replays.sorting {
ScoresSorting::Chronological => ScoresSorting::Semantic,
ScoresSorting::Semantic => ScoresSorting::Chronological,
};
}
// Delete entire slot.
Event::Key(KeyEvent {
code: KeyCode::Delete | KeyCode::Char('d' | 'D'),
kind: Press | Repeat,
modifiers,
..
}) if self.scores_and_replays.entries.len() > 0 => {
if modifiers.contains(KeyModifiers::SHIFT) {
self.scores_and_replays.entries[cursor_pos].1.take();
} else {
self.scores_and_replays.entries.remove(cursor_pos);
if 0 < cursor_pos && cursor_pos == self.scores_and_replays.entries.len() {
cursor_pos -= 1;
camera_pos = camera_pos.saturating_sub(1);
}
}
}
// Load slot as savepoint.
Event::Key(KeyEvent {
code: KeyCode::Enter | KeyCode::Char('e' | 'E'),
kind: Press | Repeat,
..
}) if self.scores_and_replays.entries.len() > 0 => {
if let (
ScoresEntry {
game_meta_data,
time_elapsed,
..
},
Some(game_restoration_data),
) = &self.scores_and_replays.entries[cursor_pos]
{
let game_meta_data = game_meta_data.clone();
let game_restoration_data = game_restoration_data
.clone()
.map(|input_history| input_history.decompress());
break Ok(MenuUpdate::Push(Menu::ReplayGame {
game_restoration_data: Box::new(game_restoration_data),
game_meta_data,
replay_length: *time_elapsed,
game_renderer: Default::default(),
}));
} else {
// FIXME: Handle game-replay-unavailable?
}
}
// Other event: don't care.
_ => {}
};
}
}
}