Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented has turn changed functionality #54

Merged
merged 1 commit into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion client/core/src/com/anything/tacticool/model/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ public class Grid {
private int width;
private int heigth;
private List<Player> players;
private boolean turn;

public Grid(String board, int width, int heigth) {
public Grid(String board, int width, int heigth, boolean turn) {
this.heigth = heigth;
this.width = width;
this.board = new int[width * heigth];
this.turn = turn;
players = new ArrayList<>();
String[] boardSplit = board.split(",");
for (int i = 0; i < this.board.length; i++) {
Expand All @@ -36,6 +38,10 @@ public int getWidth() {
return width;
}

public boolean getTurn() {
return turn;
}

public List<Player> getPlayers() {
return players;
}
Expand All @@ -46,6 +52,7 @@ public String toString() {
for (int i : board) {
result += String.format("%d,", i);
}
result += "\n" + turn;
return result + "\n" + players.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public GameView(){
tileIterator = new GridElementIterator();
skin = new Skin(Gdx.files.internal("ui/uiskin.json"));
skin.getFont("default-font").getData().setScale(3f);
grid = new Grid("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1",5,5);
grid = new Grid("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1",5,5, false);
mainPlayer = new Player(513,3,4,4);
Player enemy = new Player(543,3,2,2);
players = new ArrayList<>();
Expand Down
5 changes: 3 additions & 2 deletions client/core/src/httpRequests/Deserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ private Grid deserializeGrid(JsonObject gridJson, List<Player> players) {
System.out.println(gridJson);
Grid grid = new Grid(
gridJson.get("board").getAsString(),
Integer.parseInt(gridJson.get("width").getAsString()),
Integer.parseInt(gridJson.get("height").getAsString()));
gridJson.get("width").getAsInt(),
gridJson.get("height").getAsInt(),
gridJson.get("turnSwitch").getAsBoolean());
grid.setPlayers(players);
return grid;
}
Expand Down
58 changes: 58 additions & 0 deletions client/core/src/httpRequests/Request.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package httpRequests;

import com.anything.tacticool.model.ActionType;
import com.anything.tacticool.model.Grid;
import com.anything.tacticool.model.InputAction;
import com.anything.tacticool.model.Player;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -9,6 +12,8 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class Request {
public Request() {
Expand Down Expand Up @@ -96,4 +101,57 @@ public int getPlayerIDFromLogin(String name, String pass) throws IOException {
return Integer.parseInt(content.toString());

}

public boolean getHasChanged(int gameID, boolean turn) throws IOException {
URL url = new URL(String.format("http://localhost:8080/api/hasChanged/%d/%b", gameID, turn));
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
con.disconnect();
return Boolean.parseBoolean(content.toString());

}

public static void main(String[] args) throws IOException, InterruptedException {
Request request = new Request();
int niko = request.getPlayerIDFromLogin("nikolai", "notapassword");
int gameId = request.joinGame(4, niko);
int tester = request.getPlayerIDFromLogin("tester", "tester");
int g2 = request.joinGame(gameId, tester);

Grid grid = request.getGameState(gameId);
System.out.println(grid);
List<Player> players = grid.getPlayers();

Player nikola = null;
Player testers = null;
for (Player player : players) {
if (player.getPlayerID() == niko) {
nikola = player;
continue;
}
if (player.getPlayerID() == tester) {
testers = player;
continue;
}
}
if (nikola == null || testers == null) {
System.out.println("Something went wrong!");
}
Serializer serializer = new Serializer();
List<InputAction> actions = new ArrayList<>();
actions.add(new InputAction(ActionType.MOVE, 1, 0));
actions.add(new InputAction(ActionType.MOVE, 1, 0));
request.postMoves(serializer.serializeActions(actions), gameId, nikola.getPlayerID());

Thread.sleep(3000);

System.out.println(request.getHasChanged(gameId, grid.getTurn()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ public ResponseEntity<String> getFinishedSimulation(@PathVariable String gameId)
return new ResponseEntity<>(ah.getGameState(), HttpStatus.OK);
}

@GetMapping("/hasChanged{gameId}")
public boolean hasChanged(@PathVariable String gameId) {
// Check if map has changed, this needs to be done EFFICIENT!!
// return true if map has changed
// return false if map hasn't changed
return true;
/**
* Gets the value of the turn switch.
* @param gameId of game to check.
* @return the current turns value.
*/
@GetMapping("/hasChanged/{gameId}/{currentTurn}")
public ResponseEntity<Boolean> hasChanged(@PathVariable String gameId, @PathVariable String currentTurn) {
return new ResponseEntity<>(new DBController().getTurnSwitch(Integer.parseInt(gameId)) != Boolean.parseBoolean(currentTurn), HttpStatus.OK);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public String getGameState() {
board.addProperty("board", grid.toStringMap());
board.addProperty("width", grid.getDimensions().getX());
board.addProperty("height", grid.getDimensions().getY());
board.addProperty("turnSwitch", new DBController().getTurnSwitch(gameID));
results.add("grid", board);
results.add("winState", getWinState());
return results.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ public Player getPlayerById(int id) {
return null;
}

public boolean getTurnSwitch(int gameID) {
try (Statement stmt = getConn().createStatement()){
String sql = String.format("SELECT ready FROM GameTable WHERE IDgame = %d", gameID);
ResultSet result = stmt.executeQuery(sql);
result.next();
return result.getBoolean("ready");
} catch (SQLException e) {
e.printStackTrace();
}
throw new IllegalStateException("Something went wrong when fetching 'ready' column");
}

/**
* Adds the actions of a player in a specific game to the DB.
* @param handler An ActionHandler
Expand All @@ -150,7 +162,7 @@ public void updateGameState(ActionHandler handler, String actions) {
String updatePlayer = String.format("UPDATE miburgos_tacticool.GameToPlayer SET hp = %d, coord = '%s', ready = %b WHERE IDgame = %d AND IDplayer = %d", player.getHealthPoints(), player.getPosition().toString(), false, handler.getGameID(), player.getPlayerID());
stmt.execute(updatePlayer);
}
String updateGame = String.format("UPDATE miburgos_tacticool.GameTable SET actions = '%s' WHERE IDgame = %d", actions, handler.getGameID());
String updateGame = String.format("UPDATE miburgos_tacticool.GameTable SET actions = '%s', ready = %b WHERE IDgame = %d", actions, !getTurnSwitch(handler.getGameID()), handler.getGameID());
stmt.execute(updateGame);
} catch (SQLException e) {
e.printStackTrace();
Expand Down