Feat/ms 4 integration #282
@@ -1,6 +1,7 @@
|
||||
package ch.unibas.dmi.dbis.cs108.casono.client.network;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.RequestParameter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -129,4 +130,80 @@ public class LobbyClient {
|
||||
}
|
||||
return new LoginResult(assigned, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Request the server for the list of available lobbies.
|
||||
*
|
||||
* @return list of LobbyInfo objects representing current lobbies
|
||||
*/
|
||||
public List<LobbyInfo> getLobbyList() {
|
||||
List<String> lines = client.processCommand("GET_LOBBY_LIST");
|
||||
|
||||
List<RequestParameter> params = ClientService.convertToRequestParameters(lines);
|
||||
|
||||
List<LobbyInfo> result = new ArrayList<>();
|
||||
|
||||
Integer currentId = null;
|
||||
String currentName = null;
|
||||
Integer currentPlayerCount = null;
|
||||
|
||||
for (RequestParameter p : params) {
|
||||
String key = p.key().toUpperCase();
|
||||
String val = p.value();
|
||||
switch (key) {
|
||||
case "ID":
|
||||
// If we were collecting a lobby, flush it
|
||||
if (currentId != null) {
|
||||
result.add(
|
||||
new LobbyInfo(
|
||||
currentId,
|
||||
currentName,
|
||||
currentPlayerCount == null ? 0 : currentPlayerCount));
|
||||
currentName = null;
|
||||
currentPlayerCount = null;
|
||||
}
|
||||
try {
|
||||
currentId = Integer.parseInt(val);
|
||||
} catch (NumberFormatException e) {
|
||||
currentId = null;
|
||||
}
|
||||
break;
|
||||
case "NAME":
|
||||
currentName = val;
|
||||
break;
|
||||
case "PLAYER_COUNT":
|
||||
try {
|
||||
currentPlayerCount = Integer.parseInt(val);
|
||||
} catch (NumberFormatException e) {
|
||||
currentPlayerCount = 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentId != null) {
|
||||
result.add(
|
||||
new LobbyInfo(
|
||||
currentId,
|
||||
currentName,
|
||||
currentPlayerCount == null ? 0 : currentPlayerCount));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Simple data holder for lobby metadata returned by the server. */
|
||||
public static final class LobbyInfo {
|
||||
public final int id;
|
||||
public final String name;
|
||||
public final int playerCount;
|
||||
|
||||
public LobbyInfo(int id, String name, int playerCount) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.playerCount = playerCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+18
@@ -75,6 +75,24 @@ public class CasinomainuiController {
|
||||
// LobbyClient will use the provided ClientService; in offline mode calls will
|
||||
// fail with RuntimeException
|
||||
lobbyClient = new LobbyClient(clientService);
|
||||
// Fetch existing lobbies from server on startup so newly-created lobbies
|
||||
// by other clients are immediately visible.
|
||||
try {
|
||||
if (!lobbyClient.getClientService().isOffline()) {
|
||||
var lobbies = lobbyClient.getLobbyList();
|
||||
int bid = nextButtonId;
|
||||
for (var li : lobbies) {
|
||||
try {
|
||||
translationManager.addLobbyButton(bid++, li.id);
|
||||
} catch (Exception e) {
|
||||
LOGGER.warn("Could not add lobby button: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
nextButtonId = bid;
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
LOGGER.warn("Failed to fetch lobby list at startup: {}", e.getMessage());
|
||||
}
|
||||
casinoTable.getChildren().clear();
|
||||
casinoTable.getChildren().add(gridManager.getGridPane());
|
||||
gridManager.renderLobbyButtons();
|
||||
|
||||
Reference in New Issue
Block a user