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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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::{
menus::{Menu, MenuUpdate},
Application, CompressedInputHistory, GameRestorationData, ScoresEntry, ScoresSorting,
},
fmt_helpers::fmt_duration,
game_renderers::TetroTUIRenderer,
};
impl<T: Write> Application<T> {
#[allow(clippy::len_zero)]
pub(in crate::application) fn run_menu_scores_and_replays(
&mut self,
cursor_pos: &mut usize,
camera_pos: &mut usize,
) -> io::Result<MenuUpdate> {
let mut re_sort_scoreboard = true;
const CAMERA_SIZE: usize = 11;
const CAMERA_MARGIN: usize = 2;
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(PrintStyledContent(
format!("{:^w_main$}", "* Scores and Replays *").bold(),
))?
.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 sorting = self.scores_and_replays.sorting;
let fmt_past_game = |(rank, (entry, opt_rep)): (
usize,
&(
ScoresEntry,
Option<GameRestorationData<CompressedInputHistory>>,
),
)| {
let lhs_annotation = match sorting {
ScoresSorting::Chronological => entry.game_meta_data.datetime.to_owned(),
ScoresSorting::Scoring => {
format!("{rank: >2}{}", if rank == 1 { '#' } else { '.' })
}
};
format!(
"{} {} | {}{}{}",
lhs_annotation,
entry.game_meta_data.title,
if entry.is_win { "" } else { "unf." },
fmt_comparison_stat(entry),
if opt_rep.is_some() { " | RP" } else { "" }
)
};
if self.scores_and_replays.entries.is_empty() {
self.term
.queue(MoveTo(x_main, y_main + y_selection + 4 + 3))?
.queue(PrintStyledContent(
format!("{:^w_main$}", "The scoreboard is empty.").italic(),
))?
.queue(MoveTo(x_main, y_main + y_selection + 4 + 4))?
.queue(PrintStyledContent(
format!("{:^w_main$}", "If you finish a game it will show up here!")
.italic(),
))?;
} else if re_sort_scoreboard {
re_sort_scoreboard = false;
let mut h = std::hash::DefaultHasher::new();
std::hash::Hash::hash(&self.scores_and_replays.entries[*cursor_pos], &mut h);
let old_hash = std::hash::Hasher::finish(&h);
match self.scores_and_replays.sorting {
ScoresSorting::Chronological => self.sort_past_games_chronologically(),
ScoresSorting::Scoring => self.sort_past_games_semantically(),
};
// let d_pos = cursor_pos.saturating_sub(*camera_pos);
*cursor_pos = self
.scores_and_replays
.entries
.iter()
.enumerate()
.find_map(|(i, entry)| {
let mut h = std::hash::DefaultHasher::new();
std::hash::Hash::hash(entry, &mut h);
let new_hash = std::hash::Hasher::finish(&h);
old_hash.eq(&new_hash).then_some(i)
})
.unwrap_or(*cursor_pos);
// *camera_pos = cursor_pos.saturating_sub(d_pos);
*camera_pos = cursor_pos.saturating_sub(CAMERA_SIZE / 2).min(
self.scores_and_replays
.entries
.len()
.saturating_sub(CAMERA_SIZE),
);
}
for (i, entry) in self
.scores_and_replays
.entries
.iter()
.scan((1, None), |(i, prev_title), e| {
if Some(&e.0.game_meta_data.title) != prev_title.as_ref() {
*prev_title = Some(e.0.game_meta_data.title.clone());
*i = 1;
} else {
*i += 1;
}
Some((*i, e))
})
.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(PrintStyledContent(if *cursor_pos == *camera_pos + i {
format!("{:<w_main$}", format!(">{}", entry)).bold()
} else {
format!("{:<w_main$}", format!(" {}", entry)).reset()
}))?;
}
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(PrintStyledContent(
format!(
"{:^w_main$}",
if entries_left > 0 {
format!("... +{entries_left} more")
} else {
"".to_owned()
}
)
.italic(),
))?;
self.term
.queue(MoveTo(
x_main,
y_main + y_selection + 4 + u16::try_from(CAMERA_SIZE).unwrap() + 1,
))?
.queue(PrintStyledContent(
format!(
"{:^w_main$}",
format!("(Order = {:?} [←|→])", self.scores_and_replays.sorting)
)
.italic(),
))?;
self.term
.queue(MoveTo(
x_main,
y_main + y_selection + 4 + u16::try_from(CAMERA_SIZE).unwrap() + 2,
))?
.queue(PrintStyledContent(
format!(
"{:^w_main$}",
"(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,
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::Scoring,
ScoresSorting::Scoring => ScoresSorting::Chronological,
};
re_sort_scoreboard = true;
}
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::Scoring,
ScoresSorting::Scoring => ScoresSorting::Chronological,
};
re_sort_scoreboard = true;
}
// 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::ALT) {
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: TetroTUIRenderer::with_number(
self.temp_data.renderernumber,
)
.into(),
}));
} else {
// FIXME: Handle game-replay-unavailable?
}
}
// Other event: don't care.
_ => {}
};
}
}
}