diff options
author | Torui <44932079+ToruiDev@users.noreply.github.com> | 2022-01-24 20:00:02 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-24 20:00:02 +0100 |
commit | 1caa3e1840e51800200458b776712f38b470d772 (patch) | |
tree | 1df9f67493c87ae24f19cb1f1c8ffa9503b4125b /src | |
parent | 4d2b8de15b223b2c9dfe389db36a593b1fabff0b (diff) | |
download | COFL-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')
-rw-r--r-- | src/main/java/de/torui/coflsky/CoflSky.java | 9 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/CoflSkyCommand.java | 2 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/Config.java | 5 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/EventRegistry.java | 47 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/FlipHandler.java | 122 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/WSCommandHandler.java | 25 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/commands/CommandType.java | 32 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/commands/models/FlipData.java | 26 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/network/WSClient.java | 4 |
9 files changed, 260 insertions, 12 deletions
diff --git a/src/main/java/de/torui/coflsky/CoflSky.java b/src/main/java/de/torui/coflsky/CoflSky.java index 53ffe71..0191ea1 100644 --- a/src/main/java/de/torui/coflsky/CoflSky.java +++ b/src/main/java/de/torui/coflsky/CoflSky.java @@ -20,11 +20,12 @@ import net.minecraftforge.fml.relauncher.Side; public class CoflSky { public static final String MODID = "CoflSky"; - public static final String VERSION = "1.2-Alpha"; + public static final String VERSION = "1.3-Alpha"; public static WSClientWrapper Wrapper; public static KeyBinding[] keyBindings; + public static EventRegistry Events; public static final String[] webSocketURIPrefix = new String [] { "wss://sky.coflnet.com/modsocket", @@ -45,6 +46,7 @@ public class CoflSky keyBindings = new KeyBinding[] { new KeyBinding("key.replay_last.onclick", Keyboard.KEY_NONE, "SkyCofl"), + new KeyBinding("key.start_highest_bid", Keyboard.KEY_NONE, "SkyCofl") }; if(event.getSide() == Side.CLIENT) { @@ -57,8 +59,9 @@ public class CoflSky } - } - MinecraftForge.EVENT_BUS.register(new EventRegistry()); + } + Events = new EventRegistry(); + MinecraftForge.EVENT_BUS.register(Events); } diff --git a/src/main/java/de/torui/coflsky/CoflSkyCommand.java b/src/main/java/de/torui/coflsky/CoflSkyCommand.java index dae91a6..c6b38fd 100644 --- a/src/main/java/de/torui/coflsky/CoflSkyCommand.java +++ b/src/main/java/de/torui/coflsky/CoflSkyCommand.java @@ -60,6 +60,8 @@ public class CoflSkyCommand extends CommandBase { switch(args[0]) { case "start": //todo: start + //possible workaround for https://github.com/Coflnet/SkyblockMod/issues/48 + CoflSky.Wrapper.stop(); sender.addChatMessage(new ChatComponentText("starting connection...")); CoflSky.Wrapper.startConnection(); break; diff --git a/src/main/java/de/torui/coflsky/Config.java b/src/main/java/de/torui/coflsky/Config.java new file mode 100644 index 0000000..905f4a9 --- /dev/null +++ b/src/main/java/de/torui/coflsky/Config.java @@ -0,0 +1,5 @@ +package de.torui.coflsky; + +public class Config { + public static final int KeepFlipsForSeconds = 50; +}
\ No newline at end of file diff --git a/src/main/java/de/torui/coflsky/EventRegistry.java b/src/main/java/de/torui/coflsky/EventRegistry.java index 62f5a27..511b155 100644 --- a/src/main/java/de/torui/coflsky/EventRegistry.java +++ b/src/main/java/de/torui/coflsky/EventRegistry.java @@ -3,6 +3,7 @@ package de.torui.coflsky; import java.time.LocalDateTime; import com.mojang.realmsclient.util.Pair; +import de.torui.coflsky.FlipHandler.Flip; import de.torui.coflsky.commands.Command; import de.torui.coflsky.commands.CommandType; import de.torui.coflsky.commands.JsonStringCommand; @@ -36,6 +37,8 @@ public class EventRegistry { System.out.println("CoflSky stopped"); } } + + public long LastClick = System.currentTimeMillis(); @SideOnly(Side.CLIENT) @SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true) @@ -52,6 +55,23 @@ public class EventRegistry { } } + if(CoflSky.keyBindings[1].isKeyDown()) { + if((System.currentTimeMillis() - LastClick) >= 500) { + + Flip f = WSCommandHandler.flipHandler.fds.GetHighestFlip(); + + if(f != null) { + LastClick = System.currentTimeMillis(); + String command = WSClient.gson.toJson("/viewauction " + f.id); + WSCommandHandler.Execute("/viewauction " + f.id, null); + WSCommandHandler.flipHandler.fds.InvalidateFlip(f); + + WSCommandHandler.Execute("/cofl track besthotkey " + f.id, Minecraft.getMinecraft().thePlayer); + CoflSky.Wrapper.SendMessage(new JsonStringCommand(CommandType.Clicked, command)); + } + + } + } } @@ -61,7 +81,7 @@ public class EventRegistry { if (rgoe.type == ElementType.CROSSHAIRS) { Minecraft mc = Minecraft.getMinecraft(); - mc.ingameGUI.drawString(Minecraft.getMinecraft().fontRendererObj, "Hello World", 0, 0, Integer.MAX_VALUE); + mc.ingameGUI.drawString(Minecraft.getMinecraft().fontRendererObj, "Flips in Pipeline:" + WSCommandHandler.flipHandler.fds.CurrentFlips(), 0, 0, Integer.MAX_VALUE); } } @@ -98,7 +118,13 @@ public class EventRegistry { AuctionData ad = new AuctionData(); ad.setItemId(last.second().first()); - ad.setAuctionId(""); + + if((LastViewAuctionInvocation+60*1000) >= System.currentTimeMillis()) { + ad.setAuctionId(LastViewAuctionUUID); + } else { + ad.setAuctionId(""); + } + Command<AuctionData> data = new Command<>(CommandType.PurchaseConfirm, ad); CoflSky.Wrapper.SendMessage(data); System.out.println("PurchaseConfirm"); @@ -111,7 +137,10 @@ public class EventRegistry { } - public static long lastStartTime = Long.MAX_VALUE; + public static long lastStartTime = Long.MIN_VALUE; + + public static long LastViewAuctionInvocation = Long.MIN_VALUE; + public static String LastViewAuctionUUID =null; @SideOnly(Side.CLIENT) @SubscribeEvent @@ -134,17 +163,23 @@ public class EventRegistry { String itemUUID = ExtractUuidFromInventory(inv); - if((System.currentTimeMillis()+200) < lastStartTime) { + if(System.currentTimeMillis() > lastStartTime) { if (heldItem.isItemEqual(GOLD_NUGGET)) { AuctionData ad = new AuctionData(); ad.setItemId(itemUUID); - ad.setAuctionId(""); + + if((LastViewAuctionInvocation+60*1000) >= System.currentTimeMillis()) { + ad.setAuctionId(LastViewAuctionUUID); + } else { + ad.setAuctionId(""); + } + Command<AuctionData> data = new Command<>(CommandType.PurchaseStart, ad); CoflSky.Wrapper.SendMessage(data); System.out.println("PurchaseStart"); last = Pair.of("You claimed ", Pair.of(itemUUID, LocalDateTime.now())); - lastStartTime = System.currentTimeMillis(); + lastStartTime = System.currentTimeMillis() + 200 /*ensure a small debounce*/; } } 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(); + } + +} diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index a06640c..0fc834f 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -3,8 +3,10 @@ package de.torui.coflsky; import com.google.gson.reflect.TypeToken; import de.torui.coflsky.commands.Command; +import de.torui.coflsky.commands.CommandType; import de.torui.coflsky.commands.JsonStringCommand; import de.torui.coflsky.commands.models.ChatMessageData; +import de.torui.coflsky.commands.models.FlipData; import de.torui.coflsky.commands.models.SoundData; import de.torui.coflsky.network.WSClient; import net.minecraft.client.Minecraft; @@ -23,6 +25,7 @@ import net.minecraftforge.client.ClientCommandHandler; public class WSCommandHandler { public static transient String lastOnClickEvent; + public static FlipHandler flipHandler = new FlipHandler(); public static boolean HandleCommand(JsonStringCommand cmd, Entity sender) { // Entity sender = Minecraft.getMinecraft().thePlayer; @@ -41,6 +44,8 @@ public class WSCommandHandler { case ChatMessage: ChatMessage(cmd.GetAs(new TypeToken<ChatMessageData[]>() {})); break; + case Flip: + Flip(cmd.GetAs(new TypeToken<FlipData>() {})); default: break; } @@ -48,6 +53,17 @@ public class WSCommandHandler { return true; } + private static void Flip(Command<FlipData> cmd) { + //handle chat message + ChatMessageData[] messages = cmd.getData().Messages; + Command<ChatMessageData[]> showCmd = new Command<ChatMessageData[]>(CommandType.ChatMessage, messages); + ChatMessage(showCmd); + flipHandler.fds.Insert(new de.torui.coflsky.FlipHandler.Flip(cmd.getData().Id, cmd.getData().Worth)); + + //just to be safe emit a event + CoflSky.Events.onEvent(null); + } + private static void PlaySound(Command<SoundData> cmd, Entity sender) { SoundData sc = cmd.getData(); @@ -69,6 +85,15 @@ public class WSCommandHandler { public static void Execute(String cmd, Entity sender) { + + if(cmd.startsWith("/viewauction")){ + String[] args = cmd.split(" "); + + String uuid = args[args.length-1]; + EventRegistry.LastViewAuctionUUID = uuid; + EventRegistry.LastViewAuctionInvocation = System.currentTimeMillis(); + } + if(cmd.startsWith("/cofl") || cmd.startsWith("http")) { ClientCommandHandler.instance.executeCommand(sender, cmd); } else { diff --git a/src/main/java/de/torui/coflsky/commands/CommandType.java b/src/main/java/de/torui/coflsky/commands/CommandType.java index 10b1ca3..ead7d3e 100644 --- a/src/main/java/de/torui/coflsky/commands/CommandType.java +++ b/src/main/java/de/torui/coflsky/commands/CommandType.java @@ -1,5 +1,9 @@ package de.torui.coflsky.commands; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + import com.google.gson.annotations.SerializedName; public enum CommandType { @@ -29,5 +33,31 @@ public enum CommandType { @SerializedName("reset") Reset, - + @SerializedName("flip") + Flip, +; + public static Map<CommandType,String> data; + static { + data = new HashMap<>(); + for(CommandType ct : CommandType.values()) { + try { + Field f = CommandType.class.getField(ct.name()); + + if(f.isAnnotationPresent(SerializedName.class)) { + SerializedName sn = f.getAnnotation(SerializedName.class); + data.put(ct, sn.value()); + } else { + throw new RuntimeException("Commandtype must have SerializeName Annotation!"); + } + + } catch (NoSuchFieldException | SecurityException e) { + System.err.println("This should never occur!"); + e.printStackTrace(); + } + } + } + + public String ToJson() { + return data.get(this); + } } diff --git a/src/main/java/de/torui/coflsky/commands/models/FlipData.java b/src/main/java/de/torui/coflsky/commands/models/FlipData.java new file mode 100644 index 0000000..bdd955b --- /dev/null +++ b/src/main/java/de/torui/coflsky/commands/models/FlipData.java @@ -0,0 +1,26 @@ +package de.torui.coflsky.commands.models; + +import com.google.gson.annotations.SerializedName; + +public class FlipData { + + @SerializedName("messages") + public ChatMessageData[] Messages; + + @SerializedName("id") + public String Id; + + @SerializedName("worth") + public int Worth; + + public FlipData() {} + + public FlipData(ChatMessageData[] messages, String id, int worth) { + super(); + Messages = messages; + Id = id; + Worth = worth; + } + + +} diff --git a/src/main/java/de/torui/coflsky/network/WSClient.java b/src/main/java/de/torui/coflsky/network/WSClient.java index 353fab0..ad9c436 100644 --- a/src/main/java/de/torui/coflsky/network/WSClient.java +++ b/src/main/java/de/torui/coflsky/network/WSClient.java @@ -122,15 +122,15 @@ public class WSClient extends WebSocketAdapter { } public void SendCommand(Command cmd) { - Send(cmd); + SendCommand(new RawCommand(cmd.getType().ToJson(),gson.toJson(cmd.getData()))); } - public void SendCommand(RawCommand cmd) { Send(cmd); } public void Send(Object obj) { String json = gson.toJson(obj); + System.out.println("###Sending message of json value " + json); this.socket.sendText(json); } |