package com.xcong.excoin.websocket.handler;
|
|
import cn.hutool.core.util.StrUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import com.xcong.excoin.common.contants.AppContants;
|
import com.xcong.excoin.netty.handler.WebSocketServerHandler;
|
import com.xcong.excoin.rabbit.producer.FishHitProducer;
|
import com.xcong.excoin.utils.RedisUtils;
|
import com.xcong.excoin.websocket.fish.model.MsgModel;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.socket.*;
|
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
|
import javax.websocket.Session;
|
import java.math.BigDecimal;
|
import java.util.Map;
|
import java.util.concurrent.ConcurrentHashMap;
|
|
/**
|
* @author wzy
|
* @date 2021-11-26
|
**/
|
@Slf4j
|
@Component
|
public class FishHitWebSocketHandler extends TextWebSocketHandler {
|
|
@Autowired
|
private RedisUtils redisUtils;
|
@Autowired
|
private FishHitProducer fishHitProducer;
|
|
private final Map<Long, WebSocketSession> MEMBER_SESSIONS = new ConcurrentHashMap<>();
|
private final Map<String, Long> SID_MID = new ConcurrentHashMap<>();
|
|
|
@Override
|
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
Object token = session.getAttributes().get("token");
|
if (token != null) {
|
String redisStr = AppContants.APP_LOGIN_PREFIX + token;
|
String memberStr = redisUtils.getString(redisStr);
|
if (StrUtil.isBlank(memberStr)) {
|
session.sendMessage(new TextMessage("{ code:-1, msg: \"Unauthorized\"}"));
|
return;
|
}
|
|
JSONObject memberObject = JSONObject.parseObject(memberStr);
|
Long memberId = memberObject.getLong("id");
|
MEMBER_SESSIONS.put(memberId, session);
|
SID_MID.put(session.getId(), memberId);
|
session.sendMessage(new TextMessage("{ code:0, msg: \"login succes\" }"));
|
}
|
}
|
|
/**
|
*
|
* @param content { type : "hit", fortId : 1, fishType : "fish1" }
|
* @param session
|
*/
|
@Override
|
public void handleTextMessage(WebSocketSession session, TextMessage content) throws Exception {
|
String message = content.getPayload();
|
|
JSONObject reqParam = JSONObject.parseObject(message);
|
String type = reqParam.getString("type");
|
if (!("hit").equals(type)) {
|
session.sendMessage(new TextMessage("{code:-1}"));
|
return;
|
}
|
|
// 打中鱼奖励金币
|
int fishGold = 0;
|
String fishType = reqParam.getString("fishType");
|
|
// 如果请求参数中包含鱼类型,且类型不为空,则说明打中
|
if (reqParam.containsKey("fishType") && StrUtil.isNotBlank(fishType)) {
|
Object fishTypeObj = redisUtils.hget(AppContants.DICTIONARY_TYPE_FISH, fishType);
|
if (fishTypeObj == null) {
|
session.sendMessage(new TextMessage("{code : -1, msg : \"fish error\"}"));
|
return;
|
}
|
|
fishGold = (int) fishTypeObj;
|
}
|
|
String fortId = reqParam.getString("fortId");
|
if (!reqParam.containsKey("fortId") || fortId == null) {
|
return;
|
}
|
|
Object fortObj = redisUtils.hget(AppContants.CANNON_TYPE, fortId);
|
if (fortObj == null) {
|
session.sendMessage(new TextMessage("{code : -1, msg : \"fort error\"}"));
|
return;
|
}
|
|
// 消耗金币
|
BigDecimal consume = (BigDecimal) fortObj;
|
|
MsgModel msg = new MsgModel(consume, fishGold, SID_MID.get(session.getId()), fortId);
|
// 发送打渔消息
|
fishHitProducer.sendFishHitMsg(JSONObject.toJSONString(msg));
|
}
|
|
@Override
|
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
|
}
|
|
@Override
|
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
|
log.info("离开websocket");
|
Long memberId = SID_MID.get(session.getId());
|
|
if (memberId != null) {
|
MEMBER_SESSIONS.remove(memberId);
|
}
|
SID_MID.remove(session.getId());
|
}
|
}
|