fix
Helius
2022-07-15 c76bec54f31fc072c1b59735a7e5f04cdaeeed7c
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
120
121
122
123
124
125
126
127
128
package cc.mrbird.febs.dapp.utils;
 
import cc.mrbird.febs.common.contants.AppContants;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.common.utils.SpringContextUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * @author wzy
 * @date 2022-07-14
 **/
@Slf4j
public class BoxUtil {
 
//    public static volatile List<Box> boxList = Collections.synchronizedList(new ArrayList<>());
 
    private static final RedisUtils redisUtils = SpringContextUtil.getBean(RedisUtils.class);
    public synchronized static Box openBox() {
        String redisStr =  redisUtils.getString(AppContants.IDO_BOX_PRIZE);
        List<Box> boxList = JSONObject.parseArray(redisStr, Box.class);
        if (CollUtil.isEmpty(boxList)) {
            boxList = Collections.synchronizedList(new ArrayList<>());
            Box box1 = new Box(1, 91);
            Box box2 = new Box(2, 4);
            Box box3 = new Box(3, 3);
            Box box4 = new Box(4, 1);
            Box box5 = new Box(5, 1);
            boxList.add(box1);
            boxList.add(box2);
            boxList.add(box3);
            boxList.add(box4);
            boxList.add(box5);
        }
 
        int min = 1;
        int minIndex = 1;
        int max = 1;
 
        for (int i = 0; i < boxList.size(); i++) {
            Box box = boxList.get(i);
            if (i != 0) {
                Box lastBox = (Box) boxList.get(i - 1);
                minIndex += lastBox.getCount();
            }
            max += box.getCount();
            box.setMin(minIndex);
            box.setMax(max);
        }
 
        int i = RandomUtil.randomInt(min, max);
 
        Box result = new Box();
        for (Box box : boxList) {
            if (box.getMin() <= i && box.getMax() > i) {
                result = box;
                int count = box.getCount();
                count--;
 
                box.setCount(count);
                if (count == 0) {
                    boxList.remove(box);
                }
                break;
            }
        }
        redisUtils.set(AppContants.IDO_BOX_PRIZE, JSONObject.toJSONString(boxList));
        return result;
    }
 
    public static class Box {
        private int index;
 
        private int count;
 
        public Box() {}
 
        public Box(int index, int count) {
            this.index = index;
            this.count = count;
        }
 
        private int min;
 
        private int max;
 
        public int getIndex() {
            return index;
        }
 
        public void setIndex(int index) {
            this.index = index;
        }
 
        public int getCount() {
            return count;
        }
 
        public void setCount(int count) {
            this.count = count;
        }
 
        public int getMin() {
            return min;
        }
 
        public void setMin(int min) {
            this.min = min;
        }
 
        public int getMax() {
            return max;
        }
 
        public void setMax(int max) {
            this.max = max;
        }
    }
 
}