From 092432d1d901988d831ee43a09a19741c4d9ec92 Mon Sep 17 00:00:00 2001 From: Derrek Woodworth Date: Thu, 21 Nov 2019 18:45:31 -0500 Subject: [PATCH 1/4] Adding base timer code --- .../main/java/gamestatemanager/GameTimer.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Equations/src/main/java/gamestatemanager/GameTimer.java diff --git a/Equations/src/main/java/gamestatemanager/GameTimer.java b/Equations/src/main/java/gamestatemanager/GameTimer.java new file mode 100644 index 0000000..9dd543a --- /dev/null +++ b/Equations/src/main/java/gamestatemanager/GameTimer.java @@ -0,0 +1,75 @@ +package gamestatemanager; + +import java.util.Timer; +import java.util.TimerTask; +import java.util.function.BooleanSupplier; + +public class GameTimer { + public static final int SECONDS_PER_TURN = 15; + private static final int SECONDS_TO_MILLISECONDS = 100; + + // Timers that fire every second and on expiration of the turn + private Timer secondsTimer, expirationTimer; + + // The functions that will be called for these timers + private BooleanSupplier everySecond, onExpiration; + + private boolean hasTimeExpired; + + public boolean isHasTimeExpired() { + return hasTimeExpired; + } + + /** + * This method takes in a function that it will call every second. The function must return a boolean type + * the return should indicate whether or not the timer should keep counting down + * @param everySecond + */ + public GameTimer setOnSecondsUpdate(BooleanSupplier everySecond) { + this.everySecond = everySecond; + return this; + } + /** + * This method takes in a function that it will call on the expiration of the total time for the game timer. + * @param onExpiration + */ + public GameTimer setOnExpirationUpdate(BooleanSupplier onExpiration) { + this.onExpiration = onExpiration; + return this; + } + + /** + * This function cancels all of the timer objects, so none of the funcions passed in will be invoked following + * this call + */ + public void cancel() { + + } + + /** + * Creates the timers and they begin counting down immediately + */ + public void start() { + secondsTimer = new Timer(); + expirationTimer = new Timer(); + + // This is stupid janky... + GameTimer itself = this; + + secondsTimer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + if (!everySecond.getAsBoolean()) { + itself.cancel(); + } + } + },0, SECONDS_TO_MILLISECONDS); + + expirationTimer.schedule(new TimerTask() { + @Override + public void run() { + onExpiration.getAsBoolean(); + } + },SECONDS_PER_TURN * SECONDS_TO_MILLISECONDS); + } +} From 428fd84fa85f6ebe3cef52a06abaae214b105884 Mon Sep 17 00:00:00 2001 From: Derrek Woodworth Date: Thu, 21 Nov 2019 19:24:13 -0500 Subject: [PATCH 2/4] Adding test file --- test-file | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test-file diff --git a/test-file b/test-file new file mode 100644 index 0000000..e69de29 From 164cc1abde9d7d68f96df336e775fca1bcc377c1 Mon Sep 17 00:00:00 2001 From: Derrek Woodworth Date: Tue, 26 Nov 2019 13:48:26 -0500 Subject: [PATCH 3/4] Fixed conversion and added test --- .../main/java/gamestatemanager/GameTimer.java | 2 +- .../test/java/Equations/GameTimerTest.java | 96 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 Equations/src/test/java/Equations/GameTimerTest.java diff --git a/Equations/src/main/java/gamestatemanager/GameTimer.java b/Equations/src/main/java/gamestatemanager/GameTimer.java index 9dd543a..a29032a 100644 --- a/Equations/src/main/java/gamestatemanager/GameTimer.java +++ b/Equations/src/main/java/gamestatemanager/GameTimer.java @@ -6,7 +6,7 @@ public class GameTimer { public static final int SECONDS_PER_TURN = 15; - private static final int SECONDS_TO_MILLISECONDS = 100; + private static final int SECONDS_TO_MILLISECONDS = 1000; // Timers that fire every second and on expiration of the turn private Timer secondsTimer, expirationTimer; diff --git a/Equations/src/test/java/Equations/GameTimerTest.java b/Equations/src/test/java/Equations/GameTimerTest.java new file mode 100644 index 0000000..a8e6ddc --- /dev/null +++ b/Equations/src/test/java/Equations/GameTimerTest.java @@ -0,0 +1,96 @@ +package Equations; + +import fundementalgamemechanics.Mat; +import fundementalgamemechanics.*; +import gamestatemanager.GameTimer; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class GameTimerTest { + private GameTimer gameTimer; + @Test + /** + * Not so much a test but just shows how to use the code + */ + public void basicUsage() { + // Arrange + gameTimer = new GameTimer(); + + // Act + testMat.addToMyMat(new RedDie()); + + // Assert + assertEquals(1, testMat.getMyMat().size()); + } + + @Test + public void removeDieTest() { + // Arrange + final Mat testMat = new Mat(); + + // Act + testMat.addToMyMat(new RedDie()); + testMat.addToMyMat(new RedDie()); + testMat.removeDieByIndex(1); + + // Assert + assertEquals(1, testMat.getMyMat().size()); + } + + @Test + public void removeMissingDieTest() { + // Arrange + final Mat testMat = new Mat(); + + // Act + final boolean falureTest = testMat.removeDieByIndex(1); + + // Assert + assertEquals(false, falureTest); + + } + + @Test + public void removeDieByDie() { + //Arrange + final Die testDie = new RedDie(); + final Mat testMat = new Mat(); + + //Act + testMat.addToMyMat(testDie); + final boolean successTest = testMat.removeDie(testDie); + + //Assert + assertEquals(0, testMat.getMyMat().size()); + assertEquals(true, successTest); + } + + @Test + public void removeMissingDieByDie() { + //Arrange + final Die testDie = new RedDie(); + final Mat testMat = new Mat(); + + //Act + final boolean successTest = testMat.removeDie(testDie); + + //Assert + assertEquals(false, successTest); + } + + @Test + public void reorderMissingDice() { + //Arrange + final Die testDie = new RedDie(); + final Mat testMat = new Mat(); + + //Act + testMat.addToMyMat(testDie); + final boolean falureTest = testMat.reorderDice(0, 1); + + //Assert + assertEquals(false, falureTest); + } + +} From cd15c9e38aa8f9cb10f7b874971cc4050101125e Mon Sep 17 00:00:00 2001 From: Derrek Woodworth Date: Tue, 26 Nov 2019 14:16:49 -0500 Subject: [PATCH 4/4] Adding tests and refactoring to avoid naming collision --- .../{GameTimer.java => GameTimeUpdater.java} | 11 ++- .../main/java/gamestatemanager/Manager.java | 4 +- .../java/Equations/GameTimeUpdaterTest.java | 31 ++++++ .../test/java/Equations/GameTimerTest.java | 96 ------------------- 4 files changed, 39 insertions(+), 103 deletions(-) rename Equations/src/main/java/gamestatemanager/{GameTimer.java => GameTimeUpdater.java} (87%) create mode 100644 Equations/src/test/java/Equations/GameTimeUpdaterTest.java delete mode 100644 Equations/src/test/java/Equations/GameTimerTest.java diff --git a/Equations/src/main/java/gamestatemanager/GameTimer.java b/Equations/src/main/java/gamestatemanager/GameTimeUpdater.java similarity index 87% rename from Equations/src/main/java/gamestatemanager/GameTimer.java rename to Equations/src/main/java/gamestatemanager/GameTimeUpdater.java index a29032a..b358201 100644 --- a/Equations/src/main/java/gamestatemanager/GameTimer.java +++ b/Equations/src/main/java/gamestatemanager/GameTimeUpdater.java @@ -4,7 +4,7 @@ import java.util.TimerTask; import java.util.function.BooleanSupplier; -public class GameTimer { +public class GameTimeUpdater { public static final int SECONDS_PER_TURN = 15; private static final int SECONDS_TO_MILLISECONDS = 1000; @@ -25,7 +25,7 @@ public boolean isHasTimeExpired() { * the return should indicate whether or not the timer should keep counting down * @param everySecond */ - public GameTimer setOnSecondsUpdate(BooleanSupplier everySecond) { + public GameTimeUpdater setOnSecondsUpdate(BooleanSupplier everySecond) { this.everySecond = everySecond; return this; } @@ -33,7 +33,7 @@ public GameTimer setOnSecondsUpdate(BooleanSupplier everySecond) { * This method takes in a function that it will call on the expiration of the total time for the game timer. * @param onExpiration */ - public GameTimer setOnExpirationUpdate(BooleanSupplier onExpiration) { + public GameTimeUpdater setOnExpirationUpdate(BooleanSupplier onExpiration) { this.onExpiration = onExpiration; return this; } @@ -43,7 +43,8 @@ public GameTimer setOnExpirationUpdate(BooleanSupplier onExpiration) { * this call */ public void cancel() { - + secondsTimer.cancel(); + expirationTimer.cancel(); } /** @@ -54,7 +55,7 @@ public void start() { expirationTimer = new Timer(); // This is stupid janky... - GameTimer itself = this; + GameTimeUpdater itself = this; secondsTimer.scheduleAtFixedRate(new TimerTask() { @Override diff --git a/Equations/src/main/java/gamestatemanager/Manager.java b/Equations/src/main/java/gamestatemanager/Manager.java index 9d23603..78e4615 100644 --- a/Equations/src/main/java/gamestatemanager/Manager.java +++ b/Equations/src/main/java/gamestatemanager/Manager.java @@ -24,7 +24,7 @@ public class Manager implements Manger_Reader //Data members private Solver solver; - private GameTimer timer; + private GameTimeUpdater timer; private Player[] players; private ScriptEngine engine; @@ -52,7 +52,7 @@ public Manager(Player[] p) myForbidden = new Mat(); myPermitted = new Mat(); myRequired = new Mat(); - timer = new GameTimer(1); + timer = new GameTimeUpdater(); engine = new ScriptEngineManager().getEngineByName("js"); players = p; diff --git a/Equations/src/test/java/Equations/GameTimeUpdaterTest.java b/Equations/src/test/java/Equations/GameTimeUpdaterTest.java new file mode 100644 index 0000000..4b0cad6 --- /dev/null +++ b/Equations/src/test/java/Equations/GameTimeUpdaterTest.java @@ -0,0 +1,31 @@ +package Equations; + +import fundementalgamemechanics.Mat; +import fundementalgamemechanics.*; +import gamestatemanager.GameTimeUpdater; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class GameTimeUpdaterTest { + private GameTimeUpdater gameTimer; + + @Test + /** + * Not so much a test but just shows how to use the code + */ + public void basicUsage() { + // Arrange + gameTimer = new GameTimeUpdater(); + + //Act + gameTimer.setOnSecondsUpdate(() -> { + System.out.println("This is printed every second"); + // If false is returned the timer cancels itself + return true; + }).setOnExpirationUpdate(() -> { + System.out.println("This is printed when the timer expires"); + return true; + }).start(); + } +} diff --git a/Equations/src/test/java/Equations/GameTimerTest.java b/Equations/src/test/java/Equations/GameTimerTest.java deleted file mode 100644 index a8e6ddc..0000000 --- a/Equations/src/test/java/Equations/GameTimerTest.java +++ /dev/null @@ -1,96 +0,0 @@ -package Equations; - -import fundementalgamemechanics.Mat; -import fundementalgamemechanics.*; -import gamestatemanager.GameTimer; - -import org.junit.Test; -import static org.junit.Assert.assertEquals; - -public class GameTimerTest { - private GameTimer gameTimer; - @Test - /** - * Not so much a test but just shows how to use the code - */ - public void basicUsage() { - // Arrange - gameTimer = new GameTimer(); - - // Act - testMat.addToMyMat(new RedDie()); - - // Assert - assertEquals(1, testMat.getMyMat().size()); - } - - @Test - public void removeDieTest() { - // Arrange - final Mat testMat = new Mat(); - - // Act - testMat.addToMyMat(new RedDie()); - testMat.addToMyMat(new RedDie()); - testMat.removeDieByIndex(1); - - // Assert - assertEquals(1, testMat.getMyMat().size()); - } - - @Test - public void removeMissingDieTest() { - // Arrange - final Mat testMat = new Mat(); - - // Act - final boolean falureTest = testMat.removeDieByIndex(1); - - // Assert - assertEquals(false, falureTest); - - } - - @Test - public void removeDieByDie() { - //Arrange - final Die testDie = new RedDie(); - final Mat testMat = new Mat(); - - //Act - testMat.addToMyMat(testDie); - final boolean successTest = testMat.removeDie(testDie); - - //Assert - assertEquals(0, testMat.getMyMat().size()); - assertEquals(true, successTest); - } - - @Test - public void removeMissingDieByDie() { - //Arrange - final Die testDie = new RedDie(); - final Mat testMat = new Mat(); - - //Act - final boolean successTest = testMat.removeDie(testDie); - - //Assert - assertEquals(false, successTest); - } - - @Test - public void reorderMissingDice() { - //Arrange - final Die testDie = new RedDie(); - final Mat testMat = new Mat(); - - //Act - testMat.addToMyMat(testDie); - final boolean falureTest = testMat.reorderDice(0, 1); - - //Assert - assertEquals(false, falureTest); - } - -}