-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcharged_up.py
More file actions
50 lines (44 loc) · 1.66 KB
/
charged_up.py
File metadata and controls
50 lines (44 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from dataclasses import dataclass
from io import TextIOWrapper
import json
from models import Element, GameElementState
@dataclass
class ChargedUpGameElementState(GameElementState):
'''Represents the current state of the rapid react game'''
cones: list[Element]
cubes: list[Element]
misc: list[Element]
@staticmethod
def read(file: TextIOWrapper) -> 'ChargedUpGameElementState':
'''Returns the current state of the game'''
raw = file.read()
raw = raw.strip()
raw = json.loads(raw)
elements: list[Element] = []
for raw_object in raw['objects']:
elements.append(Element.from_json(raw_object))
cones: list[Element] = []
cubes: list[Element] = []
misc: list[Element] = []
for element in elements:
if element.element_type is None:
misc.append(element)
elif ChargedUpGameElementState.is_cone(element):
cones.append(element)
elif ChargedUpGameElementState.is_cube(element):
cubes.append(element)
else:
misc.append(element)
cones.sort(key=lambda i: i.identifier)
cubes.sort(key=lambda i: i.identifier)
return ChargedUpGameElementState(cones, cubes, misc)
def __str__(self) -> str:
return f"{[str(item) for item in self.cones]}\n" \
f"{[str(item) for item in self.cubes]}\n" \
f"{[str(item) for item in self.misc]}"
@staticmethod
def is_cone(element: Element) -> bool:
return 'Cone' in element.name
@staticmethod
def is_cube(element: Element) -> bool:
return 'Cube' in element.name