|
@@ -67,7 +67,7 @@ public class MMOWebSocket {
|
|
|
init();
|
|
init();
|
|
|
// 判断当前玩家是否在其他地方登陆
|
|
// 判断当前玩家是否在其他地方登陆
|
|
|
if (clients.containsKey(Constants.REDIS_PREFIX.concat(userId))) {
|
|
if (clients.containsKey(Constants.REDIS_PREFIX.concat(userId))) {
|
|
|
- log.info(String.format("当前玩家[%S]已经在别处登陆,sessionId为[%S]", userId, session.getId()));
|
|
|
|
|
|
|
+ log.info(String.format("当前玩家: %s 已经在别处登陆, sessionId为: %s", userId, session.getId()));
|
|
|
// 关闭连接
|
|
// 关闭连接
|
|
|
MMOSingleMessage mmoSingleMessage = new MMOSingleMessage();
|
|
MMOSingleMessage mmoSingleMessage = new MMOSingleMessage();
|
|
|
mmoSingleMessage.setMessageType(6);
|
|
mmoSingleMessage.setMessageType(6);
|
|
@@ -94,14 +94,14 @@ public class MMOWebSocket {
|
|
|
MMOMessage mmoMessage = new MMOMessage();
|
|
MMOMessage mmoMessage = new MMOMessage();
|
|
|
mmoMessage.setMessageType(5);
|
|
mmoMessage.setMessageType(5);
|
|
|
mmoMessage.setMessage(save);
|
|
mmoMessage.setMessage(save);
|
|
|
- log.info(String.format("通知玩家[%S],本次登陆信息[%S]", userId, JSON.toJSONString(mmoMessage)));
|
|
|
|
|
|
|
+ log.info(String.format("通知玩家: %s, 本次登陆信息: %s", userId, JSON.toJSONString(mmoMessage)));
|
|
|
websocketCommon.sendMessageTo(clients, JSON.toJSONString(mmoMessage), Constants.REDIS_PREFIX.concat(userId));
|
|
websocketCommon.sendMessageTo(clients, JSON.toJSONString(mmoMessage), Constants.REDIS_PREFIX.concat(userId));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@OnError
|
|
@OnError
|
|
|
public void onError(Session session, Throwable error) {
|
|
public void onError(Session session, Throwable error) {
|
|
|
// 异常处理
|
|
// 异常处理
|
|
|
- log.error(String.format("sessionId[%S]的服务端发生了错误:[%S]", session.getId(), error.getMessage()));
|
|
|
|
|
|
|
+ log.error(String.format("sessionId: %s 的服务端发生了错误, 错误信息为: %s", session.getId(), error.getMessage()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@OnClose
|
|
@OnClose
|
|
@@ -126,7 +126,7 @@ public class MMOWebSocket {
|
|
|
}
|
|
}
|
|
|
MetaMMOLoginInfo dbMetaMMOLoginInfo = metaMMOLoginInfoRepo.findById(metaMMOLoginInfo.getId()).orElse(null);
|
|
MetaMMOLoginInfo dbMetaMMOLoginInfo = metaMMOLoginInfoRepo.findById(metaMMOLoginInfo.getId()).orElse(null);
|
|
|
if (Objects.isNull(dbMetaMMOLoginInfo)) {
|
|
if (Objects.isNull(dbMetaMMOLoginInfo)) {
|
|
|
- log.error(String.format("数据库中不存在id[%S]的记录", metaMMOLoginInfo.getId()));
|
|
|
|
|
|
|
+ log.error(String.format("数据库中不存在id: %s 的记录", metaMMOLoginInfo.getId()));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
ObjUtils.merge(dbMetaMMOLoginInfo, metaMMOLoginInfo);
|
|
ObjUtils.merge(dbMetaMMOLoginInfo, metaMMOLoginInfo);
|
|
@@ -147,7 +147,7 @@ public class MMOWebSocket {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
if (Constants.HEART_RECEIVE.equals(message)) {
|
|
if (Constants.HEART_RECEIVE.equals(message)) {
|
|
|
- log.info(String.format("sessionId:[%S] userId:[%S] 连接正常", session.getId(), userId));
|
|
|
|
|
|
|
+ log.info(String.format("sessionId: %s , userId: %s 连接正常", session.getId(), userId));
|
|
|
websocketCommon.sendMessageTo(clients, Constants.HEART_RETURN, Constants.REDIS_PREFIX.concat(userId));
|
|
websocketCommon.sendMessageTo(clients, Constants.HEART_RETURN, Constants.REDIS_PREFIX.concat(userId));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -158,34 +158,46 @@ public class MMOWebSocket {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
int type = Integer.parseInt(jsonObject.getString("type"));
|
|
int type = Integer.parseInt(jsonObject.getString("type"));
|
|
|
|
|
+ // type = 9 广播物体坐标信息
|
|
|
if (type == 9) {
|
|
if (type == 9) {
|
|
|
- List<MetaObjectMove> metaObjectMoves = metaObjectMoveRepo.findAllByDelAndRun(false, true);
|
|
|
|
|
- if (CollectionUtils.isEmpty(metaObjectMoves)) {
|
|
|
|
|
|
|
+ log.info(String.format("来自客户端消息: %s", message));
|
|
|
|
|
+ Long objectId;
|
|
|
|
|
+ try {
|
|
|
|
|
+ objectId = Long.parseLong(jsonObject.getString("objectId"));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error(String.format("objectId parse to long throw exception: %s", e.getMessage()));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- for (MetaObjectMove metaObjectMove : metaObjectMoves) {
|
|
|
|
|
- String cityId = String.valueOf(metaObjectMove.getCityId());
|
|
|
|
|
- String regionId = String.valueOf(metaObjectMove.getRegionId());
|
|
|
|
|
- String key = cityId.concat(":").concat(regionId);
|
|
|
|
|
- List<String> otherUserIds = remove(key, userId);
|
|
|
|
|
- MetaObjectMoveCoordinate metaObjectMoveCoordinate = null;
|
|
|
|
|
- try {
|
|
|
|
|
- String str = websocketCommon.getRequest("/metaObjectMove/".concat(String.valueOf(metaObjectMove.getObjectId())).concat("/queryCoordinate"));
|
|
|
|
|
- metaObjectMoveCoordinate = JSONObject.parseObject(str, MetaObjectMoveCoordinate.class);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- String errMsg = String.format("MetaObjectMoveCoordinate JSON parse throw exception:[%S]", e);
|
|
|
|
|
- log.error(errMsg);
|
|
|
|
|
- }
|
|
|
|
|
- if (Objects.isNull(metaObjectMoveCoordinate)) {
|
|
|
|
|
- log.error(String.format("物体[%S]坐标不存在", metaObjectMove.getObjectId()));
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- buildMessageForSendingToAllOther(otherUserIds, 9, metaObjectMoveCoordinate);
|
|
|
|
|
|
|
+ MetaObjectMove metaObjectMove = metaObjectMoveRepo.findByObjectIdAndDel(objectId, false);
|
|
|
|
|
+ if (Objects.isNull(metaObjectMove)) {
|
|
|
|
|
+ log.error(String.format("物体: %s 不存在", objectId));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!metaObjectMove.isRun()) {
|
|
|
|
|
+ log.error(String.format("物体: %s 已经停运", objectId));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ String cityId = String.valueOf(metaObjectMove.getCityId());
|
|
|
|
|
+ String regionId = String.valueOf(metaObjectMove.getRegionId());
|
|
|
|
|
+ String key = cityId.concat(":").concat(regionId);
|
|
|
|
|
+ List<String> otherUserIds = remove(key, userId);
|
|
|
|
|
+ MetaObjectMoveCoordinate metaObjectMoveCoordinate;
|
|
|
|
|
+ try {
|
|
|
|
|
+ String str = websocketCommon.getRequest("/metaObjectMove/".concat(String.valueOf(metaObjectMove.getObjectId())).concat("/queryCoordinate"));
|
|
|
|
|
+ metaObjectMoveCoordinate = JSONObject.parseObject(str, MetaObjectMoveCoordinate.class);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ String errMsg = String.format("MetaObjectMoveCoordinate JSON parse throw exception: %s", e);
|
|
|
|
|
+ log.error(errMsg);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- ;
|
|
|
|
|
|
|
+ if (Objects.isNull(metaObjectMoveCoordinate)) {
|
|
|
|
|
+ log.error(String.format("物体: %s 坐标信息不存在", metaObjectMove.getObjectId()));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ buildMessageForSendingToAllOther(otherUserIds, 9, metaObjectMoveCoordinate);
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
- log.info("来自客户端消息:" + message + "客户端的id是:" + session.getId());
|
|
|
|
|
|
|
+ log.info(String.format("来自客户端消息:%s 客户端的id是:%s", message, session.getId()));
|
|
|
String cityId = jsonObject.getString("cityId");
|
|
String cityId = jsonObject.getString("cityId");
|
|
|
String regionId = jsonObject.getString("regionId");
|
|
String regionId = jsonObject.getString("regionId");
|
|
|
String key = cityId.concat(":").concat(regionId);
|
|
String key = cityId.concat(":").concat(regionId);
|
|
@@ -194,7 +206,7 @@ public class MMOWebSocket {
|
|
|
List<MetaMMOLoginInfo> metaMMOLoginInfos = redisTemplate.opsForValue().multiGet(otherUserIds);
|
|
List<MetaMMOLoginInfo> metaMMOLoginInfos = redisTemplate.opsForValue().multiGet(otherUserIds);
|
|
|
switch (type) {
|
|
switch (type) {
|
|
|
case 1:
|
|
case 1:
|
|
|
- log.info("当前操作类型为1 -> 玩家进入地图");
|
|
|
|
|
|
|
+ log.info(String.format("当前操作类型为: %s -> 玩家进入地图", type));
|
|
|
metaMMOLoginInfo = buildMetaMMOLoginInfo(jsonObject, Long.parseLong(cityId), Long.parseLong(regionId), nickName, userId);
|
|
metaMMOLoginInfo = buildMetaMMOLoginInfo(jsonObject, Long.parseLong(cityId), Long.parseLong(regionId), nickName, userId);
|
|
|
if (CollectionUtils.isNotEmpty(otherUserIds)) {
|
|
if (CollectionUtils.isNotEmpty(otherUserIds)) {
|
|
|
if (CollectionUtils.isNotEmpty(metaMMOLoginInfos)) {
|
|
if (CollectionUtils.isNotEmpty(metaMMOLoginInfos)) {
|
|
@@ -209,7 +221,7 @@ public class MMOWebSocket {
|
|
|
redisTemplate.opsForList().leftPush(Constants.REDIS_PREFIX.concat(key), Constants.REDIS_PREFIX.concat(userId));
|
|
redisTemplate.opsForList().leftPush(Constants.REDIS_PREFIX.concat(key), Constants.REDIS_PREFIX.concat(userId));
|
|
|
break;
|
|
break;
|
|
|
case 2:
|
|
case 2:
|
|
|
- log.info(String.format("当前操作类型为[%S] -> 玩家切换区域", type));
|
|
|
|
|
|
|
+ log.info(String.format("当前操作类型为: %s -> 玩家切换区域", type));
|
|
|
metaMMOLoginInfo = buildMetaMMOLoginInfo(jsonObject, Long.parseLong(cityId), Long.parseLong(regionId), nickName, userId);
|
|
metaMMOLoginInfo = buildMetaMMOLoginInfo(jsonObject, Long.parseLong(cityId), Long.parseLong(regionId), nickName, userId);
|
|
|
if (CollectionUtils.isNotEmpty(otherUserIds)) {
|
|
if (CollectionUtils.isNotEmpty(otherUserIds)) {
|
|
|
// 分发自己信息给区域内其他玩家
|
|
// 分发自己信息给区域内其他玩家
|
|
@@ -237,7 +249,7 @@ public class MMOWebSocket {
|
|
|
redisTemplate.opsForList().leftPush(Constants.REDIS_PREFIX.concat(key), Constants.REDIS_PREFIX.concat(userId));
|
|
redisTemplate.opsForList().leftPush(Constants.REDIS_PREFIX.concat(key), Constants.REDIS_PREFIX.concat(userId));
|
|
|
break;
|
|
break;
|
|
|
case 3:
|
|
case 3:
|
|
|
- log.info(String.format("当前操作类型为[%S] -> 玩家在地图内移动", type));
|
|
|
|
|
|
|
+ log.info(String.format("当前操作类型为: %s -> 玩家在地图内移动", type));
|
|
|
metaMMOLoginInfo = (MetaMMOLoginInfo) redisTemplate.opsForValue().get(Constants.REDIS_PREFIX.concat(userId));
|
|
metaMMOLoginInfo = (MetaMMOLoginInfo) redisTemplate.opsForValue().get(Constants.REDIS_PREFIX.concat(userId));
|
|
|
if (Objects.isNull(metaMMOLoginInfo)) {
|
|
if (Objects.isNull(metaMMOLoginInfo)) {
|
|
|
log.error("缓存中不存在本玩家信息");
|
|
log.error("缓存中不存在本玩家信息");
|
|
@@ -250,7 +262,7 @@ public class MMOWebSocket {
|
|
|
redisTemplate.opsForValue().set(Constants.REDIS_PREFIX.concat(userId), metaMMOLoginInfo);
|
|
redisTemplate.opsForValue().set(Constants.REDIS_PREFIX.concat(userId), metaMMOLoginInfo);
|
|
|
break;
|
|
break;
|
|
|
case 4:
|
|
case 4:
|
|
|
- log.info(String.format("当前操作类型为[%S] -> 玩家进入大厅", type));
|
|
|
|
|
|
|
+ log.info(String.format("当前操作类型为: %s -> 玩家进入大厅", type));
|
|
|
metaMMOLoginInfo = (MetaMMOLoginInfo) redisTemplate.opsForValue().get(Constants.REDIS_PREFIX.concat(userId));
|
|
metaMMOLoginInfo = (MetaMMOLoginInfo) redisTemplate.opsForValue().get(Constants.REDIS_PREFIX.concat(userId));
|
|
|
if (Objects.isNull(metaMMOLoginInfo)) {
|
|
if (Objects.isNull(metaMMOLoginInfo)) {
|
|
|
log.error("缓存中不存在本玩家信息");
|
|
log.error("缓存中不存在本玩家信息");
|
|
@@ -261,7 +273,7 @@ public class MMOWebSocket {
|
|
|
// 更新库中玩家位置信息
|
|
// 更新库中玩家位置信息
|
|
|
MetaMMOLoginInfo dbMetaMMOLoginInfo = metaMMOLoginInfoRepo.findById(metaMMOLoginInfo.getId()).orElse(null);
|
|
MetaMMOLoginInfo dbMetaMMOLoginInfo = metaMMOLoginInfoRepo.findById(metaMMOLoginInfo.getId()).orElse(null);
|
|
|
if (Objects.isNull(dbMetaMMOLoginInfo)) {
|
|
if (Objects.isNull(dbMetaMMOLoginInfo)) {
|
|
|
- log.error(String.format("数据库不存在id[%S]的记录", metaMMOLoginInfo.getId()));
|
|
|
|
|
|
|
+ log.error(String.format("数据库不存在id: %s 的记录", metaMMOLoginInfo.getId()));
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
dbMetaMMOLoginInfo.setAxisX(metaMMOLoginInfo.getAxisX());
|
|
dbMetaMMOLoginInfo.setAxisX(metaMMOLoginInfo.getAxisX());
|
|
@@ -286,7 +298,7 @@ public class MMOWebSocket {
|
|
|
metaMMOLoginInfoRepo.save(dbMetaMMOLoginInfo);
|
|
metaMMOLoginInfoRepo.save(dbMetaMMOLoginInfo);
|
|
|
break;
|
|
break;
|
|
|
default:
|
|
default:
|
|
|
- log.error(String.format("不存在的操作类型[%S]", type));
|
|
|
|
|
|
|
+ log.error(String.format("不存在的操作类型: %s", type));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
@@ -312,10 +324,10 @@ public class MMOWebSocket {
|
|
|
}
|
|
}
|
|
|
userIds.forEach(id -> {
|
|
userIds.forEach(id -> {
|
|
|
try {
|
|
try {
|
|
|
- log.info(String.format("服务器给所有当前区域内在线用户发送消息,当前在线人员为[%S]。消息:[%S]", id, JSON.toJSONString(mmoMessage)));
|
|
|
|
|
|
|
+ log.info(String.format("服务器给所有当前区域内在线用户发送消息, 当前在线人员为: %s, 消息内容: %s", id, JSON.toJSONString(mmoMessage)));
|
|
|
clients.get(id).getBasicRemote().sendText(JSON.toJSONString(mmoMessage));
|
|
clients.get(id).getBasicRemote().sendText(JSON.toJSONString(mmoMessage));
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- log.error(String.format("send message [%S] to [%S] throw exception [%S]:", JSON.toJSONString(mmoMessage), id, e));
|
|
|
|
|
|
|
+ log.error(String.format("send message %s to %s throw exception %s:", JSON.toJSONString(mmoMessage), id, e));
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
@@ -432,6 +444,9 @@ public class MMOWebSocket {
|
|
|
}
|
|
}
|
|
|
int type = Integer.parseInt(jsonObject.getString("type"));
|
|
int type = Integer.parseInt(jsonObject.getString("type"));
|
|
|
if (type == 9) {
|
|
if (type == 9) {
|
|
|
|
|
+ if (Objects.isNull(jsonObject.getString("objectId"))) {
|
|
|
|
|
+ return MetaServiceResult.returnError("Illegal parameter : objectId can not be null");
|
|
|
|
|
+ }
|
|
|
return MetaServiceResult.returnSuccess("check success");
|
|
return MetaServiceResult.returnSuccess("check success");
|
|
|
}
|
|
}
|
|
|
if (Objects.isNull(jsonObject.getString("cityId"))) {
|
|
if (Objects.isNull(jsonObject.getString("cityId"))) {
|
|
@@ -459,7 +474,7 @@ public class MMOWebSocket {
|
|
|
public MetaRestResult<Void> clearMMO(String userId, String regionId, String cityId) {
|
|
public MetaRestResult<Void> clearMMO(String userId, String regionId, String cityId) {
|
|
|
init();
|
|
init();
|
|
|
if (clients.containsKey(Constants.REDIS_PREFIX.concat(userId))) {
|
|
if (clients.containsKey(Constants.REDIS_PREFIX.concat(userId))) {
|
|
|
- String errMsg = String.format("userId[%S]用户mmo连接正常,无法清除缓存!", userId);
|
|
|
|
|
|
|
+ String errMsg = String.format("userId: %s 用户mmo连接正常,无法清除缓存!", userId);
|
|
|
log.info(errMsg);
|
|
log.info(errMsg);
|
|
|
return MetaRestResult.returnError(errMsg);
|
|
return MetaRestResult.returnError(errMsg);
|
|
|
}
|
|
}
|