Skip to content

Commit

Permalink
world can be loaded from file
Browse files Browse the repository at this point in the history
  • Loading branch information
wsowens committed Jul 24, 2019
1 parent 8cb9e05 commit 12261d6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
17 changes: 16 additions & 1 deletion mudworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,24 @@ def __init__(self, prelude={}, personae={}, tree={}):
symbols = load_personae(personae, type_names,
starter=self.locations)
# load the tree
load_tree(tree, symbols)

# sort out the remaining classes
self.char_classes = {}
self.item_classes = {}
self.entity_classes = {}

for cls in type_names.values():
if isinstance(cls, CharacterClass):
self.char_classes[cls.__name__] = cls
elif isinstance(cls, ItemClass):
self.item_classes[cls.__name__] = cls
elif isinstance(cls, EntityClass):
self.entity_classes[cls.__name__] = cls


@staticmethod
def from_savefile(self, save_name):
def from_savefile(save_name):
'''returns a World loaded from a save_file'''
save_data = read_savefile(save_name)
return World(**save_data)
44 changes: 42 additions & 2 deletions testing/test_mudworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_simple(self):
expected = {
"prelude": {
"testing/script/basic_rpg.py": ["Wizard", "Warrior"],
"testing/script/weapons.py": ["GoldenNugget"]
"testing/script/weapons.py": ["CursedRing"]
},
"personae": {
"Boring House" : {
Expand Down Expand Up @@ -345,4 +345,44 @@ def test_simple_tree(self):
# grug should have nothing in his inventory
self.assertEqual([], list(grug.inv))
# abra should have one thing (the cursed ring)
self.assertEqual([ring], list(abra.inv))
self.assertEqual([ring], list(abra.inv))


class TestWorld(unittest.TestCase):
'''testcase for the 'World' class, which integrates all the functionality
above'''

def setUp(self):
self.Warrior = import_class("testing.script.basic_rpg", "Warrior")
self.Wizard = import_class("testing.script.basic_rpg", "Wizard")
self.CursedRing = import_class("testing.script.weapons", "CursedRing")

def test_simple(self):
'''should successfully load in a simple world'''
world = mudimport.World.from_savefile("testing/test_saves/simple.yaml")

self.assertEqual(set(world.locations),
set(("Boring House", "Boring House Interior")))
house = world.locations["Boring House"]
interior = world.locations["Boring House Interior"]

# should be only 1 character in house.characters, Grug
grug, = tuple(house.characters)
self.assertTrue(isinstance(grug, self.Warrior))
# grug's inventory should be empty
self.assertEqual(list(grug.inv), [])

# should be only 1 character in interior.characters, Abra
abra, = tuple(interior.characters)
self.assertTrue(isinstance(abra, self.Wizard))
# should be only 1 item in abra's inventory, the ring
ring, = tuple(abra.inv)
self.assertTrue(isinstance(ring, self.CursedRing))

# test that the exits are working
# interior should only have 1 exit, to the outside
outside, = tuple(interior.exits)
outside.destination is house
# house should only have 1 exit, to the inside
inside, = tuple(house.exits)
inside.destination is interior
2 changes: 1 addition & 1 deletion testing/test_saves/simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ prelude:
- Wizard
- Warrior
testing/script/weapons.py:
- GoldenNugget
- CursedRing
personae:
Boring House:
_type: Location
Expand Down

0 comments on commit 12261d6

Please sign in to comment.