|
@@ -1,13 +1,16 @@
|
|
|
package com.zanxiang.game.module.manage.service.impl;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.zanxiang.game.module.manage.enums.KfRoomMsgOwnerEnum;
|
|
|
import com.zanxiang.game.module.manage.enums.KfRoomMsgTypeEnum;
|
|
|
+import com.zanxiang.game.module.manage.enums.KfWebSocketMsgEnum;
|
|
|
import com.zanxiang.game.module.manage.pojo.dto.KfAppletMsgDTO;
|
|
|
-import com.zanxiang.game.module.manage.pojo.dto.KfMsgDTO;
|
|
|
-import com.zanxiang.game.module.manage.service.IKfRoomMsgService;
|
|
|
+import com.zanxiang.game.module.manage.pojo.dto.KfWebSocketMsgDTO;
|
|
|
+import com.zanxiang.game.module.manage.service.*;
|
|
|
import com.zanxiang.game.module.manage.websocket.KfMsgWebsocketHandler;
|
|
|
-import com.zanxiang.game.module.mybatis.entity.KfRoomMsg;
|
|
|
+import com.zanxiang.game.module.mybatis.entity.*;
|
|
|
import com.zanxiang.game.module.mybatis.mapper.KfRoomMsgMapper;
|
|
|
import com.zanxiang.module.util.JsonUtil;
|
|
|
import com.zanxiang.module.util.bean.BeanUtil;
|
|
@@ -16,8 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
/**
|
|
@@ -29,49 +31,112 @@ import java.util.Objects;
|
|
|
@Service
|
|
|
public class KfRoomMsgServiceImpl extends ServiceImpl<KfRoomMsgMapper, KfRoomMsg> implements IKfRoomMsgService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IKfSessionUserService kfSessionUserService;
|
|
|
|
|
|
@Autowired
|
|
|
private KfMsgWebsocketHandler kfMsgWebsocketHandler;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IKfRoomService kfRoomService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IGameAppletService gameAppletService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IUserService userService;
|
|
|
+
|
|
|
@Override
|
|
|
public void appletMsg(String postData) {
|
|
|
log.error("接收到SDK转发的小程序消息, postData : {}", postData);
|
|
|
KfAppletMsgDTO kfAppletMsgDTO = JsonUtil.toObj(postData, KfAppletMsgDTO.class);
|
|
|
+ GameApplet gameApplet = gameAppletService.getOne(new LambdaQueryWrapper<GameApplet>()
|
|
|
+ .eq(GameApplet::getGhId, kfAppletMsgDTO.getToUserName()));
|
|
|
//用户进入会话事件
|
|
|
if (Objects.equals(kfAppletMsgDTO.getMsgType(), KfAppletMsgDTO.MSG_TYPE_EVENT)
|
|
|
&& Objects.equals(kfAppletMsgDTO.getEvent(), KfAppletMsgDTO.EVENT_USER_ENTER_TEMP_SESSION)) {
|
|
|
- //创建用户信息
|
|
|
+ KfSessionUser kfSessionUser = kfSessionUserService.getById(kfAppletMsgDTO.getFromUserName());
|
|
|
+ if (kfSessionUser != null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, kfAppletMsgDTO.getFromUserName()));
|
|
|
+ //创建保存用户信息
|
|
|
+ kfSessionUserService.save(this.transform(kfAppletMsgDTO, gameApplet, user));
|
|
|
return;
|
|
|
}
|
|
|
//非玩家消息, 不做处理
|
|
|
if (kfAppletMsgDTO.getMsgId() == null) {
|
|
|
return;
|
|
|
}
|
|
|
+ //查询玩家的房间连接状态
|
|
|
+ KfRoom kfRoom = kfRoomService.getOne(new LambdaQueryWrapper<KfRoom>()
|
|
|
+ .eq(KfRoom::getOpenId, kfAppletMsgDTO.getFromUserName())
|
|
|
+ .eq(KfRoom::getOnline, Boolean.TRUE));
|
|
|
//消息存储
|
|
|
- KfRoomMsg kfRoomMsg = KfRoomMsg.builder()
|
|
|
+ KfWebSocketMsgDTO.MsgContentBean msgContent = this.getMsgContent(kfAppletMsgDTO);
|
|
|
+ KfRoomMsg kfRoomMsg = this.transform(kfAppletMsgDTO, gameApplet, kfRoom, postData, msgContent);
|
|
|
+ super.save(kfRoomMsg);
|
|
|
+ //玩家状态更新为待接入状态
|
|
|
+ if (kfRoom == null) {
|
|
|
+ kfSessionUserService.update(new LambdaUpdateWrapper<KfSessionUser>()
|
|
|
+ .set(KfSessionUser::getIsWait, Boolean.TRUE)
|
|
|
+ .set(KfSessionUser::getWaitStartTime, LocalDateTime.now())
|
|
|
+ .set(KfSessionUser::getUpdateTime, LocalDateTime.now())
|
|
|
+ .eq(KfSessionUser::getOpenId, kfAppletMsgDTO.getFromUserName()));
|
|
|
+ }
|
|
|
+ //消息转发到redis频道
|
|
|
+ kfMsgWebsocketHandler.pushMessage(this.transform(kfRoomMsg, msgContent));
|
|
|
+ }
|
|
|
+
|
|
|
+ private KfWebSocketMsgDTO transform(KfRoomMsg kfRoomMsg, KfWebSocketMsgDTO.MsgContentBean msgContent) {
|
|
|
+ //todo : 需要修改
|
|
|
+ KfWebSocketMsgDTO.RoomMsgBean roomMsgBean = BeanUtil.copy(kfRoomMsg, KfWebSocketMsgDTO.RoomMsgBean.class);
|
|
|
+ roomMsgBean.setContent(msgContent);
|
|
|
+ KfWebSocketMsgEnum kfWebSocketMsgEnum = roomMsgBean.getRoomId() == null ?
|
|
|
+ KfWebSocketMsgEnum.WEBSOCKET_MSG_WAIT_LIST : KfWebSocketMsgEnum.WEBSOCKET_MSG_ROOM_MSG;
|
|
|
+ return KfWebSocketMsgDTO.builder()
|
|
|
+ .webSocketMsgType(kfWebSocketMsgEnum)
|
|
|
+ .roomMsgList(Collections.singletonList(roomMsgBean))
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private KfSessionUser transform(KfAppletMsgDTO kfAppletMsgDTO, GameApplet gameApplet, User user) {
|
|
|
+ return KfSessionUser.builder()
|
|
|
+ .openId(kfAppletMsgDTO.getFromUserName())
|
|
|
+ .gameId(gameApplet.getGameId())
|
|
|
+ .userId(user == null ? null : user.getId())
|
|
|
+ .isWait(Boolean.FALSE)
|
|
|
+ .createTime(LocalDateTime.now())
|
|
|
+ .updateTime(LocalDateTime.now())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private KfRoomMsg transform(KfAppletMsgDTO kfAppletMsgDTO, GameApplet gameApplet, KfRoom kfRoom, String postData,
|
|
|
+ KfWebSocketMsgDTO.MsgContentBean msgContent) {
|
|
|
+ return KfRoomMsg.builder()
|
|
|
.msgId(String.valueOf(kfAppletMsgDTO.getMsgId()))
|
|
|
.msgType(kfAppletMsgDTO.getMsgType())
|
|
|
+ .gameId(gameApplet.getGameId())
|
|
|
+ .openId(kfAppletMsgDTO.getFromUserName())
|
|
|
.readStatus(Boolean.FALSE)
|
|
|
+ .roomId(kfRoom == null ? null : kfRoom.getId())
|
|
|
.msgOwner(KfRoomMsgOwnerEnum.KF_MSG_OWNER_USER.getValue())
|
|
|
- .content(this.getMsgContent(kfAppletMsgDTO))
|
|
|
+ .content(JsonUtil.toString(msgContent))
|
|
|
.source(postData)
|
|
|
.createTime(LocalDateTime.now())
|
|
|
.build();
|
|
|
- super.save(kfRoomMsg);
|
|
|
- // 消息转发到redis频道
|
|
|
- kfMsgWebsocketHandler.pushMessage(JsonUtil.toString(BeanUtil.copy(kfRoomMsg, KfMsgDTO.class)));
|
|
|
}
|
|
|
|
|
|
- private String getMsgContent(KfAppletMsgDTO kfAppletMsgDTO) {
|
|
|
- Map<String, String> contentMap = new HashMap<>(2);
|
|
|
+ private KfWebSocketMsgDTO.MsgContentBean getMsgContent(KfAppletMsgDTO kfAppletMsgDTO) {
|
|
|
+ //todo : 这里要考虑图片是否要传到oss上, 用自己的网络地址, 腾讯的图片地址不能保证一直有效
|
|
|
+ KfWebSocketMsgDTO.MsgContentBean msgContentBean = new KfWebSocketMsgDTO.MsgContentBean();
|
|
|
if (Objects.equals(KfRoomMsgTypeEnum.KF_MSG_TYPE_TEXT.getValue(), kfAppletMsgDTO.getMsgType())) {
|
|
|
- contentMap.put("text", kfAppletMsgDTO.getContent());
|
|
|
+ msgContentBean.setText(kfAppletMsgDTO.getContent());
|
|
|
}
|
|
|
if (Objects.equals(KfRoomMsgTypeEnum.KF_MSG_TYPE_IMAGE.getValue(), kfAppletMsgDTO.getMsgType())) {
|
|
|
- contentMap.put("picUrl", kfAppletMsgDTO.getPicUrl());
|
|
|
- contentMap.put("mediaId", kfAppletMsgDTO.getMediaId());
|
|
|
+ msgContentBean.setImage(kfAppletMsgDTO.getPicUrl());
|
|
|
}
|
|
|
- return JsonUtil.toString(contentMap);
|
|
|
+ return msgContentBean;
|
|
|
}
|
|
|
|
|
|
}
|