aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/FlipHandler.java
diff options
context:
space:
mode:
authorTorui <44932079+ToruiDev@users.noreply.github.com>2022-01-24 20:00:02 +0100
committerGitHub <noreply@github.com>2022-01-24 20:00:02 +0100
commit1caa3e1840e51800200458b776712f38b470d772 (patch)
tree1df9f67493c87ae24f19cb1f1c8ffa9503b4125b /src/main/java/de/torui/coflsky/FlipHandler.java
parent4d2b8de15b223b2c9dfe389db36a593b1fabff0b (diff)
downloadCOFL-1caa3e1840e51800200458b776712f38b470d772.tar.gz
COFL-1caa3e1840e51800200458b776712f38b470d772.tar.bz2
COFL-1caa3e1840e51800200458b776712f38b470d772.zip
Feature/support new flips (#47)
* added rudimentary support for the new flips * Keypress * send "clicked" command * maybe fix the ConcurrentModificationException * add synchronized to relevant pieces of code * premature optimization * change timings to 50 seconds * switched to config file * workaround for #48 * change debounce mechanism * cofl track * fix #49 * fix command not getting data serialized * . * add id on claim * unset hotkey * Apply suggestions from code review remove tab from empty line Co-authored-by: Äkwav <16632490+Ekwav@users.noreply.github.com>
Diffstat (limited to 'src/main/java/de/torui/coflsky/FlipHandler.java')
-rw-r--r--src/main/java/de/torui/coflsky/FlipHandler.java122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/main/java/de/torui/coflsky/FlipHandler.java b/src/main/java/de/torui/coflsky/FlipHandler.java
new file mode 100644
index 0000000..0c89f21
--- /dev/null
+++ b/src/main/java/de/torui/coflsky/FlipHandler.java
@@ -0,0 +1,122 @@
+package de.torui.coflsky;
+
+import java.util.ConcurrentModificationException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class FlipHandler {
+ public static class Flip {
+ public String id;
+ public int worth;
+
+ public Flip(String id, int worth) {
+ super();
+ this.id = id;
+ this.worth = worth;
+ }
+
+ public Flip() {
+
+ }
+
+ }
+
+ public static class FlipDataStructure {
+
+ private Map<Long, Flip> Flips = new ConcurrentHashMap <>();
+ private Map<Flip, Long> ReverseMap = new ConcurrentHashMap <>();
+
+ private Flip HighestFlip = 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(Flip flip) {
+ Long l = System.currentTimeMillis();
+
+ synchronized(Flips) {
+ Flips.put(l, flip);
+ ReverseMap.put(flip, l);
+ }
+
+ RunHouseKeeping();
+ }
+
+ private void RemoveLong(Long l) {
+ if (l == null)
+ return;
+ synchronized(Flips) {
+ Flip f = Flips.get(l);
+ if (f != null) {
+ ReverseMap.remove(f);
+ Flips.remove(l);
+ }
+ }
+ }
+
+ private void RemoveFlip(Flip f) {
+ if (f == null)
+ return;
+
+ synchronized(Flips) {
+ Long l = ReverseMap.get(f);
+ if (l != null) {
+ Flips.remove(l);
+ ReverseMap.remove(f);
+ }
+ }
+ }
+
+ public Flip GetHighestFlip() {
+ return HighestFlip;
+ }
+
+ public void InvalidateFlip(Flip flip) {
+ RemoveFlip(flip);
+ RunHouseKeeping();
+ }
+
+ public int CurrentFlips() {
+ return Flips.size();
+ }
+
+ }
+
+ public FlipDataStructure fds;
+
+ public FlipHandler() {
+ fds = new FlipDataStructure();
+ }
+
+}