package com.xcong.excoin.websocket.fish;
|
|
import cn.hutool.core.util.StrUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import com.xcong.excoin.common.contants.AppContants;
|
import com.xcong.excoin.modules.member.dao.MemberDao;
|
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 javax.websocket.*;
|
import javax.websocket.server.ServerEndpoint;
|
import java.math.BigDecimal;
|
import java.util.Map;
|
import java.util.concurrent.ConcurrentHashMap;
|
|
/**
|
* @author wzy
|
* @date 2021-11-26
|
**/
|
@Slf4j
|
@Component
|
//@ServerEndpoint(value = "/websocket/fish/hit")
|
public class HitFishWebSocket {
|
|
@Autowired
|
private RedisUtils redisUtils;
|
@Autowired
|
private FishHitProducer fishHitProducer;
|
|
private final Map<Long, Session> MEMBER_SESSIONS = new ConcurrentHashMap<>();
|
private final Map<String, Long> SID_MID = new ConcurrentHashMap<>();
|
|
@OnOpen
|
public void onOpen(Session session) {
|
}
|
|
@OnClose
|
public void onClose(Session session) {}
|
|
/**
|
*
|
* @param message { type : "hit", fortId : 1, fishType : "fish1" }
|
*
|
* @param session
|
*/
|
@OnMessage
|
public void onMessage(String message, Session session) {
|
JSONObject reqParam = JSONObject.parseObject(message);
|
if (!reqParam.containsKey("hit") && !reqParam.containsKey("login")) {
|
session.getAsyncRemote().sendText("{code:-1}");
|
return;
|
}
|
|
// 登录逻辑
|
if (reqParam.containsKey("login")) {
|
String token = reqParam.getString("token");
|
if (StrUtil.isBlank(token)) {
|
session.getAsyncRemote().sendText("{ code:-1, msg: \"token error\" }");
|
return;
|
}
|
|
String redisStr = AppContants.APP_LOGIN_PREFIX + token;
|
String memberStr = redisUtils.getString(redisStr);
|
if (StrUtil.isBlank(memberStr)) {
|
session.getAsyncRemote().sendText("{ code:-1, msg: \"Unauthorized\"}");
|
return;
|
}
|
|
JSONObject memberObject = JSONObject.parseObject(memberStr);
|
Long memberId = memberObject.getLong("memberId");
|
MEMBER_SESSIONS.put(memberId, session);
|
SID_MID.put(session.getId(), memberId);
|
session.getAsyncRemote().sendText("{ code:0, msg: \"login succes\" }");
|
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.getAsyncRemote().sendText("{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.getAsyncRemote().sendText("{code : -1, msg : \"fort error\"}");
|
return;
|
}
|
|
// 消耗金币
|
BigDecimal consume = (BigDecimal) fortObj;
|
|
MsgModel msg = new MsgModel(consume, fishGold, SID_MID.get(session.getId()));
|
// 发送打渔消息
|
fishHitProducer.sendFishHitMsg(JSONObject.toJSONString(msg));
|
}
|
|
@OnError
|
public void onError(Session session, Throwable error) {
|
log.error("捕鱼异常", error);
|
}
|
}
|