diff --git a/CHANGES.md b/CHANGES.md index ac796719..89fa5d7c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,25 @@ +# Changes in this branch + +## This branch addresses the following issue: +### Time, ([issue #72](https://github.com/hhaslam11/Text-Fighter/issues/72) in the source hhaslam11/Text-Fighter repository, [issue #5](https://github.com/emmamickas/Text-Fighter/issues/5) in the forked emmamickas/Text-Fighter repository) + +## Desired modifications: +### Add a game clock that would keep track of time, days, months, and years. The game time would be faster than real world time by a certain amount. + +## Modified files: + * [Game](https://github.com/emmamickas/Text-Fighter/blob/AddTimeFeatures/src/com/hotmail/kalebmarc/textfighter/main/Game.java) + +## Added Files: + * [GameClock](https://github.com/emmamickas/Text-Fighter/blob/AddTimeFeatures/src/time/GameClock.java) + + +## Testing: +### The following tests were performed to ensure that behavior was preserved after refactoring/changes: + * [TestGameClock](https://github.com/emmamickas/Text-Fighter/blob/AddTimeFeatures/src/tests/TestGameClock.java) + + + ## Additional resources: + ### Please view the following to find additional documentation of the changes and the code involved in the changes. # Changes in this branch (to be used in maintainability measurement work). diff --git a/Class_Diagrams/Package_main.png b/Class_Diagrams/Package_main.png new file mode 100644 index 00000000..71123b65 Binary files /dev/null and b/Class_Diagrams/Package_main.png differ diff --git a/Class_Diagrams/Package_textfighter.png b/Class_Diagrams/Package_textfighter.png new file mode 100644 index 00000000..376885d8 Binary files /dev/null and b/Class_Diagrams/Package_textfighter.png differ diff --git a/Sequence_Dependencies/GameDependencies.png b/Sequence_Dependencies/GameDependencies.png new file mode 100644 index 00000000..86075084 Binary files /dev/null and b/Sequence_Dependencies/GameDependencies.png differ diff --git a/TESTING.md b/TESTING.md index 7f55e716..123691c5 100644 --- a/TESTING.md +++ b/TESTING.md @@ -1,3 +1,11 @@ +# Testing Game Clock Functionality: +## The following tests were performed to ensure that behavior was preserved after refactoring/changes: + ### [TestGameClock](https://github.com/emmamickas/Text-Fighter/blob/AddTimeFeatures/src/tests/TestGameClock.java) including testing of the methods: + * timeCoversion() + * updateGameTime() + * startTime() + * endTime() + # Testing critical hit functionality: ## The following tests were performed to ensure that behavior was preserved and correct after refactoring/changes: ### [Weapon](https://github.com/emmamickas/Text-Fighter/blob/CriticalHits/src/com/hotmail/kalebmarc/textfighter/main/WeaponTest.java) Tests including testing of the methods: diff --git a/src/com/hotmail/kalebmarc/textfighter/main/Game.java b/src/com/hotmail/kalebmarc/textfighter/main/Game.java index 03fe57ae..99d4a2e2 100644 --- a/src/com/hotmail/kalebmarc/textfighter/main/Game.java +++ b/src/com/hotmail/kalebmarc/textfighter/main/Game.java @@ -3,6 +3,8 @@ import com.hotmail.kalebmarc.textfighter.item.*; import com.hotmail.kalebmarc.textfighter.player.*; +import time.GameClock; + import javax.swing.*; import java.util.Scanner; @@ -81,6 +83,7 @@ public void start() { if(Saves.savesPrompt()) { // docschorsch savesPrompt() true only if not exited --> game started with loaded player gameStarted = true; + GameClock.startTimeClock(); break; // docschorsch added another return to Menu.load() if game selected but exited before start } else { @@ -100,6 +103,7 @@ public void start() { Saves.save(); // --> game started with new player gameStarted = true; + GameClock.startTimeClock(); break; } } @@ -137,6 +141,10 @@ public void start() { Ui.println(" Recovery: " + Potion.get("recovery")); Ui.println(" Equipped armour: " + Armour.getEquipped().toString()); Ui.println(" Equipped Weapon: " + Weapon.get().getName()); + GameClock.updateGameTime(); + Ui.println("--Time--"); + Ui.println(" Date: " + GameClock.getGameDate()); + Ui.println(" Clock: " + GameClock.getGameTime()); //Displays ammo only if a weapon is equipped Weapon.displayAmmo(); //-------------------- diff --git a/src/tests/TestGameClock.java b/src/tests/TestGameClock.java new file mode 100644 index 00000000..8200eb76 --- /dev/null +++ b/src/tests/TestGameClock.java @@ -0,0 +1,71 @@ +package tests; + + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Month; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.jupiter.api.Test; + +import time.GameClock; + +public class TestGameClock { + + + + @Test + void testTimeConversion() { + GameClock newClock = new GameClock(); + newClock.setStartTime(0); + newClock.setEndTime(60000); + long conversion = newClock.timeConversion(); + assertEquals(conversion, (newClock.getIncreasedTime() + (newClock.getIncreasedTime() * newClock.getIncreasePercent()))); + assertNotEquals(conversion, (newClock.getIncreasedTime() + ((newClock.getIncreasedTime() + 1) * newClock.getIncreasePercent()))); + assertNotEquals(conversion, (newClock.getIncreasedTime() + (345346 * newClock.getIncreasePercent()))); + } + + @Test + void testUpdateGameTime() { + LocalDateTime testTimeBase = LocalDateTime.of(LocalDate.of(2021, Month.DECEMBER, 2), LocalTime.of(5, 0, 0)); + String testTimeFast; + GameClock newClock = new GameClock(); + newClock.setBaseTime(testTimeBase); + newClock.setStartTime(0); + newClock.setEndTime(6000000); + newClock.updateGameTime(); + testTimeFast = newClock.getFastTime().format(newClock.getDateTimeFormat()); + assertFalse(testTimeFast.equals(newClock.getBaseTime().format(newClock.getDateTimeFormat()))); + + } + + + @BeforeClass + void testValues() { + GameClock newClock = new GameClock(); + assertEquals(newClock.getStartTime(), 0); + assertEquals(newClock.getEndTime(), 0); + } + + @Test + void testDayRollOver() { + LocalDateTime testTimeBase = LocalDateTime.of(LocalDate.of(2021, Month.OCTOBER, 23), LocalTime.of(23, 59, 59)); + GameClock newClock = new GameClock(); + newClock.setBaseTime(testTimeBase); + newClock.splitDateTime(newClock.getBaseTime()); + String baseDate = newClock.getGameDate(); + newClock.setStartTime(0); + newClock.setEndTime(6000000); + newClock.updateGameTime(); + String fastDate = newClock.getGameDate(); + assertFalse(fastDate.equalsIgnoreCase(baseDate)); + } + + + +} diff --git a/src/time/GameClock.java b/src/time/GameClock.java new file mode 100644 index 00000000..fe6d79a6 --- /dev/null +++ b/src/time/GameClock.java @@ -0,0 +1,135 @@ +package time; + +import java.time.Clock; +import java.time.Duration; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.TimeZone; + +public class GameClock { + + + private static LocalDateTime baseTime; + private static LocalDateTime fastTime; + private static long startTime; + private static long endTime; + private static long increasedTime; + // Maybe where we set increasePercent could be moved to Settings? + private static double increasePercent = 0.5; // Time increase can be changed to desired increase. + private static DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("E, MMM dd yyyy-HH:mm:ss"); + private static String gameDate; + private static String gameTime; + + + // Default constructor + public GameClock() { + + } + + // Declares the base time of the players current time. + public static void startTimeClock() { + startTime = System.currentTimeMillis(); + baseTime = LocalDateTime.now(); + } + + // This method is used as a end time place holder so that we can get a time duration. + public static void endTimeCounter() { + endTime = System.currentTimeMillis(); + } + + // The time increase is based off of getting the milliseconds and increasing it by a percentage. + // This method returns the amount of time in milliseconds that the game clock needs to be increased. + public static long timeConversion() { + increasedTime = endTime - startTime; + return (long) (increasedTime + (increasedTime * increasePercent)); + } + + // This method adds the increased time to the base time. + // The base time gets formatted into a string. + // The date and time string gets split into two separate date and time strings. + public static void updateGameTime() { + endTimeCounter(); + fastTime = baseTime.plus(Duration.ofMillis(timeConversion())); + splitDateTime(fastTime); + } + + // This method is for simply splitting up the LocalDateTime into separate strings of date and time. + public static void splitDateTime(LocalDateTime dateTime) { + String[] dateTimeArray = dateTime.format(myFormatObj).split("-"); + gameDate = dateTimeArray[0]; + gameTime = dateTimeArray[1]; + } + + + + + // All these methods below are getters and setters for the variables. + // These will be used mainly for testing purposes. + + public LocalDateTime getBaseTime() { + return GameClock.baseTime; + } + + public void setBaseTime(LocalDateTime newBaseTime) { + GameClock.baseTime = newBaseTime; + } + + public LocalDateTime getFastTime() { + return GameClock.fastTime; + } + + public void setFastTime(LocalDateTime newFastTime) { + GameClock.fastTime = newFastTime; + } + + public long getStartTime() { + return GameClock.startTime; + } + + public void setStartTime(long newStartTime) { + GameClock.startTime = newStartTime; + } + + public long getEndTime() { + return GameClock.endTime; + } + + public void setEndTime(long newEndTime) { + GameClock.endTime = newEndTime; + } + + public long getIncreasedTime() { + return GameClock.increasedTime; + } + + public double getIncreasePercent() { + return GameClock.increasePercent; + } + + public DateTimeFormatter getDateTimeFormat() { + return GameClock.myFormatObj; + } + + // The getter and setter methods below here are static so that they can be used from the Game class to grab + // the date and time as separate strings. + public static void setGameDate(String date) { + GameClock.gameDate = date; + } + + public static String getGameDate() { + return GameClock.gameDate; + } + + public static void setGameTime(String time) { + GameClock.gameTime = time; + } + + public static String getGameTime() { + return GameClock.gameTime; + } + + +}