Skip to content

Commit

Permalink
Fixing the tests (and some bugs caught by the tests).
Browse files Browse the repository at this point in the history
  • Loading branch information
danielBingham committed Feb 16, 2024
1 parent 901cb9d commit 63a5881
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 26 deletions.
1 change: 0 additions & 1 deletion game/heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ def __init__(self, store, library):
self.store = store
self.library = library


def heartbeat(self):
"""
A method called on every loop that can be used for actions that need to
Expand Down
7 changes: 4 additions & 3 deletions game/store/models/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,15 @@ def fromPrototypeJson(self, data):
return self.fromJson(data)

def toJson(self):
json = {}
data = {}

materials = []
for material in self.requiredMaterials:
materials.append(material.toJson())
json['requiredMaterials'] = materials
data['requiredMaterials'] = materials

json['requiredTools'] = self.requiredTools
data['requiredTools'] = self.requiredTools
return data

def fromJson(self, data):
for requiredMaterialJson in data['requiredMaterials']:
Expand Down
3 changes: 2 additions & 1 deletion game/store/models/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def toJson(self):

return data

def fromJson(self):
def fromJson(self, data):
self.year = data['year']
self.month = data['month']
self.day = data['day']
Expand All @@ -58,6 +58,7 @@ def fromJson(self):

self.night = data['night']
self.loop = data['loop']
return self


class World(NamedModel):
Expand Down
2 changes: 1 addition & 1 deletion game/store/store.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import glob, copy, os
import glob, os

from game.store.models.world import World
from game.store.models.account import Account
Expand Down
2 changes: 1 addition & 1 deletion generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def generate(self):
Generate the world.
'''

print("Generating world %s, width [%d, %d] totaling %d rooms of size %d meters by %d meters" \
print("Generating world %s, width [%d, %d] totaling %d rooms of size %d meters by %d meters"
% (self.world.name, self.world.width, self.world.width, self.world.width * self.world.width, self.world.room_width, self.world.room_width))

if self.generate_heights and (self.world.terrain.size == 0 or self.regenerate_heights):
Expand Down
2 changes: 1 addition & 1 deletion generator/library/inria_water.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
###############################################################################

import math, random
import math
import numpy as np
import imageio

Expand Down
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def gameLoop(serverSocket, library, store, account_interpreter, game_interpreter
# The length of a single loop in nanoseconds.
loop_length = 1000000000/store.world.time.loops_a_second

overrun = 0

heartbeat = Heartbeat(store, library)

# The Game Loop
Expand Down
4 changes: 4 additions & 0 deletions tests/game/commands/test_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from game.player import Player
from game.store.models.character import PlayerCharacter, Character
from game.store.models.room import Room
from game.store.models.world import World

from game.commands.communication import Say

Expand All @@ -15,6 +16,7 @@ def test_Say_to_empty_room():
"""

store = Store('test', '')
store.world = World()
library = Library(store)

say = Say(library, store)
Expand All @@ -40,6 +42,7 @@ def test_Say_to_player_populated_room():
"""

store = Store('test', '')
store.world = World()
library = Library(store)

say = Say(library, store)
Expand Down Expand Up @@ -84,6 +87,7 @@ def test_Say_to_npc_populated_room():
"""

store = Store('test', '')
store.world = World()
library = Library(store)

say = Say(library, store)
Expand Down
10 changes: 6 additions & 4 deletions tests/game/commands/test_crafting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
from game.store.models.character import PlayerCharacter
from game.store.models.room import Room
from game.store.models.item import Item
from game.store.models.world import World

from game.commands.crafting import Craft, Harvest

tool_json = {
"name": "tool",
"description": "a tool",
"details": "A tool for testing.",
"keywords": ["tool"],
"keywords": "tool",
"length": 1,
"width": 1,
"height": 1,
Expand Down Expand Up @@ -41,7 +42,7 @@
"name": "tool",
"description": "a tool",
"details": "A tool for testing.",
"keywords": ["tool"],
"keywords": "tool",
"length": 1,
"width": 1,
"height": 1,
Expand Down Expand Up @@ -76,7 +77,7 @@
"name": "craftable material",
"description": "a craftable material",
"details": "A material that requires a tool to craft.",
"keywords": ["craftable material", "material"],
"keywords": "craftable material",
"length": 1,
"width": 1,
"height": 1,
Expand Down Expand Up @@ -104,7 +105,7 @@
"name": "material",
"description": "a material",
"details": "A material for testing.",
"keywords": ["material"],
"keywords": "material",
"length": 1,
"width": 1,
"height": 1,
Expand Down Expand Up @@ -241,6 +242,7 @@ def test_Craft_when_called_with_missing_materials():
"""

store = Store('test', '')
store.world = World()

tool = Item()
tool.fromJson(tool_json)
Expand Down
15 changes: 14 additions & 1 deletion tests/game/commands/test_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from game.player import Player
from game.store.models.character import PlayerCharacter
from game.store.models.room import Room
from game.store.models.world import World

from game.commands.information import Status, Look

Expand Down Expand Up @@ -33,7 +34,11 @@
"maxSleep": 16,
"maxThirst": 4000,
"sleep": 16,
"thirst": 4000
"thirst": 4000 ,
"energy": 10000,
"maxEnergy": 10000,
"wind": 30,
"maxWind": 30
},
"room": 1,
"sex": "male",
Expand Down Expand Up @@ -186,6 +191,10 @@ def test_Look():
"""

store = Store('test', '')
store.world = World()
store.world.time.hour = 12
store.world.time.night = False

library = Library(store)

look = Look(library, store)
Expand Down Expand Up @@ -225,6 +234,10 @@ def test_Look_in_direction():
"""

store = Store('test', '')
store.world = World()
store.world.time.hour = 12
store.world.time.night = False

library = Library(store)

look = Look(library, store)
Expand Down
7 changes: 6 additions & 1 deletion tests/game/library/test_character.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ def test_kill():
store = Store('test', '')
library = Library(store)


account_interpreter = Mock()

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

player.character = PlayerCharacter()
Expand Down Expand Up @@ -113,6 +116,7 @@ def test_adjustCalories_to_death():
socket = Mock()
player = Player(socket, None, None)
player.write = Mock()
player.setAccountState = Mock()

character = PlayerCharacter()

Expand Down Expand Up @@ -194,6 +198,7 @@ def test_adjustThirst_to_death():
socket = Mock()
player = Player(socket, None, None)
player.write = Mock()
player.setAccountState = Mock()

character = PlayerCharacter()

Expand Down
38 changes: 26 additions & 12 deletions tests/test_heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from game.library.library import Library
from game.store.store import Store
from game.player import Player
from game.store.models.world import World
from game.store.models.character import PlayerCharacter

from game.interpreters.command.command import Command
Expand All @@ -14,9 +15,11 @@ def test_advanceActions_should_step():
"""

store = Store('test', '')
store.world = World()

library = Library(store)

heartbeat = Heartbeat(store, library, 10)
heartbeat = Heartbeat(store, library)

socket = Mock()
player = Player(socket, None, None)
Expand Down Expand Up @@ -50,9 +53,10 @@ def test_advanceActions_should_finish():


store = Store('test', '')
store.world = World()
library = Library(store)

heartbeat = Heartbeat(store, library, 10)
heartbeat = Heartbeat(store, library)

socket = Mock()
player = Player(socket, None, None)
Expand Down Expand Up @@ -86,9 +90,10 @@ def test_advanceAction_skips_players_without_characters():


store = Store('test', '')
store.world = World()
library = Library(store)

heartbeat = Heartbeat(store, library, 10)
heartbeat = Heartbeat(store, library)

socket = Mock()
player = Player(socket, None, None)
Expand All @@ -112,9 +117,10 @@ def test_advanceAction_skips_characters_with_no_action():
"""

store = Store('test', '')
store.world = World()
library = Library(store)

heartbeat = Heartbeat(store, library, 10)
heartbeat = Heartbeat(store, library)

socket = Mock()
player = Player(socket, None, None)
Expand Down Expand Up @@ -148,9 +154,10 @@ def test_calculateReserves_character_is_standing():
"""

store = Store('test', '')
store.world = World()
library = Library(store)

heartbeat = Heartbeat(store, library, 10)
heartbeat = Heartbeat(store, library)

socket = Mock()
player = Player(socket, None, None)
Expand All @@ -177,9 +184,10 @@ def test_calculateReserves_character_with_low_reseves_who_is_standing():
"""

store = Store('test', '')
store.world = World()
library = Library(store)

heartbeat = Heartbeat(store, library, 10)
heartbeat = Heartbeat(store, library)

socket = Mock()
player = Player(socket, None, None)
Expand Down Expand Up @@ -210,9 +218,10 @@ def test_calculateReserves_character_who_is_resting():
"""

store = Store('test', '')
store.world = World()
library = Library(store)

heartbeat = Heartbeat(store, library, 10)
heartbeat = Heartbeat(store, library)

socket = Mock()
player = Player(socket, None, None)
Expand All @@ -239,9 +248,10 @@ def test_calculateReserves_character_with_low_reseves_who_is_resting():
"""

store = Store('test', '')
store.world = World()
library = Library(store)

heartbeat = Heartbeat(store, library, 10)
heartbeat = Heartbeat(store, library)

socket = Mock()
player = Player(socket, None, None)
Expand Down Expand Up @@ -272,9 +282,10 @@ def test_calculateSleep_character_who_is_standing():
"""

store = Store('test', '')
store.world = World()
library = Library(store)

heartbeat = Heartbeat(store, library, 10)
heartbeat = Heartbeat(store, library)

socket = Mock()
player = Player(socket, None, None)
Expand All @@ -300,9 +311,10 @@ def test_calculateSleep_character_who_is_sleeping():
"""

store = Store('test', '')
store.world = World()
library = Library(store)

heartbeat = Heartbeat(store, library, 10)
heartbeat = Heartbeat(store, library)

socket = Mock()
player = Player(socket, None, None)
Expand All @@ -329,9 +341,10 @@ def test_calculateSleep_character_with_low_reserves_who_is_standing():
"""

store = Store('test', '')
store.world = World()
library = Library(store)

heartbeat = Heartbeat(store, library, 10)
heartbeat = Heartbeat(store, library)

socket = Mock()
player = Player(socket, None, None)
Expand Down Expand Up @@ -360,9 +373,10 @@ def test_calculateSleep_character_with_low_reserves_who_is_sleeping():
"""

store = Store('test', '')
store.world = World()
library = Library(store)

heartbeat = Heartbeat(store, library, 10)
heartbeat = Heartbeat(store, library)

socket = Mock()
player = Player(socket, None, None)
Expand Down

0 comments on commit 63a5881

Please sign in to comment.