Skip to content

Commit

Permalink
Merge pull request #20 from Gamify-IT/feature/refactor
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
LeviUniAcc authored Feb 1, 2023
2 parents e2c5b01 + 15e096f commit ef0e2d9
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,7 @@ public void joinTeam(
log.info("player '{}' joined team '{}' in lobby '{}'", user.getName(), team, lobby);
final UUID playerUUID = UUID.fromString(user.getName());
final Player player = lobbyManagerService.getPlayerFromLobby(lobby, playerUUID);
if (team.equals("teamA")) {
lobbyManagerService.switchPlayerToTeamA(lobby, player);
} else if (team.equals("teamB")) {
lobbyManagerService.switchPlayerToTeamB(lobby, player);
} else {
log.error("Team '{}' does not exist", team);
}
lobbyManagerService.switchPlayerToTeam(lobby, player, team);
broadcastLobbyUpdate(lobby);
}

Expand Down Expand Up @@ -115,9 +109,8 @@ public void evaluateAnswers(
@DestinationVariable final String lobby,
@DestinationVariable final String team
) throws JsonProcessingException {
log.info("lobby '{}' started", lobby);
gameService.evaluateAnswers(lobby, team);
broadcastLobbyUpdate(lobby);
broadcastGameUpdate(lobby);
}

@MessageMapping("/init/Game/{lobby}/configurationId/{configurationId}")
Expand All @@ -136,8 +129,14 @@ public void nextQuestion(
@DestinationVariable final String team,
final Principal user
) throws JsonProcessingException {
gameService.nextQuestion(lobby, team);
broadcastGameUpdate(lobby);
if (gameService.hasNextQuestion(lobby, team)) {
gameService.nextQuestion(lobby, team);
broadcastGameUpdate(lobby);
} else {
gameService.setWinner(lobby);
broadcastGameUpdate(lobby);
gameService.deleteGame(lobby);
}
}

/**
Expand All @@ -162,6 +161,9 @@ private void broadcastLobbyUpdate(final String lobby) throws JsonProcessingExcep
* @throws JsonProcessingException
*/
private void broadcastGameUpdate(final String lobby) throws JsonProcessingException {
if (gameService.getGameForLobby(lobby) == null) {
return;
}
final UpdateGameMessage updateGameMessage = new UpdateGameMessage(gameService.getGameForLobby(lobby));
final MessageWrapper updateLobbyMassageWrapped = websocketService.wrapMessage(
updateGameMessage,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package de.unistuttgart.towercrushbackend.data.websockets;

import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class DeveloperMessage implements Message {

List<Lobby> lobbyList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -20,44 +23,53 @@ public class Game {
@JsonIgnore
private UUID id;
private String lobbyName;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Player> teamA;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Player> teamB;

@ElementCollection
private Map<String, Team> teams;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Round> rounds;
private UUID configurationId;
private int currentQuestionTeamA;
private int currentQuestionTeamB;

@ElementCollection
private Map<String, Integer> currentQuestion;
private long initialTowerSize;
private int teamATowerSize;
private int teamBTowerSize;
private int teamAAnswerPoints;
private int teamBAnswerPoints;

@ElementCollection
private Map<String, Integer> towerSize;

@ElementCollection
private Map<String, Integer> answerPoints;
private String winnerTeam;
@JsonIgnore
private LocalDateTime startedGame;

@ElementCollection
private Map<String, Integer> correctAnswerCount;

public Game(final String lobbyName, final Set<Player> teamA, final Set<Player> teamB, final List<Round> rounds, final UUID configurationId, final long initialTowerSize) {
private static String TEAM_A_NAME = "teamA";
private static String TEAM_B_NAME = "teamB";

public Game(final String lobbyName, final Team teamA, final Team teamB, final List<Round> rounds, final UUID configurationId, final long initialTowerSize) {
this.lobbyName = lobbyName;
this.teamA = teamA;
this.teamB = teamB;
this.teams = new HashMap<>();
this.teams.put(TEAM_A_NAME, teamA);
this.teams.put(TEAM_B_NAME, teamB);
this.rounds = rounds;
this.configurationId = configurationId;
this.currentQuestionTeamA = 0;
this.currentQuestionTeamB = 0;
this.currentQuestion = new HashMap<>();
this.currentQuestion.put(TEAM_A_NAME, 0);
this.currentQuestion.put(TEAM_B_NAME, 0);
this.initialTowerSize = initialTowerSize;
this.teamATowerSize = (int) initialTowerSize;
this.teamBTowerSize = (int) initialTowerSize;
this.teamAAnswerPoints = 0;
this.teamBAnswerPoints = 0;
this.towerSize = new HashMap<>();
this.towerSize.put(TEAM_A_NAME, (int) initialTowerSize);
this.towerSize.put(TEAM_B_NAME, (int) initialTowerSize);
this.answerPoints = new HashMap<>();
this.answerPoints.put(TEAM_A_NAME, 0);
this.answerPoints.put(TEAM_B_NAME, 0);
this.winnerTeam = "";
this.startedGame = LocalDateTime.now();
this.correctAnswerCount = new HashMap<>();
this.correctAnswerCount.put("teamA", 0);
this.correctAnswerCount.put("teamB", 0);
this.correctAnswerCount.put(TEAM_A_NAME, 0);
this.correctAnswerCount.put(TEAM_B_NAME, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.ElementCollection;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Lobby {

private Set<Player> teamA = new HashSet<>();
private Set<Player> teamB = new HashSet<>();
@ElementCollection
private Map<String, Team> teams;
private Set<Player> players = new HashSet<>();

private Set<Player> readyPlayers = new HashSet<>();
Expand All @@ -27,13 +23,23 @@ public class Lobby {

private String lobbyName;

private static String TEAM_A_NAME = "teamA";

private static String TEAM_B_NAME = "teamB";

public Lobby() {
this.teams = new HashMap<>();
this.teams.put(TEAM_A_NAME, new Team());
this.teams.put(TEAM_B_NAME, new Team());
}

public void addPlayer(final Player player) {
this.players.add(player);
}

public void removePlayer(final UUID playerUUID) {
final Player player = findPlayer(playerUUID);
removePlayerTeams(player);
removePlayerFromTeams(player);
players.remove(player);
}

Expand All @@ -49,32 +55,18 @@ public Set<String> getPlayerNames() {
return players.stream().map(Player::getPlayerName).collect(Collectors.toSet());
}

public void removePlayerTeams(final Player player) {
this.teamA.remove(player);
this.teamB.remove(player);
}

public void addPlayerToTeamA(final Player player) {
this.removePlayerTeams(player);
this.teamA.add(player);
public void removePlayerFromTeams(final Player player) {
this.teams.forEach((teamName, team) -> team.getPlayers().remove(player));
}

public void addPlayerToTeamB(final Player player) {
this.removePlayerTeams(player);
this.teamB.add(player);
public void addPlayertoTeam(final Player player, final String team) {
this.removePlayerFromTeams(player);
this.teams.get(team).getPlayers().add(player);
}

public Player findPlayer(final UUID playerUUID) {
final Optional<Player> returnPlayer =
this.players.stream().filter(player -> player.getKey().equals(playerUUID)).findFirst();
return returnPlayer.orElse(null);
}

public boolean isPlayerInTeamA(final Player player) {
return this.teamA.contains(player);
}

public boolean isPlayerInTeamB(final Player player) {
return this.teamB.contains(player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -23,20 +25,22 @@ public class Round {
@JoinColumn(name = "question_id")
private Question question;

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Vote> teamAVotes;
private static String TEAM_A_NAME = "teamA";

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Vote> teamBVotes;
private static String TEAM_B_NAME = "teamB";

@ElementCollection
private Map<String, TeamVotes> teamVotes;
@ElementCollection
private Map<String, Boolean> teamReadyForNextQuestion;

public Round(final Question questionParam) {
this.question = questionParam;
this.teamAVotes = new ArrayList<>();
this.teamBVotes = new ArrayList<>();
this.teamVotes = new HashMap<>();
this.teamVotes.put(TEAM_A_NAME, new TeamVotes());
this.teamVotes.put(TEAM_B_NAME, new TeamVotes());
this.teamReadyForNextQuestion = new HashMap<>();
this.teamReadyForNextQuestion.put("teamA", false);
this.teamReadyForNextQuestion.put("teamB", false);
this.teamReadyForNextQuestion.put(TEAM_A_NAME, false);
this.teamReadyForNextQuestion.put(TEAM_B_NAME, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.unistuttgart.towercrushbackend.data.websockets;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

@AllArgsConstructor
@Data
@Embeddable
@Entity
public class Team {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonIgnore
private UUID id;

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Player> players;

public Team() {
this.players = new HashSet<>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.unistuttgart.towercrushbackend.data.websockets;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@AllArgsConstructor
@Data
@Embeddable
@Entity
public class TeamVotes {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonIgnore
private UUID id;

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Vote> votes;

public TeamVotes() {
this.votes = new ArrayList<>();
}
}
Loading

0 comments on commit ef0e2d9

Please sign in to comment.