Test merchants

This commit is contained in:
Yohann D'ANELLO 2020-12-09 15:32:37 +01:00
parent 657f4e5209
commit 19b82ff71a
7 changed files with 80 additions and 14 deletions

View File

@ -16,7 +16,7 @@ class Merchant(FriendlyEntity):
"""
Returns a friendly entitie's specific attributes
"""
return ["maxhealth", "health", "inventory", "hazel"]
return super().keys() + ["inventory", "hazel"]
def __init__(self, name: str = "merchant", inventory: list = None,
hazel: int = 75, *args, **kwargs):

View File

@ -50,7 +50,7 @@ class Item(Entity):
self.held = True
self.held_by = player
self.map.remove_entity(self)
player.inventory.append(self)
player.add_to_inventory(self)
def save_state(self) -> dict:
"""

View File

@ -43,7 +43,7 @@ class KeyValues(Enum):
EQUIP = auto()
DROP = auto()
SPACE = auto()
T = auto()
CHAT = auto()
@staticmethod
def translate_key(key: str, settings: Settings) -> Optional["KeyValues"]:
@ -74,6 +74,6 @@ class KeyValues(Enum):
return KeyValues.DROP
elif key == ' ':
return KeyValues.SPACE
elif key == 't':
return KeyValues.T
elif key == settings.KEY_CHAT:
return KeyValues.CHAT
return None

View File

@ -66,7 +66,6 @@ class Game:
We wait for the player's action, then we do what that should be done
when the given key gets pressed.
"""
self.screen = screen
while True: # pragma no cover
screen.erase()
screen.refresh()
@ -122,7 +121,7 @@ class Game:
self.state = GameMode.INVENTORY
elif key == KeyValues.SPACE:
self.state = GameMode.MAINMENU
elif key == KeyValues.T:
elif key == KeyValues.CHAT:
# Wait for the direction of the friendly entity
self.waiting_for_friendly_key = True

View File

@ -481,4 +481,4 @@ class FriendlyEntity(FightingEntity):
"""
Returns a friendly entity's specific attributes
"""
return ["maxhealth", "health", "dialogue_option"]
return ["maxhealth", "health"]

View File

@ -31,6 +31,7 @@ class Settings:
self.KEY_USE = ['u', 'Key used to use an item in the inventory']
self.KEY_EQUIP = ['e', 'Key used to equip an item in the inventory']
self.KEY_DROP = ['r', 'Key used to drop an item in the inventory']
self.KEY_CHAT = ['t', 'Key used to talk to a friendly entity']
self.TEXTURE_PACK = ['ascii', 'Texture pack']
self.LOCALE = [locale.getlocale()[0][:2], 'Language']

View File

@ -7,8 +7,8 @@ import unittest
from ..bootstrap import Bootstrap
from ..display.display import Display
from ..display.display_manager import DisplayManager
from ..entities.friendly import Sunflower, Merchant
from ..entities.items import Bomb
from ..entities.friendly import Merchant, Sunflower
from ..entities.items import Bomb, Sword
from ..entities.player import Player
from ..enums import DisplayActions
from ..game import Game, KeyValues, GameMode
@ -35,8 +35,11 @@ class TestGame(unittest.TestCase):
"""
bomb = Bomb()
self.game.map.add_entity(bomb)
# Add an item in the inventory to check that it is well loaded
sword = Sword()
self.game.map.add_entity(sword)
# Add items in the inventory to check that it is well loaded
bomb.hold(self.game.player)
sword.hold(self.game.player)
# Ensure that merchants can be saved
merchant = Merchant()
@ -125,6 +128,9 @@ class TestGame(unittest.TestCase):
self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_INVENTORY, self.game.settings),
KeyValues.INVENTORY)
self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_CHAT, self.game.settings),
KeyValues.CHAT)
self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_USE, self.game.settings),
KeyValues.USE)
@ -288,7 +294,7 @@ class TestGame(unittest.TestCase):
self.assertEqual(self.game.settings.KEY_LEFT_PRIMARY, 'a')
# Navigate to "texture pack"
for ignored in range(9):
for ignored in range(10):
self.game.handle_key_pressed(KeyValues.DOWN)
# Change texture pack
@ -433,8 +439,16 @@ class TestGame(unittest.TestCase):
sunflower.move(2, 6)
self.game.map.add_entity(sunflower)
# Does nothing
self.assertIsNone(self.game.handle_friendly_entity_chat(KeyValues.UP))
# Talk to sunflower... or not
self.game.handle_key_pressed(KeyValues.T)
self.game.handle_key_pressed(KeyValues.CHAT)
self.assertTrue(self.game.waiting_for_friendly_key)
# Wrong key
self.game.handle_key_pressed(KeyValues.EQUIP)
self.assertFalse(self.game.waiting_for_friendly_key)
self.game.handle_key_pressed(KeyValues.CHAT)
self.assertTrue(self.game.waiting_for_friendly_key)
self.game.handle_key_pressed(KeyValues.UP)
self.assertFalse(self.game.waiting_for_friendly_key)
@ -442,7 +456,7 @@ class TestGame(unittest.TestCase):
self.assertFalse(len(self.game.logs.messages) > 1)
# Talk to sunflower
self.game.handle_key_pressed(KeyValues.T)
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)
@ -451,3 +465,55 @@ class TestGame(unittest.TestCase):
# Ensure that the message is a good message
self.assertIn(self.game.logs.messages[1][21:],
Sunflower.dialogue_option)
# Test all directions to detect the friendly entity
self.game.player.move(3, 6)
self.game.handle_key_pressed(KeyValues.CHAT)
self.game.handle_key_pressed(KeyValues.UP)
self.assertEqual(len(self.game.logs.messages), 3)
self.game.player.move(2, 7)
self.game.handle_key_pressed(KeyValues.CHAT)
self.game.handle_key_pressed(KeyValues.LEFT)
self.assertEqual(len(self.game.logs.messages), 4)
self.game.player.move(2, 5)
self.game.handle_key_pressed(KeyValues.CHAT)
self.game.handle_key_pressed(KeyValues.RIGHT)
self.assertEqual(len(self.game.logs.messages), 5)
def test_talk_to_merchant(self) -> None:
"""
Interact with merchants
"""
self.game.state = GameMode.PLAY
merchant = Merchant()
merchant.move(2, 6)
self.game.map.add_entity(merchant)
# Does nothing
self.assertIsNone(self.game.handle_friendly_entity_chat(KeyValues.UP))
# 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.STORE)
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.UP)
self.assertEqual(self.game.store_menu.position, 1)
# Buy the second item
item = self.game.store_menu.validate()
self.assertIn(item, merchant.inventory)
self.game.handle_key_pressed(KeyValues.ENTER)
self.assertIn(item, self.game.player.inventory)
self.assertNotIn(item, merchant.inventory)
# Exit the menu
self.game.handle_key_pressed(KeyValues.SPACE)
self.assertEqual(self.game.state, GameMode.PLAY)