File: _interface.py

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (65 lines) | stat: -rw-r--r-- 1,839 bytes parent folder | download
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from abc import ABC
from enum import Enum
from typing import Callable, TypeVar, Union

from common.statistic_interface._dump_value import DumpDict, DumpInt, DumpMultiple
from common.statistic_interface._serizlizer import to_float, to_int, to_str

_ScalarValue = Union[str, int, float]

StatValue = TypeVar("StatValue", dict[str, _ScalarValue], _ScalarValue)


class Serializer(ABC):
    def __init__(
        self,
        *,
        serializer: Callable[[StatValue], str],
        deserializer: Callable[[str], StatValue],
    ):
        self._deserializer = deserializer
        self._serializer = serializer

    def deserialize(self, value: str) -> StatValue:
        return self._deserializer(value)

    def serialize(self, value: StatValue) -> str:
        return self._serializer(value)


class StatKey(Enum):
    PolicyAdoption = DumpMultiple("PolicyAdoption", to_str)
    PolicyDeAdoption = DumpMultiple("PolicyDeAdoption", to_str)
    EmpireColors = DumpDict(
        "EmpireColors",
        {
            "R": to_int,
            "G": to_int,
            "B": to_int,
            "A": to_int,
        },
    )
    EmpireID = DumpDict("EmpireID", {"empire_id": to_int, "name": to_str, "turn": to_int})
    CapitalID = DumpDict(
        "CapitalID",
        {
            "capital_planet_id": to_int,
            "capital_planet_name": to_str,
            "capital_species": to_str,
        },
    )
    Output = DumpDict("CurrentOutput", {"turn": to_int, "RP": to_float, "PP": to_float})
    SHIP_CONT = DumpInt("ShipCount")

    def is_multi(self):
        return self.value.multi

    @classmethod
    def get_by_value_name(cls, name: str) -> "StatKey":
        for item in cls:
            if item.value.name == name:
                return item
        raise ValueError(f"Cannot find value with {name}")


LOG_PREFIX = "##"