Skip to content

Commit

Permalink
port more settings
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Oct 23, 2024
1 parent fb19f99 commit 8cc055f
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 339 deletions.
3 changes: 0 additions & 3 deletions client/src/main/java/online/util/ResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,5 @@ private static void handleStatisticsResponse(Element root, EntropyLobby lobby)
{
OnlineStatsPanel statsPanel = lobby.getOnlineStatsPanel();
statsPanel.updateVariablesFromResponse(root);

//Also update our local stats so we can unlock achievements
AchievementsUtil.updateOnlineStats(root);
}
}
24 changes: 8 additions & 16 deletions client/src/main/java/screen/ClearDataDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,14 @@ else if (!result.isEmpty())

private void removeStatisticsVariablesFromNode()
{
try
{
ScreenCache.getMainScreen().resetStartTime();
achievementStore.delete(AchievementSetting.TimePlayed);
achievements.remove(STATISTICS_INT_BEST_STREAK);
achievements.remove(STATISTICS_INT_CURRENT_STREAK);
achievements.remove(STATISTICS_INT_ENTROPY_GAMES_PLAYED);
achievements.remove(STATISTICS_INT_VECTROPY_GAMES_PLAYED);
achievements.remove(STATISTICS_INT_ENTROPY_GAMES_WON);
achievements.remove(STATISTICS_INT_VECTROPY_GAMES_WON);
achievements.remove(STATISTICS_INT_WORST_STREAK);
}
catch (Throwable t)
{
Debug.stackTrace(t);
}
ScreenCache.getMainScreen().resetStartTime();
achievementStore.delete(AchievementSetting.TimePlayed);
achievementStore.delete(AchievementSetting.BestStreak);
achievementStore.delete(AchievementSetting.CurrentStreak);
achievementStore.delete(AchievementSetting.EntropyGamesPlayed);
achievementStore.delete(AchievementSetting.VectropyGamesPlayed);
achievementStore.delete(AchievementSetting.EntropyGamesWon);
achievementStore.delete(AchievementSetting.VectropyGamesWon);
}

private void resetPreferences()
Expand Down
26 changes: 12 additions & 14 deletions client/src/main/java/screen/StatisticsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ private void setEntropyStatisticsText()
NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMinimumFractionDigits(1);

double eGamesPlayed = achievements.getInt(STATISTICS_INT_ENTROPY_GAMES_PLAYED, 0);
double eGamesWon = achievements.getInt(STATISTICS_INT_ENTROPY_GAMES_WON, 0);
double eGamesPlayed = achievementStore.get(AchievementSetting.EntropyGamesPlayed);
double eGamesWon = achievementStore.get(AchievementSetting.EntropyGamesWon);

String eWinRate = "N/A";
if (eGamesPlayed > 0)
Expand Down Expand Up @@ -190,9 +190,9 @@ private void setVectropyStatisticsText()
{
NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMinimumFractionDigits(1);
double vGamesPlayed = achievements.getInt(STATISTICS_INT_VECTROPY_GAMES_PLAYED, 0);
double vGamesWon = achievements.getInt(STATISTICS_INT_VECTROPY_GAMES_WON, 0);

double vGamesPlayed = achievementStore.get(AchievementSetting.VectropyGamesPlayed);
double vGamesWon = achievementStore.get(AchievementSetting.VectropyGamesWon);

String vWinRate = "N/A";
if (vGamesPlayed > 0)
Expand All @@ -218,11 +218,11 @@ private void setOverallStatisticsText()
{
NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMinimumFractionDigits(1);
double eGamesPlayed = achievements.getInt(STATISTICS_INT_ENTROPY_GAMES_PLAYED, 0);
double eGamesWon = achievements.getInt(STATISTICS_INT_ENTROPY_GAMES_WON, 0);
double vGamesPlayed = achievements.getInt(STATISTICS_INT_VECTROPY_GAMES_PLAYED, 0);
double vGamesWon = achievements.getInt(STATISTICS_INT_VECTROPY_GAMES_WON, 0);

double eGamesPlayed = achievementStore.get(AchievementSetting.EntropyGamesPlayed);
double eGamesWon = achievementStore.get(AchievementSetting.EntropyGamesWon);
double vGamesPlayed = achievementStore.get(AchievementSetting.VectropyGamesPlayed);
double vGamesWon = achievementStore.get(AchievementSetting.VectropyGamesWon);

double overallGamesPlayed = eGamesPlayed + vGamesPlayed;
double overallGamesWon = eGamesWon + vGamesWon;
Expand Down Expand Up @@ -253,11 +253,9 @@ private void setStreaksText()

sb.append("Streaks:");
sb.append("\n\nLongest Winning Streak: ");
sb.append(achievements.getInt(STATISTICS_INT_BEST_STREAK, 0));
sb.append("\nLongest Losing Streak: ");
sb.append(achievements.getInt(STATISTICS_INT_WORST_STREAK, 0));
sb.append(achievementStore.get(AchievementSetting.BestStreak));
sb.append("\nCurrent Streak: ");
sb.append(achievements.getInt(STATISTICS_INT_CURRENT_STREAK, 0));
sb.append(achievementStore.get(AchievementSetting.CurrentStreak));

String statsString = sb.toString();
streaksField.setText(statsString);
Expand Down
98 changes: 18 additions & 80 deletions client/src/main/java/util/AchievementsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,20 @@ public class AchievementsUtil implements Registry

public static void updateStreaksForLoss()
{
int currentStreak = achievements.getInt(STATISTICS_INT_CURRENT_STREAK, 0);
int worstStreak = achievements.getInt(STATISTICS_INT_WORST_STREAK, 0);

if (currentStreak > 0)
{
currentStreak = -1;
achievements.putInt(STATISTICS_INT_CURRENT_STREAK, currentStreak);
}
else
{
currentStreak--;
achievements.putInt(STATISTICS_INT_CURRENT_STREAK, currentStreak);
}

if (currentStreak < -worstStreak)
{
achievements.putInt(STATISTICS_INT_WORST_STREAK, -currentStreak);
}
achievementStore.save(AchievementSetting.CurrentStreak, 0);
}

public static void recordWin(GameMode gameMode)
{
if (gameMode == GameMode.Entropy)
{
int newGamesWon = achievements.getInt(STATISTICS_INT_ENTROPY_GAMES_WON, 0) + 1;
achievements.putInt(STATISTICS_INT_ENTROPY_GAMES_WON, newGamesWon);
int newGamesWon = achievementStore.get(AchievementSetting.EntropyGamesWon) + 1;
achievementStore.save(AchievementSetting.EntropyGamesWon, newGamesWon);
}
else if (gameMode == GameMode.Vectropy)
{
int newGamesWon = achievements.getInt(STATISTICS_INT_VECTROPY_GAMES_WON, 0) + 1;
achievements.putInt(STATISTICS_INT_VECTROPY_GAMES_WON, newGamesWon);
int newGamesWon = achievementStore.get(AchievementSetting.VectropyGamesWon) + 1;
achievementStore.save(AchievementSetting.VectropyGamesWon, newGamesWon);
}

updateStreaksForWin();
Expand All @@ -79,27 +62,19 @@ else if (gameMode == GameMode.Vectropy)

private static void updateStreaksForWin()
{
int currentStreak = achievements.getInt(STATISTICS_INT_CURRENT_STREAK, 0);
int bestStreak = achievements.getInt(STATISTICS_INT_BEST_STREAK, 0);
if (currentStreak < 0)
{
achievements.putInt(STATISTICS_INT_CURRENT_STREAK, 1);
}
else
{
currentStreak++;
achievements.putInt(STATISTICS_INT_CURRENT_STREAK, currentStreak);
}
int currentStreak = achievementStore.get(AchievementSetting.CurrentStreak) + 1;
achievementStore.save(AchievementSetting.CurrentStreak, currentStreak);

int bestStreak = achievementStore.get(AchievementSetting.BestStreak);
if (currentStreak > bestStreak)
{
achievements.putInt(STATISTICS_INT_BEST_STREAK, currentStreak);
achievementStore.save(AchievementSetting.BestStreak, currentStreak);
}
}

private static void unlockWinningStreakAchievements()
{
int currentStreak = achievements.getInt(STATISTICS_INT_CURRENT_STREAK, 0);
int currentStreak = achievementStore.get(AchievementSetting.CurrentStreak);

if (currentStreak >= 3)
{
Expand All @@ -119,8 +94,7 @@ private static void unlockWinningStreakAchievements()

private static void unlockEntropyWinAchievements()
{
int gamesWon = achievements.getInt(STATISTICS_INT_ENTROPY_GAMES_WON, 0)
+ achievements.getInt(STATISTICS_INT_ENTROPY_ONLINE_GAMES_WON, 0);
int gamesWon = achievementStore.get(AchievementSetting.EntropyGamesWon);

if (gamesWon > 0)
{
Expand All @@ -145,8 +119,7 @@ private static void unlockEntropyWinAchievements()

private static void unlockVectropyWinAchievements()
{
int gamesWon = achievements.getInt(STATISTICS_INT_VECTROPY_GAMES_WON, 0)
+ achievements.getInt(STATISTICS_INT_VECTROPY_ONLINE_GAMES_WON, 0);
int gamesWon = achievementStore.get(AchievementSetting.VectropyGamesWon);

if (gamesWon > 0)
{
Expand Down Expand Up @@ -291,17 +264,15 @@ else if (totalPlayers == 4)

public static void recordGamePlayed(GameMode gameMode)
{
int gamesPlayed = 1;

if (gameMode == GameMode.Entropy)
{
gamesPlayed += achievements.getInt(STATISTICS_INT_ENTROPY_GAMES_PLAYED, 0);
achievements.putInt(STATISTICS_INT_ENTROPY_GAMES_PLAYED, gamesPlayed);
int newGamesPlayed = achievementStore.get(AchievementSetting.EntropyGamesPlayed) + 1;
achievementStore.save(AchievementSetting.EntropyGamesPlayed, newGamesPlayed);
}
else if (gameMode == GameMode.Vectropy)
{
gamesPlayed += achievements.getInt(STATISTICS_INT_VECTROPY_GAMES_PLAYED, 0);
achievements.putInt(STATISTICS_INT_VECTROPY_GAMES_PLAYED, gamesPlayed);
int newGamesPlayed = achievementStore.get(AchievementSetting.VectropyGamesPlayed) + 1;
achievementStore.save(AchievementSetting.VectropyGamesPlayed, newGamesPlayed);
}

unlockGamesPlayedAchievements();
Expand Down Expand Up @@ -335,41 +306,8 @@ private static void unlockGamesPlayedAchievements()

private static int getTotalGamesPlayed()
{
return achievements.getInt(STATISTICS_INT_ENTROPY_GAMES_PLAYED, 0)
+ achievements.getInt(STATISTICS_INT_VECTROPY_GAMES_PLAYED, 0)
+ achievements.getInt(STATISTICS_INT_ENTROPY_ONLINE_GAMES_PLAYED, 0)
+ achievements.getInt(STATISTICS_INT_VECTROPY_ONLINE_GAMES_PLAYED, 0);
}

public static void updateOnlineStats(Element rootElement)
{
int entropyWins = getTotalWins("Entropy", rootElement);
int vectropyWins = getTotalWins("Vectropy", rootElement);
int entropyPlayed = entropyWins + getTotalLosses("Entropy", rootElement);
int vectropyPlayed = vectropyWins + getTotalLosses("Vectropy", rootElement);

achievements.putInt(STATISTICS_INT_ENTROPY_ONLINE_GAMES_WON, entropyWins);
achievements.putInt(STATISTICS_INT_ENTROPY_ONLINE_GAMES_PLAYED, entropyPlayed);
achievements.putInt(STATISTICS_INT_VECTROPY_ONLINE_GAMES_WON, vectropyWins);
achievements.putInt(STATISTICS_INT_VECTROPY_ONLINE_GAMES_PLAYED, vectropyPlayed);

unlockGamesPlayedAchievements();
unlockEntropyWinAchievements();
unlockVectropyWinAchievements();
}
private static int getTotalWins(String mode, Element rootElement)
{
return getTotal(mode, rootElement, "Won");
}
private static int getTotalLosses(String mode, Element rootElement)
{
return getTotal(mode, rootElement, "Lost");
}
private static int getTotal(String mode, Element rootElement, String wonOrLost)
{
return XmlUtil.getAttributeInt(rootElement, mode + "2" + wonOrLost)
+ XmlUtil.getAttributeInt(rootElement, mode + "3" + wonOrLost)
+ XmlUtil.getAttributeInt(rootElement, mode + "4" + wonOrLost);
return achievementStore.get(AchievementSetting.EntropyGamesPlayed)
+ achievementStore.get(AchievementSetting.VectropyGamesPlayed);
}

public static void unlockSecondThoughts(String roomId)
Expand Down
6 changes: 6 additions & 0 deletions client/src/main/kotlin/achievement/AchievementSetting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ object AchievementSetting {
val ChattyCount = Setting("chattyCount", 0)
@JvmField val TimePlayed = Setting("timePlayed", 0L)
@JvmField val WillUnlockCoward = Setting("willUnlockCoward", false)
@JvmField val BestStreak = Setting("bestStreak", 0)
@JvmField val CurrentStreak = Setting("currentStreak", 0)
@JvmField val EntropyGamesPlayed = Setting("entropyGamesPlayed", 0)
@JvmField val VectropyGamesPlayed = Setting("vectropyGamesPlayed", 0)
@JvmField val EntropyGamesWon = Setting("entropyGamesWon", 0)
@JvmField val VectropyGamesWon = Setting("vectropyGamesWon", 0)
}
1 change: 1 addition & 0 deletions client/src/main/kotlin/preference/AbstractSettingStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ abstract class AbstractSettingStore {
when (val desiredType = setting.default::class) {
Boolean::class -> raw.toBoolean() as T
Double::class -> raw.toDouble() as T
Long::class -> raw.toLong() as T
Int::class -> raw.toInt() as T
String::class -> raw as T
else ->
Expand Down
13 changes: 0 additions & 13 deletions core/src/main/java/util/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,6 @@ public interface Registry extends CoreRegistry
public static final String REWARDS_BOOLEAN_EXTRA_SUITS = "extraSuits";
public static final String REWARDS_BOOLEAN_CHEATS = "cheats";

//statistics
public static final String STATISTICS_INT_ENTROPY_GAMES_PLAYED = "gamesPlayed";
public static final String STATISTICS_INT_ENTROPY_ONLINE_GAMES_PLAYED = "onlineGamesPlayed";
public static final String STATISTICS_INT_VECTROPY_GAMES_PLAYED = "vGamesPlayed";
public static final String STATISTICS_INT_VECTROPY_ONLINE_GAMES_PLAYED = "vOnlineGamesPlayed";
public static final String STATISTICS_INT_ENTROPY_GAMES_WON = "gamesWon";
public static final String STATISTICS_INT_ENTROPY_ONLINE_GAMES_WON = "onlineGamesWon";
public static final String STATISTICS_INT_VECTROPY_GAMES_WON = "vGamesWon";
public static final String STATISTICS_INT_VECTROPY_ONLINE_GAMES_WON = "vOnlineGamesWon";
public static final String STATISTICS_INT_BEST_STREAK = "bestStreak";
public static final String STATISTICS_INT_WORST_STREAK = "worstStreak";
public static final String STATISTICS_INT_CURRENT_STREAK = "currentStreak";

//savedGame
public static final String SAVED_GAME_STRING_RESULT_TEXT = "resultText";
public static final String SAVED_GAME_STRING_TOTAL_CARDS_LABEL = "totalCardsLabel";
Expand Down
48 changes: 0 additions & 48 deletions server/src/main/java/object/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@ public void removePlayer(String username, boolean fireLobbyChanged)
BidHistory history = currentGame.getCurrentBidHistory();
history.addBidForPlayer(playerNumber, bid);

int roundNumber = currentGame.getRoundNumber();
if (roundNumber > 1)
{
StatisticsUtil.recordGamePlayed(username, capacity, settings.getMode());
}

//Moved this into here as otherwise we set it to 0 incorrectly and a person ends up with no cards!
HandDetails details = currentGame.getCurrentRoundDetails();
ConcurrentHashMap<Integer, Integer> hmHandSizeByPlayerNumber = details.getHandSizes();
Expand Down Expand Up @@ -174,7 +168,6 @@ private void finishCurrentGame(int winningPlayer)
{
int roundNumber = currentGame.getRoundNumber();
String winningUsername = hmPlayerByPlayerNumber.get(winningPlayer);
saveStatistics(winningUsername, roundNumber);
resetCurrentPlayers();
currentGame.setWinningPlayer(winningPlayer);

Expand Down Expand Up @@ -400,47 +393,6 @@ private int getWinningPlayer(ConcurrentHashMap<Integer, Integer> hmHandSizeByPla
return potentialWinner;
}
}

private void saveStatistics(String winningPlayer, int roundNumber)
{
//don't save any statistics if the game didn't go beyond the first round
if (roundNumber == 1)
{
return;
}

StatisticsUtil.recordWin(winningPlayer, capacity, settings.getMode());

int size = currentPlayers.size();
for (int i=0; i<size; i++)
{
String player = currentPlayers.get(i);
if (hmPlayerByPlayerNumber.containsValue(player))
{
//They didn't leave, so record a game played
StatisticsUtil.recordGamePlayed(player, capacity, settings.getMode());

//Push out a stats notification
Document statsNotification = XmlBuilderServer.factoryStatisticsNotification(player);
String notificationString = XmlUtil.getStringFromDocument(statsNotification);
UserConnection usc = ServerGlobals.INSTANCE.getUscStore().findForName(player);
usc.sendNotificationInWorkerPool(notificationString, server, XmlConstants.SOCKET_NAME_LOBBY, null);
}
}
}

public long getCurrentGameDuration()
{
long gameStart = currentGame.getGameStartMillis();
if (gameStart == -1
|| currentGame.getRoundNumber() == 1)
{
return 0;
}

long currentTime = System.currentTimeMillis();
return (currentTime - gameStart);
}

/**
* Returns a HashSet since it's possible for a player to be present as a player AND an observer.
Expand Down
Loading

0 comments on commit 8cc055f

Please sign in to comment.