Skip to content

Commit 4b6acaa

Browse files
committed
fix(server): prevent ControlTimeoutRequest from throwing
1 parent 2c0f229 commit 4b6acaa

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

sdk/src/server-api/sc/protocol/requests/ControlTimeoutRequest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import com.thoughtworks.xstream.annotations.XStreamAlias
44
import com.thoughtworks.xstream.annotations.XStreamAsAttribute
55

66
/** Used to change whether a player in a slot can time out. */
7+
// TODO this shouldn't need changing after a game was started
8+
// instead, a game should be preparable without reservations
79
@XStreamAlias("timeout")
810
data class ControlTimeoutRequest(
911
@XStreamAsAttribute

server/src/sc/server/Lobby.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ open class Lobby: GameRoomManager(), IClientListener, Closeable {
8484
}
8585
is ControlTimeoutRequest -> {
8686
val room = this.findRoom(packet.roomId)
87+
room.ensureOpenSlots(packet.slot + 1)
8788
val slot = room.slots[packet.slot]
88-
slot.role.player.canTimeout = packet.activate
89+
slot.descriptor = slot.descriptor.copy(canTimeout = packet.activate)
90+
slot.role?.player?.canTimeout = packet.activate
8991
}
9092
is ObservationRequest -> {
9193
val room = this.findRoom(packet.roomId)

server/src/sc/server/gaming/GameRoom.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,23 @@ public List<PlayerSlot> getSlots() {
384384
return Collections.unmodifiableList(this.playerSlots);
385385
}
386386

387+
public void ensureOpenSlots(int count) throws TooManyPlayersException {
388+
if (count > getMaximumPlayerCount()) {
389+
throw new TooManyPlayersException();
390+
}
391+
while (playerSlots.size() < count) {
392+
this.playerSlots.add(new PlayerSlot(this));
393+
}
394+
}
395+
396+
/** Set descriptors of PlayerSlots. */
397+
public void openSlots(SlotDescriptor[] descriptors) throws TooManyPlayersException {
398+
ensureOpenSlots(descriptors.length);
399+
for (int i = 0; i < descriptors.length; i++) {
400+
this.playerSlots.get(i).setDescriptor(descriptors[i]);
401+
}
402+
}
403+
387404
/**
388405
* Threadsafe method to reserve all PlayerSlots.
389406
*
@@ -538,20 +555,6 @@ public void onPaused(Player nextPlayer) {
538555
observerBroadcast(new RoomPacket(getId(), new GamePausedEvent(nextPlayer)));
539556
}
540557

541-
/** Set descriptors of PlayerSlots. */
542-
public void openSlots(SlotDescriptor[] descriptors)
543-
throws TooManyPlayersException {
544-
if (descriptors.length > getMaximumPlayerCount()) {
545-
throw new TooManyPlayersException();
546-
}
547-
this.playerSlots.add(new PlayerSlot(this));
548-
this.playerSlots.add(new PlayerSlot(this));
549-
550-
for (int i = 0; i < descriptors.length; i++) {
551-
this.playerSlots.get(i).setDescriptor(descriptors[i]);
552-
}
553-
}
554-
555558
/** Return true if GameStatus is OVER. */
556559
public boolean isOver() {
557560
return getStatus() == GameStatus.OVER;

0 commit comments

Comments
 (0)