Base game & server skeleton
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package gltronic.tronio.business;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import gltronic.tronio.model.Player;
|
||||
|
||||
public class GameManager implements IGameManager {
|
||||
|
||||
@Override
|
||||
public void login(WebSocketSession session) throws InterruptedException, IOException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leave(WebSocketSession session) throws InterruptedException, IOException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(WebSocketSession session, Player player) throws InterruptedException, IOException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package gltronic.tronio.business;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import gltronic.tronio.model.Player;
|
||||
|
||||
public interface IGameManager {
|
||||
void login(WebSocketSession session) throws InterruptedException, IOException;
|
||||
void leave(WebSocketSession session) throws InterruptedException, IOException;
|
||||
void update(WebSocketSession session, Player player) throws InterruptedException, IOException;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package gltronic.tronio.business;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
public interface IRoomManager {
|
||||
public void login(WebSocketSession session, String name) throws InterruptedException, IOException;
|
||||
public void leave(WebSocketSession session);
|
||||
public void createRoom(WebSocketSession session) throws InterruptedException, IOException;
|
||||
public void connectRoom(WebSocketSession session, String roomName) throws InterruptedException, IOException;
|
||||
public void followRTC(WebSocketSession session, TextMessage message) throws InterruptedException, IOException;
|
||||
public void sendMessage(WebSocketSession session, String type, String message) throws InterruptedException, IOException;
|
||||
public void sendServerInfos(WebSocketSession session) throws InterruptedException, IOException;
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
package gltronic.tronio.business;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import gltronic.tronio.model.BiMap;
|
||||
|
||||
@Service
|
||||
public class RoomManager implements IRoomManager{
|
||||
private volatile BiMap<String, WebSocketSession> users = new BiMap<String, WebSocketSession>();
|
||||
private volatile BiMap<String, String> rooms = new BiMap<String, String>();
|
||||
|
||||
public void login(WebSocketSession session, String name) throws InterruptedException, IOException {
|
||||
if (name == null) {
|
||||
sendMessage(session, "error", "bad command command");
|
||||
return;
|
||||
}
|
||||
if (users.containsKey(name)) {
|
||||
sendMessage(session, "login", "false");
|
||||
return;
|
||||
}
|
||||
|
||||
users.put(name, session);
|
||||
sendMessage(session, "login", "true");
|
||||
sendServerInfos(session);
|
||||
|
||||
System.err.println("[ROOM] Logged "+name);
|
||||
}
|
||||
|
||||
public void leave(WebSocketSession session) {
|
||||
String name = users.getKey(session);
|
||||
users.removeValue(session);
|
||||
if(rooms.containsValue(name)) rooms.removeValue(name);
|
||||
}
|
||||
|
||||
public void createRoom(WebSocketSession session) throws InterruptedException, IOException {
|
||||
if (!users.containsValue(session)) {
|
||||
sendMessage(session, "error", "need login");
|
||||
return;
|
||||
}
|
||||
String userName = users.getKey(session);
|
||||
if (rooms.containsKey(userName)) {
|
||||
sendMessage(session, "error", "no multiple room");
|
||||
return;
|
||||
}
|
||||
|
||||
Random random = new Random();
|
||||
String roomName = Integer.toString(random.nextInt(9999));
|
||||
|
||||
while (roomName.length() < 4) roomName += 0 + roomName;
|
||||
|
||||
rooms.put(roomName, userName);
|
||||
sendMessage(session, "createRoom", roomName);
|
||||
|
||||
System.err.println("[ROOM] Created room "+roomName+" by "+userName);
|
||||
}
|
||||
|
||||
public void connectRoom(WebSocketSession session, String roomName) throws InterruptedException, IOException {
|
||||
if (roomName == null) {
|
||||
sendMessage(session, "error", "bad command command");
|
||||
return;
|
||||
}
|
||||
if (!users.containsValue(session)) {
|
||||
sendMessage(session, "error", "need login");
|
||||
return;
|
||||
}
|
||||
if (!rooms.containsKey(roomName)) {
|
||||
sendMessage(session, "error", "no room");
|
||||
return;
|
||||
}
|
||||
|
||||
String roomAdmin = rooms.get(roomName);
|
||||
sendMessage(session, "connectRoom", roomAdmin);
|
||||
|
||||
String userName = users.getKey(session);
|
||||
System.err.println("[ROOM] Connection to room "+roomName+" ("+roomAdmin+") by "+userName);
|
||||
}
|
||||
|
||||
public void followRTC(WebSocketSession session, TextMessage message) throws InterruptedException, IOException {
|
||||
String payload = message.getPayload();
|
||||
JSONObject jsonObject = new JSONObject(payload);
|
||||
String target = (String) jsonObject.get("target");
|
||||
|
||||
if (target == null) {
|
||||
sendMessage(session, "error", "no target");
|
||||
return;
|
||||
}
|
||||
|
||||
WebSocketSession targetSession = users.get(target);
|
||||
|
||||
if (targetSession == null) {
|
||||
sendMessage(session, "error", "unknow target");
|
||||
return;
|
||||
}
|
||||
|
||||
System.err.println("[ROOM] Foward RTC message");
|
||||
followMessage(targetSession, message);
|
||||
}
|
||||
|
||||
public void followMessage (WebSocketSession session, TextMessage message) throws IOException {
|
||||
session.sendMessage(message);
|
||||
}
|
||||
|
||||
public void sendMessage(WebSocketSession session, String type, String message) throws InterruptedException, IOException {
|
||||
session.sendMessage(new TextMessage("{\"type\":\""+type+"\",\"message\":\""+message+"\"}"));
|
||||
}
|
||||
|
||||
public void sendServerInfos(WebSocketSession session) throws InterruptedException, IOException {
|
||||
session.sendMessage(new TextMessage("{\"type\":\"serverInfos\",\"userCount\":\""+users.size()+"\",\"roomCount\":\""+rooms.size()+"\"}"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package gltronic.tronio.business;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import gltronic.tronio.model.Game;
|
||||
|
||||
public class SocketUtils {
|
||||
public static void sendMessage(WebSocketSession session, String type, String message) {
|
||||
try {
|
||||
session.sendMessage(new TextMessage("{\"type\":\"" + type + "\",\"message\": \"" + message.toString() + "\" }"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void forwardMessage(WebSocketSession session, String type, JSONObject message) {
|
||||
try {
|
||||
session.sendMessage(new TextMessage("{\"type\":\"" + type + "\",\"message\": " + message.toString() + " }"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void broadcast(Game game, String type, JSONObject message) {
|
||||
game.getSessions().forEach((id, session) -> {
|
||||
forwardMessage(session, type, message);
|
||||
});
|
||||
}
|
||||
}
|
||||
18
server/src/main/java/gltronic/tronio/model/Game.java
Normal file
18
server/src/main/java/gltronic/tronio/model/Game.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package gltronic.tronio.model;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class Game {
|
||||
private Map<String, Player> players;
|
||||
private Map<String, WebSocketSession> sessions;
|
||||
private GameSettings settings;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package gltronic.tronio.model;
|
||||
|
||||
public abstract class GameEntity {
|
||||
|
||||
}
|
||||
17
server/src/main/java/gltronic/tronio/model/GameSettings.java
Normal file
17
server/src/main/java/gltronic/tronio/model/GameSettings.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package gltronic.tronio.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class GameSettings {
|
||||
private double playerSize;
|
||||
private double playerSpeed;
|
||||
private double playerTurnSpeed;
|
||||
private double wallSize;
|
||||
private double wallUpdate;
|
||||
private double arenaSize;
|
||||
}
|
||||
22
server/src/main/java/gltronic/tronio/model/Player.java
Normal file
22
server/src/main/java/gltronic/tronio/model/Player.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package gltronic.tronio.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class Player {
|
||||
private String id;
|
||||
private double x;
|
||||
private double y;
|
||||
private double angle;
|
||||
private double targetAngle;
|
||||
private ArrayList<Wall> walls;
|
||||
private int lastWall;
|
||||
private String color;
|
||||
|
||||
}
|
||||
13
server/src/main/java/gltronic/tronio/model/Wall.java
Normal file
13
server/src/main/java/gltronic/tronio/model/Wall.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package gltronic.tronio.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class Wall {
|
||||
private double x;
|
||||
private double y;
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package gltronic.tronio.web;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -12,63 +10,49 @@ import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
||||
|
||||
import gltronic.tronio.business.IRoomManager;
|
||||
import gltronic.tronio.business.IGameManager;
|
||||
import gltronic.tronio.business.SocketUtils;
|
||||
import gltronic.tronio.model.Player;
|
||||
|
||||
@Component
|
||||
public class SocketHandler extends TextWebSocketHandler {
|
||||
private volatile List<WebSocketSession> sessions = new CopyOnWriteArrayList<>();
|
||||
|
||||
@Autowired
|
||||
IRoomManager roomManager;
|
||||
IGameManager gameManager;
|
||||
|
||||
@Override
|
||||
public void handleTextMessage(WebSocketSession session, TextMessage message) throws InterruptedException, IOException {
|
||||
public void handleTextMessage(WebSocketSession session, TextMessage message)
|
||||
throws InterruptedException, IOException {
|
||||
System.err.println("[WS] message :" + message.getPayload());
|
||||
|
||||
|
||||
String payload = message.getPayload();
|
||||
JSONObject jsonObject = new JSONObject(payload);
|
||||
|
||||
|
||||
String type = (String) jsonObject.get("type");
|
||||
switch (type) {
|
||||
case "serverInfos":
|
||||
roomManager.sendServerInfos(session);
|
||||
break;
|
||||
case "login":
|
||||
roomManager.login(session, (String) jsonObject.get("name"));
|
||||
break;
|
||||
case "createRoom":
|
||||
roomManager.createRoom(session);
|
||||
break;
|
||||
case "connectRoom":
|
||||
roomManager.connectRoom(session, (String) jsonObject.get("name"));
|
||||
break;
|
||||
case "leave":
|
||||
roomManager.leave(session);
|
||||
break;
|
||||
case "offer":
|
||||
case "answer":
|
||||
case "candidate":
|
||||
roomManager.followRTC(session, message);
|
||||
break;
|
||||
case "search":
|
||||
break;
|
||||
case "alive":
|
||||
break;
|
||||
case "update":
|
||||
gameManager.update(session, (Player) jsonObject.get("update"));
|
||||
break;
|
||||
default:
|
||||
roomManager.sendMessage(session, "error", "unknow command");
|
||||
SocketUtils.sendMessage(session, "error", "unknow command");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
System.err.println("[WS] new connection " + this);
|
||||
sessions.add(session);
|
||||
gameManager.login(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus){
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) {
|
||||
System.err.println("[WS] connection closed");
|
||||
sessions.remove(session);
|
||||
roomManager.leave(session);
|
||||
try {
|
||||
gameManager.leave(session);
|
||||
} catch (InterruptedException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user