This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathatplayers.py
128 lines (96 loc) · 3.25 KB
/
atplayers.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""Operators, whitelist and banned players lists"""
import enum
from typing import List, Union
from typing import TYPE_CHECKING
import lxml.html
from .atconnect import BASE_URL, AJAX_URL
if TYPE_CHECKING:
from .atserver import AternosServer
class Lists(enum.Enum):
"""Players list type enum"""
whl = 'whitelist'
whl_je = 'whitelist'
whl_be = 'allowlist'
ops = 'ops'
ban = 'banned-players'
ips = 'banned-ips'
class PlayersList:
"""Class for managing operators,
whitelist and banned players lists"""
def __init__(
self,
lst: Union[str, Lists],
atserv: 'AternosServer') -> None:
"""Class for managing operators,
whitelist and banned players lists
Args:
lst (Union[str,Lists]): Players list type, must be
atplayers.Lists enum value
atserv (python_aternos.atserver.AternosServer):
atserver.AternosServer instance
"""
self.atserv = atserv
self.lst = Lists(lst)
# Fix for #30 issue
# whl_je = whitelist for java
# whl_be = whitelist for bedrock
# whl = common whitelist
common_whl = self.lst == Lists.whl
bedrock = atserv.is_bedrock
if common_whl and bedrock:
self.lst = Lists.whl_be
self.players: List[str] = []
self.parsed = False
def list_players(self, cache: bool = True) -> List[str]:
"""Parse a players list
Args:
cache (bool, optional): If the function should
return cached list (highly recommended)
Returns:
List of players' nicknames
"""
if cache and self.parsed:
return self.players
listreq = self.atserv.atserver_request(
f'{BASE_URL}/players/{self.lst.value}',
'GET'
)
listtree = lxml.html.fromstring(listreq.content)
items = listtree.xpath(
'//div[@class="list-item"]'
)
result = []
for i in items:
name = i.xpath('./div[@class="list-name"]')
result.append(name[0].text.strip())
self.players = result
self.parsed = True
return result
def add(self, name: str) -> None:
"""Appends a player to the list by the nickname
Args:
name (str): Player's nickname
"""
self.atserv.atserver_request(
f'{AJAX_URL}/server/players/lists/add',
'POST', data={
'list': self.lst.value,
'name': name
}, sendtoken=True
)
self.players.append(name)
def remove(self, name: str) -> None:
"""Removes a player from the list by the nickname
Args:
name (str): Player's nickname
"""
self.atserv.atserver_request(
f'{AJAX_URL}/server/players/lists/remove',
'POST', data={
'list': self.lst.value,
'name': name
}, sendtoken=True
)
for i, j in enumerate(self.players):
if j == name:
del self.players[i]