Skip to content

Commit

Permalink
Add GameLifecycleHook For Scoreboard Trophies
Browse files Browse the repository at this point in the history
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
ahahn94 committed Jan 27, 2022
1 parent 4258edd commit 005a679
Show file tree
Hide file tree
Showing 19 changed files with 346 additions and 48 deletions.
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class FightingFirstPlaceTrophy extends Trophy implements ScoreboardTrophy {
public class FightingFirstPlaceTrophy extends ScoreboardChangingTrophy implements ScoreboardTrophy {

private long id;
private String name = "Fighting - First Place";
private String badgeUrl = "https://raw.githubusercontent.com/wiki/The-Microservice-Dungeon/gamelog/assets/pictures/trophies/scoreboard/Fighting%20Score%20-%20First%20Place.png";

@Override
public void awardToQualifiedPlayer(Scoreboard scoreboard) {
Set<Player> players = scoreboard.getGameScores().keySet();
List<Player> players = scoreboard.getGameScores().keySet().stream().toList();
for (Player player : players) {
if (scoreboard.getFightingPlacementOfPlayer(player) == 1) {
player.awardTrophy(this, scoreboard.getGame().getId().id());
awardTrophyAndUpdateScoreboard(scoreboard, player);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class FightingSecondPlaceTrophy extends Trophy implements ScoreboardTrophy {
public class FightingSecondPlaceTrophy extends ScoreboardChangingTrophy implements ScoreboardTrophy {

private long id;
private String name = "Fighting - Second Place";
private String badgeUrl = "https://raw.githubusercontent.com/wiki/The-Microservice-Dungeon/gamelog/assets/pictures/trophies/scoreboard/Fighting%20Score%20-%20Second%20Place.png";

@Override
public void awardToQualifiedPlayer(Scoreboard scoreboard) {
Set<Player> players = scoreboard.getGameScores().keySet();
List<Player> players = scoreboard.getGameScores().keySet().stream().toList();
for (Player player : players) {
if (scoreboard.getFightingPlacementOfPlayer(player) == 2) {
player.awardTrophy(this, scoreboard.getGame().getId().id());
awardTrophyAndUpdateScoreboard(scoreboard, player);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class FightingThirdPlaceTrophy extends Trophy implements ScoreboardTrophy {
public class FightingThirdPlaceTrophy extends ScoreboardChangingTrophy implements ScoreboardTrophy {

private long id;
private String name = "Fighting - Third Place";
private String badgeUrl = "https://raw.githubusercontent.com/wiki/The-Microservice-Dungeon/gamelog/assets/pictures/trophies/scoreboard/Fighting%20Score%20-%20Third%20Place.png";

@Override
public void awardToQualifiedPlayer(Scoreboard scoreboard) {
Set<Player> players = scoreboard.getGameScores().keySet();
List<Player> players = scoreboard.getGameScores().keySet().stream().toList();
for (Player player : players) {
if (scoreboard.getFightingPlacementOfPlayer(player) == 3) {
player.awardTrophy(this, scoreboard.getGame().getId().id());
awardTrophyAndUpdateScoreboard(scoreboard, player);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class GameFirstPlaceTrophy extends Trophy implements ScoreboardTrophy {
public class GameFirstPlaceTrophy extends ScoreboardChangingTrophy implements ScoreboardTrophy {

private long id;
private String name = "Game - First Place";
private String badgeUrl = "https://raw.githubusercontent.com/wiki/The-Microservice-Dungeon/gamelog/assets/pictures/trophies/scoreboard/Game%20Score%20-%20First%20Place.png";

@Override
public void awardToQualifiedPlayer(Scoreboard scoreboard) {
Set<Player> players = scoreboard.getGameScores().keySet();
List<Player> players = scoreboard.getGameScores().keySet().stream().toList();
for (Player player : players) {
if (scoreboard.getTotalPlacementOfPlayer(player) == 1) {
player.awardTrophy(this, scoreboard.getGame().getId().id());
awardTrophyAndUpdateScoreboard(scoreboard, player);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class GameSecondPlaceTrophy extends Trophy implements ScoreboardTrophy {
public class GameSecondPlaceTrophy extends ScoreboardChangingTrophy implements ScoreboardTrophy {

private long id;
private String name = "Game - Second Place";
private String badgeUrl = "https://raw.githubusercontent.com/wiki/The-Microservice-Dungeon/gamelog/assets/pictures/trophies/scoreboard/Game%20Score%20-%20Second%20Place.png";

@Override
public void awardToQualifiedPlayer(Scoreboard scoreboard) {
Set<Player> players = scoreboard.getGameScores().keySet();
List<Player> players = scoreboard.getGameScores().keySet().stream().toList();
for (Player player : players) {
if (scoreboard.getTotalPlacementOfPlayer(player) == 2) {
player.awardTrophy(this, scoreboard.getGame().getId().id());
awardTrophyAndUpdateScoreboard(scoreboard, player);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
import com.github.tmd.gamelog.domain.Player;
import com.github.tmd.gamelog.domain.score.entity.Scoreboard;
import com.github.tmd.gamelog.domain.trophies.ScoreboardTrophy;
import com.github.tmd.gamelog.domain.trophies.Trophy;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class GameThirdPlaceTrophy extends Trophy implements ScoreboardTrophy {
public class GameThirdPlaceTrophy extends ScoreboardChangingTrophy implements ScoreboardTrophy {

private long id;
private String name = "Game - Third Place";
private String badgeUrl = "https://raw.githubusercontent.com/wiki/The-Microservice-Dungeon/gamelog/assets/pictures/trophies/scoreboard/Game%20Score%20-%20Third%20Place.png";

@Override
public void awardToQualifiedPlayer(Scoreboard scoreboard) {
Set<Player> players = scoreboard.getGameScores().keySet();
List<Player> players = scoreboard.getGameScores().keySet().stream().toList();
for (Player player : players) {
if (scoreboard.getTotalPlacementOfPlayer(player) == 3) {
player.awardTrophy(this, scoreboard.getGame().getId().id());
awardTrophyAndUpdateScoreboard(scoreboard, player);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MiningFirstPlaceTrophy extends Trophy implements ScoreboardTrophy {
public class MiningFirstPlaceTrophy extends ScoreboardChangingTrophy implements ScoreboardTrophy {

private long id;
private String name = "Mining - First Place";
private String badgeUrl = "https://raw.githubusercontent.com/wiki/The-Microservice-Dungeon/gamelog/assets/pictures/trophies/scoreboard/Mining%20Score%20-%20First%20Place.png";

@Override
public void awardToQualifiedPlayer(Scoreboard scoreboard) {
Set<Player> players = scoreboard.getGameScores().keySet();
List<Player> players = scoreboard.getGameScores().keySet().stream().toList();
for (Player player : players) {
if (scoreboard.getMiningPlacementOfPlayer(player) == 1) {
player.awardTrophy(this, scoreboard.getGame().getId().id());
awardTrophyAndUpdateScoreboard(scoreboard, player);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MiningSecondPlaceTrophy extends Trophy implements ScoreboardTrophy {
public class MiningSecondPlaceTrophy extends ScoreboardChangingTrophy implements ScoreboardTrophy {

private long id;
private String name = "Mining - Second Place";
private String badgeUrl = "https://raw.githubusercontent.com/wiki/The-Microservice-Dungeon/gamelog/assets/pictures/trophies/scoreboard/Mining%20Score%20-%20Second%20Place.png";

@Override
public void awardToQualifiedPlayer(Scoreboard scoreboard) {
Set<Player> players = scoreboard.getGameScores().keySet();
List<Player> players = scoreboard.getGameScores().keySet().stream().toList();
for (Player player : players) {
if (scoreboard.getMiningPlacementOfPlayer(player) == 2) {
player.awardTrophy(this, scoreboard.getGame().getId().id());
awardTrophyAndUpdateScoreboard(scoreboard, player);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MiningThirdPlaceTrophy extends Trophy implements ScoreboardTrophy {
public class MiningThirdPlaceTrophy extends ScoreboardChangingTrophy implements ScoreboardTrophy {

private long id;
private String name = "Mining - Third Place";
private String badgeUrl = "https://raw.githubusercontent.com/wiki/The-Microservice-Dungeon/gamelog/assets/pictures/trophies/scoreboard/Mining%20Score%20-%20Third%20Place.png";

@Override
public void awardToQualifiedPlayer(Scoreboard scoreboard) {
Set<Player> players = scoreboard.getGameScores().keySet();
List<Player> players = scoreboard.getGameScores().keySet().stream().toList();
for (Player player : players) {
if (scoreboard.getMiningPlacementOfPlayer(player) == 3) {
player.awardTrophy(this, scoreboard.getGame().getId().id());
awardTrophyAndUpdateScoreboard(scoreboard, player);
}
}
}
Expand Down
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TradingFirstPlaceTrophy extends Trophy implements ScoreboardTrophy {
public class TradingFirstPlaceTrophy extends ScoreboardChangingTrophy implements ScoreboardTrophy {

private long id;
private String name = "Trading - First Place";
private String badgeUrl = "https://raw.githubusercontent.com/wiki/The-Microservice-Dungeon/gamelog/assets/pictures/trophies/scoreboard/Trading%20Score%20-%20First%20Place.png";

@Override
public void awardToQualifiedPlayer(Scoreboard scoreboard) {
Set<Player> players = scoreboard.getGameScores().keySet();
List<Player> players = scoreboard.getGameScores().keySet().stream().toList();
for (Player player : players) {
if (scoreboard.getTradingPlacementOfPlayer(player) == 1) {
player.awardTrophy(this, scoreboard.getGame().getId().id());
awardTrophyAndUpdateScoreboard(scoreboard, player);
}
}
}
Expand Down
Loading

0 comments on commit 005a679

Please sign in to comment.