Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Labyrinth: typification (main part) #213

Merged
merged 3 commits into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion labyrinth_engine/common_functions.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def from_module_name_to_path(module):
def from_module_name_to_path(module: str) -> str:
return module.replace('.', '\\')
4 changes: 2 additions & 2 deletions labyrinth_engine/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ class LabyrinthError(Exception):


class LabyrinthLoadError(LabyrinthError):
def __init__(self, msg):
def __init__(self, msg: str):
self.msg = msg

def __str__(self):
def __str__(self) -> str:
return 'File "{}"\n{}'.format(self.file, self.msg)

# TODO: understand errors. to continue the list of errors. Issue #44
10 changes: 5 additions & 5 deletions labyrinth_engine/labyrinth.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def get_active_player(self):
return self.players_list[self.active_player_number]

def get_active_player_username(self):
return self.get_active_player().get_username() if self.get_active_player() is not None else None
return self.get_active_player().get_username()

def get_active_player_ats(self):
"""
Expand Down Expand Up @@ -247,10 +247,10 @@ def get_objects(self, lrtype=['location', 'item', 'player', 'creature'], class_n

def save(self):
save = {
'seed': self.seed,
'loadseed': self.loadseed,
'users': list(map(lambda user: user.get_username(), self.players_list)),
'turns': self.turns_log,
'seed': self.seed,
'loadseed': self.loadseed,
'users': list(map(lambda user: user.get_username(), self.players_list)),
'turns': self.turns_log
}
return json.dumps(save, indent=4, ensure_ascii=False)

Expand Down
78 changes: 78 additions & 0 deletions labyrinth_engine/labyrinth.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from labyrinth_engine.labyrinth_object import AnyLO
from labyrinth_engine.lr_types import Location, Item, Player, Creature
from labyrinth_engine.ui_buttons import Button
from labyrinth_engine.ui_status_bars import Bar

from typing import Any, Dict, Union, List, Callable, Set


class Labyrinth:

seed: Union[int, Any]
loadseed: Union[int, Any]
imagepath: str

unique_objects: Dict[str, AnyLO]

locations: Set[Location]
items: Set[Item]
creatures: Set[Creature]
players_list: List[Player]

to_send: Dict[int, Dict[str, List[str]]]
active_player_number: int
is_game_ended: bool

turns_log: List[Dict[str, str]]
msgs_log: Dict[str, List[str]]

MAX_COUNT_OF_SKIPS: int

def __init__(self,
locations: List[Location],
items: List[Item],
creatures: List[Creature],
players: List[Player],
adjacence_list: Dict,
settings: Dict[str, Any],
imagepath: str,
seed: Union[int, Any],
loadseed: Union[int, Any]) -> None: ...

# Сообщения.
def send_msg(self, msg: str, player: Player, priority: int = ...) -> None: ...
def clear_to_send(self) -> Dict[int, Dict[str, List[str]]]: ...
def regularize_to_send(self) -> Dict[str, List[str]]: ...
def player_to_send(self, username: str) -> List[str]: ...
def get_msgs(self, username: str) -> List[str]: ...

# Уникальные предметы.
def set_unique(self, obj: AnyLO, key: str) -> None: ...
def get_unique(self, key: str) -> AnyLO: ...

# Ход игрока.
def make_turn(self, turn: str) -> Dict[str, List[str]]: ...
def skip_turn(self) -> None: ...
def get_turns(self, number: Union[int, None] = ..., username: Union[str, None] = ...) -> Union[List[str], str]: ...
def end_game(self) -> None: ...

# Активный игрок.
def get_next_active_player_number(self) -> Union[None, int]: ...
def get_next_active_player(self) -> Union[None, Player]: ...
def get_active_player(self) -> Player: ...
def get_active_player_username(self) -> str: ...
def get_active_player_ats(self) -> List[str]: ...

# Объекты Лабиринта.
def get_all_objects(self) -> Set[AnyLO]: ...
def get_objects(self,
lrtype: Union[str, List[str]] = ...,
class_names: Union[str, List[str]] = ...,
flags: List[str] = ...,
key: Callable[[AnyLO], bool] = lambda x: True): ...

def save(self) -> str: ...

def get_buttons(self) -> List[Button]: ...

def get_bars(self, username: str) -> List[Bar]: ...
5 changes: 2 additions & 3 deletions labyrinth_engine/labyrinth_object.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from labyrinth_engine.ui_buttons import CommonButton, DirectionButton, ListButton
from labyrinth_engine.ui_buttons import CommonButton, DirectionButton, ListButton
from labyrinth_engine.ui_status_bars import StringBar


Expand All @@ -7,7 +7,6 @@ class LabyrinthObject:
LabyrinthObject is class of objects that can be used by players at their turns
"""

labyrinth = None
_lrtype = 'labyrinth_object'

def __init__(self):
Expand Down Expand Up @@ -65,7 +64,7 @@ def get_bars(self):

# Родители, дети и т.д.
def set_parent(self, parent):
if not (isinstance(parent, LabyrinthObject) or parent is None):
if not (isinstance(parent, LabyrinthObject)):
raise ValueError(
'Invalid type of "parent" argument for LabyrinthObject.set_parent: ' + str(type(parent)))
else:
Expand Down
70 changes: 70 additions & 0 deletions labyrinth_engine/labyrinth_object.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from labyrinth_engine.labyrinth import Labyrinth
from labyrinth_engine.lr_types import Location, Item, Player, Creature
from labyrinth_engine.ui_buttons import Button
from labyrinth_engine.ui_status_bars import Bar, StringBar

from typing import Any, Dict, Union, List, Callable, TypeVar

class LabyrinthObject:
labyrinth: Labyrinth
_lrtype: str
turn_set: Dict[str, Dict[str, Callable[[], Any]]]
flags: Dict[str: Any]
button_set: List[Button]
bar_set: List[Bar]
parent: Union[None, AnyLO]
name: str

def __init__(self) -> None: ...

# Предлагаемые игрокам ходы.
def new_at(self, function: Callable[[], None], condition: Callable[[], bool], turn_name: str) -> None: ...
def get_turns(self) -> Dict[str, Dict[str, Callable[[], Any]]]: ...

# Флаги.
def set_flag(self, flag_name: str, arg: Any = ...) -> None: ...
def delete_flag(self, flag_name: str) -> Any: ...
def have_flag(self, flag_name: str) -> bool: ...
def get_flag(self, flag_name: str, default: Any = ...) -> Any: ...

# Кнопки.
def new_button(self, turn: str, image: str): ...
def new_dbutton(self, turns: List[str], image: str): ...
def new_lbutton(self, turns: List[str], image: str, turn_images): ...
def get_buttons(self) -> List[Button]: ...

# Бары.
def new_status_bar(self, name: str, init_value: Dict[Player, Any]) -> StringBar: ...
def get_bars(self) -> List[Bar]: ...

# Родители, дети и т.д.
def set_parent(self, parent: AnyLO) -> None: ...
def get_parent(self) -> Union[None, AnyLO]: ...
def get_children(self,
lrtype: Union[str, List[str]] = ...,
class_names: Union[str, List[str]] = ...,
flags: List[str] = ...,
key: Callable[[AnyLO], bool] = ...): ...

@property
def lrtype(self) -> str:
return self._lrtype

def main(self) -> None: ...

def set_settings(self,
settings: Dict[str, Any],
locations: List[Location],
items: List[Item],
creatures: List[Creature],
players: List[Player]) -> None: ...

def get_name(self) -> str: ...

def set_name(self, name: str) -> None: ...

def __str__(self) -> str: ...

def __repr__(self) -> str: ...

AnyLO = TypeVar('AnyLO', bound=LabyrinthObject)
8 changes: 8 additions & 0 deletions labyrinth_engine/load_save.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from labyrinth_engine.labyrinth import Labyrinth
from typing import Any, Dict, Union, List


def load_save(save: Union[str, Dict[str, Union[int, List[str], List[Dict[str, str]]]]], _map: Union[str, Dict]) -> Labyrinth: ...


def load_map(_map: Union[str, Dict], users: List[str], loadseed: Union[int, Any] = ..., **kwargs) -> Labyrinth: ...
2 changes: 1 addition & 1 deletion labyrinth_engine/lr_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def get_neighbour(self, direction):
return self.directions[direction]

def set_neighbour(self, direction, neighbour):
if not isinstance(neighbour, LO) or neighbour.lrtype != 'location':
if not isinstance(neighbour, Location):
raise ValueError(
'Invalid "neighbour" argument for LabyrinthObject.set_neighbour: ' + str(neighbour))
else:
Expand Down
42 changes: 42 additions & 0 deletions labyrinth_engine/lr_types.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from labyrinth_engine import LabyrinthObject as LO

from typing import Any, Dict, TypeVar


class Location(LO):
directions: Dict[str, AnyLocation]

def get_neighbour(self, direction: str) -> AnyLocation: ...

def set_neighbour(self, direction: str, neighbour: AnyLocation) -> Any: ...

AnyLocation = TypeVar('AnyLocation', bound=Location)


class Item(LO): ...

AnyItem = TypeVar('AnyItem', bound=Item)


class Player(LO):
name: str
username: str

def __init__(self, username: str): ...

def get_username(self) -> str: ...

def set_turns_skip(self, count: int) -> None: ...

def add_turns_skip(self, count: int) -> None: ...

def die(self) -> None: ...

def revive(self) -> None: ...

AnyPlayer = TypeVar('AnyPlayer', bound=Player)


class Creature(LO): ...

AnyCreature = TypeVar('AnyCreature', bound=Creature)
3 changes: 3 additions & 0 deletions labyrinth_engine/ui_buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class Button:
def __str__(self):
return '<UI.button: {}: {}>'.format(self.btn_type, ', '.join(self.turns))

def get(self, *args, **kwargs):
pass


class CommonButton(Button):
"""
Expand Down
30 changes: 30 additions & 0 deletions labyrinth_engine/ui_buttons.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import Any, Dict, Union, List

class Button:
btn_type: str
turns: List[str]
image: str

def __str__(self) -> str: ...

def get(self, *args, **kwargs) -> Any: ...


class CommonButton(Button):
def __init__(self, turns: List[str], image: str) -> None: ...

def get(self, ats: List[str], imagepath: str) -> Dict[str, Union[str, List[str]]]: ...


class DirectionButton(Button):
def __init__(self, turns: List[str], image: str) -> None: ...

def get(self, ats: List[str], imagepath: str) -> Dict[str, Union[str, List[str]]]: ...


class ListButton(Button):
turn_images: List[str]

def __init__(self, turns: List[str], image: str, turn_images: List[str]) -> None: ...

def get(self, ats: List[str], imagepath: str) -> Dict[str, Union[str, List[str]]]: ...
3 changes: 3 additions & 0 deletions labyrinth_engine/ui_status_bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class Bar:
def __str__(self):
return '<UI.bar: {}: {}>'.format(self.bar_type, self.name)

def get(self, *args, **kwargs):
pass


class StringBar(Bar):
"""
Expand Down
22 changes: 22 additions & 0 deletions labyrinth_engine/ui_status_bars.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from labyrinth_engine.lr_types import AnyPlayer

from typing import Any, Dict, Union

class Bar:
def __str__(self) -> str: ...

def get(self, *args, **kwargs) -> Any: ...


class StringBar(Bar):
bar_type: str
name: str
values: Dict[AnyPlayer, Any]

def __init__(self, name: str, init_values: Dict[AnyPlayer, Any]) -> None: ...

def set_value(self, new_value: Any, player: AnyPlayer) -> None: ...

def set_all_values(self, new_values: Dict[AnyPlayer, Any]) -> None: ...

def get(self, player: AnyPlayer) -> Dict[str, Union[str, Any]]: ...
8 changes: 4 additions & 4 deletions labyrinth_objects/Vanilla/ammo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def __init__(self):

super().__init__()

self.bullet_counter_bar = self.new_status_bar('Пули', None)
self.bomb_counter_bar = self.new_status_bar('Бомбы', None)
self.bullet_counter_bar = self.new_status_bar('Пули', {})
self.bomb_counter_bar = self.new_status_bar('Бомбы', {})

def update_bars(self):
self.bullet_counter_bar.set_all_values(self.bullets)
Expand All @@ -20,8 +20,8 @@ def set_settings(self, settings, locations, items, creatures, players):
self.MAX_BULLETS_COUNT = settings['max_bullets_count']
self.MAX_BOMBS_COUNT = settings['max_bombs_count']

self.INIT_BULLETS_COUNT = settings.get('init_bullets_count') or self.MAX_BULLETS_COUNT
self.INIT_BOMBS_COUNT = settings.get('init_bombs_count') or self.MAX_BOMBS_COUNT
self.INIT_BULLETS_COUNT = settings.get('init_bullets_count', self.MAX_BULLETS_COUNT)
self.INIT_BOMBS_COUNT = settings.get('init_bombs_count', self.MAX_BOMBS_COUNT)

self.bullets = {player: self.INIT_BULLETS_COUNT for player in players}
self.bombs = {player: self.INIT_BOMBS_COUNT for player in players}
Expand Down
2 changes: 1 addition & 1 deletion labyrinth_objects/Vanilla/bomb.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def blow_up():
current_location = active_player.get_parent()
location_in_direction = current_location.get_neighbour(direction)

health = self.labyrinth.get_unique('health')
health = self.labyrinth.get_unique('health') #

if type(location_in_direction) is Wall:
location_in_direction.break_wall()
Expand Down