Skip to content

Commit

Permalink
server: fixed npe error on load tests (miss buffer timer), code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
JayDi85 committed Oct 11, 2023
1 parent 09dbdcc commit fe8b8e1
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import mage.constants.RangeOfInfluence;
import mage.constants.SkillLevel;
import mage.game.match.MatchOptions;
import mage.game.mulligan.MulliganType;
import mage.players.PlayerType;
import mage.view.GameTypeView;
import mage.view.TableView;
Expand Down Expand Up @@ -876,15 +875,15 @@ private void onLoadSettings(int version) {
}
int timeLimit = Integer.parseInt(PreferencesDialog.getCachedValue(PreferencesDialog.KEY_NEW_TABLE_TIME_LIMIT + versionStr, "1500"));
for (MatchTimeLimit mtl : MatchTimeLimit.values()) {
if (mtl.getTimeLimit() == timeLimit) {
if (mtl.getPrioritySecs() == timeLimit) {
this.cbTimeLimit.setSelectedItem(mtl);
break;
}
}
// TODO: Rethink defaults with buffer time?
int bufferTime = Integer.parseInt(PreferencesDialog.getCachedValue(PreferencesDialog.KEY_NEW_TABLE_BUFFER_TIME + versionStr, "0"));
for (MatchBufferTime mtl : MatchBufferTime.values()) {
if (mtl.getBufferTime() == bufferTime) {
if (mtl.getBufferSecs() == bufferTime) {
this.cbBufferTime.setSelectedItem(mtl);
break;
}
Expand Down Expand Up @@ -951,8 +950,8 @@ private void onSaveSettings(int version, MatchOptions options, String deckFile)
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TABLE_NAME + versionStr, options.getName());
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TABLE_PASSWORD + versionStr, options.getPassword());
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TABLE_DECK_TYPE + versionStr, options.getDeckType());
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TABLE_TIME_LIMIT + versionStr, Integer.toString(options.getPriorityTime()));
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TABLE_BUFFER_TIME + versionStr, Integer.toString(options.getBufferTime()));
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TABLE_TIME_LIMIT + versionStr, Integer.toString(options.getMatchTimeLimit().getPrioritySecs()));
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TABLE_BUFFER_TIME + versionStr, Integer.toString(options.getMatchBufferTime().getBufferSecs()));
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TABLE_GAME_TYPE + versionStr, options.getGameType());
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TABLE_NUMBER_OF_WINS + versionStr, Integer.toString(options.getWinsNeeded()));
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TABLE_ROLLBACK_TURNS_ALLOWED + versionStr, options.isRollbackTurnsAllowed() ? "Yes" : "No");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1345,15 +1345,15 @@ private void onLoadSettings(int version) {
txtPassword.setText(PreferencesDialog.getCachedValue(PreferencesDialog.KEY_NEW_TOURNAMENT_PASSWORD + versionStr, ""));
int timeLimit = Integer.parseInt(PreferencesDialog.getCachedValue(PreferencesDialog.KEY_NEW_TOURNAMENT_TIME_LIMIT + versionStr, "1500"));
for (MatchTimeLimit mtl : MatchTimeLimit.values()) {
if (mtl.getTimeLimit() == timeLimit) {
if (mtl.getPrioritySecs() == timeLimit) {
this.cbTimeLimit.setSelectedItem(mtl);
break;
}
}
// TODO: Rethink default match time with buffer time.
int bufferTime = Integer.parseInt(PreferencesDialog.getCachedValue(PreferencesDialog.KEY_NEW_TOURNAMENT_BUFFER_TIME + versionStr, "0"));
for (MatchBufferTime mtl : MatchBufferTime.values()) {
if (mtl.getBufferTime() == bufferTime) {
if (mtl.getBufferSecs() == bufferTime) {
this.cbBufferTime.setSelectedItem(mtl);
break;
}
Expand Down Expand Up @@ -1424,8 +1424,8 @@ private void onSaveSettings(int version, TournamentOptions tOptions) {
String versionStr = prepareVersionStr(version, true);
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TOURNAMENT_NAME + versionStr, tOptions.getName());
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TOURNAMENT_PASSWORD + versionStr, tOptions.getPassword());
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TOURNAMENT_TIME_LIMIT + versionStr, Integer.toString(tOptions.getMatchOptions().getPriorityTime()));
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TOURNAMENT_BUFFER_TIME + versionStr, Integer.toString(tOptions.getMatchOptions().getBufferTime()));
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TOURNAMENT_TIME_LIMIT + versionStr, Integer.toString(tOptions.getMatchOptions().getMatchTimeLimit().getPrioritySecs()));
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TOURNAMENT_BUFFER_TIME + versionStr, Integer.toString(tOptions.getMatchOptions().getMatchBufferTime().getBufferSecs()));
if (this.spnConstructTime.isVisible()) {
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TOURNAMENT_CONSTR_TIME + versionStr, Integer.toString(tOptions.getLimitedOptions().getConstructionTime()));
}
Expand Down
3 changes: 3 additions & 0 deletions Mage.Common/src/main/java/mage/utils/timer/PriorityTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import org.apache.log4j.Logger;

/**
* Priority timer for both server and client sides
* Client side version used for GUI-avatars redraw
*
* @author noxx
*/
public class PriorityTimer extends TimerTask {
Expand Down
10 changes: 5 additions & 5 deletions Mage/src/main/java/mage/constants/MatchBufferTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ public enum MatchBufferTime {
SEC__25(25, "25 Seconds"),
SEC__30(30, "30 Seconds");

private final int matchSeconds;
private final int bufferSecs;
private final String name;

MatchBufferTime(int matchSeconds, String name) {
this.matchSeconds = matchSeconds;
MatchBufferTime(int bufferSecs, String name) {
this.bufferSecs = bufferSecs;
this.name = name;
}

public int getBufferTime() {
return matchSeconds;
public int getBufferSecs() {
return bufferSecs;
}

public String getName() {
Expand Down
10 changes: 5 additions & 5 deletions Mage/src/main/java/mage/constants/MatchTimeLimit.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public enum MatchTimeLimit {
MIN__90(5400, "90 Minutes"),
MIN_120(7200, "120 Minutes");

private final int matchSeconds;
private final int prioritySecs;
private final String name;

MatchTimeLimit(int matchSeconds, String name) {
this.matchSeconds = matchSeconds;
MatchTimeLimit(int prioritySecs, String name) {
this.prioritySecs = prioritySecs;
this.name = name;
}

public int getTimeLimit() {
return matchSeconds;
public int getPrioritySecs() {
return prioritySecs;
}

public String getName() {
Expand Down
15 changes: 8 additions & 7 deletions Mage/src/main/java/mage/game/match/MatchImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,15 @@ protected void initGame(Game game) throws GameException {
game.loadCards(matchPlayer.getDeck().getCards(), matchPlayer.getPlayer().getId());
game.loadCards(matchPlayer.getDeck().getSideboard(), matchPlayer.getPlayer().getId());
game.addPlayer(matchPlayer.getPlayer(), matchPlayer.getDeck());
// set the priority time left for the match
if (games.isEmpty()) { // first game full time
matchPlayer.getPlayer().setPriorityTimeLeft(options.getPriorityTime());
matchPlayer.getPlayer().setBufferTimeLeft(options.getBufferTime());
// time limits
matchPlayer.getPlayer().setBufferTimeLeft(options.getMatchBufferTime().getBufferSecs());
if (games.isEmpty()) {
// first game
matchPlayer.getPlayer().setPriorityTimeLeft(options.getMatchTimeLimit().getPrioritySecs());
} else {
// 2+ games must keep times
if (matchPlayer.getPriorityTimeLeft() > 0) {
matchPlayer.getPlayer().setPriorityTimeLeft(matchPlayer.getPriorityTimeLeft());
matchPlayer.getPlayer().setBufferTimeLeft(options.getBufferTime());
}
}
} else {
Expand All @@ -214,8 +215,8 @@ protected void initGame(Game game) throws GameException {
}
}
}
game.setPriorityTime(options.getPriorityTime());
game.setBufferTime(options.getBufferTime());
game.setPriorityTime(options.getMatchTimeLimit().getPrioritySecs());
game.setBufferTime(options.getMatchBufferTime().getBufferSecs());
}

protected void shufflePlayers() {
Expand Down
36 changes: 8 additions & 28 deletions Mage/src/main/java/mage/game/match/MatchOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,11 @@ public class MatchOptions implements Serializable {
protected int minimumRating;
protected int edhPowerLevel;
protected boolean rated;
protected int numSeatsForMatch;
protected Set<String> bannedUsers = new HashSet<>();

/**
* Time each player has during the game to play using his\her priority.
*/
protected MatchTimeLimit matchTimeLimit; // 0 = no priorityTime handling
protected MatchBufferTime matchBufferTime; // Amount of time each player gets before their normal time limit counts down. Refreshes each time the normal timer is invoked.
protected MulliganType mulliganType;
protected MatchTimeLimit matchTimeLimit = MatchTimeLimit.NONE; // total time limit for priority
protected MatchBufferTime matchBufferTime = MatchBufferTime.NONE; // additional/buffer time limit for each priority before real time ticking starts
protected MulliganType mulliganType = MulliganType.GAME_DEFAULT;

protected Collection<DeckCardInfo> perPlayerEmblemCards;
protected Collection<DeckCardInfo> globalEmblemCards;
Expand Down Expand Up @@ -178,34 +174,20 @@ public void setLimited(boolean limited) {
this.limited = limited;
}

public int getPriorityTime() {
if (matchTimeLimit == null) {
return MatchTimeLimit.NONE.getTimeLimit();
}
return matchTimeLimit.getTimeLimit();
}

public MatchTimeLimit getMatchTimeLimit() {
return this.matchTimeLimit;
}

public void setMatchTimeLimit(MatchTimeLimit matchTimeLimit) {
this.matchTimeLimit = matchTimeLimit;
}

public int getBufferTime() {
if (matchBufferTime == null) {
return MatchBufferTime.NONE.getBufferTime();
}
return matchBufferTime.getBufferTime();
this.matchTimeLimit = Optional.ofNullable(matchTimeLimit).orElse(MatchTimeLimit.NONE);
}

public MatchBufferTime getMatchBufferTime() {
return this.matchBufferTime;
}

public void setMatchBufferTime(MatchBufferTime matchBufferTime) {
this.matchBufferTime = matchBufferTime;
this.matchBufferTime = Optional.ofNullable(matchBufferTime).orElse(MatchBufferTime.NONE);
}

public String getPassword() {
Expand Down Expand Up @@ -295,9 +277,10 @@ public ResultProtos.MatchOptionsProto toProto() {
.setRated(this.isRated())
.setWinsNeeded(this.getWinsNeeded());

ResultProtos.SkillLevel skillLevel = ResultProtos.SkillLevel.BEGINNER;
ResultProtos.SkillLevel skillLevel;
switch (this.getSkillLevel()) {
case BEGINNER:
default:
skillLevel = ResultProtos.SkillLevel.BEGINNER;
break;
case CASUAL:
Expand All @@ -313,13 +296,10 @@ public ResultProtos.MatchOptionsProto toProto() {
}

public void setMullgianType(MulliganType mulliganType) {
this.mulliganType = mulliganType;
this.mulliganType = Optional.ofNullable(mulliganType).orElse(MulliganType.GAME_DEFAULT);
}

public MulliganType getMulliganType() {
if (mulliganType == null) {
return MulliganType.GAME_DEFAULT;
}
return mulliganType;
}

Expand Down
2 changes: 1 addition & 1 deletion Mage/src/main/java/mage/game/match/MatchPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MatchPlayer implements Serializable {

private boolean quit;
private boolean doneSideboarding;
private int priorityTimeLeft;
private int priorityTimeLeft; // keep left time for next game

public MatchPlayer(Player player, Deck deck, Match match) {
this.player = player;
Expand Down
3 changes: 1 addition & 2 deletions Mage/src/main/java/mage/players/PlayerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4548,8 +4548,7 @@ public boolean lookAtFaceDownCard(Card card, Game game, int abilitiesToActivate)
}

@Override
public void setPriorityTimeLeft(int timeLeft
) {
public void setPriorityTimeLeft(int timeLeft) {
priorityTimeLeft = timeLeft;
}

Expand Down

0 comments on commit fe8b8e1

Please sign in to comment.