Skip to content

Commit

Permalink
feat: Server can make a random position, role, character to start game
Browse files Browse the repository at this point in the history
  • Loading branch information
LapisBerry committed May 29, 2024
1 parent f90c910 commit 3f1524d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package com.lapisberry.game.controllers;

import com.lapisberry.game.entities.characters.CharacterEnum;
import com.lapisberry.game.entities.players.Player;
import com.lapisberry.game.entities.players.Role;
import com.lapisberry.utils.Randomizer;
import com.lapisberry.utils.RoleCountHelper;
import javafx.util.Pair;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;

public class LobbyController implements Serializable {
@Serial
private static final long serialVersionUID = -3466527055512798470L;
private static final long serialVersionUID = -5453436562134546375L;
// Fields
private ArrayList<Pair<Integer, String>> players; // Pair<clientId, playerName>
private ArrayList<Player> shuffledPlayers;
private ArrayList<CharacterEnum> shuffledCharacters;

// Constructors
public LobbyController() {
Expand All @@ -26,6 +34,39 @@ public void removePlayer(int clientId) {
players.removeIf(pair -> pair.getKey() == clientId);
}

public void setupShuffledPlayersAndShuffledCharacters() {
shuffledPlayers = new ArrayList<>();
shuffledCharacters = new ArrayList<>(Arrays.asList(CharacterEnum.values()));
for (Pair<Integer, String> player : players) {
shuffledPlayers.add(new Player(player.getKey(), player.getValue()));
}
Randomizer.shuffleArrayList(shuffledPlayers);
assignRole(shuffledPlayers);
Randomizer.shuffleArrayList(shuffledPlayers);
Randomizer.shuffleArrayList(shuffledCharacters);
}

private void assignRole(ArrayList<Player> shuffledPlayers) {
int size = shuffledPlayers.size();
int emCount = RoleCountHelper.getEmperors(size);
int roCount = RoleCountHelper.getRoyalists(size);
int reCount = RoleCountHelper.getRebels(size);
int spCount = RoleCountHelper.getSpies(size);

for (int i = 0; i < emCount; ++i) {
shuffledPlayers.get(i).setRole(Role.EMPEROR);
}
for (int i = emCount; i < roCount + emCount; ++i) {
shuffledPlayers.get(i).setRole(Role.ROYALIST);
}
for (int i = roCount + emCount; i < roCount + reCount + emCount; ++i) {
shuffledPlayers.get(i).setRole(Role.REBEL);
}
for (int i = roCount + reCount + emCount; i < roCount + reCount + spCount + emCount; ++i) {
shuffledPlayers.get(i).setRole(Role.SPY);
}
}

@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
Expand All @@ -43,4 +84,20 @@ public ArrayList<Pair<Integer, String>> getPlayers() {
public void setPlayers(ArrayList<Pair<Integer, String>> players) {
this.players = players;
}

public ArrayList<Player> getShuffledPlayers() {
return shuffledPlayers;
}

public void setShuffledPlayers(ArrayList<Player> shuffledPlayers) {
this.shuffledPlayers = shuffledPlayers;
}

public ArrayList<CharacterEnum> getShuffledCharacters() {
return shuffledCharacters;
}

public void setShuffledCharacters(ArrayList<CharacterEnum> shuffledCharacters) {
this.shuffledCharacters = shuffledCharacters;
}
}
7 changes: 3 additions & 4 deletions src/main/java/com/lapisberry/net/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.lapisberry.game.controllers.GameController;
import com.lapisberry.game.controllers.LobbyController;
import com.lapisberry.gui.scenes.LobbyScene;
import com.lapisberry.net.packets.ClientPacket;
import com.lapisberry.net.packets.JoinResponsePacket;
import com.lapisberry.net.packets.LobbyPacket;
import com.lapisberry.net.packets.ServerPacket;
import com.lapisberry.net.packets.*;
import com.lapisberry.utils.Config;
import com.lapisberry.utils.exceptions.ConnectionRefusedException;
import javafx.application.Platform;
Expand Down Expand Up @@ -69,6 +66,8 @@ private void processPacketFromServer(ServerPacket packet) {
} else if (packet instanceof LobbyPacket lobbyPacket) {
clientLobby.setPlayers(lobbyPacket.getPlayers());
LobbyScene.updatePlayerList(clientLobby);
} else if (packet instanceof ServerStartGamePacket) {
// TODO: Implement this packet
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/lapisberry/net/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.lapisberry.game.controllers.GameController;
import com.lapisberry.game.controllers.LobbyController;
import com.lapisberry.net.packets.ClientPacket;
import com.lapisberry.net.packets.JoinRequestPacket;
import com.lapisberry.net.packets.LobbyPacket;
import com.lapisberry.net.packets.ServerPacket;
import com.lapisberry.net.packets.*;
import com.lapisberry.utils.Config;

import java.io.IOException;
Expand Down Expand Up @@ -66,6 +63,9 @@ public void processPacketFromClient(ClientHandler sender, ClientPacket packet) {
if (packet instanceof JoinRequestPacket joinRequestPacket) {
serverLobby.addPlayer(sender.getClientId(), joinRequestPacket.getUsername());
sendPacketToAllClients(new LobbyPacket(serverLobby.getPlayers()));
} else if (packet instanceof PartyLeaderStartGamePacket) {
serverLobby.setupShuffledPlayersAndShuffledCharacters();
sendPacketToAllClients(new ServerStartGamePacket(serverLobby.getShuffledPlayers(), serverLobby.getShuffledCharacters()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
package com.lapisberry.net.packets;

import com.lapisberry.game.entities.characters.CharacterEnum;
import com.lapisberry.game.entities.players.Player;

import java.io.Serial;
import java.util.ArrayList;

/**
* The {@code ServerStartGamePacket} is a packet that is sent to all clients by the server to start the game.
*/
public class ServerStartGamePacket extends ServerPacket {
@Serial
private static final long serialVersionUID = -4405052934644343377L;
// Fields
private final ArrayList<Player> shuffledPlayers;
private final ArrayList<CharacterEnum> shuffledCharacters;

// Constructors
public ServerStartGamePacket(ArrayList<Player> shuffledPlayers, ArrayList<CharacterEnum> shuffledCharacters) {
this.shuffledPlayers = new ArrayList<>(shuffledPlayers);
this.shuffledCharacters = new ArrayList<>(shuffledCharacters);
}

// Getter Setters
public ArrayList<Player> getShuffledPlayers() {
return shuffledPlayers;
}

public ArrayList<CharacterEnum> getShuffledCharacters() {
return shuffledCharacters;
}
}

0 comments on commit 3f1524d

Please sign in to comment.