| 0 | 1 | | from typing import Any, NewType |
| | 2 | |
|
| 0 | 3 | | from injector import inject |
| 0 | 4 | | from minesweeper.minemap import MineMap |
| 0 | 5 | | import uuid |
| | 6 | |
|
| 0 | 7 | | Session = NewType("SessionFactory", Any) |
| | 8 | |
|
| 0 | 9 | | mine_maps = {} |
| | 10 | |
|
| | 11 | |
|
| 0 | 12 | | def check_available_mine_map(session): |
| 0 | 13 | | if "mine_map" in session: |
| 0 | 14 | | if session["mine_map"] in mine_maps.keys(): |
| 0 | 15 | | return True |
| | 16 | |
|
| 0 | 17 | | return False |
| | 18 | |
|
| | 19 | |
|
| 0 | 20 | | class MineSweeperService: |
| 0 | 21 | | @inject |
| 0 | 22 | | def __init__(self, session: Session): |
| 0 | 23 | | self._session = session |
| | 24 | |
|
| 0 | 25 | | if check_available_mine_map(session) is False: |
| 0 | 26 | | session["mine_map"] = uuid.uuid1() |
| 0 | 27 | | mine_maps[session["mine_map"]] = MineMap(3, 3, random_bombs=3) |
| | 28 | |
|
| 0 | 29 | | self.mine_map = mine_maps[session["mine_map"]] |
| | 30 | |
|
| 0 | 31 | | def __str__(self): |
| 0 | 32 | | l = str(self.mine_map) |
| 0 | 33 | | return l |
| | 34 | |
|
| 0 | 35 | | def click(self, x: int, y: int): |
| 0 | 36 | | self.mine_map.click(x, y) |