Linting and tests for chests

This commit is contained in:
eichhornchen 2021-01-08 23:32:47 +01:00
parent bdbf214d8d
commit 2eb42668c8
5 changed files with 67 additions and 8 deletions

View File

@ -6,7 +6,7 @@ from random import randint
from typing import List
from squirrelbattle.menus import Menu, MainMenu, SettingsMenu, StoreMenu,\
ChestMenu
ChestMenu
from .display import Box, Display
from ..entities.player import Player
from ..enums import KeyValues, GameMode
@ -244,6 +244,7 @@ class StoreInventoryDisplay(MenuDisplay):
game.is_in_store_menu = True
game.handle_key_pressed(KeyValues.ENTER)
class ChestInventoryDisplay(MenuDisplay):
"""
A class to handle the display of a merchant's inventory.

View File

@ -1,4 +1,5 @@
from ..interfaces import FriendlyEntity, InventoryHolder, Map, FightingEntity
from ..interfaces import Entity, FriendlyEntity, InventoryHolder, \
Map, FightingEntity
from ..translations import gettext as _
from .player import Player
from .monsters import Monster
@ -17,7 +18,7 @@ class Merchant(InventoryHolder, FriendlyEntity):
return super().keys() + ["inventory", "hazel"]
def __init__(self, name: str = "merchant", inventory: list = None,
hazel: int = 75, maxhealth = 8, *args, **kwargs):
hazel: int = 75, maxhealth: int = 8, *args, **kwargs):
super().__init__(name=name, maxhealth=maxhealth, *args, **kwargs)
self.inventory = self.translate_inventory(inventory or [])
self.hazel = hazel
@ -38,6 +39,7 @@ class Merchant(InventoryHolder, FriendlyEntity):
"""
self.hazel += hz
class Chest(InventoryHolder, FriendlyEntity):
"""
A class of chest inanimate entities which contain objects.
@ -58,7 +60,7 @@ class Chest(InventoryHolder, FriendlyEntity):
"""
return _("You have opened the chest")
def take_damage(self, attacker: "Entity", amount: int) -> str:
def take_damage(self, attacker: Entity, amount: int) -> str:
"""
A chest is not living, it can not take damage
"""

View File

@ -88,7 +88,7 @@ class Item(Entity):
Chestplate, Helmet, RingCritical, RingXP,
ScrollofDamage, ScrollofWeakening, Ruler, Bow, FireBallStaff]
def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder,\
def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder,
for_free: bool = False) -> bool:
"""
Does all necessary actions when an object is to be sold.

View File

@ -341,7 +341,7 @@ class Game:
self.display_actions(DisplayActions.UPDATE)
# Ensure that the cursor has a good position
menu.position = min(menu.position, len(menu.values) - 1)
def handle_key_pressed_chest(self, key: KeyValues) -> None:
"""
In a chest menu, we can take or put items or close the menu.
@ -367,7 +367,7 @@ class Game:
else self.player
buyer = self.player if self.is_in_chest_menu \
else self.chest_menu.chest
flag = item.be_sold(buyer, owner, for_free = True)
item.be_sold(buyer, owner, for_free=True)
self.display_actions(DisplayActions.UPDATE)
# Ensure that the cursor has a good position
menu.position = min(menu.position, len(menu.values) - 1)

View File

@ -8,7 +8,7 @@ import unittest
from ..bootstrap import Bootstrap
from ..display.display import Display
from ..display.display_manager import DisplayManager
from ..entities.friendly import Merchant, Sunflower
from ..entities.friendly import Merchant, Sunflower, Chest
from ..entities.items import Bomb, Heart, Sword, Explosion, Shield, Helmet, \
Chestplate, RingCritical, Bow, FireBallStaff, ScrollofDamage,\
ScrollofWeakening
@ -854,3 +854,59 @@ class TestGame(unittest.TestCase):
self.game.map.tick(self.game.player)
self.assertEqual(entity2.effects, [])
def test_chests(self) -> None:
"""
Interacts with chests.
"""
self.game.state = GameMode.PLAY
chest = Chest()
chest.move(2, 6)
self.game.map.add_entity(chest)
chest.inventory.append(FireBallStaff())
# Talk to merchant
self.game.handle_key_pressed(KeyValues.CHAT)
self.assertTrue(self.game.waiting_for_friendly_key)
self.game.handle_key_pressed(KeyValues.DOWN)
self.assertFalse(self.game.waiting_for_friendly_key)
self.assertEqual(self.game.state, GameMode.CHEST)
self.assertTrue(self.game.logs.messages)
# Navigate in the menu
self.game.handle_key_pressed(KeyValues.DOWN)
self.game.handle_key_pressed(KeyValues.DOWN)
self.game.handle_key_pressed(KeyValues.LEFT)
self.assertFalse(self.game.is_in_chest_menu)
self.game.handle_key_pressed(KeyValues.RIGHT)
self.assertTrue(self.game.is_in_chest_menu)
self.game.handle_key_pressed(KeyValues.UP)
self.assertEqual(self.game.chest_menu.position, 1)
# The second item is not a heart
chest.inventory[1] = sword = Sword()
# Take the second item by clicking on it
item = self.game.chest_menu.validate()
self.assertIn(item, chest.inventory)
self.game.display_actions(DisplayActions.MOUSE, 7, 25)
self.assertIn(item, self.game.player.inventory)
self.assertNotIn(item, chest.inventory)
# Give an item back
self.game.inventory_menu.position = len(self.game.player.inventory) - 1
self.game.handle_key_pressed(KeyValues.LEFT)
self.assertFalse(self.game.is_in_chest_menu)
self.assertIn(sword, self.game.player.inventory)
self.assertEqual(self.game.inventory_menu.validate(), sword)
self.game.handle_key_pressed(KeyValues.ENTER)
self.assertNotIn(sword, self.game.player.inventory)
self.assertIn(sword, chest.inventory)
# Test immortality
self.game.player.hit(chest)
self.assertTrue(not chest.dead)
# Exit the menu
self.game.handle_key_pressed(KeyValues.SPACE)
self.assertEqual(self.game.state, GameMode.PLAY)