Added client socket

This commit is contained in:
Thomas
2020-08-21 15:53:16 +02:00
parent 861105eb92
commit e2e4495f02
17 changed files with 200 additions and 55 deletions

View File

@@ -6,7 +6,7 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@ComponentScan(basePackages = "gltronic.voozik")
@ComponentScan(basePackages = "gltronic.tronio")
@PropertySource("classpath:application.properties")
public class TronIoApplication {

View File

@@ -6,12 +6,14 @@ import java.util.Random;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketSession;
import gltronic.tronio.model.Game;
import gltronic.tronio.model.Player;
import gltronic.tronio.model.Wall;
@Service
public class GameManager implements IGameManager {
@Autowired
Game game;
@@ -29,6 +31,7 @@ public class GameManager implements IGameManager {
game.getPlayers().put(session.getId(), player);
SocketUtils.forwardMessage(session, "login", new JSONObject(player));
SocketUtils.forwardMessage(session, "gameSettings", new JSONObject(game.getSettings()));
}
@Override
@@ -90,7 +93,7 @@ public class GameManager implements IGameManager {
private void killPlayer (String id) {
Player player = game.getPlayers().get(id);
initPlayer(player);
SocketUtils.sendMessage(game.getSessions().get(id), "dead", "yo dead");
SocketUtils.sendMessage(game.getSessions().get(id), "gamePlayerDead", "yo dead");
}
private boolean isCloseToWall (double xa, double ya, double xb, double yb, double xc, double yc, double radius) {

View File

@@ -1,18 +1,32 @@
package gltronic.tronio.model;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@Component
public class Game {
private Map<String, Player> players;
private Map<String, WebSocketSession> sessions;
private GameSettings settings;
public Game() {
this.players = new HashMap<>();
this.sessions = new HashMap<>();
this.settings = new GameSettings();
this.settings.setArenaSize(1000);
this.settings.setPlayerSize(10);
this.settings.setPlayerSpeed(2);
this.settings.setPlayerTurnSpeed(10);
this.settings.setWallSize(8);
this.settings.setWallUpdate(5);
}
}

View File

@@ -31,7 +31,7 @@ public class SocketHandler extends TextWebSocketHandler {
String type = (String) jsonObject.get("type");
switch (type) {
case "update":
gameManager.updatePlayer(session, (Player) jsonObject.get("update"));
gameManager.updatePlayer(session, (Player) jsonObject.get("message"));
break;
default:
SocketUtils.sendMessage(session, "error", "unknow command");