aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/FlipHandler.java
blob: 2b59c7fa4130ca2dcc95e22aff2481b43f8c63f4 (plain)
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
package de.torui.coflsky;

import de.torui.coflsky.commands.models.FlipData;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class FlipHandler {

    public static class FlipDataStructure {

        private Map<Long, FlipData> Flips = new ConcurrentHashMap<>();
        private Map<FlipData, Long> ReverseMap = new ConcurrentHashMap<>();

        private FlipData HighestFlip = null;
        private FlipData LastFlip = null;

        private Timer t = new Timer();
        private TimerTask CurrentTask = null;

        public synchronized void RunHouseKeeping() {
            synchronized (Flips) {

                Long RemoveAllPrior = System.currentTimeMillis() - (Config.KeepFlipsForSeconds * 1000);
                Flips.keySet().stream().filter(l -> l <= RemoveAllPrior).forEach(l -> RemoveLong(l));
                if (!Flips.isEmpty()) {
                    HighestFlip = Flips.values().stream().max((f1, f2) -> f1.Worth - f2.Worth).orElse(null);
                } else {
                    HighestFlip = null;
                }
            }

            if (CurrentTask != null) {
                CurrentTask.cancel();
                CurrentTask = null;
                t.purge();
            }
            if (!Flips.isEmpty()) {
                CurrentTask = new TimerTask() {
                    @Override
                    public void run() {
                        RunHouseKeeping();
                    }
                };
                t.schedule(CurrentTask, Config.KeepFlipsForSeconds * 1000 + /* small arbitrary delay */150);
            }
        }

        public synchronized void Insert(FlipData flip) {
            Long l = System.currentTimeMillis();
            LastFlip = flip;

            synchronized (Flips) {
                Flips.put(l, flip);
                ReverseMap.put(flip, l);
            }

            RunHouseKeeping();
        }

        private void RemoveLong(Long l) {
            if (l == null)
                return;
            synchronized (Flips) {
                FlipData f = Flips.get(l);
                if (f != null) {
                    ReverseMap.remove(f);
                    Flips.remove(l);
                }
            }
        }

        private void RemoveFlip(FlipData f) {
            if (f == null)
                return;

            synchronized (Flips) {
                Long l = ReverseMap.get(f);
                if (l != null) {
                    Flips.remove(l);
                    ReverseMap.remove(f);
                }
            }
        }

        public FlipData GetHighestFlip() {
            return HighestFlip;
        }

        public FlipData GetLastFlip() {
            if (LastFlip == null) {
                return null;
            }
            Long l = ReverseMap.get(LastFlip);
            if (l == null) {
                LastFlip = null;
            }
            return LastFlip;
        }

        public FlipData getFlipById(String id) {
            FlipData[] flips = Flips.values().stream().filter(flipData -> flipData.Id.equals(id)).toArray(FlipData[]::new);
            if (flips.length == 0) {
                return null;
            }
            return flips[0];
        }

        public void InvalidateFlip(FlipData flip) {
            RemoveFlip(flip);
            RunHouseKeeping();
        }

        public int CurrentFlips() {
            return Flips.size();
        }

    }

    public FlipDataStructure fds;
    public String lastClickedFlipMessage;

    public FlipHandler() {
        fds = new FlipDataStructure();
    }

}