Skip to content

Commit

Permalink
fix(server): prevent ControlTimeoutRequest from throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
xeruf committed Dec 14, 2020
1 parent 2c0f229 commit 4b6acaa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.thoughtworks.xstream.annotations.XStreamAlias
import com.thoughtworks.xstream.annotations.XStreamAsAttribute

/** Used to change whether a player in a slot can time out. */
// TODO this shouldn't need changing after a game was started
// instead, a game should be preparable without reservations
@XStreamAlias("timeout")
data class ControlTimeoutRequest(
@XStreamAsAttribute
Expand Down
4 changes: 3 additions & 1 deletion server/src/sc/server/Lobby.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ open class Lobby: GameRoomManager(), IClientListener, Closeable {
}
is ControlTimeoutRequest -> {
val room = this.findRoom(packet.roomId)
room.ensureOpenSlots(packet.slot + 1)
val slot = room.slots[packet.slot]
slot.role.player.canTimeout = packet.activate
slot.descriptor = slot.descriptor.copy(canTimeout = packet.activate)
slot.role?.player?.canTimeout = packet.activate
}
is ObservationRequest -> {
val room = this.findRoom(packet.roomId)
Expand Down
31 changes: 17 additions & 14 deletions server/src/sc/server/gaming/GameRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,23 @@ public List<PlayerSlot> getSlots() {
return Collections.unmodifiableList(this.playerSlots);
}

public void ensureOpenSlots(int count) throws TooManyPlayersException {
if (count > getMaximumPlayerCount()) {
throw new TooManyPlayersException();
}
while (playerSlots.size() < count) {
this.playerSlots.add(new PlayerSlot(this));
}
}

/** Set descriptors of PlayerSlots. */
public void openSlots(SlotDescriptor[] descriptors) throws TooManyPlayersException {
ensureOpenSlots(descriptors.length);
for (int i = 0; i < descriptors.length; i++) {
this.playerSlots.get(i).setDescriptor(descriptors[i]);
}
}

/**
* Threadsafe method to reserve all PlayerSlots.
*
Expand Down Expand Up @@ -538,20 +555,6 @@ public void onPaused(Player nextPlayer) {
observerBroadcast(new RoomPacket(getId(), new GamePausedEvent(nextPlayer)));
}

/** Set descriptors of PlayerSlots. */
public void openSlots(SlotDescriptor[] descriptors)
throws TooManyPlayersException {
if (descriptors.length > getMaximumPlayerCount()) {
throw new TooManyPlayersException();
}
this.playerSlots.add(new PlayerSlot(this));
this.playerSlots.add(new PlayerSlot(this));

for (int i = 0; i < descriptors.length; i++) {
this.playerSlots.get(i).setDescriptor(descriptors[i]);
}
}

/** Return true if GameStatus is OVER. */
public boolean isOver() {
return getStatus() == GameStatus.OVER;
Expand Down

0 comments on commit 4b6acaa

Please sign in to comment.