]> jfr.im git - irc/rizon/acid.git/commitdiff
Order the results of a drawing before entering in the database
authorOrillion <redacted>
Tue, 22 Aug 2017 19:56:29 +0000 (21:56 +0200)
committerOrillion <redacted>
Tue, 22 Aug 2017 19:59:38 +0000 (21:59 +0200)
vizon/src/main/java/net/rizon/acid/plugins/vizon/LotteryThread.java
vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/CheckCommand.java
vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/OrderResultsCommand.java [new file with mode: 0644]
vizon/src/main/java/net/rizon/acid/plugins/vizon/db/VizonDatabase.java
vizon/src/main/java/net/rizon/acid/plugins/vizon/db/VizonDrawing.java
vizon/vizon.example.yml

index 92da679414963db3b05e4412c49ebf2122b6658f..3d5448a5e906c9e8a337f10ba106caa95f0bf238 100644 (file)
@@ -96,8 +96,12 @@ public class LotteryThread extends Thread
                        winning = generator.generate();
                }
 
+               // Sort the winning numbers from low to high.
+               List<Integer> winningNumbers = winning.asList();
+               Collections.sort(winningNumbers);
+
                // Set the results of the drawing.
-               drawing.setDrawingResult(winning);
+               drawing.setDrawingResult(winningNumbers);
 
                // Announce we're starting the drawing
                privmsgChannel(String.format(
@@ -142,10 +146,6 @@ public class LotteryThread extends Thread
                        return;
                }
 
-               // Sort the winning numbers from low to high.
-               List<Integer> winningNumbers = winning.asList();
-               Collections.sort(winningNumbers);
-
                // Announce results of the drawing, ordered.
                privmsgChannel(String.format(
                                "The result of No.%d drawing of VIzon is %d %d %d %d %d %d",
index 84acbe62a19e3460cd4bd4913f1a6406266d9170..5293f4bc7e1f7aceabf3052194f2447cd618e01a 100644 (file)
@@ -26,7 +26,6 @@
 package net.rizon.acid.plugins.vizon.commands;
 
 import java.time.LocalDateTime;
-import java.util.Collections;
 import java.util.List;
 import net.rizon.acid.core.AcidUser;
 import net.rizon.acid.core.Acidictive;
@@ -115,7 +114,6 @@ public class CheckCommand extends Command
                }
 
                List<Integer> bets = drawing.getDraws();
-               Collections.sort(bets);
 
                if (bet == null)
                {
diff --git a/vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/OrderResultsCommand.java b/vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/OrderResultsCommand.java
new file mode 100644 (file)
index 0000000..c175888
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2017, Orillion <orillion@rizon.net>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+package net.rizon.acid.plugins.vizon.commands;
+
+import java.util.Collections;
+import java.util.List;
+import net.rizon.acid.core.AcidUser;
+import net.rizon.acid.core.Acidictive;
+import net.rizon.acid.core.Channel;
+import net.rizon.acid.core.Command;
+import net.rizon.acid.core.User;
+import net.rizon.acid.plugins.vizon.Vizon;
+import net.rizon.acid.plugins.vizon.db.VizonDrawing;
+import net.rizon.acid.util.Format;
+
+/**
+ *
+ * @author Orillion <orillion@rizon.net>
+ */
+public class OrderResultsCommand extends Command
+{
+
+       public OrderResultsCommand()
+       {
+               super(0, 0);
+       }
+
+       @Override
+       public void Run(User source, AcidUser to, Channel c, String[] args)
+       {
+               Acidictive.reply(source, to, c, "Updating records...");
+
+               List<VizonDrawing> drawings = Vizon.getVizonDatabase().findAllRunDrawings();
+
+               for (VizonDrawing drawing : drawings)
+               {
+                       List<Integer> winning = drawing.getDraws();
+                       Collections.sort(winning);
+                       drawing.setDrawingResult(winning);
+
+                       Vizon.getVizonDatabase().updateDrawing(drawing);
+               }
+
+               Acidictive.reply(source, to, c, String.format(
+                               "Updated %s%d%s records",
+                               Format.color(Format.ORANGE),
+                               drawings.size(),
+                               Format.color(0)));
+       }
+
+       @Override
+       public void onHelp(User u, AcidUser to, Channel c)
+       {
+               Acidictive.reply(u, to, c, "\002ORDER\002 Orders all drawing results from low to high");
+       }
+
+       @Override
+       public boolean onHelpCommand(User u, AcidUser to, Channel c)
+       {
+               Acidictive.reply(u, to, c, "Syntax: \002ORDER\002");
+               Acidictive.reply(u, to, c, " ");
+               Acidictive.reply(u, to, c, "Orders all drawing results from low to high");
+
+               return true;
+       }
+
+}
index bdc1ca982aa0b21f4c944a463b2a8f72a5c3549b..fd23ccbcd348157d2b10668fa747bcca14658522 100644 (file)
@@ -771,4 +771,41 @@ public class VizonDatabase
                        return 0;
                }
        }
+
+       /**
+        * Finds all drawings that have a result tied to them. A run is considered
+        * having a result when the 'first' column is not null.
+        *
+        * @return List of all drawings that have run in the past
+        */
+       public List<VizonDrawing> findAllRunDrawings()
+       {
+               List<VizonDrawing> drawings = new ArrayList<>();
+
+               try
+               {
+                       PreparedStatement statement = vizonSql.prepare("SELECT * FROM `vizon_drawings` WHERE first IS NOT NULL");
+
+                       ResultSet rs = vizonSql.executeQuery(statement);
+
+                       while (rs.next())
+                       {
+                               VizonDrawing drawing = VizonDrawing.fromResultSet(rs);
+
+                               if (drawing == null)
+                               {
+                                       continue;
+                               }
+
+                               drawings.add(drawing);
+                       }
+
+               }
+               catch (SQLException ex)
+               {
+                       logger.warn("Error in SQL statement to fetch all run drawings", ex);
+               }
+
+               return drawings;
+       }
 }
index ce26a3b0167dfb99a552f0696117025c13cd8a75..26ecc6aca4aa53324b3857a4b8a885e1ab2b5fd8 100644 (file)
@@ -31,6 +31,7 @@ import java.sql.Timestamp;
 import java.time.Duration;
 import java.time.LocalDateTime;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import net.rizon.acid.plugins.vizon.Bet;
 import net.rizon.acid.plugins.vizon.DrawingState;
@@ -44,6 +45,8 @@ import org.slf4j.LoggerFactory;
  */
 public class VizonDrawing
 {
+
+       private static final int DRAWING_SIZE = 6;
        private static final Logger logger = LoggerFactory.getLogger(VizonDrawing.class);
 
        private final int id;
@@ -97,6 +100,8 @@ public class VizonDrawing
 
        private VizonDrawing(int id, List<Integer> draws, LocalDateTime date)
        {
+               assert (draws.size() == DRAWING_SIZE);
+
                this.id = id;
                this.draws = draws;
                this.date = date;
@@ -138,14 +143,16 @@ public class VizonDrawing
                return correct;
        }
 
-       public void setDrawingResult(Bet result)
+       public void setDrawingResult(List<Integer> result)
        {
-               this.draws.set(0, result.getFirst());
-               this.draws.set(1, result.getSecond());
-               this.draws.set(2, result.getThird());
-               this.draws.set(3, result.getFourth());
-               this.draws.set(4, result.getFifth());
-               this.draws.set(5, result.getSixth());
+               if (result.size() != DRAWING_SIZE)
+               {
+                       logger.error("Attempt to set a drawing result which is not of length 6");
+                       return;
+               }
+
+               // Array always has correct size, since it's set in the constructor at 6
+               Collections.copy(this.draws, result);
        }
 
        public DrawingState getState()
index 8e78c1a2221cdabde8065fbcf3790a85362438bb..6f1802afa578b2daf58dc261e0b2d075746f08c4 100644 (file)
@@ -6,7 +6,7 @@ clients:
   vhost: for.everyone
   name: VIzon
   modes: iUop
-  channels: [ vizonChannel ]
+  channels: [ vizonChannel, vizonCommandChannel ]
   commands:
    -
     name: help
@@ -64,6 +64,11 @@ clients:
     channels: [vhost]
     privilege: none
     clazz: net.rizon.acid.plugins.vizon.commands.DeleteCommand
+   -
+    name: order
+    channels: [vizonCommandChannel]
+    privilege: none
+    clazz: net.rizon.acid.plugins.vizon.commands.OrderResultsCommand
 
 vizonChannel: "#opers"
 vizonBot: "VizonBot"