Last active
April 1, 2025 22:51
-
-
Save Pinacolada64/7c475b5ccddfbb54c978d9b14573fb24 to your computer and use it in GitHub Desktop.
Item ID strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Optional, Any | |
class Item: | |
def __init__(self, item_id: int, name: str, description: str, owner: Optional[None], prefix: str = "I"): | |
self.prefix = prefix | |
self.item_id = item_id | |
self.owner = owner # TODO: can be a Player, but moving this after Player breaks inheritance | |
self.name = name | |
self.description = description | |
def __str__(self, player=None): | |
print(f"From {player.name}'s perspective:") | |
print(f"\t{player.debug=}, {player.dungeon_master=}, {self.owner=}") | |
if player and (player.has_item(self) or player.debug or player.dungeon_master or player == self.owner): | |
return f"{self.name} [{self.prefix}#{self.item_id}]" | |
else: | |
return f"{self.name}" | |
class Monster(Item): | |
def __init__(self, item_id: int, name: str, description: str, strength: int, alignment: str, owner: Optional[None] = None): | |
# TODO: the Owner is set only if the Monster joins the Player's party | |
# FIXME: 'owner: Player' is unresolved reference | |
super().__init__(item_id, name, description, owner, prefix="M") | |
self.strength = strength | |
self.alignment = alignment | |
class Player: | |
def __init__(self, player_id, name, debug=False, dungeon_master=False): | |
self.player_id = player_id | |
self.name = name | |
self.inventory = [] | |
# These are just stand-ins for the Flag properties: | |
self.debug = debug | |
self.dungeon_master = dungeon_master | |
self.party = [] | |
def add_item(self, item): | |
item.owner = self | |
self.inventory.append(item) | |
def has_item(self, item): | |
return item in self.inventory | |
def add_to_party(self, party_addition: Any) -> bool: | |
# FIXME: specifying 'party_addition: Monster | Player' leads to an unresolved reference error | |
# should the syntax to add a Monster to Player's party be 'Player.add_item(Monster)' or 'Monster.add_item(Player'? | |
# print(f"Add {party_addition.name} to {self.name}'s party") | |
# check that party_addition is not self: | |
if party_addition is self: | |
print(f"This is getting a bit surreal. You can't add {self.name} to {self.name}'s party.") | |
return False | |
# make sure a party_addition is not already in the party: | |
if party_addition in self.party: | |
print(f"Seeing another {party_addition.name} is already in your party, they turn sadly away.") | |
return False | |
self.party.append(party_addition) | |
print(f"{party_addition.name} joins {self.name}'s party!") | |
return True | |
def is_in_party(self, addition) -> bool: | |
# checks if 'addition' already in 'party', returns bool | |
result = addition in self.party | |
if result: | |
print(f"{addition.name} beams with pride.") | |
else: | |
print(f"{addition.name} scowls!") | |
return result | |
def list_party_members(self): | |
if not self.party: | |
print(f"There are no other members in {self.name}'s party.") | |
return | |
else: | |
print(f"Members of {self.name}'s party:") | |
for num, member in enumerate(self.party, start=1): | |
print(f"{num}. {member.name}") | |
def __repr__(self): | |
return self.name | |
class Weapon(Item): | |
def __init__(self, item_id: int, name: str, description: str, owner: Optional[Player]): | |
super().__init__(item_id, name, description, owner, prefix="W") | |
if __name__ == '__main__': | |
# Example Usage | |
alice = Player(1, "Alice", debug=True) | |
bob = Player(2, "Bob", dungeon_master=True) | |
charlie = Player(3, "Charlie") | |
sword = Weapon(101, "Sword", "A sharp, steel sword.", None) | |
hammer = Weapon(102, "Hammer", "A metal claw on a stick.", None) | |
potion = Item(201, "Potion", "A healing potion.", None) | |
rock = Item(202, "Rock", "A heavy lump of rock.", None) | |
minotaur = Monster(301, "Minotaur", "Half-man, half-bull, likes mazes.", 25, "evil") | |
generic_monster = Monster(302, "Generic Monster", "A generic monster.", 15, "good") | |
alice.add_item(sword) # adds it to her inventory, makes her the owner | |
print("Weapon 1:\t", sword.__str__(alice)) | |
print("Weapon 2:\t", hammer.__str__(bob)) | |
print("Weapon 1:\t", hammer.__str__(charlie)) | |
print("Weapon 2:\t", hammer.__str__(bob)) | |
print("Item 1:\t", potion.__str__(charlie)) | |
print("Item 2:\t", rock.__str__(bob)) | |
print(f"Monster 1:", minotaur.__str__(bob), f"\t{minotaur.description}") | |
# This does not work: | |
# print(f"Monster 1: {minotaur(bob)}\t{minotaur.description}") | |
print(f"Monster 2: {generic_monster.__str__(alice)}\t{generic_monster.description}") | |
# show Bob's empty party: | |
bob.list_party_members() | |
# add duplicate monsters to Bob's party: | |
bob.add_to_party(minotaur) | |
bob.add_to_party(minotaur) | |
bob.is_in_party(minotaur) | |
# ...including Bob himself, which should fail humorously: | |
bob.add_to_party(bob) | |
# add alice and charlie to Bob's party: | |
bob.add_to_party(alice) | |
bob.add_to_party(charlie) | |
# list members of Bob's party: | |
bob.list_party_members() |
Author
Pinacolada64
commented
Apr 1, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment