Skip to content

Commit 1e19c2b

Browse files
committed
style(server): improve readability of slot & reservation code
1 parent e00aa58 commit 1e19c2b

File tree

2 files changed

+24
-37
lines changed

2 files changed

+24
-37
lines changed

server/src/sc/server/gaming/PlayerSlot.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,33 @@ public boolean isReserved() {
3737
}
3838

3939
public synchronized String reserve() {
40-
if (isReserved()) {
40+
if (isReserved())
4141
throw new IllegalStateException("Slot already reserved.");
42-
} else if (!isEmpty()) {
42+
if (!isEmpty())
4343
throw new IllegalStateException("This slot is already occupied.");
44-
} else {
45-
this.reserved = true;
46-
return ReservationManager.reserve(this);
47-
}
44+
45+
this.reserved = true;
46+
return ReservationManager.reserve(this);
4847
}
4948

5049
public void setClient(IClient client) {
51-
if (!isEmpty()) {
50+
if (!isEmpty())
5251
throw new IllegalStateException("This slot is already occupied.");
53-
}
5452

5553
this.role = new PlayerRole(client, this);
5654
client.addRole(this.role);
5755
}
5856

5957
public void setPlayer(Player player) {
60-
if (this.role == null) {
61-
throw new IllegalStateException(
62-
"Slot isn't linked to a Client yet.");
63-
}
58+
if (this.role == null)
59+
throw new IllegalStateException("Slot isn't linked to a Client yet.");
6460

6561
this.role.setPlayer(player);
6662
}
6763

6864
public synchronized void free() {
69-
if (!this.reserved) {
65+
if (!this.reserved)
7066
throw new IllegalStateException("This slot isn't reserved.");
71-
}
7267

7368
this.reserved = false;
7469
}
@@ -78,9 +73,8 @@ public void setDisplayName(String displayName) {
7873
}
7974

8075
public String getDisplayName() {
81-
if (this.displayName == null) {
76+
if (this.displayName == null)
8277
return "Unknown";
83-
}
8478

8579
return this.displayName;
8680
}
@@ -90,14 +84,12 @@ public SlotDescriptor getDescriptor() {
9084
}
9185

9286
public void setDescriptor(SlotDescriptor descriptor) {
93-
if (descriptor == null) this.descriptor = new SlotDescriptor();
94-
else this.descriptor = descriptor;
87+
this.descriptor = descriptor != null ? descriptor : new SlotDescriptor();
9588
}
9689

9790
public IClient getClient() {
98-
if (this.role == null) {
91+
if (this.role == null)
9992
return null;
100-
}
10193

10294
return this.role.getClient();
10395
}

server/src/sc/server/gaming/ReservationManager.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public final class ReservationManager {
1414
private static Logger logger = LoggerFactory.getLogger(ReservationManager.class);
1515
private static Map<String, PlayerSlot> reservations = new HashMap<String, PlayerSlot>();
1616

17-
/** create Object as singleton */
17+
/** Create Object as singleton. */
1818
private ReservationManager() {
1919
// singleton
2020
}
@@ -32,14 +32,12 @@ private ReservationManager() {
3232
public static synchronized PlayerSlot redeemReservationCode(Client client, String reservation)
3333
throws RescuableClientException {
3434
PlayerSlot result = reservations.remove(reservation);
35-
36-
if (result == null) {
35+
if (result == null)
3736
throw new UnknownReservationException();
38-
} else {
39-
logger.info("Reservation {} was redeemed.", reservation);
40-
result.getRoom().fillSlot(result, client);
41-
return result;
42-
}
37+
38+
logger.info("Reservation {} was redeemed.", reservation);
39+
result.getRoom().fillSlot(result, client);
40+
return result;
4341
}
4442

4543
/**
@@ -52,32 +50,29 @@ public static synchronized PlayerSlot redeemReservationCode(Client client, Strin
5250
* @throws RuntimeException if the slot is already reserved
5351
*/
5452
public synchronized static String reserve(PlayerSlot playerSlot) {
55-
if (reservations.containsValue(playerSlot)) {
53+
if (reservations.containsValue(playerSlot))
5654
throw new RuntimeException("This slot is already reserved.");
57-
}
5855

5956
String key = generateUniqueId();
6057
reservations.put(key, playerSlot);
6158
return key;
6259
}
6360

6461
/**
65-
* Generate a uniqe String ID
62+
* Generate a unique String ID.
6663
*
6764
* @return the unique ID
6865
*/
6966
private synchronized static String generateUniqueId() {
70-
String key = UUID.randomUUID().toString();
71-
72-
while (reservations.containsKey(key)) {
67+
String key;
68+
do {
7369
key = UUID.randomUUID().toString();
74-
}
75-
70+
} while (reservations.containsKey(key));
7671
return key;
7772
}
7873

7974
/**
80-
* Remove reservation with given redeem code and free that slot
75+
* Remove reservation with given redeem code and free that slot.
8176
*
8277
* @param reservation the redeem code
8378
*/

0 commit comments

Comments
 (0)