pinch-points 0.8.0

A fast, kid-friendly crab-routing game: route streams of crabs into your sandcastle before time runs out
Documentation
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
//! Round-end overlays: the tide-is-in standings card, the puzzle victory
//! card, and the winner confetti.

use crate::app::i18n::fill;
use crate::app::menu_ui;
use crate::app::net::Online;
use crate::app::palette;
use crate::app::settings::GameSettings;
use crate::app::side_panels::leading_seats;
use crate::app::teams::TeamMode;
use crate::app::{Campaign, Sim};
use crate::sim::MAX_PLAYERS;
use bevy::prelude::*;

/// Everything spawned for a results overlay (versus standings, puzzle win).
#[derive(Component)]
pub struct ResultsPanel;

const CARD_TEXT: Color = palette::PARCHMENT;

/// The winner headline and its display colour, derived from the same
/// [`leading_seats`] logic the panels crown with.
fn winner_line(
    settings: &GameSettings,
    names: &crate::app::SeatNames,
    scores: &[u32; MAX_PLAYERS],
    seats: u8,
    mode: TeamMode,
) -> (String, Color) {
    let tr = settings.tr();
    let leaders = leading_seats(scores, seats, mode);
    if mode != TeamMode::Solo {
        let Some(seat) = leaders.iter().position(|&led| led) else {
            return (tr.dead_heat.to_string(), CARD_TEXT);
        };
        let team = mode.team_of(seat as u8);
        return (
            tr.team_wins.replace(
                "{t}",
                &crate::app::teams::label(settings, names, mode, team, seats),
            ),
            palette::player_color(seat as u8),
        );
    }
    match leaders.iter().position(|&led| led) {
        Some(winner) => (
            fill(tr.wins, &[("p", &names.label(tr, winner as u8))]),
            palette::player_color(winner as u8).lighter(0.10),
        ),
        None => (tr.dead_heat.to_string(), CARD_TEXT),
    }
}

/// How many pixels a tile is when the results card draws the winner's
/// castle: bigger than on the board, since it is the one thing on the card
/// that is a picture.
const CASTLE_TILE_PX: f32 = 96.0;

/// The winner's castle on the results card, built from the same pieces as
/// the one on their tile, at the tier they finished on: a round won big
/// shows off a big castle, moat and all.
fn winner_castle(
    card: &mut ChildSpawnerCommands,
    art: &crate::app::art::Art,
    tier: u8,
    color: Color,
) {
    let t = CASTLE_TILE_PX;
    // Room for the widest castle (the moat) and its flag above the keep.
    let side = 1.4 * t;
    card.spawn(Node {
        width: Val::Px(side),
        height: Val::Px(side),
        margin: UiRect::vertical(Val::Px(4.0)),
        ..default()
    })
    .with_children(|stage| {
        // A mound of sand to stand on, or it floats on the dark card.
        let mound = Vec2::new(1.55, 0.6) * t;
        stage.spawn((
            // Stretched: left to keep its square, the mound drew as a round
            // blob under the gate rather than ground under the castle.
            ImageNode::new(art.puddle.clone())
                .with_color(Color::srgba(0.84, 0.77, 0.62, 0.9))
                .with_mode(NodeImageMode::Stretch),
            Node {
                position_type: PositionType::Absolute,
                left: Val::Px(side / 2.0 - mound.x / 2.0),
                top: Val::Px(side / 2.0 + 0.40 * t - mound.y / 2.0),
                width: Val::Px(mound.x),
                height: Val::Px(mound.y),
                ..default()
            },
        ));
        // The parts come back to front, which is the order UI draws in.
        for part in crate::app::board_render::castle_parts(art, tier, color) {
            let size = part.size * t;
            let node = Node {
                position_type: PositionType::Absolute,
                left: Val::Px(side / 2.0 + part.at.x * t - size.x / 2.0),
                // UI runs down, the castle's y up.
                top: Val::Px(side / 2.0 - part.at.y * t - size.y / 2.0),
                width: Val::Px(size.x),
                height: Val::Px(size.y),
                ..default()
            };
            match part.image {
                Some(image) => {
                    stage.spawn((ImageNode::new(image).with_color(part.tint), node));
                }
                None => {
                    stage.spawn((BackgroundColor(part.tint), node));
                }
            }
        }
    });
}

/// Spawn the wrapper that centres the results card, and return its entity.
fn results_card(commands: &mut Commands) -> Entity {
    commands
        .spawn((
            ResultsPanel,
            // Above the header/prompt bars in the UI stack.
            GlobalZIndex(menu_ui::layer::RESULTS),
            menu_ui::centred_overlay(),
        ))
        .id()
}

/// The series under the standings: each seat's wins at this size, this
/// far apart, wrapping to lines no wider than this. Wide enough for the
/// widest entry there is, a team of three twelve-letter names with three
/// wins (see `the_series_fits_the_card`), and two seats to a line when
/// every name is that long.
const SERIES_FONT: f32 = 23.0;
const SERIES_GAP: f32 = 24.0;
const SERIES_W: f32 = 600.0;

fn card_text(size: f32, color: Color) -> (TextFont, TextColor) {
    (
        TextFont {
            font_size: FontSize::Px(size),
            ..default()
        },
        TextColor(color),
    )
}

/// The standings, best first: place, who, and what they banked, each in its
/// seat's colour. In 2v2 the two teams' totals stand in for the four seats.
///
/// Pure, so the ordering and the team arithmetic can be checked without
/// building a card. `tag` supplies the "(you)" / "(AI)" marker.
fn standings_rows(
    settings: &GameSettings,
    names: &crate::app::SeatNames,
    scores: &[u32; MAX_PLAYERS],
    seats: u8,
    mode: TeamMode,
    tag: impl Fn(u8) -> &'static str,
) -> Vec<(String, Color)> {
    if mode != TeamMode::Solo {
        let totals = crate::app::teams::team_scores(scores, seats, mode);
        let mut standing: Vec<(String, u32, u8)> = totals
            .iter()
            .enumerate()
            .map(|(team, &total)| {
                let team = team as u8;
                let lead_seat = crate::app::teams::face_of(mode, team, seats);
                (
                    crate::app::teams::label(settings, names, mode, team, seats),
                    total,
                    lead_seat,
                )
            })
            .collect();
        standing.sort_by_key(|&(_, total, seat)| (std::cmp::Reverse(total), seat));
        return standing
            .iter()
            .map(|(name, total, seat)| (name.clone(), *total, *seat))
            .enumerate()
            .map(|(place, (name, total, seat))| {
                (
                    format!("{}  {name:<13} {total:>4}", place + 1),
                    palette::player_color(seat),
                )
            })
            .collect();
    }
    // Every row as wide as the widest, name and tag together, so the
    // scores stand in one column: each row is centred on its own, and a
    // long name ("Longnamebot99") pushed its row half a name off the rest.
    let who = |seat: u8| format!("{}{}", names.label(settings.tr(), seat), tag(seat));
    let width = (0..seats)
        .map(|seat| who(seat).chars().count())
        .max()
        .unwrap_or(0)
        .max(MIN_WHO);
    standing_order(scores, seats)
        .iter()
        .enumerate()
        .map(|(place, &seat)| {
            (
                format!(
                    "{}  {:<width$} {:>4}",
                    place + 1,
                    who(seat),
                    scores[seat as usize]
                ),
                palette::player_color(seat),
            )
        })
        .collect()
}

/// The narrowest the name column of the standings is drawn: a seat label
/// and its tag, "P2 (AI)", which every row was padded to before rows took
/// the widest name's width.
const MIN_WHO: usize = 9;

/// The seats best first, the order a free-for-all's standings are written
/// in: by score, and by seat between equals.
fn standing_order(scores: &[u32; MAX_PLAYERS], seats: u8) -> Vec<u8> {
    let mut order: Vec<u8> = (0..seats).collect();
    order.sort_by_key(|&seat| std::cmp::Reverse(scores[seat as usize]));
    order
}

/// The round's awards as card lines, each in the colour of the first seat
/// it names. Nothing for a solo table, where every award goes to the one
/// player there, or for a round the tally did not see whole.
fn award_rows(
    settings: &GameSettings,
    names: &crate::app::SeatNames,
    tally: &crate::app::awards::RoundTally,
    board: &crate::sim::Board,
    seats: u8,
    mode: TeamMode,
) -> Vec<(String, Color)> {
    if seats < 2 || !tally.covers(board) {
        return Vec::new();
    }
    let tr = settings.tr();
    let winners = leading_seats(board.scores(), seats, mode);
    tally
        .awards(seats, mode, &winners)
        .iter()
        .map(|award| {
            (
                award.line(tr, |seat| names.label(tr, seat)),
                palette::player_color(award.who[0]),
            )
        })
        .collect()
}

/// What the crowd called, for everyone at an online table, and for a
/// spectator how their own call went. Nothing when nobody called anyone.
fn crowd_rows(
    settings: &GameSettings,
    names: &crate::app::SeatNames,
    online: &Online,
) -> Vec<(String, Color)> {
    let Some(session) = &online.0 else {
        return Vec::new();
    };
    let tr = settings.tr();
    let mut rows = Vec::new();
    if session.stands.picks.any() {
        let list = session.stands.picks.line(|seat| names.label(tr, seat));
        rows.push((
            fill(tr.crowd_picked, &[("l", &list)]),
            CARD_TEXT.darker(0.15),
        ));
    }
    if let Some((seat, right)) = session.stands.last_call {
        let calls = &session.stands.calls;
        let (a, b) = (calls.right.to_string(), calls.made.to_string());
        rows.push(match right {
            true => (fill(tr.call_right, &[("a", &a), ("b", &b)]), palette::GOLD),
            false => (
                fill(
                    tr.call_wrong,
                    &[("p", &names.label(tr, seat)), ("a", &a), ("b", &b)],
                ),
                palette::player_color(seat),
            ),
        });
    }
    rows
}

/// What a finished round fed into besides its own scores: the day's best,
/// the reel being cut from it, the series it belongs to, and the awards.
#[derive(bevy::ecs::system::SystemParam)]
pub struct RoundExtras<'w> {
    daily: Res<'w, crate::app::Daily>,
    highlight: Res<'w, crate::app::Highlight>,
    stats: Res<'w, crate::app::achievements::Stats>,
    tournament: Res<'w, crate::app::tournament::Tournament>,
    tally: Res<'w, crate::app::awards::RoundTally>,
    kinds: Res<'w, crate::app::SeatKinds>,
}

/// The tide-is-in standings card: winner headline, ranked scores in seat
/// colours (with AI/you markers), the round's awards, and its total haul.
pub fn spawn_versus_results(
    mut commands: Commands,
    sim: Res<Sim>,
    settings: Res<GameSettings>,
    art: Res<crate::app::art::Art>,
    mut rng: ResMut<crate::app::effects::VisualRng>,
    seating: crate::app::side_panels::Seating,
    extras: RoundExtras,
) {
    let local = seating.local();
    let crate::app::side_panels::Seating {
        seats,
        controllers,
        names,
        online,
        ..
    } = seating;
    let RoundExtras {
        daily,
        highlight,
        stats,
        tournament,
        tally,
        kinds,
    } = extras;
    let board = &sim.0;
    let scores = board.scores();
    let count = seats.0.max(2);
    let mode = crate::app::teams::in_play(&settings, &online, count);
    let tr = settings.tr();
    let (headline, headline_color) = winner_line(&settings, &names, scores, count, mode);
    // Confetti for a decided round, in the winners' colours, unless the
    // player asked for less motion, in which case the card speaks for itself.
    let winners = leading_seats(scores, count, mode);
    for seat in 0..MAX_PLAYERS as u8 {
        if winners[seat as usize] && !settings.reduced_motion {
            crate::app::effects::confetti(
                &mut commands,
                &mut rng,
                &art,
                palette::player_color(seat),
            );
        }
    }

    let rows = standings_rows(&settings, &names, scores, count, mode, |seat| {
        crate::app::side_panels::seat_tag(tr, &controllers, local, seat)
    });
    let haul = board.crabs_banked();
    let awards = award_rows(&settings, &names, &tally, board, seats.0, mode);

    // Whose castle the card shows off: the winner's, at the tier the round
    // left it on. A dead heat has no one to crown, and keeps the card's gold.
    let crowned = winners.iter().position(|&won| won).map(|seat| seat as u8);
    let card = results_card(&mut commands);
    commands.entity(card).with_children(|wrap| {
        // Opaque, as the round's other cards are: it stands on the board,
        // and the browsing cards' 0.95, blended in linear light, let the
        // crabs and fences show through the standings.
        let mut frame = wrap.spawn(menu_ui::screen_card());
        frame.insert(BackgroundColor(palette::CARD_BG));
        if crowned.is_some() {
            frame.insert(BorderColor::all(headline_color));
        }
        frame.with_children(|card| {
            card.spawn((
                Text::new(tr.tide_is_in),
                menu_ui::display_font(20.0),
                TextColor(CARD_TEXT.darker(0.1)),
            ));
            if let Some(seat) = crowned {
                let tier = crate::sim::castle_tier(scores[usize::from(seat)]);
                winner_castle(card, &art, tier, palette::player_color(seat));
            }
            card.spawn((
                Text::new(headline),
                menu_ui::display_font(34.0),
                TextColor(headline_color),
            ));
            card.spawn(Node {
                height: Val::Px(6.0),
                ..default()
            });
            // A bot wears the robot beside its line. The rows keep one
            // left edge between them, so a slot is held on every row of a
            // table with any bot at it.
            let seat_rows: Vec<Option<u8>> = match mode {
                TeamMode::Solo => standing_order(scores, count)
                    .into_iter()
                    .map(Some)
                    .collect(),
                TeamMode::Pairs | TeamMode::Trios => vec![None; rows.len()],
            };
            let any_bot = seat_rows.iter().flatten().any(|&seat| kinds.bot(seat));
            for ((line, color), seat) in rows.into_iter().zip(seat_rows) {
                let row = card_text(23.0, color);
                if !any_bot {
                    card.spawn((Text::new(line), row.0, row.1));
                    continue;
                }
                // At a table with a bot every row leads with a robot's
                // room, and only the bots' draw one: the columns of the
                // table stay lined up whoever holds which seat.
                let bot = seat.filter(|&seat| kinds.bot(seat));
                let room = match bot {
                    Some(seat) => crate::app::side_panels::robot_inline(&art, seat, 22.0),
                    // The robot itself, unseen: a box only takes its room
                    // once its image has loaded, and Bevy's own blank one
                    // never does, so it took none and these rows sat a
                    // robot's width off the bots'.
                    None => InlineImage {
                        color: Color::NONE,
                        ..crate::app::side_panels::robot_inline(&art, 0, 22.0)
                    },
                };
                let (font, ink) = (row.0.clone(), row.1);
                card.spawn((Text::new(""), row.0, row.1))
                    .with_children(|text| {
                        text.spawn(room);
                        text.spawn((
                            TextSpan::new(crate::app::side_panels::beside_the_robot(true, &line)),
                            font,
                            ink,
                        ));
                    });
            }
            if !awards.is_empty() {
                card.spawn(Node {
                    height: Val::Px(6.0),
                    ..default()
                });
            }
            for (line, color) in awards {
                let row = card_text(17.0, color);
                card.spawn((Text::new(line), row.0, row.1));
            }
            for (line, color) in crowd_rows(&settings, &names, &online) {
                let row = card_text(17.0, color);
                card.spawn((Text::new(line), row.0, row.1));
            }
            card.spawn(Node {
                height: Val::Px(6.0),
                ..default()
            });
            let foot = card_text(17.0, CARD_TEXT.darker(0.15));
            card.spawn((
                Text::new(fill(tr.haul, &[("n", &haul.to_string())])),
                foot.0,
                foot.1,
            ));
            // The reel is written off-thread and is usually not there yet
            // when the card goes up: the line is spawned hidden and shown
            // by `update_highlight_line` once the save has happened.
            let line = card_text(15.0, CARD_TEXT.darker(0.3));
            card.spawn((
                highlight_line_node(highlight.0.is_some()),
                Text::new(highlight_line_text(tr, &highlight)),
                line.0,
                line.1,
                HighlightLine,
            ));
            if daily.active {
                let best = card_text(17.0, palette::GOLD);
                card.spawn((
                    Text::new(fill(tr.daily_best, &[("n", &stats.daily_best.to_string())])),
                    best.0,
                    best.1,
                ));
            }
            if tournament.in_series() {
                card.spawn(Node {
                    height: Val::Px(6.0),
                    ..default()
                });
                let round = card_text(17.0, CARD_TEXT.darker(0.15));
                card.spawn((
                    Text::new(crate::app::tournament::round_line(tr, &tournament)),
                    round.0,
                    round.1,
                ));
                // Each seat's wins in its own colour, as many to a line as
                // fit. One gold line joined with dots ran a table of six
                // past the card's frame, and six full names past the window.
                card.spawn(Node {
                    flex_wrap: FlexWrap::Wrap,
                    justify_content: JustifyContent::Center,
                    column_gap: Val::Px(SERIES_GAP),
                    max_width: Val::Px(SERIES_W),
                    ..default()
                })
                .with_children(|series| {
                    for standing in crate::app::tournament::standings(
                        &settings,
                        &names,
                        &tournament,
                        mode,
                        count,
                    ) {
                        let entry = card_text(SERIES_FONT, standing.color);
                        series.spawn((
                            Text::new(standing.line()),
                            entry.0,
                            entry.1,
                            TextLayout::no_wrap(),
                        ));
                    }
                });
                if tournament.is_decided() {
                    if let Some(champ) = tournament.winner(mode, count) {
                        let (who, seat) = crate::app::tournament::champion_name(
                            &settings, &names, mode, champ, count,
                        );
                        let line = card_text(26.0, palette::player_color(seat).lighter(0.1));
                        card.spawn((
                            Text::new(fill(tr.tour_champion, &[("p", &who)])),
                            line.0,
                            line.1,
                        ));
                    } else {
                        // Over with nobody ahead. The card used to say
                        // nothing at all, neither a champion nor a next
                        // round, as though the series had simply stopped.
                        let line = card_text(26.0, CARD_TEXT);
                        card.spawn((Text::new(tr.tour_drawn), line.0, line.1));
                    }
                } else {
                    let hint = card_text(17.0, CARD_TEXT.darker(0.15));
                    let door =
                        crate::app::play_input::enter_door(tr, &online, tournament.is_running());
                    card.spawn((Text::new(door), hint.0, hint.1));
                }
            }
        });
    });
}

/// The results card's "highlight reel: …" line, shown once the reel is
/// on disk and not before.
#[derive(Component)]
pub struct HighlightLine;

/// The line's node: laid out only when there is a path to say.
fn highlight_line_node(shown: bool) -> Node {
    Node {
        display: if shown { Display::Flex } else { Display::None },
        ..default()
    }
}

/// What the line says: the reel's path, or nothing yet.
fn highlight_line_text(tr: &crate::app::i18n::Tr, highlight: &crate::app::Highlight) -> String {
    highlight
        .0
        .as_ref()
        .map(|path| fill(tr.highlight_saved, &[("path", path)]))
        .unwrap_or_default()
}

/// Show the reel line when the reel thread reports the GIF written.
pub fn update_highlight_line(
    highlight: Res<crate::app::Highlight>,
    settings: Res<GameSettings>,
    mut lines: Query<(&mut Text, &mut Node), With<HighlightLine>>,
) {
    if !highlight.is_changed() {
        return;
    }
    let tr = settings.tr();
    for (mut text, mut node) in &mut lines {
        text.0 = highlight_line_text(tr, &highlight);
        *node = highlight_line_node(highlight.0.is_some());
    }
}

/// The puzzle victory card: level cleared, name, and what comes next.
pub fn spawn_puzzle_won(
    mut commands: Commands,
    campaign: Res<Campaign>,
    settings: Res<GameSettings>,
    sim: Res<Sim>,
    art: Res<crate::app::art::Art>,
) {
    let tr = settings.tr();
    let name = campaign
        .shown_name(campaign.index, settings.language)
        .to_string();
    // The shipped campaign ends with its last shipped stage; the player's
    // own levels behind it are a shelf, not stage eighty-three, and the
    // last of those ends only the list. So the card counts this level
    // within whichever of the two it belongs to, and only the shipped
    // section's end is the campaign's end.
    let (place, of, custom) = campaign.place();
    let last = campaign.is_last();
    let last_shipped = last && !custom;
    let board = &sim.0;
    // The castle the crabs were routed to: the first on the beach, which
    // is the only one in all but a handful of stages.
    let castle = board.castle_owners().next();
    let card = results_card(&mut commands);
    commands.entity(card).with_children(|wrap| {
        let mut frame = wrap.spawn(menu_ui::screen_card());
        frame.insert(BackgroundColor(palette::CARD_BG));
        frame.with_children(|card| {
            let saved_all = campaign.current().goal.saves_every_crab();
            let title = match (last_shipped, saved_all) {
                (true, _) => tr.campaign_done,
                (false, true) => tr.all_safe,
                (false, false) => tr.goal_met,
            };
            card.spawn((
                Text::new(title),
                menu_ui::display_font(34.0),
                TextColor(palette::GOLD),
            ));
            if let Some(owner) = castle {
                let tier = crate::sim::castle_tier(board.scores()[usize::from(owner)]);
                winner_castle(card, &art, tier, palette::player_color(owner));
            }
            saved_row(card, &art, &campaign, board);
            let sub = card_text(21.0, CARD_TEXT);
            let shelf = match custom {
                true => format!("{}  ", tr.stage_custom),
                false => String::new(),
            };
            card.spawn((
                Text::new(format!("{shelf}{place} / {of}  -  {name}")),
                sub.0,
                sub.1,
            ));
            let foot = card_text(17.0, CARD_TEXT.darker(0.15));
            card.spawn((
                Text::new(match (last, saved_all) {
                    (true, _) => tr.last_level,
                    (false, true) => tr.prompt_won,
                    (false, false) => tr.prompt_won_goal,
                }),
                foot.0,
                foot.1,
            ));
        });
    });
}

/// How big a crab is in the row of them on a puzzle's card.
const ROW_CRAB_PX: f32 = 34.0;
/// Most crabs the row shows: a stage with spawners can save dozens, and a
/// row of forty is a smear. Past this the row is as many as fit.
const ROW_CRABS_MAX: usize = 12;

/// The level's crabs in a row on a strip of sand, the ones saved bright and
/// the rest faded, so a win shows off who made it home and a loss shows
/// how near it came.
///
/// Drawn as the level's own crabs, kind and claw and all, in the order the
/// level lists them: a crab from a spawner, which the level cannot name,
/// is a common one. Which crabs were the lost ones is not something the
/// board keeps once they are gone, so a loss dims the tail of the row: the
/// count is exact, the faces are the level's.
fn saved_row(
    card: &mut ChildSpawnerCommands,
    art: &crate::app::art::Art,
    campaign: &Campaign,
    board: &crate::sim::Board,
) {
    use crate::app::creatures::{body_color, claw_color, mirrored};
    use crate::sim::{CrabKind, Handedness};
    let level = campaign.current();
    let of = board.crabs_spawned().max(level.crab_count()) as usize;
    let saved = board.crabs_banked() as usize;
    let shown = of.min(ROW_CRABS_MAX);
    let start = level.board();
    let faces: Vec<(CrabKind, Handedness)> = start
        .crabs()
        .iter()
        .map(|crab| (crab.kind, crab.handed))
        .chain(std::iter::repeat((CrabKind::Common, Handedness::Right)))
        .take(shown)
        .collect();
    card.spawn((
        Node {
            column_gap: Val::Px(2.0),
            padding: UiRect::axes(Val::Px(12.0), Val::Px(4.0)),
            margin: UiRect::vertical(Val::Px(4.0)),
            border_radius: BorderRadius::all(Val::Px(14.0)),
            ..default()
        },
        BackgroundColor(Color::srgb(0.84, 0.77, 0.62)),
    ))
    .with_children(|row| {
        for (i, (kind, handed)) in faces.into_iter().enumerate() {
            let home = i < saved;
            let fade = if home { 1.0 } else { 0.28 };
            let layer = |image: &Handle<Image>, color: Color| {
                let mut node = ImageNode::new(image.clone()).with_color(color.with_alpha(fade));
                node.flip_y = mirrored(handed);
                (
                    node,
                    Node {
                        position_type: PositionType::Absolute,
                        width: Val::Percent(100.0),
                        height: Val::Percent(100.0),
                        ..default()
                    },
                )
            };
            row.spawn(Node {
                width: Val::Px(ROW_CRAB_PX),
                height: Val::Px(ROW_CRAB_PX),
                ..default()
            })
            .with_children(|icon| {
                // Facing along the row, the way they walked home.
                icon.spawn(layer(&art.crab, body_color(kind)));
                icon.spawn(layer(&art.claw, claw_color(handed)));
            });
        }
    });
}

/// The puzzle loss card: which level, and Enter to try again.
///
/// A loss stops the sim on the tick the crab was lost, and the board holds
/// still under whatever it was doing, a gull mid-tile with its meal. With
/// no card that reads as the game hanging, the prompt pill along the bottom
/// being the only word of it.
pub fn spawn_puzzle_lost(
    mut commands: Commands,
    campaign: Res<Campaign>,
    settings: Res<GameSettings>,
    // Optional, so the card goes up whatever else is loaded: the row of
    // crabs is a picture on it, and the words are the news.
    sim: Option<Res<Sim>>,
    art: Option<Res<crate::app::art::Art>>,
) {
    let tr = settings.tr();
    let name = campaign
        .shown_name(campaign.index, settings.language)
        .to_string();
    let card = results_card(&mut commands);
    commands.entity(card).with_children(|wrap| {
        let mut frame = wrap.spawn(menu_ui::screen_card());
        frame.insert((
            BackgroundColor(palette::CARD_BG),
            BorderColor::all(palette::INK_RAID.with_alpha(0.6)),
        ));
        frame.with_children(|card| {
            card.spawn((
                Text::new(tr.crabs_lost),
                menu_ui::display_font(34.0),
                TextColor(palette::INK_RAID),
            ));
            if let (Some(sim), Some(art)) = (&sim, &art) {
                saved_row(card, art, &campaign, &sim.0);
            }
            let sub = card_text(21.0, CARD_TEXT);
            card.spawn((
                Text::new(format!(
                    "{} / {}  -  {name}",
                    campaign.index + 1,
                    campaign.levels.len()
                )),
                sub.0,
                sub.1,
            ));
            let foot = card_text(17.0, CARD_TEXT.darker(0.15));
            card.spawn((Text::new(tr.prompt_lost), foot.0, foot.1));
        });
    });
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::i18n::EN;

    /// Losing a puzzle raises a card over the held board, and leaving the
    /// phase takes it down again: the stop is said, not left to be guessed.
    #[test]
    fn a_lost_puzzle_says_so_on_a_card() {
        use crate::app::{CampaignKind, Phase, Screen};
        use crate::sim::campaign_levels;
        let mut app = App::new();
        app.add_plugins(bevy::state::app::StatesPlugin);
        let levels = campaign_levels();
        let builtins = levels.len();
        app.insert_resource(Campaign {
            kind: CampaignKind::TidePool,
            levels,
            index: 3,
            builtins,
        });
        app.insert_resource(GameSettings::default());
        app.insert_state(Screen::Puzzle);
        app.init_state::<Phase>();
        app.add_systems(
            OnEnter(Phase::Lost),
            spawn_puzzle_lost.run_if(in_state(Screen::Puzzle)),
        );
        app.add_systems(OnExit(Phase::Lost), menu_ui::despawn_marked::<ResultsPanel>);
        let cards = |app: &mut App| {
            app.world_mut()
                .query_filtered::<Entity, With<ResultsPanel>>()
                .iter(app.world())
                .count()
        };
        app.update();
        assert_eq!(cards(&mut app), 0, "no card while the round runs");
        app.world_mut()
            .resource_mut::<NextState<Phase>>()
            .set(Phase::Lost);
        app.update();
        assert_eq!(cards(&mut app), 1, "one card on the loss");
        let said: Vec<String> = app
            .world_mut()
            .query::<&Text>()
            .iter(app.world())
            .map(|t| t.0.clone())
            .collect();
        assert!(said.iter().any(|t| t == EN.crabs_lost), "{said:?}");
        assert!(said.iter().any(|t| t == EN.prompt_lost), "{said:?}");
        assert!(said.iter().any(|t| t.starts_with("4 / ")), "{said:?}");
        app.world_mut()
            .resource_mut::<NextState<Phase>>()
            .set(Phase::Setup);
        app.update();
        assert_eq!(cards(&mut app), 0, "and gone again on retry");
    }

    /// The series line fits the card at its widest. A team of three with
    /// the longest names a seat can carry and the most wins a series can
    /// give is one entry, and has to fit a line on its own; the card around
    /// the lines has to fit the window the interface is drawn for.
    #[test]
    fn the_series_fits_the_card() {
        use crate::app::i18n::metrics::text_px;
        use crate::app::settings::{DESIGN_W, NAME_MAX};
        let name = "M".repeat(NAME_MAX);
        let trio = [name.as_str(); 3].join("+");
        let widest = format!("{trio}  ***");
        let px = text_px(&widest, SERIES_FONT);
        assert!(px <= SERIES_W, "{widest:?} is {px}px of {SERIES_W}");
        // Two solo seats of the longest name share a line.
        let solo = text_px(&format!("{name}  ***"), SERIES_FONT);
        assert!(2.0 * solo + SERIES_GAP <= SERIES_W, "{solo}px a seat");
        let card = SERIES_W + 2.0 * 22.0;
        assert!(card <= DESIGN_W, "a {card}px card");
    }

    /// The standings run best-first, carry each seat's marker, and in 2v2
    /// collapse to the two team totals.
    #[test]
    fn standings_rank_by_score() {
        let rows = standings_rows(
            &GameSettings::default(),
            &crate::app::SeatNames::default(),
            &[3, 9, 5, 0, 0, 0],
            4,
            TeamMode::Solo,
            |seat| {
                if seat == 1 { EN.tag_you } else { EN.tag_ai }
            },
        );
        let lines: Vec<&str> = rows.iter().map(|(line, _)| line.as_str()).collect();
        assert!(
            lines[0].starts_with("1  P2"),
            "the leader is first: {lines:?}"
        );
        assert!(lines[0].contains("(you)"), "and carries its marker");
        assert!(lines[1].starts_with("2  P3"));
        assert!(lines[2].starts_with("3  P1"));
        assert!(lines[3].starts_with("4  P4"));
        assert!(lines[3].ends_with('0'), "the score is on the line");
        // Each row wears its own seat colour, not its placing colour.
        assert_eq!(rows[0].1, palette::player_color(1));
        assert_eq!(rows[2].1, palette::player_color(0));
    }

    /// Team standings collapse to one row per team, named by the seats on
    /// it, best first.
    #[test]
    fn standings_collapse_to_one_row_a_team() {
        let rows = standings_rows(
            &GameSettings::default(),
            &crate::app::SeatNames::default(),
            &[1, 2, 10, 0, 0, 0],
            4,
            TeamMode::Pairs,
            |_| "",
        );
        assert_eq!(rows.len(), 2, "one row per team");
        assert!(
            rows[0].0.contains("P3+P4"),
            "the leading team leads: {}",
            rows[0].0
        );
        assert!(rows[0].0.trim_end().ends_with("10"), "{}", rows[0].0);
        assert!(rows[1].0.contains("P1+P2"), "{}", rows[1].0);
        assert!(rows[1].0.trim_end().ends_with('3'), "{}", rows[1].0);

        // Six seats in trios: two rows of three, and the mirror-image split
        // means the label lists every member.
        let rows = standings_rows(
            &GameSettings::default(),
            &crate::app::SeatNames::default(),
            &[1, 9, 1, 9, 1, 9],
            6,
            TeamMode::Trios,
            |_| "",
        );
        assert_eq!(rows.len(), 2);
        assert!(rows[0].0.contains("P2+P4+P6"), "{}", rows[0].0);
        assert!(rows[0].0.trim_end().ends_with("27"), "{}", rows[0].0);

        // And six seats in pairs is three rows.
        let rows = standings_rows(
            &GameSettings::default(),
            &crate::app::SeatNames::default(),
            &[1, 1, 5, 5, 3, 3],
            6,
            TeamMode::Pairs,
            |_| "",
        );
        assert_eq!(rows.len(), 3, "2v2v2");
        assert!(rows[0].0.contains("P3+P4"), "{}", rows[0].0);
    }

    #[test]
    fn winner_line_names_the_unique_top_scorer() {
        let (text, _) = winner_line(
            &GameSettings::default(),
            &crate::app::SeatNames::default(),
            &[3, 9, 0, 0, 0, 0],
            2,
            TeamMode::Solo,
        );
        assert_eq!(text, "P2 wins!");
        let (text, _) = winner_line(
            &GameSettings::default(),
            &crate::app::SeatNames::default(),
            &[4, 4, 0, 0, 0, 0],
            2,
            TeamMode::Solo,
        );
        assert_eq!(text, EN.dead_heat);
        // Seats outside the match never win, whatever their array slots say.
        let (text, _) = winner_line(
            &GameSettings::default(),
            &crate::app::SeatNames::default(),
            &[1, 2, 99, 0, 0, 0],
            2,
            TeamMode::Solo,
        );
        assert_eq!(text, "P2 wins!");
    }

    /// A named seat wins under its own name, in the standings and in the
    /// headline both.
    #[test]
    fn a_named_seat_wins_under_its_name() {
        let settings = GameSettings {
            names: std::array::from_fn(|seat| match seat {
                0 => "Anna".to_string(),
                1 => "Bo".to_string(),
                _ => String::new(),
            }),
            ..GameSettings::default()
        };
        let names = crate::app::SeatNames(settings.names.clone());
        let (text, _) = winner_line(&settings, &names, &[3, 9, 0, 0, 0, 0], 2, TeamMode::Solo);
        assert_eq!(text, "Bo wins!");
        let rows = standings_rows(
            &settings,
            &names,
            &[3, 9, 0, 0, 0, 0],
            2,
            TeamMode::Solo,
            |_| "",
        );
        assert!(rows[0].0.contains("Bo"), "{}", rows[0].0);
        assert!(rows[1].0.contains("Anna"), "{}", rows[1].0);
    }

    #[test]
    fn winner_line_sums_teams() {
        let pairs = |scores: &[u32; MAX_PLAYERS], seats| {
            winner_line(
                &GameSettings::default(),
                &crate::app::SeatNames::default(),
                scores,
                seats,
                TeamMode::Pairs,
            )
            .0
        };
        assert_eq!(pairs(&[5, 5, 4, 5, 0, 0], 4), "P1+P2 win!");
        assert_eq!(pairs(&[2, 2, 2, 2, 0, 0], 4), EN.dead_heat);
        // Three pairs on six seats: the winner is named by its members.
        assert_eq!(pairs(&[1, 1, 2, 2, 9, 9], 6), "P5+P6 win!");
        let (text, _) = winner_line(
            &GameSettings::default(),
            &crate::app::SeatNames::default(),
            &[9, 1, 9, 1, 9, 1],
            6,
            TeamMode::Trios,
        );
        assert_eq!(text, "P1+P3+P5 win!");
    }
}