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

Track game play time #133

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
22 changes: 22 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Changes in this branch

## This branch addresses the following issue:
### Time, ([issue #128](https://github.com/hhaslam11/Text-Fighter/issues/128) in the source hhaslam11/Text-Fighter repository, [issue #10](https://github.com/emmamickas/Text-Fighter/issues/10) in the forked emmamickas/Text-Fighter repository)

## Desired modifications:
### Add a feature that will keep track of the total time played and the current session game play.

## Modified files:
* [Game](https://github.com/emmamickas/Text-Fighter/blob/AddTimeFeatures/src/com/hotmail/kalebmarc/textfighter/main/Game.java)
* [Saves](https://github.com/emmamickas/Text-Fighter/blob/TrackGamePlayTime/src/com/hotmail/kalebmarc/textfighter/main/Saves.java)

## Added Files:
* [TestTimeTrack](https://github.com/emmamickas/Text-Fighter/blob/TrackGamePlayTime/src/tests/TestTimeTrack.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.
Binary file added Class_Diagrams/Package_main_after.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_main_before.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_player_after.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_player_before.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_before.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.
Binary file added Sequence_Dependencies/SavesDependencies.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Testing Game Play Time Tracking Functionality:
## The following tests were performed to ensure that behavior was preserved after refactoring/changes:
### [TestTimeTrack](https://github.com/emmamickas/Text-Fighter/blob/TrackGamePlayTime/src/tests/TestTimeTrack.java) including testing of the methods:
* timeCoversion()
* testTimeUpdate()
* testNewPlayerTime()
* testSavedPlayersTime()
5 changes: 5 additions & 0 deletions src/com/hotmail/kalebmarc/textfighter/main/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void start() {
if(Saves.savesPrompt()) {
// docschorsch savesPrompt() true only if not exited --> game started with loaded player
gameStarted = true;
GameTime.startSessionTime();
break;
// docschorsch added another return to Menu.load() if game selected but exited before start
} else {
Expand All @@ -100,6 +101,7 @@ public void start() {
Saves.save();
// --> game started with new player
gameStarted = true;
GameTime.startSessionTime();
break;
}
}
Expand All @@ -110,6 +112,7 @@ public void start() {
if (Stats.kills > Stats.highScore) Stats.highScore = Stats.kills;
Achievements.check();
Saves.save();

Ui.cls();

/*
Expand Down Expand Up @@ -137,6 +140,8 @@ public void start() {
Ui.println(" Recovery: " + Potion.get("recovery"));
Ui.println(" Equipped armour: " + Armour.getEquipped().toString());
Ui.println(" Equipped Weapon: " + Weapon.get().getName());
Ui.println("--Session Time--");
Ui.println(" " + GameTime.printSessionTime());
//Displays ammo only if a weapon is equipped
Weapon.displayAmmo();
//--------------------
Expand Down
11 changes: 11 additions & 0 deletions src/com/hotmail/kalebmarc/textfighter/main/Saves.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static void save() {
path = path.replaceAll("%20", " ");

setup();
GameTime.updateTotalTime();

/*
* TODO: make a version checker that checks each part of a version ex: 1.4.1DEV
Expand Down Expand Up @@ -163,6 +164,10 @@ public static void save() {
set("Stats.Items_Crafted", Stats.timesCheated);
set("Stats.Games_Played.Dice", Stats.diceGamesPlayed);
set("Stats.Games_Played.Slots", Stats.slotGamesPlayed);
set("Stats.Days_Played", GameTime.getTotalDayTime());
set("Stats.Hours_Played", GameTime.getTotalHourTime());
set("Stats.Minutes_Played", GameTime.getTotalMinuteTime());
set("Stats.Seconds_Played", GameTime.getTotalSecondTime());

try {
if (!saveLocation.exists())
Expand Down Expand Up @@ -315,6 +320,12 @@ public static boolean load() {
Stats.itemsCrafted = getInteger("Stats.Items_Crafted");
Stats.diceGamesPlayed = getInteger("Stats.Games_Played.Dice");
Stats.slotGamesPlayed = getInteger("Stats.Games_Played.Slots");

//Time Played
GameTime.setTotalDayTime(getDouble("Stats.Days_Played"));
GameTime.setTotalHourTime(getDouble("Stats.Hours_Played"));
GameTime.setTotalMinuteTime(getDouble("Stats.Minutes_Played"));
GameTime.setTotalSecondTime(getDouble("Stats.Seconds_Player"));

return true;
}
Expand Down
165 changes: 165 additions & 0 deletions src/com/hotmail/kalebmarc/textfighter/player/GameTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package com.hotmail.kalebmarc.textfighter.player;

public class GameTime {


// Variables
private static long startTime;
private static long endTime;
private static String sessionTime;
private static String totalTime;
private static double totalSecondTime;
private static double totalMinuteTime;
private static double totalHourTime;
private static double totalDayTime;
private static double oldTotalSecond;
private static double oldTotalMinute;
private static double oldTotalHour;
private static double oldTotalDay;

//Variables
private static double secondTime;
private static double minuteTime;
private static double hourTime;
private static double dayTime;

public GameTime() {

}

// Set the total time to 0 for new players.
public static void initializeNewTime() {

setTotalSecondTime(0.0);
setTotalMinuteTime(0.0);
setTotalHourTime(0.0);
setTotalDayTime(0.0);
secondTime = 0.0;
minuteTime = 0.0;
hourTime = 0.0;
dayTime = 0.0;
}

public static void initializeSavedTime() {
oldTotalSecond = totalSecondTime;
oldTotalMinute = totalMinuteTime;
oldTotalHour = totalHourTime;
oldTotalDay = totalDayTime;

}

// This method is to start counting the time when the game has started.
public static void startSessionTime() {
startTime = System.currentTimeMillis();
}

// This method is for the end time place holder.
public static void endTime() {
endTime = System.currentTimeMillis();
calculateTime();
}

// This method will calculate how much time has been played.
// It will set the seconds, minutes, hours, and days.
public static void calculateTime() {
secondTime = (double) (endTime - startTime) / 1000;
minuteTime = secondTime / 60;
hourTime = minuteTime / 60;
dayTime = hourTime / 24;
}

// This method will print the current session time played to the console.
public static String printSessionTime() {
endTime();
sessionTime = " Days: " + String.format("%.2f", dayTime) + "\n"
+ " Hours: " + String.format("%.2f", hourTime) + "\n"
+ " Minutes: " + String.format("%.2f", minuteTime) + "\n"
+ " Seconds: " + String.format("%.2f", secondTime);
return sessionTime;
}

// This method will update the total amount of time played.
public static void updateTotalTime() {
endTime();
totalTime = " Days: " + String.format("%.2f", getTotalDayTime()) + "\n"
+ " Hours: " + String.format("%.2f", getTotalHourTime()) + "\n"
+ " Minutes: " + String.format("%.2f", getTotalMinuteTime()) + "\n"
+ " Seconds: " + String.format("%.2f", getTotalSecondTime());
}

// The following methods will up update the total time for seconds, minutes, hours, and days.
public static double getTotalSecondTime() {
return totalSecondTime = oldTotalSecond + secondTime;
}

public static double getTotalMinuteTime() {
return totalMinuteTime = oldTotalMinute + minuteTime;
}

public static double getTotalHourTime() {
return totalHourTime = oldTotalHour + hourTime;
}

public static double getTotalDayTime() {
return totalDayTime = oldTotalDay + dayTime;
}


// The following methods sets the time totals for the saved file.
// Set time totals from save file
public static void setTotalSecondTime(double oldSeconds) {
oldTotalSecond = oldSeconds;
}

// Set time totals from save file
public static void setTotalMinuteTime(double oldMinutes) {
oldTotalMinute = oldMinutes;
}

// Set time totals from save file
public static void setTotalHourTime(double oldHours) {
oldTotalHour = oldHours;
}

// Set time totals from save file
public static void setTotalDayTime(double oldDays) {
oldTotalDay = oldDays;
}

public static String getTotalTime() {
return GameTime.totalTime;
}

// These getters and setter will mainly be used for testing purposes.
public void setStartTime(long start) {
GameTime.startTime = start;
}

public long getStartTime() {
return GameTime.startTime;
}

public void setEndTime(long end) {
GameTime.endTime = end;
}

public long getEndTime() {
return GameTime.endTime;
}

public Double getSeconds() {
return GameTime.secondTime;
}

public Double getMinutes() {
return GameTime.minuteTime;
}

public Double getHours() {
return GameTime.hourTime;
}

public Double getDays() {
return GameTime.dayTime;
}
}
3 changes: 3 additions & 0 deletions src/com/hotmail/kalebmarc/textfighter/player/Stats.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ private Stats() {
public static void view() {

updateKillDeathRatio();
GameTime.updateTotalTime();

Ui.cls();
Ui.println("-------------------------------------------------");
Expand Down Expand Up @@ -86,6 +87,8 @@ public static void view() {
Ui.println(" Lottery Tickets Bought - " + lotteryTicketsBought);
Ui.println(" Lotteries Won - " + lotteryWon);
Ui.println();
Ui.println("Total Time Played: ");
Ui.println(" " + GameTime.getTotalTime());
Ui.println("-------------------------------------------------");
Ui.pause();
}
Expand Down
56 changes: 56 additions & 0 deletions src/tests/TestTimeTrack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package tests;

import static org.junit.Assert.*;

import org.junit.Test;

import com.hotmail.kalebmarc.textfighter.player.GameTime;

public class TestTimeTrack {

@Test
public void testTimeConversion() {
GameTime newTime = new GameTime();
newTime.setStartTime(0);
newTime.setEndTime(600000);
newTime.calculateTime();
assertEquals(600.0, newTime.getSeconds(), 0.0);
assertEquals(10.0, newTime.getMinutes(), 0.0);
assertEquals(0.167, newTime.getHours(), 0.05);
assertEquals(0.007, newTime.getDays(), 0.05);
}

@Test
public void testTimeUpdated() {
GameTime newTime = new GameTime();
newTime.setStartTime(0);
newTime.setEndTime(600000);
newTime.calculateTime();
assertEquals(600.0, newTime.getTotalSecondTime(), 0.0);
assertEquals(10.0, newTime.getTotalMinuteTime(), 0.0);
assertEquals(0.167, newTime.getTotalHourTime(), 0.05);
assertEquals(0.007, newTime.getTotalDayTime(), 0.05);
}

@Test
public void testNewPlayerTime() {
GameTime newTime = new GameTime();
newTime.initializeNewTime();
assertEquals(0.0, newTime.getSeconds(), 0.0);
assertEquals(0.0, newTime.getMinutes(), 0.0);
assertEquals(0.0, newTime.getHours(), 0.0);
assertEquals(0.0, newTime.getDays(), 0.0);

}

@Test
public void testSavedPlayerTime() {
GameTime newTime = new GameTime();
newTime.initializeSavedTime();
assertNotEquals(0.0, newTime.getSeconds(), 0.0);
assertNotEquals(0.0, newTime.getMinutes(), 0.0);
assertNotEquals(0.0, newTime.getHours(), 0.0);
assertNotEquals(0.0, newTime.getDays(), 0.0);
}

}