|
@ -1,10 +1,16 @@ |
|
|
package com.ccsens.game.netty.wsserver; |
|
|
package com.ccsens.game.netty.wsserver; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil; |
|
|
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
|
import com.ccsens.game.bean.dto.ClientDto; |
|
|
import com.ccsens.game.bean.dto.message.*; |
|
|
import com.ccsens.game.bean.dto.message.*; |
|
|
import com.ccsens.game.netty.ChannelManager; |
|
|
import com.ccsens.game.netty.ChannelManager; |
|
|
import com.ccsens.game.service.IClientService; |
|
|
import com.ccsens.game.service.IClientService; |
|
|
import com.ccsens.game.service.IMessageService; |
|
|
import com.ccsens.game.service.IMessageService; |
|
|
|
|
|
import com.ccsens.game.util.GameConstant; |
|
|
|
|
|
import com.ccsens.util.RedisUtil; |
|
|
import com.ccsens.util.WebConstant; |
|
|
import com.ccsens.util.WebConstant; |
|
|
import com.ccsens.util.bean.message.common.MessageConstant; |
|
|
import com.ccsens.util.bean.message.common.MessageConstant; |
|
|
import io.netty.channel.ChannelHandler; |
|
|
import io.netty.channel.ChannelHandler; |
|
@ -16,8 +22,12 @@ import lombok.extern.slf4j.Slf4j; |
|
|
import org.slf4j.Logger; |
|
|
import org.slf4j.Logger; |
|
|
import org.slf4j.LoggerFactory; |
|
|
import org.slf4j.LoggerFactory; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import org.springframework.data.redis.core.ZSetOperations; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.Date; |
|
|
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @author wei |
|
|
* @author wei |
|
|
*/ |
|
|
*/ |
|
@ -33,6 +43,8 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<BaseMessageDto |
|
|
private IClientService clientService; |
|
|
private IClientService clientService; |
|
|
@Autowired |
|
|
@Autowired |
|
|
private IMessageService messageService; |
|
|
private IMessageService messageService; |
|
|
|
|
|
@Autowired |
|
|
|
|
|
private RedisUtil redisUtil; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { |
|
|
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { |
|
@ -76,7 +88,7 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<BaseMessageDto |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected void channelRead0(ChannelHandlerContext ctx, BaseMessageDto baseMessage) throws Exception { |
|
|
protected void channelRead0(ChannelHandlerContext ctx, BaseMessageDto baseMessage) throws Exception { |
|
|
log.info("接受到消息的时间+++++++++++++++++++++++:{}",System.currentTimeMillis()); |
|
|
log.info("接受到消息的时间+++++++++++++++++++++++{}:{},{}",baseMessage.getType(), new Date(), System.currentTimeMillis()); |
|
|
MessageConstant.GameClientMessageType gameClientMessageType = MessageConstant.GameClientMessageType.valueOf(baseMessage.getType()); |
|
|
MessageConstant.GameClientMessageType gameClientMessageType = MessageConstant.GameClientMessageType.valueOf(baseMessage.getType()); |
|
|
|
|
|
|
|
|
System.out.println(baseMessage); |
|
|
System.out.println(baseMessage); |
|
@ -109,8 +121,11 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<BaseMessageDto |
|
|
String userId = ChannelManager.getUserIdByChannel(ctx.channel()); |
|
|
String userId = ChannelManager.getUserIdByChannel(ctx.channel()); |
|
|
String recordId = ChannelManager.getRecordIdByChannel(ctx.channel()); |
|
|
String recordId = ChannelManager.getRecordIdByChannel(ctx.channel()); |
|
|
log.info("统计分数:{},{}",userId, recordId); |
|
|
log.info("统计分数:{},{}",userId, recordId); |
|
|
GameMessageCountOut gameMessageCountOut = clientService.clientAddTimes(userId,recordId); |
|
|
long t1 = System.currentTimeMillis(); |
|
|
log.info("统计分数返回结果:{}", gameMessageCountOut); |
|
|
// GameMessageCountOut gameMessageCountOut = clientService.clientAddTimes(userId,recordId);
|
|
|
|
|
|
GameMessageCountOut gameMessageCountOut = clientAddTimes(userId,recordId); |
|
|
|
|
|
long t2 = System.currentTimeMillis(); |
|
|
|
|
|
log.info("统计分数返回结果:{}, 计算耗时:{}", gameMessageCountOut, t2 - t1); |
|
|
ChannelManager.sendTo(ctx.channel(), gameMessageCountOut); |
|
|
ChannelManager.sendTo(ctx.channel(), gameMessageCountOut); |
|
|
Long sendTime = System.currentTimeMillis(); |
|
|
Long sendTime = System.currentTimeMillis(); |
|
|
log.info("消耗的时长:{}", sendTime - receiveTime); |
|
|
log.info("消耗的时长:{}", sendTime - receiveTime); |
|
@ -131,6 +146,40 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<BaseMessageDto |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public GameMessageCountOut clientAddTimes(String userId, String recordId) { |
|
|
|
|
|
GameMessageCountOut gameMessageCountOut = new GameMessageCountOut(); |
|
|
|
|
|
log.info("userId:{}", userId); |
|
|
|
|
|
if (StrUtil.isBlank(userId) || StrUtil.isBlank(userId)) { |
|
|
|
|
|
return gameMessageCountOut; |
|
|
|
|
|
} |
|
|
|
|
|
Long recordIdLong = Long.parseLong(recordId); |
|
|
|
|
|
String gameUserKey = GameConstant.generateGameKey(recordIdLong); |
|
|
|
|
|
Set<ZSetOperations.TypedTuple<Object>> typedTuples = redisUtil.zsRevGetWithScore(gameUserKey, 0, -1); |
|
|
|
|
|
|
|
|
|
|
|
if (CollectionUtil.isEmpty(typedTuples)) { |
|
|
|
|
|
return gameMessageCountOut; |
|
|
|
|
|
} |
|
|
|
|
|
for(ZSetOperations.TypedTuple<Object> type : typedTuples) { |
|
|
|
|
|
ClientDto.RedisUser user = JSON.parseObject((String)type.getValue(), ClientDto.RedisUser.class); |
|
|
|
|
|
if (user.getUserId().longValue() == Long.parseLong(userId)) { |
|
|
|
|
|
int score = type.getScore().intValue(); |
|
|
|
|
|
String userStatus = GameConstant.generateGameStatusKey(recordIdLong); |
|
|
|
|
|
String gameStausObj = (String)redisUtil.get(userStatus); |
|
|
|
|
|
if (StrUtil.isBlank(gameStausObj) || gameStausObj.equals(String.valueOf(GameConstant.GAME_COMPLETED))){ |
|
|
|
|
|
gameMessageCountOut = new GameMessageCountOut(score/100, score); |
|
|
|
|
|
return gameMessageCountOut; |
|
|
|
|
|
} |
|
|
|
|
|
score += 100; |
|
|
|
|
|
redisUtil.zsSet(gameUserKey, JSON.toJSONString(user), score); |
|
|
|
|
|
gameMessageCountOut.getData().setTotalScore(score); |
|
|
|
|
|
gameMessageCountOut.getData().setTotalTimes(score/100); |
|
|
|
|
|
return gameMessageCountOut; |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
return gameMessageCountOut; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private void doAuthMessage(ChannelHandlerContext ctx, AuthMessageDto message) throws Exception { |
|
|
private void doAuthMessage(ChannelHandlerContext ctx, AuthMessageDto message) throws Exception { |
|
|
WebConstant.Message_Auth_Event event = WebConstant.Message_Auth_Event.phaseOf(message.getEvent()); |
|
|
WebConstant.Message_Auth_Event event = WebConstant.Message_Auth_Event.phaseOf(message.getEvent()); |
|
|
switch (event) { |
|
|
switch (event) { |
|
|