Skip to content

Commit

Permalink
Lobby size logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Lost-Fly committed May 23, 2024
1 parent 81d0385 commit 9be319a
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ private void processObject(JsonValue object) {
String sessionId = object.getString("id");
String sessionPassword = object.getString("password");
String sessionMsg = object.getString("msg");
int sessionSize = object.getInt("size");
String sessionSize = object.getString("size");
Gdx.app.log("SESSION STATE", "MSG " + sessionMsg);
try {
gameSession.setConnected(true);
gameSession.setId(sessionId);
gameSession.setPassword(sessionPassword);
gameSession.setSessionMsg(sessionMsg);
gameSession.setSize(sessionSize);
gameSession.setSize(Integer.parseInt(sessionSize));
} catch (Exception e) {
// TODO
}
Expand Down
9 changes: 2 additions & 7 deletions core/src/com/aqwsxlostfly/packandgo/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ public class Main extends Game {


public Main(InputState inputState) {

Main.inputState = inputState;



gameSession = new GameSession(false);
}

Expand All @@ -47,15 +43,14 @@ public void create() {

renderer = new Renderer();
renderer.setHomeScreen(new HomeSc(this));
//renderer.setGameScreen(new PlayScreen());

}

@Override
public void render() {
renderer.render();
}


public void evict(String idToEvict) {
PlayScreen.players.remove(idToEvict);
}
Expand Down Expand Up @@ -85,7 +80,7 @@ public void setMeId(String meId) {
}

public void setMessageSender(MessageSender messageSender) {
this.messageSender = messageSender;
Main.messageSender = messageSender;
}

public boolean getSocketState() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/com/aqwsxlostfly/packandgo/Screens/HomeSc.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ private boolean validateFields(TextField passwordField, TextField idField, TextF

private void connectToServer(String password, String roomId, int size, boolean isCreating) {
String action = isCreating ? CREATE_ROOM : JOIN_ROOM;
SessionState sessionState = new SessionStateToSend(action, roomId, password, size);
SessionState sessionState = new SessionStateToSend(action, roomId, password, String.valueOf(size));
try {
Main.messageSender.sendMessage(sessionState);
if (isCreating) {
Expand Down
57 changes: 37 additions & 20 deletions core/src/com/aqwsxlostfly/packandgo/Screens/PlayScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public class PlayScreen implements Screen {

private BitmapFont font;

private static final float STEP_TIME = 1 / 60f;
private float accumulator;

private boolean isScreamPlaying = false;

Joystick joy;
Expand Down Expand Up @@ -111,6 +114,7 @@ public class PlayScreen implements Screen {


public PlayScreen() {
accumulator = 0f;

isWaitingForPlayers = true;
isGameCountdownRunning = false;
Expand Down Expand Up @@ -330,36 +334,25 @@ public void multitouch(float x, float y, boolean isDownTouch, int pointer) {
}

public void render(SpriteBatch batch, OrthographicCamera camera) {

if (players.get(meId) == null) return;

updatePlayers();
updateGameTimers(frameRateRender);
float delta = Gdx.graphics.getDeltaTime();
accumulator += delta;

while (accumulator >= STEP_TIME) {
updateAll();
accumulator -= STEP_TIME;
}


cameraUpdate(camera);

world.step(frameRateRender, 8, 3);

orthogonalTiledMapRenderer.setView(camera); // Устанавливаем камеру для рендерера карты
orthogonalTiledMapRenderer.render(); // Рендерим карту

if (players.get(meId).isHolding()) {
try {

if(furnitures.get(players.get(meId).getContactFurnitureKey()).getHoldersCount()!=0) {
players.get(meId).decreaseSpeed(furnitures.get(players.get(meId).getContactFurnitureKey()).getWeight() /
furnitures.get(players.get(meId).getContactFurnitureKey()).getHoldersCount());
}else{
players.get(meId).decreaseSpeed(furnitures.get(players.get(meId).getContactFurnitureKey()).getWeight());
}

updateContactfurniture();

} catch (Exception e) {
// TODO
}
} else {
players.get(meId).decreaseSpeed(0f);
}


furnitures.forEach(e -> e.value.draw(batch));
Expand Down Expand Up @@ -397,6 +390,30 @@ public void render(SpriteBatch batch, OrthographicCamera camera) {

}

private void updateAll() {
updatePlayers();
updateGameTimers(frameRateRender);

if (players.get(meId).isHolding()) {
try {

if(furnitures.get(players.get(meId).getContactFurnitureKey()).getHoldersCount()!=0) {
players.get(meId).decreaseSpeed(furnitures.get(players.get(meId).getContactFurnitureKey()).getWeight() /
furnitures.get(players.get(meId).getContactFurnitureKey()).getHoldersCount());
}else{
players.get(meId).decreaseSpeed(furnitures.get(players.get(meId).getContactFurnitureKey()).getWeight());
}

updateContactfurniture();

} catch (Exception e) {
// TODO
}
} else {
players.get(meId).decreaseSpeed(0f);
}
}


private void drawFurnitureCount(SpriteBatch batch) {
String currentFurniture = getCurrentFurnitureCount();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.aqwsxlostfly.packandgo.Tools.maptools;

import static com.aqwsxlostfly.packandgo.Main.meId;
import static com.aqwsxlostfly.packandgo.Screens.PlayScreen.furnitures;
import static com.aqwsxlostfly.packandgo.Screens.PlayScreen.players;
Expand Down Expand Up @@ -31,10 +32,6 @@
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.joints.DistanceJoint;
import com.badlogic.gdx.physics.box2d.joints.DistanceJointDef;
import com.badlogic.gdx.utils.Array;


import java.util.Objects;

Expand Down
1 change: 1 addition & 0 deletions core/src/com/aqwsxlostfly/packandgo/render/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class Renderer {

private static Screen currentScreen;


public Renderer() {
camera = new OrthographicCamera(150, 100);
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/com/aqwsxlostfly/packandgo/session/SessionState.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface SessionState {
void setSessionPassword(String password);
String getSessionPassword();

void setSessionSize(int size);
int getSessionSize();
void setSessionSize(String size);
String getSessionSize();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ public class SessionStateToSend implements SessionState {
private String type;
private String Id;
private String password;
private String size;

private int size;


public SessionStateToSend(String type, String id, String password, int size) {
public SessionStateToSend(String type, String id, String password, String size) {
this.type = type;
Id = id;
this.password = password;
Expand Down Expand Up @@ -47,12 +46,12 @@ public String getSessionPassword() {
}

@Override
public void setSessionSize(int size) {
public void setSessionSize(String size) {
this.size = size;
}

@Override
public int getSessionSize() {
public String getSessionSize() {
return this.size;
}
}
Binary file modified server/out/artifacts/packandgo_jar/packandgo.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
public class GameSession {
private final String sessionId;
private final String sessionPassword;
private final Integer userLimit;
private final String userLimit;

private final Map<String, String> activeUserSessions = new HashMap<>();


private GameState gameState;

public GameSession(String sessionId, String sessionPassword, Integer userLimit, GameState gameState) {
public GameSession(String sessionId, String sessionPassword, String userLimit, GameState gameState) {
this.sessionId = sessionId;
this.sessionPassword = sessionPassword;
this.userLimit = userLimit;
Expand All @@ -46,7 +46,7 @@ public void updateGameState(JsonNode state) {
}

public boolean canJoin() {
return gameState.getPLayersAmount() + 1 <= userLimit;
return gameState.getPLayersAmount() + 1 <= Integer.parseInt(userLimit);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void joinGameSession(String userId, String sessionId, String sessionPassw
if (!activeGameSessions.containsKey(sessionId)) {
Gdx.app.log("JOIN SESSION", "ERROR JOINING SESSION - SESSION "
+ sessionId + " DOES NOT EXISTS");
sendSessionState(sessionPassword, sessionId, SESSION_STATE_NO_EXISTS, session);
sendSessionState(sessionPassword, sessionId, SESSION_STATE_NO_EXISTS, String.valueOf(1000), session);
return;
}
if (!userToSession.containsKey(userId) && activeGameSessions.get(sessionId).canJoin()) {
Expand All @@ -73,23 +73,23 @@ public void joinGameSession(String userId, String sessionId, String sessionPassw
if (Objects.equals(sessionPassword, activeGameSessions.get(sessionId).getSessionPassword())) {
userToSession.put(userId, sessionId);
activeGameSessions.get(sessionId).addUserSession(userId);
sendSessionState(sessionPassword, sessionId, SESSION_STATE_OK, session);
sendSessionState(sessionPassword, sessionId, SESSION_STATE_OK, activeGameSessions.get(sessionId).getUserLimit(), session);
} else {
// TODO
Gdx.app.log("JOIN SESSION", "ERROR JOINING SESSION - USER "
+ userId + " INCORRECT PASSWORD " + sessionPassword);
sendSessionState(sessionPassword, sessionId, SESSION_STATE_WRONG_PASSWORD, session);
sendSessionState(sessionPassword, sessionId, SESSION_STATE_WRONG_PASSWORD, String.valueOf(1000), session);
}
} else {
// TODO
Gdx.app.log("JOIN SESSION", "ERROR JOINING SESSION - USER " + userId
+ " EXISTS IN SESSION OR PLAYER LIMIT ");
sendSessionState(sessionPassword, sessionId, SESSION_STATE_MAX_PLAYERS, session);
sendSessionState(sessionPassword, sessionId, SESSION_STATE_MAX_PLAYERS, String.valueOf(1000), session);
}
}


public void createGameSession(String userId, String sessionId, String sessionPassword, Integer userLimit, StandardWebSocketSession session) {
public void createGameSession(String userId, String sessionId, String sessionPassword, String userLimit, StandardWebSocketSession session) {
if (!userToSession.containsKey(userId) && !activeGameSessions.containsKey(sessionId)) {
GameState gameState = new GameState();
GameSession gameSession = new GameSession(sessionId, sessionPassword, userLimit, gameState);
Expand All @@ -100,7 +100,7 @@ public void createGameSession(String userId, String sessionId, String sessionPas

Gdx.app.log("CREATED_GAME_SESSION", gameSession.toString());

sendSessionState(sessionPassword, sessionId, SESSION_STATE_OK, session);
sendSessionState(sessionPassword, sessionId, SESSION_STATE_OK, gameSession.getUserLimit(), session);
} else {
if (userToSession.containsKey(userId) && !activeGameSessions.containsKey(sessionId)) {

Expand All @@ -115,18 +115,18 @@ public void createGameSession(String userId, String sessionId, String sessionPas

Gdx.app.log("CREATED_GAME_SESSION", gameSession.toString());

sendSessionState(sessionPassword, sessionId, SESSION_STATE_OK, session);
sendSessionState(sessionPassword, sessionId, SESSION_STATE_OK, String.valueOf(userLimit), session);
}else{
// TODO
Gdx.app.log("ADD SESSION", "ERROR ADDING SESSION - SESSION " + sessionId + " EXISTS");
sendSessionState(sessionPassword, sessionId, SESSION_STATE_EXISTS, session);
sendSessionState(sessionPassword, sessionId, SESSION_STATE_EXISTS, String.valueOf(userLimit), session);
}
}
}

private void sendSessionState(String sessionPassword, String sessionId, String sessionMsg, StandardWebSocketSession session) {
private void sendSessionState(String sessionPassword, String sessionId, String sessionMsg, String size, StandardWebSocketSession session) {
Array<GameSessionToSend> stateToSend = new Array<>();
GameSessionToSend gameSessionToSend = new GameSessionToSend(sessionId, sessionPassword, sessionMsg);
GameSessionToSend gameSessionToSend = new GameSessionToSend(sessionId, sessionPassword, size, sessionMsg);
stateToSend.add(gameSessionToSend);

String stateJson = json.toJson(stateToSend);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ public class GameSessionToSend implements Json.Serializable {
private String sessionId;
private String sessionPassword;
private String sessionMsg;
private String size;

public GameSessionToSend(String sessionId, String sessionPassword, String sessionMsg) {

public GameSessionToSend(String sessionId, String sessionPassword, String size, String sessionMsg) {
this.sessionId = sessionId;
this.sessionPassword = sessionPassword;
this.sessionMsg = sessionMsg;
this.size = size;
}

@Override
public void write(Json json) {
json.writeValue("id", sessionId);
json.writeValue("password", sessionPassword);
json.writeValue("msg", sessionMsg);
json.writeValue("size", size);
}

@Override
Expand Down Expand Up @@ -50,4 +52,12 @@ public String getSessionMsg() {
public void setSessionMsg(String sessionMsg) {
this.sessionMsg = sessionMsg;
}

public String getSize() {
return size;
}

public void setSize(String size) {
this.size = size;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class MessageDispatcher {
public static final String PLAYER_INTERACTION = "playerInteraction";
public static final String SESSION_PASSWORD = "sessionPassword";
public static final String SESSION_ID = "sessionId";
public static final String SESSION_SIZE = "sessionSize";

@Autowired
private GameSessionManager gameSessionManager;
Expand All @@ -33,7 +34,8 @@ public void processMessage(StandardWebSocketSession session, JsonNode message) {
case CREATE_ROOM -> {
String sessionId = message.get(SESSION_ID).asText();
String sessionPassword = message.get(SESSION_PASSWORD).asText();
gameSessionManager.createGameSession(userId, sessionId, sessionPassword, 2, session);
String sessionCreateSize = message.get(SESSION_SIZE).asText();
gameSessionManager.createGameSession(userId, sessionId, sessionPassword, sessionCreateSize, session);
}
case JOIN_ROOM -> {
String sessionJoinId = message.get(SESSION_ID).asText();
Expand Down

0 comments on commit 9be319a

Please sign in to comment.