Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add time features #132

Merged
merged 9 commits into from
Nov 15, 2021
22 changes: 22 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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).


Expand Down
Binary file added Class_Diagrams/Package_main.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Class_Diagrams/Package_textfighter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sequence_Dependencies/GameDependencies.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
8 changes: 8 additions & 0 deletions src/com/hotmail/kalebmarc/textfighter/main/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -100,6 +103,7 @@ public void start() {
Saves.save();
// --> game started with new player
gameStarted = true;
GameClock.startTimeClock();
break;
}
}
Expand Down Expand Up @@ -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();
//--------------------
Expand Down
71 changes: 71 additions & 0 deletions src/tests/TestGameClock.java
Original file line number Diff line number Diff line change
@@ -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));
}



}
135 changes: 135 additions & 0 deletions src/time/GameClock.java
Original file line number Diff line number Diff line change
@@ -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;
}


}