Skip to content

Commit

Permalink
RoomState: Control rooms join errors
Browse files Browse the repository at this point in the history
  • Loading branch information
davixcky committed May 27, 2021
1 parent 9fa9508 commit 9e7e6d1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
11 changes: 7 additions & 4 deletions src/main/java/com/uninorte/base/api/GameClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class GameClient {

private SocketClient socketClient;

private RequestHandler requestHandler;
private final RequestHandler requestHandler;

public GameClient(String basePath) {
requestHandler = new RequestHandler(basePath);
Expand All @@ -42,6 +42,7 @@ public void connectToSocket() {
}

public void createUser(String nickname) {
lastError = null;
User tmpUser = new User(nickname);

RequestHandler.RequestResponse response = doPost(USER_CREATE, tmpUser.toJson());
Expand All @@ -52,6 +53,7 @@ public void createUser(String nickname) {
}

public void createRoom() {
lastError = null;
validateUserIsLogged();
if (lastError != null)
return;
Expand All @@ -63,6 +65,7 @@ public void createRoom() {
}

public void joinToRoom(String joinCode) {
lastError = null;
validateUserIsLogged();
if (lastError != null)
return;
Expand All @@ -77,15 +80,15 @@ public void joinToRoom(String joinCode) {
public List<User> getUsersFromARoom(String joinCode) {
String url = String.format(ROOMS_GET_USERS, joinCode);
RequestHandler.RequestResponse response = doGet(url);

List<User> users = null;
if (response.statusCode < 400) {
users = User.createUsersFromJson(response.bodyResponse);
}

return users;
}

public List<Room> getRooms() {
validateUserIsLogged();
if (lastError != null)
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/com/uninorte/base/game/states/RoomsState.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uninorte.base.game.states;

import com.uninorte.base.api.models.Error;
import com.uninorte.base.api.models.Room;
import com.uninorte.base.game.Handler;
import com.uninorte.base.game.gfx.Assets;
Expand All @@ -13,9 +14,12 @@

public class RoomsState extends State {

private Error error;

int width = handler.boardDimensions().width;
int height = handler.boardDimensions().height;

private final Point cordTextError = new Point((int) (width * 0.5f), (int) (height * 0.2f));
private final Point cordText1 = new Point((int) (width * 0.5f), (int) (height * 0.5f));
private final Point cordJoinInput = new Point((int) (width * 0.5f - UIButton.btnImage.getWidth() * 0.5f), cordText1.y + 30);
private final Point cordText2 = new Point((int) (width * 0.5f), cordJoinInput.y + UIButton.btnImage.getHeight() + 80);
Expand All @@ -36,9 +40,13 @@ protected void initComponents() {
UIInput joinCodeInput = new UIInput(this, cordJoinInput.x, cordJoinInput.y);
joinCodeInput.setListener(() -> {
handler.getGameClient().joinToRoom(joinCodeInput.getText().toUpperCase());
handler.getGameClient().getUsersFromARoom(joinCodeInput.getText().toUpperCase()).forEach(user -> {
System.out.println(user.toString());
});

error = handler.getGameClient().getLastError();
if (error != null) {
return;
}

setCurrentState(handler.getGame().multiplayerState);
});

UIButton newRoomBtn = new UIButton(this, cordCreateBtn.x, cordCreateBtn.y, assets.get(0), () -> {
Expand Down Expand Up @@ -69,6 +77,15 @@ public void render(Graphics g) {
g.setColor(new Color(3, 39, 78, 198));
g.fillRect(0, 0, width, height);

if (error != null) {
Text.drawString(g, error.getErrorMessage(),
cordTextError.x,
cordTextError.y,
true,
Color.red,
Assets.getFont(Assets.FontsName.SLKSCR, 20));
}

Text.drawString(
g,
"JOIN TO A PRIVATE ROOM",
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/com/uninorte/base/game/states/SignUpState.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,6 @@ public void render(Graphics g) {
Assets.getFont(Assets.FontsName.SLKSCR, 20));
}

// if (additionalError != null) {
// Text.drawString(g, additionalError,
// handler.boardDimensions().width / 2,
// (int) (handler.boardDimensions().height * 0.30f),
// true,
// Color.red,
// Assets.getFont(Assets.FontsName.SLKSCR, 20));
// }

uiManager.render(g);
}
}

0 comments on commit 9e7e6d1

Please sign in to comment.