Passed
Push — master ( 91831d...3add59 )
by Steffen
01:51
created

tw_serverinfo.models.player.Player.country_code()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
1
#!/usr/local/bin/python
2
# coding: utf-8
3 1
from pycountry import countries
4
5 1
from tw_serverinfo.utility.countries import custom_countries
6
7
8 1
class Player(object):
9
    """Player Model Template, containing properties for all possible attributes of players"""
10 1
    _name = ''
11 1
    _clan = ''
12 1
    _country_index = '-1'
13 1
    _score = 0
14 1
    _ingame = False
15
16 1
    def __init__(self, name: str, clan: str = '', country: int = -1, score: int = 0, ingame: bool = False) -> None:
17
        """Initializing function
18
19
        :type name: str
20
        :type clan: str
21
        :type country: int
22
        :type score: int
23
        :type ingame: bool
24
        """
25 1
        self.name = name
26 1
        self.clan = clan
27 1
        if country == -1:
28 1
            self._country_index = str(country)
29
        else:
30 1
            self.country = str(country)
31 1
        self.score = score
32 1
        self.ingame = ingame
33
34
    def __repr__(self) -> str:
35
        """Reprint function, displays player details instead of instance information
36
37
        :return:
38
        """
39
        return 'Player(name={name:s}, clan={clan:s}, country={country:s}, country_index={country_index:d}' \
40
               'score={score:d}, ingame={ingame!r})'.format(name=self.name, clan=self.clan, country=self.country,
41
                                                            country_index=self.country_index, score=self.score,
42
                                                            ingame=self.ingame)
43
44 1
    def __eq__(self, other) -> bool:
45
        """Check for equality of objects
46
47
        :type other: Player
48
        :return:
49
        """
50 1
        return self.name == other.name and self.clan == other.clan
51
52 1
    @property
53 1
    def name(self) -> str:
54 1
        return self._name
55
56 1
    @name.setter
57 1
    def name(self, name: str) -> None:
58 1
        self._name = name
59
60 1
    @property
61 1
    def clan(self) -> str:
62 1
        return self._clan
63
64 1
    @clan.setter
65 1
    def clan(self, clan: str) -> None:
66 1
        self._clan = clan
67
68 1
    @property
69 1
    def country(self) -> str:
70 1
        if self._country_index in custom_countries:
71 1
            return custom_countries[self._country_index]['name']
72
        else:
73 1
            return countries.get(numeric=str(self._country_index)).name
74
75 1
    @country.setter
76 1
    def country(self, country: str) -> None:
77 1
        if country in custom_countries:
78 1
            self._country_index = country
79
        else:
80 1
            zfilled_country = country.zfill(3)
81
            # only allow to set indexes which are known
82 1
            try:
83 1
                self._country_index = countries.get(numeric=zfilled_country).numeric
84 1
            except KeyError:
85 1
                pass
86
87 1
    @property
88 1
    def country_code(self) -> str:
89 1
        if self._country_index in custom_countries:
90 1
            return custom_countries[self._country_index]['code']
91
        else:
92 1
            return countries.get(numeric=str(self._country_index)).alpha_2
93
94 1
    @property
95 1
    def country_index(self) -> int:
96 1
        return int(self._country_index)
97
98 1
    @property
99 1
    def score(self) -> int:
100 1
        return self._score
101
102 1
    @score.setter
103 1
    def score(self, score: int) -> None:
104 1
        self._score = score
105
106 1
    @property
107 1
    def ingame(self) -> bool:
108 1
        return self._ingame
109
110 1
    @ingame.setter
111 1
    def ingame(self, ingame: bool) -> None:
112
        self._ingame = ingame
113