Helius
2021-11-26 4017fe347792c7e28695c455a40874f0c647cc9b
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
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()));
        // 发送打渔消息
        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 {
        Long memberId = SID_MID.get(session.getId());
 
        SID_MID.remove(session.getId());
        MEMBER_SESSIONS.remove(memberId);
    }
}