1
1
package com .faforever .client .replay ;
2
2
3
3
import com .faforever .client .config .ClientProperties ;
4
+ import com .faforever .client .domain .AbstractEntityBean ;
4
5
import com .faforever .client .domain .FeaturedModBean ;
5
6
import com .faforever .client .domain .GamePlayerStatsBean ;
7
+ import com .faforever .client .domain .GameResult ;
6
8
import com .faforever .client .domain .LeagueScoreJournalBean ;
7
9
import com .faforever .client .domain .MapBean ;
8
10
import com .faforever .client .domain .MapVersionBean ;
45
47
import com .faforever .commons .api .dto .Validity ;
46
48
import com .google .common .annotations .VisibleForTesting ;
47
49
import com .google .common .eventbus .EventBus ;
50
+ import javafx .application .Platform ;
48
51
import javafx .beans .binding .Bindings ;
49
52
import javafx .beans .binding .BooleanExpression ;
50
53
import javafx .beans .binding .StringExpression ;
85
88
import java .util .List ;
86
89
import java .util .Map ;
87
90
import java .util .Objects ;
91
+ import java .util .Set ;
88
92
import java .util .concurrent .CompletableFuture ;
89
93
import java .util .function .Function ;
90
94
import java .util .stream .Collectors ;
@@ -305,7 +309,13 @@ private void onReplayChanged(ReplayBean newValue) {
305
309
enrichReplayLater (newValue .getReplayFile (), newValue );
306
310
}
307
311
308
- leaderboardService .getLeagueScoreJournalForReplay (newValue ).thenAccept (newValue ::setLeagueScores );
312
+ leaderboardService .getLeagueScoreJournalForReplay (newValue )
313
+ .thenAccept (scores -> Platform .runLater (() -> {
314
+ newValue .setLeagueScores (scores );
315
+ // This looks extractResultFromJournal bit ugly. Ideally we should wait with drawing the window until we have the league scores,
316
+ // then we don't need to trigger extractResultFromJournal redraw here
317
+ populateTeamsContainer (teams .getValue ());
318
+ }));
309
319
310
320
reviewsController .setCanWriteReview (true );
311
321
@@ -445,6 +455,7 @@ private List<TeamCardController> createTeamCardControllers(Map<String, List<Game
445
455
446
456
TeamCardController controller = uiService .loadFxml ("theme/team_card.fxml" );
447
457
458
+ controller .setTeamResult (calculateGameResult (statsByPlayer .keySet ()));
448
459
controller .setRatingPrecision (RatingPrecision .EXACT );
449
460
controller .setRatingProvider (player -> getPlayerRating (player , statsByPlayer ));
450
461
controller .setDivisionProvider (this ::getPlayerDivision );
@@ -455,6 +466,45 @@ private List<TeamCardController> createTeamCardControllers(Map<String, List<Game
455
466
return controller ;
456
467
}).toList ();
457
468
}
469
+
470
+ private GameResult calculateGameResult (Set <PlayerBean > playerBeans ) {
471
+ int change ;
472
+ if (replay .get ().getLeagueScores () != null ) {
473
+ change = replay
474
+ .map (ReplayBean ::getLeagueScores )
475
+ .map (leagueScoreJournalBeans -> leagueScoreJournalBeans .stream ()
476
+ .filter (leagueScoreJournalBean -> playerBeans .stream ()
477
+ .map (AbstractEntityBean ::getId )
478
+ .toList ()
479
+ .contains (leagueScoreJournalBean .getLoginId ()))
480
+ .map (this ::extractResultFromJournal )
481
+ .reduce (0 , Integer ::sum )
482
+ ).getValue ();
483
+ } else {
484
+ // calculate from league player stats score changes
485
+ return GameResult .UNKNOWN ;
486
+ }
487
+
488
+ if (change > 0 ) {
489
+ return GameResult .VICTORY ;
490
+ } else if (change < 0 ) {
491
+ return GameResult .DEFEAT ;
492
+ } else {
493
+ return GameResult .DRAW ;
494
+ }
495
+ }
496
+
497
+ private int extractResultFromJournal (LeagueScoreJournalBean journalEntry ) {
498
+ int result ;
499
+ if (journalEntry .getDivisionAfter () == journalEntry .getDivisionBefore ()) {
500
+ result = Integer .compare (journalEntry .getScoreAfter (), journalEntry .getScoreBefore ());
501
+ } else if (journalEntry .getDivisionAfter ().getDivision () == journalEntry .getDivisionBefore ().getDivision ()) {
502
+ result = Integer .compare (journalEntry .getDivisionAfter ().getIndex (), journalEntry .getDivisionBefore ().getIndex ());
503
+ } else {
504
+ result = Integer .compare (journalEntry .getDivisionAfter ().getDivision ().getIndex (), journalEntry .getDivisionBefore ().getDivision ().getIndex ());
505
+ }
506
+ return result ;
507
+ }
458
508
459
509
private Faction getPlayerFaction (PlayerBean player , Map <PlayerBean , GamePlayerStatsBean > statsByPlayerId ) {
460
510
GamePlayerStatsBean playerStats = statsByPlayerId .get (player );
@@ -463,21 +513,24 @@ private Faction getPlayerFaction(PlayerBean player, Map<PlayerBean, GamePlayerSt
463
513
464
514
private Integer getPlayerRating (PlayerBean player , Map <PlayerBean , GamePlayerStatsBean > statsByPlayerId ) {
465
515
GamePlayerStatsBean playerStats = statsByPlayerId .get (player );
466
- return playerStats == null ? null : playerStats .getLeaderboardRatingJournals ()
467
- .stream ()
468
- .findFirst ()
469
- .filter (ratingJournal -> ratingJournal .getMeanBefore () != null )
470
- .filter (ratingJournal -> ratingJournal .getDeviationBefore () != null )
471
- .map (RatingUtil ::getRating )
472
- .orElse (null );
516
+ if (replay .get ().getLeagueScores () != null || playerStats == null ) {
517
+ return null ;
518
+ }
519
+ return playerStats .getLeaderboardRatingJournals ()
520
+ .stream ()
521
+ .findFirst ()
522
+ .filter (ratingJournal -> ratingJournal .getMeanBefore () != null )
523
+ .filter (ratingJournal -> ratingJournal .getDeviationBefore () != null )
524
+ .map (RatingUtil ::getRating )
525
+ .orElse (null );
473
526
}
474
-
527
+
475
528
private SubdivisionBean getPlayerDivision (PlayerBean player ) {
476
529
return replay .map (ReplayBean ::getLeagueScores ).map (leagueScoreJournalBeans -> leagueScoreJournalBeans
477
530
.stream ()
478
531
.filter (leagueScoreJournalBean -> leagueScoreJournalBean .getLoginId () == player .getId ())
479
532
.findFirst ()
480
- .map (LeagueScoreJournalBean ::getDivisionAfter )
533
+ .map (LeagueScoreJournalBean ::getDivisionBefore )
481
534
.orElse (null )
482
535
).getValue ();
483
536
}
@@ -531,9 +584,11 @@ public void copyLink() {
531
584
}
532
585
533
586
public void showRatingChange () {
534
- Map <String , List <GamePlayerStatsBean >> teamsValue = teams .get ();
535
-
536
- teamCardControllers .forEach (teamCardController -> teamCardController .setStats (teamsValue .get (String .valueOf (teamCardController .getTeamId ()))));
587
+ teamCardControllers .forEach (TeamCardController ::showGameResult );
588
+ if (replay .get ().getLeagueScores ().isEmpty ()) {
589
+ Map <String , List <GamePlayerStatsBean >> teamsValue = teams .get ();
590
+ teamCardControllers .forEach (teamCardController -> teamCardController .setStats (teamsValue .get (String .valueOf (teamCardController .getTeamId ()))));
591
+ }
537
592
}
538
593
539
594
public void onMapPreviewImageClicked () {
0 commit comments