-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GameLifecycleHook For Scoreboard Trophies
Add a GameLifecycleHook for awarding the scoreboard trophies to the players: - runs when a game has ended - gets the scoreboard and checks if the players have qualified for trophies by their rank on the boards - awards earned trophies to the qualified players Signed-off-by: ahahn94 <[email protected]>
- Loading branch information
Showing
19 changed files
with
346 additions
and
48 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/main/java/com/github/tmd/gamelog/domain/trophies/ScoreboardTrophiesHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.github.tmd.gamelog.domain.trophies; | ||
|
||
import com.github.tmd.gamelog.adapter.event.gameEvent.game.GameStatus; | ||
import com.github.tmd.gamelog.adapter.event.gameEvent.game.GameStatusEvent; | ||
import com.github.tmd.gamelog.adapter.jpa.*; | ||
import com.github.tmd.gamelog.application.GameLifecycleHook; | ||
import com.github.tmd.gamelog.domain.Game; | ||
import com.github.tmd.gamelog.domain.Player; | ||
import com.github.tmd.gamelog.domain.score.entity.Scoreboard; | ||
import com.github.tmd.gamelog.domain.score.repository.ScoreboardRepository; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Game lifecycle hook that awards the ScoreboardTrophies to the Players. | ||
*/ | ||
@Component | ||
public class ScoreboardTrophiesHook implements GameLifecycleHook { | ||
private final ScoreboardRepository scoreboardJpaRepository; | ||
private final ScoreboardRepositoryImpl scoreboardRepository; | ||
|
||
private final PlayerJpaRepository playerJpaRepository; | ||
private final PlayerRepositoryImpl playerRepository; | ||
|
||
private final TrophyJpaRepository trophyJpaRepository; | ||
private final TrophyRepository trophyRepository; | ||
|
||
public ScoreboardTrophiesHook( | ||
ScoreboardRepository scoreboardJpaRepository, | ||
ScoreboardRepositoryImpl scoreboardRepository, | ||
PlayerJpaRepository playerJpaRepository, | ||
PlayerRepositoryImpl playerRepository, | ||
TrophyJpaRepository trophyJpaRepository, | ||
TrophyRepository trophyRepository) { | ||
this.scoreboardJpaRepository = scoreboardJpaRepository; | ||
this.scoreboardRepository = scoreboardRepository; | ||
this.playerJpaRepository = playerJpaRepository; | ||
this.playerRepository = playerRepository; | ||
this.trophyJpaRepository = trophyJpaRepository; | ||
this.trophyRepository = trophyRepository; | ||
} | ||
|
||
/** | ||
* Method that plugs into the game lifecycle. | ||
* Awards the Trophies to the Players after the game has ended. | ||
* | ||
* @param event Event that contains the game status change. | ||
* @param timestamp Timestamp of the status change. | ||
*/ | ||
@Override | ||
public void onGameStatus(GameStatusEvent event, Instant timestamp) { | ||
if (event.status() == GameStatus.ENDED) { | ||
awardScoreboardTrophies(event.gameId()); | ||
} | ||
} | ||
|
||
/** | ||
* Award the ScoreboardTrophies to the players that have qualified. | ||
* | ||
* @param gameId ID of the current game. | ||
*/ | ||
private void awardScoreboardTrophies(UUID gameId) { | ||
Scoreboard scoreboard = scoreboardRepository.getScoreboardByGameId(new Game.GameId(gameId)).get(); | ||
ArrayList<Trophy> trophies = trophyRepository.findAll(); | ||
|
||
for (Trophy trophy : trophies) { | ||
if (trophy instanceof ScoreboardTrophy) { | ||
((ScoreboardTrophy) trophy).awardToQualifiedPlayer(scoreboard); | ||
} | ||
} | ||
|
||
for (Player player : scoreboard.getGameScores().keySet()) { | ||
playerRepository.upsert(player); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...main/java/com/github/tmd/gamelog/domain/trophies/scoreboard/ScoreboardChangingTrophy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.github.tmd.gamelog.domain.trophies.scoreboard; | ||
|
||
import com.github.tmd.gamelog.domain.Player; | ||
import com.github.tmd.gamelog.domain.score.entity.Scoreboard; | ||
import com.github.tmd.gamelog.domain.score.vo.AggregatedScore; | ||
import com.github.tmd.gamelog.domain.trophies.Trophy; | ||
|
||
/** | ||
* Class that extends Trophy with a function to update a player on the Scoreboard. | ||
*/ | ||
public abstract class ScoreboardChangingTrophy extends Trophy { | ||
|
||
void awardTrophyAndUpdateScoreboard(Scoreboard scoreboard, Player player) { | ||
AggregatedScore score = scoreboard.getGameScores().get(player); | ||
scoreboard.getGameScores().remove(player); | ||
player.awardTrophy(this, scoreboard.getGame().getId().id()); | ||
scoreboard.getGameScores().put(player, score); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.