xiaoyong931011
2021-12-08 f5e6133809c553cfd9fb28ee61019927c547c374
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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()), fortId);
        // 发送打渔消息
        fishHitProducer.sendFishHitMsg(JSONObject.toJSONString(msg));
    }
 
    @OnError
    public void onError(Session session, Throwable error) {
        log.error("捕鱼异常", error);
    }
}