def load_from_file(self): """ gets the player and room files from the save game dir. moves the room and items files to the temp save dir. Restores the player state """ p, success, msg = files.load_game() if success == True and p is not None and 'current_room' in p: r = files.load_room(p['current_room']) self.validate_object(r, p['current_room']) elif success == False: r = files.load_room('shore') self.validate_object(r, 'shore') self.write_main_bottom_handler(msg) else: text = 'Something went wrong loading the rooms in loadgame.' self.write_main_handler(text) self.exitGame() #if something went wrong returning the player from the #checking for False because p could return as False if the files did not #get copied correctly self.room.current_room = r if p is None: self.player = self.gen_player() else: #load player info from saved game self.player = self.player.set_player_stats(p) self.number_of_turns = p['turns'] self.reset_art()
def test_load_save_game(self): print "" print "TEST load_game functionality, first start a new game." print "Make some changes, save the game, start another new game." print "Then load game and check that the changes we made are as expected." files.new_game() rooms = name_lists.room_info().get_titles() items = name_lists.item_info().get_titles() new_rooms_dir = name_lists.save_info().get_temp_save_dir_rooms() new_items_dir = name_lists.save_info().get_temp_save_dir_items() save_rooms_dir = name_lists.save_info().get_save_dir_rooms() save_items_dir = name_lists.save_info().get_save_dir_items() print "Open each room and edit the visited attribute" for title in rooms: room = files.load_room(title) room['visited'] = "" files.store_room(room) print "Open each item and edit the active attribute" for title in items: item = files.load_item(title) if item['active']: item['active'] = False else: item['active'] = True files.store_item(item) title = random.choice(rooms) current_room = files.load_room(title) files.save_game(self.player, current_room, 0) files.new_game() print "TEST compare the files in temp and save and confirm no match" match, mismatch, errors = filecmp.cmpfiles(save_rooms_dir, new_rooms_dir, rooms) for file_ in match: self.assertNotIn(file_, rooms) self.assertEqual(len(match), 0) self.assertEqual(len(errors), 0) match, mismatch, errors = filecmp.cmpfiles(save_items_dir, new_items_dir, items) for file_ in match: self.assertNotIn(file_, items) self.assertEqual(len(match), 0) self.assertEqual(len(errors), 0) print "TEST that player file exists and that it is a JSON dict" save_dir = name_lists.save_info().get_save_dir() player_file = os.path.join(save_dir, 'player') self.assertEqual(os.path.isfile(player_file), True) try: with open(player_file, 'r') as player_file: player = json.load(player_file) except Exception, e: player = None
def test_save_game(self): print "" print "TEST the save game of file_lib" files.new_game() rooms = name_lists.room_info().get_titles() items = name_lists.item_info().get_titles() title = random.choice(rooms) current_room = files.load_room(title) new_rooms_dir = name_lists.save_info().get_temp_save_dir_rooms() new_items_dir = name_lists.save_info().get_temp_save_dir_items() save_rooms_dir = name_lists.save_info().get_save_dir_rooms() save_items_dir = name_lists.save_info().get_save_dir_items() print "Open each room and edit the visited attribute" for title in rooms: room = files.load_room(title) room['visited'] = True files.store_room(room) print "Open each item and edit the active attribute" for title in items: item = files.load_item(title) if item['active']: item['active'] = False else: item['active'] = True files.store_item(item) files.save_game(self.player, current_room, 0) print "TEST compare the room files in both dirs to check that they match" match, mismatch, errors = filecmp.cmpfiles(save_rooms_dir, new_rooms_dir, rooms) for file_ in match: self.assertIn(file_, rooms) self.assertEqual(len(mismatch), 0) self.assertEqual(len(errors), 0) print "TEST compare the item files in both dirs to check that they match" match, mismatch, errors = filecmp.cmpfiles(save_items_dir, new_items_dir, items) for file_ in match: self.assertIn(file_, items) self.assertEqual(len(mismatch), 0) self.assertEqual(len(errors), 0) print "TEST that player file exists and that it is a JSON dict" save_dir = name_lists.save_info().get_save_dir() player_file = os.path.join(save_dir, 'player') self.assertEqual(os.path.isfile(player_file), True) try: with open(player_file, 'r') as player_file: player = json.load(player_file) except Exception, e: player = None
def newGame(self): """ copies the files over from template dir to the temp save dir and starts a new player """ files.new_game() self.player = self.gen_player() #New games start at the shore self.room.current_room = files.load_room("shore") self.validate_object(self.room.current_room, 'shore') #for testing purposes load a specific room and start from there if LOAD_SPECIFIC_ROOM_ON_NEW_GAME: self.room.current_room = files.load_room(SPECIFIC_ROOM) self.validate_object(self.room.current_room, SPECIFIC_ROOM)
def room_action(self, title_or_compass, action): """ tries to move to the passed in room title or compass direction also expects a list of items currently held in the inventory defaults to None """ res = self.room.check_connections(title_or_compass, self.player.inventory) res.action = action if action == "go": res.action = 'go' if res.success == True: #before we leave the room we set the field visited to true self.room.visited = True files.store_room(self.room.current_room) self.room.current_room = files.load_room(res.title) self.validate_object(self.room.current_room, res.title) res.description = self.room.get_room_desc elif title_or_compass == self.room.title: res.description = 'You are already in that room.' elif res.success == False and res.description is None: res.description = "You were not able to move in that direction. " elif action == 'look': res.action = 'look' res.description = self.room.long_desc if (title_or_compass != self.room.title): res.warning = 'You are not in '+title_or_compass+'. But you can look around the ' + self.room.title + '.' else: res.description = "You can't " + action + " the " + title_or_compass+". " return res
def setUp(self): self.game_file = game self.game_file.USE_CURSES = False self.game = self.game_file.Game() files.new_game() self.game.player = player.Player("Test Player") self.game.room.current_room = files.load_room("shore") self.game.validate_object(self.game.room.current_room, "shore")
def update_external_room(self, updates, key): """ opens the room file named in the key, updates that room and then stores that room back to file """ other_room = files.load_room(key) self.validate_object(other_room, key) other_room = files.update(updates, other_room) files.store_room(other_room)
import json import game_engine.game as game import game_engine.player as player from file_handler.file_lib import load_room current_room = "shore" current_player = player.Player("Test Player") current_game = game.Game(current_player, load_room(current_room)) user_choice = current_game.startGame()