Added kick & lil refactor
This commit is contained in:
@@ -18,6 +18,7 @@ public interface IRoomManager {
|
||||
|
||||
public void forwardRoomMessage(WebSocketSession session, TextMessage message) throws InterruptedException, IOException;
|
||||
|
||||
public void sendMessage(WebSocketSession session, String type, String message) throws InterruptedException, IOException;
|
||||
public void kickUser(WebSocketSession session, String username) throws InterruptedException, IOException;
|
||||
|
||||
public void sendServerInfos(WebSocketSession session) throws InterruptedException, IOException;
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import gltronic.voozik.model.ServerStatus;
|
||||
import gltronic.voozik.model.VRoom;
|
||||
import gltronic.voozik.model.VUser;
|
||||
|
||||
@@ -18,19 +19,20 @@ public class RoomManager implements IRoomManager {
|
||||
private volatile ArrayList<VUser> users = new ArrayList<VUser>();
|
||||
private volatile ArrayList<VRoom> rooms = new ArrayList<VRoom>();
|
||||
|
||||
@Override
|
||||
public void login(WebSocketSession session, String name) throws InterruptedException, IOException {
|
||||
if (name == null) {
|
||||
sendMessage(session, "error", "bad command command");
|
||||
SocketUtils.sendMessage(session, "error", "bad command command");
|
||||
return;
|
||||
}
|
||||
|
||||
if (users.stream().anyMatch(user -> user.getSession().equals(session))) {
|
||||
sendMessage(session, "login", "false");
|
||||
SocketUtils.sendMessage(session, "login", "false");
|
||||
return;
|
||||
}
|
||||
|
||||
if (users.stream().anyMatch(user -> user.getName().equals(name))) {
|
||||
sendMessage(session, "login", "false");
|
||||
SocketUtils.sendMessage(session, "login", "false");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -40,21 +42,16 @@ public class RoomManager implements IRoomManager {
|
||||
|
||||
users.add(user);
|
||||
|
||||
sendMessage(session, "login", "true");
|
||||
SocketUtils.sendMessage(session, "login", "true");
|
||||
sendServerInfos(session);
|
||||
|
||||
System.err.println("[ROOM] Logged " + name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leave(WebSocketSession session) throws InterruptedException, IOException {
|
||||
Optional<VUser> ouser = users.stream().filter(user -> user.getSession().equals(session)).findFirst();
|
||||
|
||||
if (ouser.isEmpty()) {
|
||||
sendMessage(session, "error", "need login");
|
||||
return;
|
||||
}
|
||||
|
||||
VUser user = ouser.get();
|
||||
VUser user = UserUtils.getUser(session, users);
|
||||
if (user == null) return;
|
||||
|
||||
users.remove(user);
|
||||
Optional<VRoom> oroom = rooms.stream().filter(room -> room.getUsers().contains(user)).findFirst();
|
||||
@@ -64,16 +61,13 @@ public class RoomManager implements IRoomManager {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createRoom(WebSocketSession session) throws InterruptedException, IOException {
|
||||
if (!users.stream().anyMatch(user -> user.getSession().equals(session))) {
|
||||
sendMessage(session, "error", "need login");
|
||||
return;
|
||||
}
|
||||
|
||||
VUser user = users.stream().filter(suser -> suser.getSession().equals(session)).findFirst().get();
|
||||
VUser user = UserUtils.getUser(session, users);
|
||||
if (user == null) return;
|
||||
|
||||
if (user.getRoom() != null) {
|
||||
sendMessage(session, "error", "no already in room");
|
||||
SocketUtils.sendMessage(session, "error", "no already in room");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -92,36 +86,42 @@ public class RoomManager implements IRoomManager {
|
||||
rooms.add(room);
|
||||
user.setRoom(room);
|
||||
|
||||
sendMessage(session, "roomCreate", roomCode);
|
||||
SocketUtils.sendMessage(session, "roomCreate", roomCode);
|
||||
|
||||
System.err.println("[ROOM] Created room " + roomCode + " by " + user.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectRoom(WebSocketSession session, String roomCode) throws InterruptedException, IOException {
|
||||
if (roomCode == null) {
|
||||
sendMessage(session, "error", "bad command command");
|
||||
return;
|
||||
}
|
||||
if (!users.stream().anyMatch(user -> user.getSession().equals(session))) {
|
||||
sendMessage(session, "error", "need login");
|
||||
SocketUtils.sendMessage(session, "error", "bad command command");
|
||||
return;
|
||||
}
|
||||
|
||||
Optional<VRoom> oroom = rooms.stream().filter(room -> room.getCode().equals(roomCode)).findFirst();
|
||||
if (oroom.isEmpty()) {
|
||||
sendMessage(session, "error", "no room");
|
||||
SocketUtils.sendMessage(session, "error", "no room");
|
||||
return;
|
||||
}
|
||||
|
||||
VRoom room = oroom.get();
|
||||
VUser user = users.stream().filter(suser -> suser.getSession().equals(session)).findFirst().get();
|
||||
VUser user = UserUtils.getUser(session, users);
|
||||
if (user == null) return;
|
||||
|
||||
room.getUsers().add(user);
|
||||
user.setRoom(room);
|
||||
room.getUsers().add(user);
|
||||
|
||||
sendMessage(session, "roomConnect", "true");
|
||||
forwardMessage(session, "roomStatus", room.getRoomStatus());
|
||||
forwardMessage(session, "roomSettings", room.getRoomSettings());
|
||||
ArrayList<String> usersName = new ArrayList<String>();
|
||||
room.getUsers().forEach(userL -> usersName.add(userL.getName()));
|
||||
|
||||
JSONObject roomStatus = room.getRoomStatus();
|
||||
roomStatus.put("users", usersName);
|
||||
room.setRoomStatus(roomStatus);
|
||||
|
||||
SocketUtils.sendMessage(session, "roomConnect", "true");
|
||||
SocketUtils.forwardMessage(session, "roomSettings", room.getRoomSettings());
|
||||
SocketUtils.broadcast(room, "roomStatus", room.getRoomStatus());
|
||||
SocketUtils.forwardMessage(room.getAdmin().getSession(), "roomStatus", room.getRoomStatus());
|
||||
|
||||
System.err
|
||||
.println("[ROOM] Connection to room " + roomCode + " (" + room.getAdmin().getName() + ") by " + user.getName());
|
||||
@@ -129,127 +129,124 @@ public class RoomManager implements IRoomManager {
|
||||
|
||||
@Override
|
||||
public void leaveRoom(WebSocketSession session) throws InterruptedException, IOException {
|
||||
Optional<VUser> ouser = users.stream().filter(user -> user.getSession().equals(session)).findFirst();
|
||||
VUser user = UserUtils.getUser(session, users);
|
||||
if (user == null) return;
|
||||
|
||||
if (ouser.isEmpty()) {
|
||||
sendMessage(session, "error", "need login");
|
||||
return;
|
||||
}
|
||||
VRoom room = UserUtils.getRoom(session, user);
|
||||
if (room == null) return;
|
||||
|
||||
VUser user = ouser.get();
|
||||
|
||||
if (user.getRoom() == null) {
|
||||
sendMessage(session, "error", "no room");
|
||||
return;
|
||||
}
|
||||
|
||||
user.getRoom().getUsers().remove(user);
|
||||
room.getUsers().remove(user);
|
||||
user.setRoom(null);
|
||||
|
||||
ArrayList<String> usersName = new ArrayList<String>();
|
||||
room.getUsers().forEach(userL -> usersName.add(userL.getName()));
|
||||
|
||||
JSONObject roomStatus = room.getRoomStatus();
|
||||
roomStatus.put("users", usersName);
|
||||
room.setRoomStatus(roomStatus);
|
||||
|
||||
SocketUtils.broadcast(room, "roomStatus", room.getRoomStatus());
|
||||
SocketUtils.forwardMessage(room.getAdmin().getSession(), "roomStatus", room.getRoomStatus());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRoomStatus(WebSocketSession session, TextMessage message) throws InterruptedException, IOException {
|
||||
Optional<VUser> ouser = users.stream().filter(user -> user.getSession().equals(session)).findFirst();
|
||||
VUser user = UserUtils.getUser(session, users);
|
||||
if (user == null) return;
|
||||
|
||||
if (ouser.isEmpty()) {
|
||||
sendMessage(session, "error", "need login");
|
||||
return;
|
||||
}
|
||||
VRoom room = UserUtils.getRoom(session, user);
|
||||
if (room == null) return;
|
||||
|
||||
VUser user = ouser.get();
|
||||
|
||||
if (user.getRoom() == null) {
|
||||
sendMessage(session, "error", "no room");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!user.getRoom().getAdmin().equals(user)) {
|
||||
sendMessage(session, "error", "not admin");
|
||||
return;
|
||||
}
|
||||
if (!UserUtils.checkIsAdmin(session, user)) return;
|
||||
|
||||
String payload = message.getPayload();
|
||||
JSONObject jsonObject = new JSONObject(payload);
|
||||
|
||||
user.getRoom().setRoomStatus((JSONObject) jsonObject.get("message"));
|
||||
broadcast(user.getRoom(), "roomStatus", user.getRoom().getRoomStatus());
|
||||
ArrayList<String> usersName = new ArrayList<String>();
|
||||
room.getUsers().forEach(userL -> usersName.add(userL.getName()));
|
||||
|
||||
JSONObject roomStatus = (JSONObject) jsonObject.get("message");
|
||||
roomStatus.put("users", usersName);
|
||||
|
||||
room.setRoomStatus(roomStatus);
|
||||
SocketUtils.broadcast(room, "roomStatus", room.getRoomStatus());
|
||||
SocketUtils.forwardMessage(session, "roomStatus", room.getRoomStatus());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRoomSettings(WebSocketSession session, TextMessage message) throws InterruptedException, IOException {
|
||||
Optional<VUser> ouser = users.stream().filter(user -> user.getSession().equals(session)).findFirst();
|
||||
VUser user = UserUtils.getUser(session, users);
|
||||
if (user == null) return;
|
||||
|
||||
if (ouser.isEmpty()) {
|
||||
sendMessage(session, "error", "need login");
|
||||
return;
|
||||
}
|
||||
VRoom room = UserUtils.getRoom(session, user);
|
||||
if (room == null) return;
|
||||
|
||||
VUser user = ouser.get();
|
||||
|
||||
if (user.getRoom() == null) {
|
||||
sendMessage(session, "error", "no room");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!user.getRoom().getAdmin().equals(user)) {
|
||||
sendMessage(session, "error", "not admin");
|
||||
return;
|
||||
}
|
||||
if (!UserUtils.checkIsAdmin(session, user)) return;
|
||||
|
||||
String payload = message.getPayload();
|
||||
JSONObject jsonObject = new JSONObject(payload);
|
||||
|
||||
user.getRoom().setRoomSettings((JSONObject) jsonObject.get("message"));
|
||||
broadcast(user.getRoom(), "roomSettings", user.getRoom().getRoomSettings());
|
||||
}
|
||||
|
||||
public void sendMessage(WebSocketSession session, String type, String message) throws InterruptedException, IOException {
|
||||
session.sendMessage(new TextMessage("{\"type\":\"" + type + "\",\"message\": \"" + message.toString() + "\" }"));
|
||||
}
|
||||
|
||||
public void forwardMessage(WebSocketSession session, String type, JSONObject message) throws InterruptedException, IOException {
|
||||
session.sendMessage(new TextMessage("{\"type\":\"" + type + "\",\"message\": " + message.toString() + " }"));
|
||||
}
|
||||
|
||||
public void broadcast(VRoom room, String type, JSONObject message) {
|
||||
room.getUsers().forEach(user -> {
|
||||
try {
|
||||
forwardMessage(user.getSession(), type, message);
|
||||
} catch (InterruptedException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void sendServerInfos(WebSocketSession session) throws InterruptedException, IOException {
|
||||
session.sendMessage(new TextMessage(
|
||||
"{\"type\":\"serverInfos\",\"userCount\":\"" + users.size() + "\",\"roomCount\":\"" + rooms.size() + "\"}"));
|
||||
room.setRoomSettings((JSONObject) jsonObject.get("message"));
|
||||
SocketUtils.broadcast(room, "roomSettings", room.getRoomSettings());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forwardRoomMessage(WebSocketSession session, TextMessage message) throws InterruptedException, IOException {
|
||||
Optional<VUser> ouser = users.stream().filter(user -> user.getSession().equals(session)).findFirst();
|
||||
VUser user = UserUtils.getUser(session, users);
|
||||
if (user == null) return;
|
||||
|
||||
if (ouser.isEmpty()) {
|
||||
sendMessage(session, "error", "need login");
|
||||
return;
|
||||
}
|
||||
|
||||
VUser user = ouser.get();
|
||||
|
||||
if (user.getRoom() == null) {
|
||||
sendMessage(session, "error", "no room");
|
||||
return;
|
||||
}
|
||||
VRoom room = UserUtils.getRoom(session, user);
|
||||
if (room == null) return;
|
||||
|
||||
String payload = message.getPayload();
|
||||
JSONObject jsonObject = new JSONObject(payload);
|
||||
String type = (String) jsonObject.get("type");
|
||||
JSONObject data = (JSONObject) jsonObject.get("message");
|
||||
|
||||
if (user.getRoom().getAdmin().equals(user)) broadcast(user.getRoom(), type, data);
|
||||
else forwardMessage(user.getRoom().getAdmin().getSession(), type, data);
|
||||
if (room.getAdmin().equals(user)) SocketUtils.broadcast(room, type, data);
|
||||
else SocketUtils.forwardMessage(room.getAdmin().getSession(), type, data);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kickUser(WebSocketSession session, String username) throws InterruptedException, IOException {
|
||||
VUser user = UserUtils.getUser(session, users);
|
||||
if (user == null) return;
|
||||
|
||||
VRoom room = UserUtils.getRoom(session, user);
|
||||
if (room == null) return;
|
||||
|
||||
if (!UserUtils.checkIsAdmin(session, user)) return;
|
||||
|
||||
if (username.isEmpty()) {
|
||||
SocketUtils.sendMessage(session, "error", "bad command command");
|
||||
return;
|
||||
}
|
||||
|
||||
Optional<VUser> ouserToKick = room.getUsers().stream().filter(userL -> userL.getName().equals(username)).findFirst();
|
||||
|
||||
if (!ouserToKick.isPresent()) return;
|
||||
|
||||
VUser userToKick = ouserToKick.get();
|
||||
|
||||
room.getUsers().remove(userToKick);
|
||||
|
||||
SocketUtils.sendMessage(userToKick.getSession(), "roomUserKick", "true");
|
||||
|
||||
SocketUtils.broadcast(room, "roomStatus", room.getRoomStatus());
|
||||
SocketUtils.forwardMessage(session, "roomStatus", room.getRoomStatus());
|
||||
}
|
||||
|
||||
public void sendServerInfos(WebSocketSession session) throws InterruptedException, IOException {
|
||||
ServerStatus status = new ServerStatus();
|
||||
status.setRoomCount(rooms.size());
|
||||
status.setUserCount(users.size());
|
||||
status.setRooms(new ArrayList<String>());
|
||||
|
||||
rooms.forEach(room -> {
|
||||
if(room.isPublic()) status.getRooms().add(room.getCode());
|
||||
});
|
||||
|
||||
SocketUtils.forwardMessage(session, "serverInfos", new JSONObject(status));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package gltronic.voozik.business;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import gltronic.voozik.model.VRoom;
|
||||
|
||||
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(VRoom room, String type, JSONObject message) {
|
||||
room.getUsers().forEach(user -> {
|
||||
forwardMessage(user.getSession(), type, message);
|
||||
});
|
||||
}
|
||||
}
|
||||
38
server/src/main/java/gltronic/voozik/business/UserUtils.java
Normal file
38
server/src/main/java/gltronic/voozik/business/UserUtils.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package gltronic.voozik.business;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import gltronic.voozik.model.VRoom;
|
||||
import gltronic.voozik.model.VUser;
|
||||
|
||||
public class UserUtils {
|
||||
public static VUser getUser (WebSocketSession session, ArrayList<VUser> users) {
|
||||
Optional<VUser> ouser = users.stream().filter(user -> user.getSession().equals(session)).findFirst();
|
||||
|
||||
if (ouser.isEmpty()) {
|
||||
SocketUtils.sendMessage(session, "error", "need login");
|
||||
return null;
|
||||
}
|
||||
|
||||
return ouser.get();
|
||||
}
|
||||
|
||||
public static VRoom getRoom (WebSocketSession session, VUser user) {
|
||||
if (user.getRoom() == null) {
|
||||
SocketUtils.sendMessage(session, "error", "no room");
|
||||
return null;
|
||||
}
|
||||
else return user.getRoom();
|
||||
}
|
||||
|
||||
public static boolean checkIsAdmin (WebSocketSession session, VUser user) {
|
||||
if (!user.getRoom().getAdmin().equals(user)) {
|
||||
SocketUtils.sendMessage(session, "error", "not admin");
|
||||
return false;
|
||||
}
|
||||
else return true;
|
||||
}
|
||||
}
|
||||
18
server/src/main/java/gltronic/voozik/model/ServerStatus.java
Normal file
18
server/src/main/java/gltronic/voozik/model/ServerStatus.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package gltronic.voozik.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class ServerStatus implements Serializable{
|
||||
private static final long serialVersionUID = 1L;
|
||||
int userCount;
|
||||
int roomCount;
|
||||
List<String> rooms;
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import lombok.Setter;
|
||||
@NoArgsConstructor
|
||||
public class VRoom {
|
||||
private String code;
|
||||
private String name;
|
||||
private boolean isPublic;
|
||||
private JSONObject roomStatus;
|
||||
private JSONObject roomSettings;
|
||||
|
||||
@@ -13,6 +13,7 @@ import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
||||
|
||||
import gltronic.voozik.business.IRoomManager;
|
||||
import gltronic.voozik.business.SocketUtils;
|
||||
|
||||
@Component
|
||||
public class SocketHandler extends TextWebSocketHandler {
|
||||
@@ -55,6 +56,9 @@ public class SocketHandler extends TextWebSocketHandler {
|
||||
case "roomSettings":
|
||||
roomManager.setRoomSettings(session, message);
|
||||
break;
|
||||
case "roomUserKick":
|
||||
roomManager.kickUser(session, (String) jsonObject.get("name"));
|
||||
break;
|
||||
case "roomVote":
|
||||
case "roomUserCommand":
|
||||
roomManager.forwardRoomMessage(session, message);
|
||||
@@ -64,7 +68,7 @@ public class SocketHandler extends TextWebSocketHandler {
|
||||
case "alive":
|
||||
break;
|
||||
default:
|
||||
roomManager.sendMessage(session, "error", "unknow command");
|
||||
SocketUtils.sendMessage(session, "error", "unknow command");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user