Skip to content

Commit

Permalink
Additional tests for CharacterLibrary, as well as some fixes.
Browse files Browse the repository at this point in the history
A few more tests to go.  Also, a gentle reminder not to code while
feeling feverish.  Some rather embarrassing fixes.
  • Loading branch information
danielBingham committed Jan 16, 2024
1 parent b43fd94 commit ea7f7c2
Show file tree
Hide file tree
Showing 2 changed files with 333 additions and 11 deletions.
27 changes: 16 additions & 11 deletions game/library/character.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ def kill(self, character):
CharacterLibrary: Returns `self` to allow chaining.
"""

if character.player:
character.player.status = character.player.STATUS_ACCOUNT
character.player.account_state = "menu"
player = character.player
if player:
player.status = character.player.STATUS_ACCOUNT
player.current_account_state = "account-menu"

character.position = character.POSITION_DEAD
player.character = None
character.player = None
return self

def adjustSleep(self, character, amount):
Expand Down Expand Up @@ -81,8 +86,8 @@ def adjustCalories(self, character, amount):
# max_calories is the number of calories needed to survive a day,
# this results in stamina effectively being the number of days a
# character can survive without eating.
character.stamina = character.max_stamina + (character.reserves.calories / character.reserves.max_calories)
if character.stamina <= 0:
character.attributes.stamina = character.attributes.max_stamina + (character.reserves.calories / character.reserves.max_calories)
if character.attributes.stamina <= 0:
self.kill(character)
return True

Expand All @@ -106,8 +111,8 @@ def adjustThirst(self, character, amount):
return False
character.reserves.thirst += amount
if character.reserves.thirst < 0:
character.stamina = character.max_stamina + 4 * (character.reserves.thirst / character.reserves.max_thirst)
if character.stamina <= 0:
character.attributes.stamina = character.attributes.max_stamina + 4 * (character.reserves.thirst / character.reserves.max_thirst)
if character.attributes.stamina <= 0:
self.kill(character)
return True

Expand All @@ -128,9 +133,9 @@ def adjustWind(self, character, amount):
"""

# Wind can't go negative.
if self.reserves.wind + amount < 0:
if character.reserves.wind + amount < 0:
return False
self.reserves.wind = min(self.reserves.wind + amount, self.reserves.max_wind)
character.reserves.wind = min(character.reserves.wind + amount, character.reserves.max_wind)
return True

def adjustEnergy(self, character, amount):
Expand All @@ -150,9 +155,9 @@ def adjustEnergy(self, character, amount):
"""

# Energy can't go negative.
if self.reserves.energy + amount < 0:
if character.reserves.energy + amount < 0:
return False
self.reserves.energy = min(self.reserves.energy + amount, self.reserves.max_energy)
character.reserves.energy = min(character.reserves.energy + amount, character.reserves.max_energy)
return True

def calculateReserves(self, character):
Expand Down
317 changes: 317 additions & 0 deletions tests/game/library/test_character.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,317 @@
from unittest.mock import Mock

from game.library.library import Library
from game.store.store import Store
from game.player import Player
from game.store.models.character import PlayerCharacter
from game.account_menu.menu import AccountMenu

def test_kill():
"""
Test kill appropriately kills a character.
"""

store = Store('test', '')
library = Library(store)

socket = Mock()
player = Player(socket, None, None)
player.write = Mock()

player.character = PlayerCharacter()
player.character.player = player

store.players.append(player)

library.character.kill(player.character)

assert player.status == player.STATUS_ACCOUNT
assert player.current_account_state == AccountMenu.NAME

def test_adjustSleep():
"""
Test adjustSleep to determine if it appropriately adjusts sleep.
"""

store = Store('test', '')
library = Library(store)

socket = Mock()
player = Player(socket, None, None)
player.write = Mock()

player.character = PlayerCharacter()
player.character.player = player

store.players.append(player)

success = library.character.adjustSleep(player.character, -2)
assert player.character.reserves.sleep == 14
assert success is True

success = library.character.adjustSleep(player.character, 2)
assert player.character.reserves.sleep == 16
assert success is True

def test_adjustCalories():
"""
Test adjustCalories to determine if it appropriately adjusts calories.
"""

store = Store('test', '')
library = Library(store)

socket = Mock()
player = Player(socket, None, None)
player.write = Mock()

player.character = PlayerCharacter()
player.character.player = player

store.players.append(player)

success = library.character.adjustCalories(player.character, -2)
assert player.character.reserves.calories == 2398
assert success is True

success = library.character.adjustCalories(player.character, 2)
assert player.character.reserves.calories == 2400
assert success is True

def test_adjustCalories_past_zero():
"""
Test adjustCalories down below zero.
"""

store = Store('test', '')
library = Library(store)

socket = Mock()
player = Player(socket, None, None)
player.write = Mock()

player.character = PlayerCharacter()
player.character.player = player

store.players.append(player)

player.character.reserves.calories = 0

success = library.character.adjustCalories(player.character, -2400)
assert player.character.reserves.calories == -2400
assert player.character.attributes.stamina == 9
assert success is True

def test_adjustCalories_to_death():
"""
Test adjusting calories to the point where it kills the character.
"""

store = Store('test', '')
library = Library(store)

socket = Mock()
player = Player(socket, None, None)
player.write = Mock()

character = PlayerCharacter()

player.character = character
character.player = player

store.players.append(player)

character.reserves.calories = -9*2400

success = library.character.adjustCalories(character, -2400)
assert character.reserves.calories == -10*2400
assert character.attributes.stamina == 0
assert character.position == character.POSITION_DEAD
assert character.player is None

assert player.status == player.STATUS_ACCOUNT
assert player.character is None

assert success is True

def test_adjustThirst():
"""
Test using adjustThirst to adjust the player's thirst reserve.
"""

store = Store('test', '')
library = Library(store)

socket = Mock()
player = Player(socket, None, None)
player.write = Mock()

player.character = PlayerCharacter()
player.character.player = player

store.players.append(player)

success = library.character.adjustThirst(player.character, -2)
assert player.character.reserves.thirst == 3998
assert success is True

success = library.character.adjustThirst(player.character, 2)
assert player.character.reserves.thirst == 4000
assert success is True

def test_adjustThirst_past_zero():
"""
Test using adjustThirst to adjust the player's thirst reserve below zero.
"""

store = Store('test', '')
library = Library(store)

socket = Mock()
player = Player(socket, None, None)
player.write = Mock()

player.character = PlayerCharacter()
player.character.player = player

store.players.append(player)

player.character.reserves.thirst = 0

success = library.character.adjustThirst(player.character, -4000)
assert player.character.reserves.thirst == -4000
assert player.character.attributes.stamina == 6
assert success is True

def test_adjustThirst_to_death():
"""
Test adjusting thirst to the point where it kills the character.
"""

store = Store('test', '')
library = Library(store)

socket = Mock()
player = Player(socket, None, None)
player.write = Mock()

character = PlayerCharacter()

player.character = character
character.player = player

store.players.append(player)

character.reserves.thirst = 2*-4000

success = library.character.adjustThirst(character, -2000)
assert character.reserves.thirst == -10000
assert character.attributes.stamina == 0
assert character.position == character.POSITION_DEAD
assert character.player is None

assert player.status == player.STATUS_ACCOUNT
assert player.character is None

assert success is True

def test_adjustWind():
"""
Test adjusting the character's wind reserve.
"""

store = Store('test', '')
library = Library(store)

socket = Mock()
player = Player(socket, None, None)
player.write = Mock()

player.character = PlayerCharacter()
player.character.player = player

store.players.append(player)

success = library.character.adjustWind(player.character, -2)
assert player.character.reserves.wind == 28
assert success is True

success = library.character.adjustWind(player.character, 2)
assert player.character.reserves.wind == 30
assert success is True

def test_adjustWind_boundaries():
"""
Test adjusting the character's wind reserve. Confirm it can't go over max_wind or negative.
"""

store = Store('test', '')
library = Library(store)

socket = Mock()
player = Player(socket, None, None)
player.write = Mock()

player.character = PlayerCharacter()
player.character.player = player

store.players.append(player)

success = library.character.adjustWind(player.character, -32)
assert player.character.reserves.wind == 30
assert success is False

success = library.character.adjustWind(player.character, 2)
assert player.character.reserves.wind == 30
assert success is True

def test_adjustEnergy():
"""
Test adjusting the character's energy reserve.
"""

store = Store('test', '')
library = Library(store)

socket = Mock()
player = Player(socket, None, None)
player.write = Mock()

player.character = PlayerCharacter()
player.character.player = player

store.players.append(player)

success = library.character.adjustEnergy(player.character, -2)
assert player.character.reserves.energy == 9998
assert success is True

success = library.character.adjustEnergy(player.character, 2)
assert player.character.reserves.energy == 10000
assert success is True

def test_adjustEnergy_boundaries():
"""
Test adjusting the character's energy reserve. Confirm it can't go over max_energy or negative.
"""

store = Store('test', '')
library = Library(store)

socket = Mock()
player = Player(socket, None, None)
player.write = Mock()

player.character = PlayerCharacter()
player.character.player = player

store.players.append(player)

success = library.character.adjustEnergy(player.character, -10002)
assert player.character.reserves.energy == 10000
assert success is False

success = library.character.adjustEnergy(player.character, 2)
assert player.character.reserves.energy == 10000
assert success is True

0 comments on commit ea7f7c2

Please sign in to comment.