Skip to content

Commit d5f985c

Browse files
committed
Use the messages class instead of just printing
This makes multiplayer output work.
1 parent 789a5c4 commit d5f985c

17 files changed

Lines changed: 294 additions & 369 deletions

File tree

‎src/fantasy_forge/area.py‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,20 @@ class Area(Entity):
1414
__important_attributes__ = ("name",)
1515
contents: dict[str, Entity]
1616

17-
def __init__(self: Self, world: World, config_dict: dict[str, Any]):
18-
super().__init__(world, config_dict)
17+
def __init__(self: Self, messages: Messages, config_dict: dict[str, Any]):
18+
super().__init__(messages, config_dict)
1919
self.contents: dict = {}
2020

2121
def __iter__(self: Self) -> Iterator:
2222
for obj in self.contents:
2323
yield obj
2424

25-
def on_look(self: Self) -> str:
26-
return self.world.l10n.format_value(
25+
def on_look(self: Self, actor: Player):
26+
self.messages.to(
27+
[actor],
2728
"look-around-begin",
28-
{
29-
"area-name": self.name,
30-
"area-description": self.description,
31-
},
29+
area_name=self.name,
30+
area_description=self.description,
3231
)
3332

3433
def to_dict(self: Self) -> dict:
@@ -37,60 +36,61 @@ def to_dict(self: Self) -> dict:
3736
return area_dict
3837

3938
@staticmethod
40-
def from_dict(world: World, area_dict: dict) -> Area:
39+
def from_dict(messages: Messages, area_dict: dict) -> Area:
4140
contents_list: list[Entity] = []
4241
for entity_dict in area_dict.get("contents", []):
4342
match entity_dict.get("kind", "entity"):
4443
case "item":
4544
from fantasy_forge.item import Item
4645

47-
contents_list.append(Item(world, entity_dict))
46+
contents_list.append(Item(messages, entity_dict))
4847
case "gateway":
4948
from fantasy_forge.gateway import Gateway
5049

51-
contents_list.append(Gateway(world, entity_dict))
50+
contents_list.append(Gateway(messages, entity_dict))
5251
case "key":
5352
from fantasy_forge.key import Key
5453

55-
contents_list.append(Key(world, entity_dict))
54+
contents_list.append(Key(messages, entity_dict))
5655
case "enemy":
5756
from fantasy_forge.enemy import Enemy
5857

59-
contents_list.append(Enemy(world, entity_dict))
58+
contents_list.append(Enemy(messages, entity_dict))
6059
case "weapon":
6160
from fantasy_forge.weapon import Weapon
6261

63-
contents_list.append(Weapon(world, entity_dict))
62+
contents_list.append(Weapon(messages, entity_dict))
6463
case "armour":
6564
from fantasy_forge.armour import Armour
6665

67-
contents_list.append(Armour(world, entity_dict))
66+
contents_list.append(Armour(messages, entity_dict))
6867

6968
case default:
70-
contents_list.append(Entity(world, entity_dict))
69+
contents_list.append(Entity(messages, entity_dict))
7170
contents = {entity.name: entity for entity in contents_list}
72-
area = Area(world, area_dict)
71+
area = Area(messages, area_dict)
7372
area.contents = contents
7473
return area
7574

7675
@staticmethod
77-
def load(world, root_path: Path, name: str):
76+
def load(messages: Messages, root_path: Path, name: str):
7877
path = root_path / "areas" / f"{name}.toml"
7978
with path.open() as area_file:
8079
area_toml = toml.load(area_file)
81-
return Area.from_dict(world, area_toml)
80+
return Area.from_dict(messages, area_toml)
8281

8382
@staticmethod
84-
def empty(world: World) -> Area:
83+
def empty(messages: Messages) -> Area:
8584
"""Return an empty area, this is a placeholder."""
8685
return Area(
87-
world,
86+
messages,
8887
dict(
89-
name=world.l10n.format_value("void-name"),
90-
description=world.l10n.format_value("void-description"),
88+
name=messages.l10n.format_value("void-name"),
89+
description=messages.l10n.format_value("void-description"),
9190
),
9291
)
9392

9493

9594
if TYPE_CHECKING:
96-
from fantasy_forge.world import World
95+
from fantasy_forge.messages import Messages
96+
from fantasy_forge.player import Player

‎src/fantasy_forge/armour.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from __future__ import annotations
22

3-
from typing import Self
3+
from typing import TYPE_CHECKING, Self
44

55
from fantasy_forge.item import Item
6-
from fantasy_forge.world import World
76

87
# Armour types:
98
# 'head': for caps, hats, helmets
@@ -19,12 +18,12 @@ class Armour(Item):
1918

2019
__important_attributes__ = ("name", "armour_type", "defense")
2120

22-
def __init__(self, world, config_dict):
21+
def __init__(self, messages: Messages, config_dict):
2322
a_type: str = config_dict.pop("armour_type")
2423
assert a_type in ARMOUR_TYPES
2524
self.armour_type = a_type
2625
self.defense = config_dict.pop("defense")
27-
super().__init__(world, config_dict)
26+
super().__init__(messages, config_dict)
2827

2928
def to_dict(self: Self) -> dict:
3029
armour_dict: dict = super().to_dict()
@@ -33,9 +32,10 @@ def to_dict(self: Self) -> dict:
3332
return armour_dict
3433

3534
@staticmethod
36-
def from_dict(world: World, armour_dict: dict) -> Armour:
37-
armour: Armour = Armour(
38-
world,
39-
armour_dict
40-
)
35+
def from_dict(messages: Messages, armour_dict: dict) -> Armour:
36+
armour: Armour = Armour(messages, armour_dict)
4137
return armour
38+
39+
40+
if TYPE_CHECKING:
41+
from fantasy_forge.messages import Messages

‎src/fantasy_forge/character.py‎

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
from __future__ import annotations
22

3-
from typing import Any, Self
3+
from typing import TYPE_CHECKING, Any, Self
44

55
from fantasy_forge.entity import Entity
66
from fantasy_forge.inventory import Inventory
7+
from fantasy_forge.messages import Messages
78
from fantasy_forge.weapon import Weapon
89
from fantasy_forge.world import World
910

1011
BASE_INVENTORY_CAPACITY = 10
1112
BASE_DAMAGE = 1
1213

13-
def bare_hands(world: World):
14+
15+
def bare_hands(messages: Messages):
1416
return Weapon(
15-
world,
17+
messages,
1618
{
17-
"name": world.l10n.format_value("bare-hands-name"),
18-
"description": world.l10n.format_value("bare-hands-description"),
19+
"name": messages.l10n.format_value("bare-hands-name"),
20+
"description": messages.l10n.format_value("bare-hands-description"),
1921
"damage": BASE_DAMAGE,
2022
},
2123
)
2224

25+
2326
class Character(Entity):
2427
"""A character in the world."""
2528

@@ -30,11 +33,11 @@ class Character(Entity):
3033
main_hand: Weapon | None
3134
_alive: bool
3235

33-
def __init__(self: Self, world: World, config_dict: dict[str, Any]) -> None:
36+
def __init__(self: Self, messages: Messages, config_dict: dict[str, Any]) -> None:
3437
self.health = config_dict.pop("health")
35-
self.inventory = Inventory(world, BASE_INVENTORY_CAPACITY)
38+
self.inventory = Inventory(messages, BASE_INVENTORY_CAPACITY)
3639
self.main_hand = None
37-
super().__init__(world, config_dict)
40+
super().__init__(messages, config_dict)
3841

3942
@property
4043
def alive(self: Self) -> bool:
@@ -43,62 +46,52 @@ def alive(self: Self) -> bool:
4346

4447
def attack(self: Self, target: Character) -> None:
4548
if self.main_hand is None:
46-
weapon = bare_hands(self.world)
49+
weapon = bare_hands(self.messages)
4750
else:
4851
weapon = self.main_hand
49-
target.on_attack(weapon)
50-
print(
51-
self.world.l10n.format_value(
52-
"attack-character-message",
53-
{
54-
"source": self.name,
55-
"target": target.name,
56-
"weapon": getattr(weapon, "name", None),
57-
},
58-
)
52+
target.on_attack(weapon)
53+
self.messages.to(
54+
[self, target],
55+
"attack-character-message",
56+
source=self.name,
57+
target=target.name,
58+
weapon=getattr(weapon, "name", None),
5959
)
6060

6161
def on_attack(self: Self, weapon: Weapon):
6262
self.health -= weapon.damage
6363

6464
def _on_death(self: Self, player: Player):
6565
"""
66-
Automatic on death call.
66+
Automatic on death call.
6767
"""
6868
assert not self.alive, "On_death called while entity is alive"
69-
print(
70-
self.world.l10n.format_value(
71-
"attack-character-dead-message",
72-
{
73-
"target": self.name,
74-
},
75-
)
76-
)
69+
self.messages.to(
70+
[player],
71+
"attack-character-dead-message",
72+
target=self.name,
73+
)
7774
# Populate area with loot
78-
print(
79-
self.world.l10n.format_value(
80-
"attack-drop-begin",
81-
{
82-
"target": self.name,
83-
"loot_count": len(self.inventory),
84-
},
85-
)
75+
self.messages.to(
76+
[player],
77+
"attack-drop-begin",
78+
target=self.name,
79+
loot_count=len(self.inventory),
8680
)
8781
for loot_item in self.inventory:
8882
player.area.contents[loot_item.name] = loot_item
8983
player.seen_entities[loot_item.name] = loot_item
90-
print(
91-
self.world.l10n.format_value(
92-
"attack-drop-single",
93-
{"item": loot_item.name},
94-
)
84+
self.messages.to(
85+
[player],
86+
"attack-drop-single",
87+
item=loot_item.name,
9588
)
9689
# if the target is dead, remove it from the area and drop their inventory
9790
del player.area.contents[self.name]
9891
del player.seen_entities[self.name]
99-
100-
# remove entity from area
101-
10292

93+
# remove entity from area
10394

10495

96+
if TYPE_CHECKING:
97+
from fantasy_forge.player import Player

‎src/fantasy_forge/enemy.py‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
from typing import Any, Self
22

3-
from fantasy_forge.character import bare_hands, Character
3+
from fantasy_forge.character import Character, bare_hands
44
from fantasy_forge.item import Item
5-
from fantasy_forge.world import World
6-
7-
5+
from fantasy_forge.messages import Messages
86

97

108
class Enemy(Character):
119
"""An enemy is a person which will fight back."""
1210

13-
def __init__(self: Self, world: World, config_dict: dict[str, Any]):
14-
super().__init__(world, config_dict)
11+
def __init__(self: Self, messages: Messages, config_dict: dict[str, Any]):
12+
super().__init__(messages, config_dict)
1513
for item_dict in config_dict.get("loot", []):
16-
self.inventory.add(Item(world, item_dict))
14+
self.inventory.add(Item(messages, item_dict))
1715

1816
def __str__(self: Self) -> str:
1917
return self.name
20-

‎src/fantasy_forge/entity.py‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,30 @@ class Entity:
88

99
__important_attributes__ = ("name",)
1010

11-
world: World
11+
messages: Messages
1212
name: str
1313
description: str
1414
obvious: bool # obvious entities are seen when entering the room
1515

1616
def __init__(
1717
self: Self,
18-
world: World,
18+
messages: Messages,
1919
config_dict: dict[str, Any],
2020
) -> None:
21-
self.world = world
21+
self.messages = messages
2222
self.name = config_dict.pop("name")
2323
self.description = config_dict.pop("description", "")
2424
self.obvious = config_dict.pop("obvious", False)
2525

26-
def on_look(self: Self) -> str:
27-
return self.description
28-
29-
def on_use(self: Self, other: Entity | None = None):
30-
print(
31-
self.world.l10n.format_value(
32-
"cannot-use-message",
33-
{
34-
"self": self.name,
35-
"other": getattr(other, "name", None),
36-
},
37-
)
26+
def on_look(self: Self, actor: Player):
27+
actor.shell.stdout.write(self.description + "\n")
28+
29+
def on_use(self: Self, actor: Player, other: Entity | None = None):
30+
self.messages.to(
31+
[actor],
32+
"cannot-use-message",
33+
self=self.name,
34+
other=getattr(other, "name", None),
3835
)
3936

4037
def __repr__(self: Self) -> str:
@@ -54,10 +51,11 @@ def to_dict(self: Self) -> dict:
5451
entity_dict: dict = {"name": self.name, "description": self.description}
5552
return entity_dict
5653

57-
58-
def resolve(self):
54+
def resolve(self, world: World):
5955
pass
6056

6157

6258
if TYPE_CHECKING:
59+
from fantasy_forge.messages import Messages
60+
from fantasy_forge.player import Player
6361
from fantasy_forge.world import World

0 commit comments

Comments
 (0)