Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion schemas/player.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@
"priceForBait": {
"type": "number",
"minimum": 0
},
"energy": {
"type": "integer",
"minimum": 0,
"maximum": 100
}
},
"required": [
"fishCount",
"money",
"moneyInBank",
"fishMultiplier",
"priceForBait"
"priceForBait",
"energy"
]
}
18 changes: 16 additions & 2 deletions src/location/docks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ def run(self):
)

if input == "1":
self.fish()
return LocationType.DOCKS
if self.player.energy >= 10:
self.fish()
return LocationType.DOCKS
else:
self.currentPrompt.text = "You're too tired to fish! Go home and sleep."
return LocationType.DOCKS

elif input == "2":
self.currentPrompt.text = "What would you like to do?"
Expand Down Expand Up @@ -65,12 +69,22 @@ def fish(self):

hours = random.randint(1, 10)

# Check if player has enough energy for all hours
energy_needed = hours * 10
if self.player.energy < energy_needed:
# Fish for as many hours as energy allows
hours = self.player.energy // 10
if hours == 0:
self.currentPrompt.text = "You're too tired to fish! Go home and sleep."
return

for i in range(hours):
print("><> ")
sys.stdout.flush()
time.sleep(0.5)
self.stats.hoursSpentFishing += 1
self.timeService.increaseTime()
self.player.energy -= 10 # Consume 10 energy per hour

fishToAdd = random.randint(1, 10) * self.player.fishMultiplier
self.player.fishCount += fishToAdd
Expand Down
3 changes: 2 additions & 1 deletion src/location/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def run(self):

def sleep(self):
self.timeService.increaseDay()
self.currentPrompt.text = "You sleep until the next morning."
self.player.energy = 100 # Restore full energy when sleeping
self.currentPrompt.text = "You sleep until the next morning. You feel refreshed!"

def displayStats(self):
self.userInterface.lotsOfSpace()
Expand Down
1 change: 1 addition & 0 deletions src/player/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ def __init__(self):
self.moneyInBank = 0.01
self.fishMultiplier = 1
self.priceForBait = 50
self.energy = 100
2 changes: 2 additions & 0 deletions src/player/playerJsonReaderWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def createJsonFromPlayer(self, player):
"money": player.money,
"moneyInBank": player.moneyInBank,
"priceForBait": player.priceForBait,
"energy": player.energy,
}

def createPlayerFromJson(self, playerJson):
Expand All @@ -19,6 +20,7 @@ def createPlayerFromJson(self, playerJson):
player.money = playerJson["money"]
player.moneyInBank = playerJson["moneyInBank"]
player.priceForBait = playerJson["priceForBait"]
player.energy = playerJson.get("energy", 100) # Default to 100 for backwards compatibility
return player

def writePlayerToFile(self, player, jsonFile):
Expand Down
1 change: 1 addition & 0 deletions src/ui/userInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def showOptions(
print(" | " + self.times[self.timeService.time])
print(" | Money: $%d" % self.player.money)
print(" | Fish: %d" % self.player.fishCount)
print(" | Energy: %d" % self.player.energy)
print("\n " + self.currentPrompt.text)
self.divider()
self.n = 1
Expand Down
53 changes: 53 additions & 0 deletions tests/location/test_docks.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,56 @@ def test_fish():
assert docks.time.sleep.call_count == 4
assert docksInstance.player.fishCount == 3
assert docksInstance.stats.totalFishCaught == 3


def test_run_fish_action_low_energy():
# prepare
docksInstance = createDocks()
docksInstance.player.energy = 5 # Too low to fish
docksInstance.userInterface.showOptions = MagicMock(return_value="1")

# call
nextLocation = docksInstance.run()

# check
assert nextLocation == LocationType.DOCKS
assert docksInstance.currentPrompt.text == "You're too tired to fish! Go home and sleep."


def test_fish_consumes_energy():
# prepare
docksInstance = createDocks()
docksInstance.player.energy = 100
docksInstance.userInterface.lotsOfSpace = MagicMock()
docksInstance.userInterface.divider = MagicMock()
docks.print = MagicMock()
docks.sys.stdout.flush = MagicMock()
docks.time.sleep = MagicMock()
docks.random.randint = MagicMock(return_value=3) # Fish for 3 hours, catch 3 fish
docksInstance.timeService.increaseTime = MagicMock()

# call
docksInstance.fish()

# check
assert docksInstance.player.energy == 100 - (3 * 10) # Should lose 30 energy (3 hours * 10 per hour)


def test_fish_with_limited_energy():
# prepare
docksInstance = createDocks()
docksInstance.player.energy = 25 # Only enough for 2 hours
docksInstance.userInterface.lotsOfSpace = MagicMock()
docksInstance.userInterface.divider = MagicMock()
docks.print = MagicMock()
docks.sys.stdout.flush = MagicMock()
docks.time.sleep = MagicMock()
docks.random.randint = MagicMock(return_value=5) # Would normally fish for 5 hours, but energy limits to 2
docksInstance.timeService.increaseTime = MagicMock()

# call
docksInstance.fish()

# check
assert docksInstance.player.energy == 5 # Should be 25 - (2 * 10)
assert docksInstance.timeService.increaseTime.call_count == 2 # Only fished for 2 hours due to energy limit
17 changes: 16 additions & 1 deletion tests/location/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,28 @@ def test_sleep():
# prepare
homeInstance = createHome()
homeInstance.timeService.increaseDay = MagicMock()
homeInstance.player.energy = 50 # Set energy to something less than 100

# call
homeInstance.sleep()

# check
homeInstance.timeService.increaseDay.assert_called_once()
assert homeInstance.currentPrompt.text == "You sleep until the next morning."
assert homeInstance.currentPrompt.text == "You sleep until the next morning. You feel refreshed!"
assert homeInstance.player.energy == 100 # Energy should be restored to full


def test_sleep_restores_energy():
# prepare
homeInstance = createHome()
homeInstance.timeService.increaseDay = MagicMock()
homeInstance.player.energy = 10 # Low energy

# call
homeInstance.sleep()

# check
assert homeInstance.player.energy == 100


def test_displayStats():
Expand Down
1 change: 1 addition & 0 deletions tests/player/test_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ def test_initialization():
assert player.money == 20
assert player.moneyInBank == 0.01
assert player.fishMultiplier == 1
assert player.energy == 100
23 changes: 23 additions & 0 deletions tests/player/test_playerJsonReaderWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def test_createPlayerFromJson():
"money": 0,
"moneyInBank": 0,
"priceForBait": 50,
"energy": 100,
}

playerJsonReaderWriter = createPlayerJsonReaderWriter()
Expand All @@ -49,3 +50,25 @@ def test_createPlayerFromJson():
assert player.fishMultiplier == playerJson["fishMultiplier"]
assert player.money == playerJson["money"]
assert player.moneyInBank == playerJson["moneyInBank"]
assert player.energy == playerJson["energy"]


def test_createPlayerFromJson_backwards_compatibility():
# Test that old save files without energy still work
playerJson = {
"fishCount": 5,
"fishMultiplier": 2,
"money": 100,
"moneyInBank": 50,
"priceForBait": 75,
# Note: no energy field
}

playerJsonReaderWriter = createPlayerJsonReaderWriter()
player = playerJsonReaderWriter.createPlayerFromJson(playerJson)

assert player.fishCount == playerJson["fishCount"]
assert player.fishMultiplier == playerJson["fishMultiplier"]
assert player.money == playerJson["money"]
assert player.moneyInBank == playerJson["moneyInBank"]
assert player.energy == 100 # Should default to 100
2 changes: 1 addition & 1 deletion tests/ui/test_userInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_showOptions():
userInterfaceInstance.showOptions("descriptor", ["option1", "option2"])

# check
assert userInterface.print.call_count == 8
assert userInterface.print.call_count == 9
userInterfaceInstance.lotsOfSpace.assert_called()
assert userInterfaceInstance.divider.call_count == 3
userInterface.input.assert_called_with("\n> ")
Loading