mirror of
https://github.com/Quackster/Havana.git
synced 2025-07-03 13:17:46 +00:00
Make game scores on BattleBall and SnowStorm thread-safe
This commit is contained in:
parent
cf5d1b61c7
commit
e790ba6f25
9 changed files with 56 additions and 32 deletions
|
@ -260,9 +260,11 @@ public abstract class Game {
|
||||||
var gameHistoryData = new GameHistoryData();
|
var gameHistoryData = new GameHistoryData();
|
||||||
var gameHistory = new GameHistory(gameHistoryData);
|
var gameHistory = new GameHistory(gameHistoryData);
|
||||||
|
|
||||||
|
this.teams.values().forEach(GameTeam::calculateScore);
|
||||||
|
|
||||||
if (this.teams.size() > 0) {
|
if (this.teams.size() > 0) {
|
||||||
var sortedTeamList = new ArrayList<>(this.teams.values());
|
List<GameTeam> sortedTeamList = new ArrayList<>(this.teams.values());
|
||||||
sortedTeamList.sort(Comparator.comparingInt(GameTeam::getScore).reversed());
|
sortedTeamList.sort(Comparator.comparingInt(GameTeam::getPoints).reversed());
|
||||||
|
|
||||||
var winningTeam = sortedTeamList.get(0);
|
var winningTeam = sortedTeamList.get(0);
|
||||||
|
|
||||||
|
@ -270,13 +272,14 @@ public abstract class Game {
|
||||||
|
|
||||||
for (GameTeam team : sortedTeamList) {
|
for (GameTeam team : sortedTeamList) {
|
||||||
for (GamePlayer gamePlayer : team.getPlayers()) {
|
for (GamePlayer gamePlayer : team.getPlayers()) {
|
||||||
|
gamePlayer.calculateScore();
|
||||||
gameHistoryData.addPlayer(gamePlayer.getUserId(), gamePlayer.getScore(), gamePlayer.getTeamId());
|
gameHistoryData.addPlayer(gamePlayer.getUserId(), gamePlayer.getScore(), gamePlayer.getTeamId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gameHistory.setName(this.getName());
|
gameHistory.setName(this.getName());
|
||||||
gameHistory.setWinningTeam(winningTeam.getId());
|
gameHistory.setWinningTeam(winningTeam.getId());
|
||||||
gameHistory.setWinningTeamScore(winningTeam.getScore());
|
gameHistory.setWinningTeamScore(winningTeam.getPoints());
|
||||||
gameHistory.setGameType(this.gameType);
|
gameHistory.setGameType(this.gameType);
|
||||||
gameHistory.setMapId(this.getMapId());
|
gameHistory.setMapId(this.getMapId());
|
||||||
|
|
||||||
|
@ -769,9 +772,9 @@ public abstract class Game {
|
||||||
* Method called when the game initially began
|
* Method called when the game initially began
|
||||||
*/
|
*/
|
||||||
public void gamePrepare() {
|
public void gamePrepare() {
|
||||||
for (GameTeam gameTeam : this.getTeams().values()) {
|
// for (GameTeam gameTeam : this.getTeams().values()) {
|
||||||
gameTeam.setScore(0);
|
// gameTeam.setScore(0);
|
||||||
}
|
// }
|
||||||
|
|
||||||
for (GamePlayer gamePlayer : this.getActivePlayers()) {
|
for (GamePlayer gamePlayer : this.getActivePlayers()) {
|
||||||
gamePlayer.setXp(0);
|
gamePlayer.setXp(0);
|
||||||
|
|
|
@ -10,6 +10,7 @@ import org.alexdev.havana.game.games.battleball.objects.PlayerUpdateObject;
|
||||||
import org.alexdev.havana.game.games.battleball.objects.PowerUpUpdateObject;
|
import org.alexdev.havana.game.games.battleball.objects.PowerUpUpdateObject;
|
||||||
import org.alexdev.havana.game.games.enums.GameState;
|
import org.alexdev.havana.game.games.enums.GameState;
|
||||||
import org.alexdev.havana.game.games.player.GamePlayer;
|
import org.alexdev.havana.game.games.player.GamePlayer;
|
||||||
|
import org.alexdev.havana.game.games.player.GameTeam;
|
||||||
import org.alexdev.havana.game.pathfinder.Position;
|
import org.alexdev.havana.game.pathfinder.Position;
|
||||||
import org.alexdev.havana.game.pathfinder.Rotation;
|
import org.alexdev.havana.game.pathfinder.Rotation;
|
||||||
import org.alexdev.havana.game.player.Player;
|
import org.alexdev.havana.game.player.Player;
|
||||||
|
@ -50,6 +51,7 @@ public class BattleBallTask implements Runnable {
|
||||||
this.game.getObjectsQueue().drainTo(objects);
|
this.game.getObjectsQueue().drainTo(objects);
|
||||||
this.game.getUpdateTilesQueue().drainTo(updateTiles);
|
this.game.getUpdateTilesQueue().drainTo(updateTiles);
|
||||||
this.game.getFillTilesQueue().drainTo(fillTiles);
|
this.game.getFillTilesQueue().drainTo(fillTiles);
|
||||||
|
this.game.getTeams().values().forEach(GameTeam::calculateScore);
|
||||||
|
|
||||||
for (GamePlayer gamePlayer : this.game.getActivePlayers()) {
|
for (GamePlayer gamePlayer : this.game.getActivePlayers()) {
|
||||||
Player player = gamePlayer.getPlayer();
|
Player player = gamePlayer.getPlayer();
|
||||||
|
|
|
@ -11,6 +11,8 @@ import org.alexdev.havana.game.pathfinder.Position;
|
||||||
import org.alexdev.havana.game.player.Player;
|
import org.alexdev.havana.game.player.Player;
|
||||||
import org.alexdev.havana.game.games.snowstorm.util.SnowStormAttributes;
|
import org.alexdev.havana.game.games.snowstorm.util.SnowStormAttributes;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class GamePlayer {
|
public class GamePlayer {
|
||||||
private Player player;
|
private Player player;
|
||||||
private GameObject gameObject;
|
private GameObject gameObject;
|
||||||
|
@ -27,7 +29,7 @@ public class GamePlayer {
|
||||||
private GamePlayer harlequinPlayer;
|
private GamePlayer harlequinPlayer;
|
||||||
|
|
||||||
private boolean assignedSpawn;
|
private boolean assignedSpawn;
|
||||||
private int score;
|
private AtomicInteger score;
|
||||||
private int xp;
|
private int xp;
|
||||||
|
|
||||||
private SnowStormAttributes snowStormAttributes;
|
private SnowStormAttributes snowStormAttributes;
|
||||||
|
@ -42,7 +44,7 @@ public class GamePlayer {
|
||||||
this.enteringGame = false;
|
this.enteringGame = false;
|
||||||
this.clickedRestart = false;
|
this.clickedRestart = false;
|
||||||
this.position = new Position();
|
this.position = new Position();
|
||||||
this.score = 0;
|
this.score = new AtomicInteger(0);
|
||||||
this.xp = 0;
|
this.xp = 0;
|
||||||
this.snowStormAttributes = new SnowStormAttributes();
|
this.snowStormAttributes = new SnowStormAttributes();
|
||||||
}
|
}
|
||||||
|
@ -53,7 +55,7 @@ public class GamePlayer {
|
||||||
* @param score the score
|
* @param score the score
|
||||||
*/
|
*/
|
||||||
public void setScore(int score) {
|
public void setScore(int score) {
|
||||||
this.score = score;
|
this.score.set(score);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,13 +63,15 @@ public class GamePlayer {
|
||||||
*
|
*
|
||||||
* @return the score
|
* @return the score
|
||||||
*/
|
*/
|
||||||
public int getScore() {
|
public void calculateScore() {
|
||||||
if (!this.inGame) {
|
if (!this.inGame) {
|
||||||
return 0;
|
this.score.set(0);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.getGame() instanceof BattleBallGame) {
|
if (this.getGame() instanceof BattleBallGame) {
|
||||||
this.score = 0;
|
this.score.set(0);
|
||||||
|
|
||||||
BattleBallGame battleBallGame = (BattleBallGame) this.getGame();
|
BattleBallGame battleBallGame = (BattleBallGame) this.getGame();
|
||||||
|
|
||||||
for (BattleBallTile battleBallTile : battleBallGame.getTiles()) {
|
for (BattleBallTile battleBallTile : battleBallGame.getTiles()) {
|
||||||
|
@ -76,12 +80,14 @@ public class GamePlayer {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.score += scoreReference.getScore();
|
this.score.addAndGet(scoreReference.getScore());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return this.score;
|
public int getScore() {
|
||||||
|
return score.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -7,19 +7,22 @@ import org.alexdev.havana.game.games.utils.ScoreReference;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class GameTeam {
|
public class GameTeam {
|
||||||
private int id;
|
private int id;
|
||||||
private Game game;
|
private Game game;
|
||||||
private List<GamePlayer> playerList;
|
private List<GamePlayer> playerList;
|
||||||
private int score;
|
|
||||||
|
public AtomicInteger points;
|
||||||
|
public AtomicInteger snowstormPoints;
|
||||||
|
|
||||||
public GameTeam(int id, Game game) {
|
public GameTeam(int id, Game game) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.game = game;
|
this.game = game;
|
||||||
this.playerList = new CopyOnWriteArrayList<>();
|
this.playerList = new CopyOnWriteArrayList<>();
|
||||||
this.score = 0;
|
this.points = new AtomicInteger(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
|
@ -30,10 +33,10 @@ public class GameTeam {
|
||||||
return playerList;
|
return playerList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getScore() {
|
public void calculateScore() {
|
||||||
if (this.game instanceof BattleBallGame) {
|
this.points.set(0);
|
||||||
this.score = 0;
|
|
||||||
|
|
||||||
|
if (this.game instanceof BattleBallGame) {
|
||||||
BattleBallGame battleBallGame = (BattleBallGame) this.game;
|
BattleBallGame battleBallGame = (BattleBallGame) this.game;
|
||||||
|
|
||||||
for (BattleBallTile battleBallTile : battleBallGame.getTiles()) {
|
for (BattleBallTile battleBallTile : battleBallGame.getTiles()) {
|
||||||
|
@ -42,18 +45,27 @@ public class GameTeam {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.score += scoreReference.getScore();
|
this.points.addAndGet(scoreReference.getScore());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
this.points.set(0);
|
||||||
|
|
||||||
return this.score;
|
int totalScore = this.playerList.stream()
|
||||||
|
.mapToInt(player -> player.getScore())
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
this.points.addAndGet(totalScore);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setScore(int score) {
|
public int getPoints() {
|
||||||
this.score = score;
|
return points.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public List<GamePlayer> getActivePlayers() {
|
public List<GamePlayer> getActivePlayers() {
|
||||||
return playerList.stream().filter(GamePlayer::isInGame).collect(Collectors.toList());
|
return playerList.stream().filter(GamePlayer::isInGame).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,7 +116,8 @@ public class SnowStormGame extends Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (GameTeam team : this.getTeams().values()) {
|
for (GameTeam team : this.getTeams().values()) {
|
||||||
team.setScore(team.getPlayers().stream().mapToInt(GamePlayer::getScore).sum());
|
team.calculateScore();
|
||||||
|
// team.setScore(team.getPlayers().stream().mapToInt(GamePlayer::getScore).sum());
|
||||||
}
|
}
|
||||||
|
|
||||||
super.finishGame();
|
super.finishGame();
|
||||||
|
|
|
@ -24,12 +24,12 @@ import java.util.UUID;
|
||||||
|
|
||||||
public class GameFinishTask implements Runnable {
|
public class GameFinishTask implements Runnable {
|
||||||
private final List<GamePlayer> players;
|
private final List<GamePlayer> players;
|
||||||
private final ArrayList<GameTeam> sortedTeamList;
|
private final List<GameTeam> sortedTeamList;
|
||||||
private final GameType gameType;
|
private final GameType gameType;
|
||||||
private final Game game;
|
private final Game game;
|
||||||
private final GameHistory gameHistory;
|
private final GameHistory gameHistory;
|
||||||
|
|
||||||
public GameFinishTask(Game game, GameHistory gameHistory, GameType gameType, ArrayList<GameTeam> sortedTeamList, List<GamePlayer> players) {
|
public GameFinishTask(Game game, GameHistory gameHistory, GameType gameType, List<GameTeam> sortedTeamList, List<GamePlayer> players) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
this.gameHistory = gameHistory;
|
this.gameHistory = gameHistory;
|
||||||
this.gameType = gameType;
|
this.gameType = gameType;
|
||||||
|
@ -58,8 +58,8 @@ public class GameFinishTask implements Runnable {
|
||||||
|
|
||||||
if (this.game.canIncreasePoints()) {
|
if (this.game.canIncreasePoints()) {
|
||||||
if ((this.sortedTeamList.size() == 1 && this.sortedTeamList.get(0).getPlayers().size() > 1) || (this.sortedTeamList.size() > 1
|
if ((this.sortedTeamList.size() == 1 && this.sortedTeamList.get(0).getPlayers().size() > 1) || (this.sortedTeamList.size() > 1
|
||||||
&& this.sortedTeamList.get(0).getScore() > 0
|
&& this.sortedTeamList.get(0).getPoints() > 0
|
||||||
&& this.sortedTeamList.get(0).getScore() != this.sortedTeamList.get(1).getScore()) &&
|
&& this.sortedTeamList.get(0).getPoints() != this.sortedTeamList.get(1).getPoints()) &&
|
||||||
this.sortedTeamList.get(0).getPlayers().size() > 0 &&
|
this.sortedTeamList.get(0).getPlayers().size() > 0 &&
|
||||||
this.sortedTeamList.get(1).getPlayers().size() > 0) {
|
this.sortedTeamList.get(1).getPlayers().size() > 0) {
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@ public class FinishedGame {
|
||||||
private List<Pair<String, Integer>> playerScores;
|
private List<Pair<String, Integer>> playerScores;
|
||||||
|
|
||||||
private FinishedGameTeam(GameTeam gameTeam) {
|
private FinishedGameTeam(GameTeam gameTeam) {
|
||||||
this.score = gameTeam.getScore();
|
this.score = gameTeam.getPoints();
|
||||||
this.playerScores = new ArrayList<>();
|
this.playerScores = new ArrayList<>();
|
||||||
|
|
||||||
for (var gamePlayer : gameTeam.getPlayers()) {
|
for (var gamePlayer : gameTeam.getPlayers()) {
|
||||||
|
|
|
@ -34,7 +34,7 @@ public class GAMEEND extends MessageComposer {
|
||||||
response.writeInt(gamePlayer.getScore());
|
response.writeInt(gamePlayer.getScore());
|
||||||
}
|
}
|
||||||
|
|
||||||
response.writeInt(team.getScore());
|
response.writeInt(team.getPoints());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,7 +83,7 @@ public class GAMESTATUS extends MessageComposer {
|
||||||
response.writeInt(this.gameTeams.size());
|
response.writeInt(this.gameTeams.size());
|
||||||
|
|
||||||
for (GameTeam team : this.gameTeams) {
|
for (GameTeam team : this.gameTeams) {
|
||||||
response.writeInt(team.getScore());
|
response.writeInt(team.getPoints());
|
||||||
}
|
}
|
||||||
|
|
||||||
response.writeInt(1);
|
response.writeInt(1);
|
||||||
|
|
Loading…
Add table
Reference in a new issue