Преглед на файлове

Merge branch 'dev' of sunkean/raex_mmo into master

sunkean преди 2 години
родител
ревизия
faf93a00e6

+ 4 - 0
src/main/java/com/izouma/meta/config/Constants.java

@@ -37,6 +37,10 @@ public interface Constants {
 
     String META_WEBSOCKET_NOTICE_LIVE = "META_WEBSOCKET_NOTICE_LIVE";
 
+    String PUBLIC_CHAT = "PUBLIC_CHAT";
+
+    String PRIVATE_CHAT = "PRIVATE_CHAT";
+
     interface MetaRestCode {
 
         int success = 200;

+ 43 - 12
src/main/java/com/izouma/meta/domain/PublicScreenChat.java

@@ -2,6 +2,7 @@ package com.izouma.meta.domain;
 
 import com.alibaba.excel.annotation.ExcelIgnore;
 import com.alibaba.excel.annotation.ExcelProperty;
+import com.izouma.meta.enums.ChatType;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
@@ -9,6 +10,8 @@ import lombok.Data;
 import lombok.NoArgsConstructor;
 
 import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
 import javax.persistence.Transient;
 import java.time.LocalDateTime;
 
@@ -19,28 +22,28 @@ import java.time.LocalDateTime;
 @ApiModel("元宇宙用户聊天信息")
 public class PublicScreenChat extends BaseEntity {
 
-    @ApiModelProperty("发送方昵称")
-    @ExcelProperty("发送方昵称")
+    @ApiModelProperty("消息发送方昵称")
+    @ExcelProperty("消息发送方昵称")
     private String nickname;
 
-    @ApiModelProperty("发送方用户id")
-    @ExcelProperty("发送方用户id")
+    @ApiModelProperty("消息发送方用户id")
+    @ExcelProperty("消息发送方用户id")
     private String userId;
 
-    @ApiModelProperty("勋章等级")
-    @ExcelProperty("勋章等级")
+    @ApiModelProperty("消息发送方勋章等级")
+    @ExcelProperty("消息发放送勋章等级")
     private int level;
 
-    @ApiModelProperty("境界")
-    @ExcelProperty("境界")
+    @ApiModelProperty("消息发送方境界")
+    @ExcelProperty("消息发送方境界")
     private String realm;
 
-    @ApiModelProperty("头衔")
-    @ExcelProperty("头衔")
+    @ApiModelProperty("消息发送方头衔")
+    @ExcelProperty("消息发送方头衔")
     private String title;
 
-    @ApiModelProperty("头像")
-    @ExcelProperty("头像")
+    @ApiModelProperty("消息发送方头像")
+    @ExcelProperty("消息发送方头像")
     private String avatar;
 
     @ApiModelProperty("消息内容")
@@ -69,4 +72,32 @@ public class PublicScreenChat extends BaseEntity {
 
     @Transient
     private boolean myself;
+
+    @ApiModelProperty("消息类型")
+    @Enumerated(EnumType.STRING)
+    private ChatType chatType;
+
+    @ApiModelProperty("消息接受方昵称")
+    @ExcelProperty("消息接受方昵称")
+    private String toUserNickname;
+
+    @ApiModelProperty("消息接收方用户id")
+    @ExcelProperty("消息接收方用户id")
+    private Long toUserId;
+
+    @ApiModelProperty("消息接收方勋章等级")
+    @ExcelProperty("消息接收方勋章等级")
+    private int toUserLevel;
+
+    @ApiModelProperty("消息接收方境界")
+    @ExcelProperty("消息接收方境界")
+    private String toUserRealm;
+
+    @ApiModelProperty("消息接收方头衔")
+    @ExcelProperty("消息接收方头衔")
+    private String toUserTitle;
+
+    @ApiModelProperty("消息接收方头像")
+    @ExcelProperty("消息接收方头像")
+    private String toUserAvatar;
 }

+ 19 - 0
src/main/java/com/izouma/meta/enums/ChatType.java

@@ -0,0 +1,19 @@
+package com.izouma.meta.enums;
+
+public enum ChatType {
+
+    DEFAULT("默认"),
+    PUBLIC("公频聊天"),
+
+    PRIVATE("私聊");
+
+    private final String description;
+
+    ChatType(String description) {
+        this.description = description;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+}

+ 88 - 8
src/main/java/com/izouma/meta/websocket/PublicScreenChatWebsocket.java

@@ -7,6 +7,7 @@ import com.izouma.meta.domain.PublicScreenChat;
 import com.izouma.meta.dto.PublicScreenChatExceptionMsg;
 import com.izouma.meta.dto.PurchaseLevelDTO;
 import com.izouma.meta.dto.WebsocketUser;
+import com.izouma.meta.enums.ChatType;
 import com.izouma.meta.exception.BusinessException;
 import com.izouma.meta.repo.PublicScreenChatRepo;
 import com.izouma.meta.service.ContentAuditService;
@@ -74,7 +75,7 @@ public class PublicScreenChatWebsocket {
         String format = String.format("玩家[%S][%S]进入大厅", userId, nickName);
         PublicScreenChat publicScreenChat;
         try {
-            publicScreenChat = savePublicScreenChat(userId, format, true);
+            publicScreenChat = savePublicScreenChat(userId, format, true, ChatType.DEFAULT, null);
         } catch (Exception e) {
             String errMsg = String.format("玩家进入大厅,保存信息发生异常[%S]", e);
             exceptionHandle(userId, new PublicScreenChatExceptionMsg(2, errMsg));
@@ -97,7 +98,7 @@ public class PublicScreenChatWebsocket {
         String format = String.format("玩家[%S][%S]离开大厅", userId, nickName);
         PublicScreenChat publicScreenChat;
         try {
-            publicScreenChat = savePublicScreenChat(userId, format, true);
+            publicScreenChat = savePublicScreenChat(userId, format, true, ChatType.DEFAULT, null);
         } catch (Exception e) {
             String errMsg = String.format("玩家离开大厅,保存信息发生异常[%S]", e);
             exceptionHandle(userId, new PublicScreenChatExceptionMsg(2, errMsg));
@@ -121,20 +122,61 @@ public class PublicScreenChatWebsocket {
             return;
         }
         log.info(String.format("收到来自userId[%S]的消息[%S]", userId, message));
-        if (!contentAuditService.auditText(message)) {
-            savePublicScreenChat(userId, message, false);
+        JSONObject jsonObject;
+        try {
+            jsonObject = JSON.parseObject(message);
+        } catch (Exception e) {
+            String errMsg = String.format("json parse throw exception [%S]", e.getMessage());
+            log.error(errMsg);
+            return;
+        }
+        if (Objects.isNull(jsonObject)) {
+            log.error("Illegal parameter : jsonObject can not be null");
+            return;
+        }
+        String type = jsonObject.getString("type");
+        if (StringUtils.isBlank(type)) {
+            log.error("Illegal parameter : type can not be null");
+            return;
+        }
+        String msg = jsonObject.getString("msg");
+        String toUserId = jsonObject.getString("toUserId");
+        PublicScreenChat publicScreenChat = new PublicScreenChat();
+        if (Constants.PRIVATE_CHAT.equals(type)) {
+            if (StringUtils.isBlank(toUserId)) {
+                log.error("Illegal parameter : toUserId can not be null");
+                return;
+            }
+        }
+        if (!contentAuditService.auditText(msg)) {
+            if (Constants.PUBLIC_CHAT.equals(type)) {
+                savePublicScreenChat(userId, message, false, ChatType.PUBLIC, null);
+            }
+            if (Constants.PRIVATE_CHAT.equals(type)) {
+                savePublicScreenChat(userId, message, false, ChatType.PRIVATE, toUserId);
+            }
             exceptionHandle(userId, new PublicScreenChatExceptionMsg(3, "消息包含非法内容"));
             return;
         }
-        PublicScreenChat publicScreenChat;
         try {
-            publicScreenChat = savePublicScreenChat(userId, message, true);
+            if (Constants.PUBLIC_CHAT.equals(type)) {
+                publicScreenChat = savePublicScreenChat(userId, msg, true, ChatType.PUBLIC, null);
+            }
+            if (Constants.PRIVATE_CHAT.equals(type)) {
+                publicScreenChat = savePublicScreenChat(userId, msg, true, ChatType.PRIVATE, toUserId);
+            }
         } catch (Exception e) {
             String errMsg = String.format("玩家发送消息,保存信息发生异常[%S]", e);
             exceptionHandle(userId, new PublicScreenChatExceptionMsg(2, errMsg));
             return;
         }
-        sendMessageToAll(clients, JSON.toJSONString(publicScreenChat), PREFIX.concat(userId));
+        if (Constants.PUBLIC_CHAT.equals(type)) {
+            sendMessageToAll(clients, JSON.toJSONString(publicScreenChat), PREFIX.concat(userId));
+        }
+        if (Constants.PRIVATE_CHAT.equals(type)) {
+            websocketCommon.sendMessageTo(clients, JSON.toJSONString(publicScreenChat), PREFIX.concat(toUserId));
+        }
+
     }
 
     /**
@@ -157,7 +199,7 @@ public class PublicScreenChatWebsocket {
         });
     }
 
-    private PublicScreenChat savePublicScreenChat(String userId, String messageInfo, boolean illegal) {
+    private PublicScreenChat savePublicScreenChat(String userId, String messageInfo, boolean illegal, ChatType chatType, String toUserId) {
         String resultForWebsocketUser = websocketCommon.getRequest("/user/websocket/".concat(userId));
         log.info(String.format("userId:[%S] userInfo:[%S]", userId, resultForWebsocketUser));
         WebsocketUser websocketUser;
@@ -198,6 +240,44 @@ public class PublicScreenChatWebsocket {
         publicScreenChat.setIllegal(illegal);
         publicScreenChat.setRecall(1);
         publicScreenChat.setType(1);
+        publicScreenChat.setChatType(chatType);
+        if (!ChatType.PRIVATE.equals(chatType)) {
+            return publicScreenChatRepo.save(publicScreenChat);
+        }
+        String resultForWebsocketToUser = websocketCommon.getRequest("/user/websocket/".concat(toUserId));
+        log.info(String.format("toUserId:[%S] toUserInfo:[%S]", toUserId, resultForWebsocketToUser));
+        WebsocketUser websocketToUser;
+        try {
+            websocketToUser = JSONObject.parseObject(resultForWebsocketToUser, WebsocketUser.class);
+        } catch (Exception e) {
+            String errMsg = String.format("WebsocketUser JSON parse throw exception:[%S]", e);
+            log.error(errMsg);
+            throw new BusinessException(errMsg);
+        }
+        if (Objects.isNull(websocketToUser)) {
+            String errMsg = "用户信息不存在";
+            log.error(errMsg);
+            throw new BusinessException(errMsg);
+        }
+        publicScreenChat.setToUserAvatar(websocketToUser.getAvatar());
+        publicScreenChat.setToUserNickname(websocketToUser.getNickname());
+        publicScreenChat.setToUserLevel(websocketToUser.getLevel());
+        String resultForToUserPurchaseLevel = websocketCommon.getRequest("/purchaseLevel/websocket/".concat(String.valueOf(websocketToUser.getLevel())));
+        log.info(String.format("toUserId:[%S] toUserPurchaseLevelInfo:[%S]", toUserId, resultForToUserPurchaseLevel));
+        PurchaseLevelDTO toUserPurchaseLevelDTO;
+        try {
+            toUserPurchaseLevelDTO = JSONObject.parseObject(resultForToUserPurchaseLevel, PurchaseLevelDTO.class);
+        } catch (Exception e) {
+            String errMsg = String.format("toUserPurchaseLevelDTO JSON parse throw exception:[%S]", e);
+            log.error(errMsg);
+            throw new BusinessException(errMsg);
+        }
+        if (Objects.isNull(toUserPurchaseLevelDTO)) {
+            throw new BusinessException("用户等级称号信息不存在");
+        }
+        publicScreenChat.setToUserRealm(toUserPurchaseLevelDTO.getRealm());
+        publicScreenChat.setToUserTitle(toUserPurchaseLevelDTO.getTitle());
+        publicScreenChat.setToUserId(Long.parseLong(toUserId));
         return publicScreenChatRepo.save(publicScreenChat);
     }