-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
244 lines (188 loc) · 7.6 KB
/
Copy pathbot.py
File metadata and controls
244 lines (188 loc) · 7.6 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# bot.py
import os
import importlib.util
import ctypes
import threading
import time
from core.network import Network
from core import security
from core import logger
from core.protocol import RakNetProtocol
from samp.api import BotAPI
from samp.session import SampSession
from logic.events import emitter
MAX_NICKNAME_LEN = 20
BAD_NICKNAME_LEN_ERROR = "Bad NickName Len (MAX 20 CHARS)"
def validate_nickname_length(nickname: str) -> None:
nickname = str(nickname or "")
if len(nickname) > int(MAX_NICKNAME_LEN):
raise ValueError(BAD_NICKNAME_LEN_ERROR)
class Bot:
def __init__(
self,
host: str,
port: int = 7777,
nickname: str = "Bot",
sync_scheduler=None,
fps: float = 100.0,
pings_ips_continuous: bool = False,
proxy_address: str | None = None,
):
validate_nickname_length(nickname)
self.host = host
self.port = port
self.network = Network(host, port)
self.protocol = RakNetProtocol(self.network, security)
# Сессия владеет runtime-состоянием и инфраструктурой; BotAPI — только
# публичный интерфейс поверх неё.
self.session = SampSession(
self.protocol,
self.host,
self.port,
nickname,
sync_scheduler=sync_scheduler,
fps=fps,
pings_ips_continuous=pings_ips_continuous,
)
self.api = BotAPI(self.session)
self.network.set_proxy_manager(self.session.proxy)
if proxy_address:
try:
configured = bool(self.session.proxy.connect(str(proxy_address)))
except Exception as exc:
raise ValueError(f"failed to configure --proxy: {exc}") from exc
if not configured:
raise ValueError("failed to configure --proxy")
self.session._proxy_required = True
self._window_title_running = True
self._window_title_thread = None
self._start_window_title_updater()
self._load_scripts()
emitter.emit("onBotCreated", self.api)
@staticmethod
def _safe_window_title_value(value, fallback: str = "?") -> str:
try:
value = str(value)
except Exception:
return fallback
value = " ".join(value.replace("\r", " ").replace("\n", " ").split())
return value if value else fallback
@staticmethod
def _set_process_window_title(title: str) -> None:
title = Bot._safe_window_title_value(title, "Bot")
if os.name == "nt":
try:
ctypes.windll.kernel32.SetConsoleTitleW(title)
return
except Exception:
return
try:
logger.raw(f"\033]0;{title}\007")
except Exception:
pass
def _build_window_title(self) -> str:
nick = self._safe_window_title_value(
getattr(self.session, "nickname", None),
self._safe_window_title_value(getattr(self, "host", "Bot"), "Bot"),
)
try:
player_id = int(self.api.getbotid())
except Exception:
player_id = -1
try:
hostname = self.api.getservername()
except Exception:
hostname = ""
hostname = self._safe_window_title_value(
hostname,
f"{self.host}:{self.port}",
)
try:
x, y, z = self.api.getbotposition()
xyz = f"{float(x):.2f}, {float(y):.2f}, {float(z):.2f}"
except Exception:
xyz = "0.00, 0.00, 0.00"
try:
ping = int(self.session.players.get_bot_ping())
except Exception:
ping = 0
try:
lvl = int(self.session.players.get_bot_score())
except Exception:
lvl = 0
try:
money = int(self.api.getbotmoney())
except Exception:
money = 0
try:
hp = float(self.api.getbothealth())
except Exception:
hp = 0.0
return (
f"{nick} ({player_id}), "
f"ping: {ping}, "
f"{hostname}, "
f"XYZ: {xyz}, "
f"lvl: {lvl}, "
f"money: {money}, "
f"hp: {hp:.0f}"
)
def _window_title_worker(self) -> None:
last_title = None
while getattr(self, "_window_title_running", False):
try:
title = self._build_window_title()
if title != last_title:
self._set_process_window_title(title)
last_title = title
except Exception:
pass
time.sleep(0.5)
def _start_window_title_updater(self) -> None:
if self._window_title_thread is not None:
return
self._window_title_thread = threading.Thread(
target=self._window_title_worker,
name="bot-window-title",
daemon=True,
)
self._window_title_thread.start()
def _load_scripts(self):
base_dir = os.path.dirname(os.path.abspath(__file__))
scripts_dir = os.path.join(base_dir, "scripts")
if not os.path.exists(scripts_dir):
os.makedirs(scripts_dir)
return
for file in sorted(os.listdir(scripts_dir)):
if not file.endswith(".py"):
continue
path = os.path.join(scripts_dir, file)
spec = importlib.util.spec_from_file_location(file[:-3], path)
if spec is None or spec.loader is None:
continue
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
logger.info(f"Script Loaded: {file}")
def connect(self) -> bool:
logger.raw("""
██████╗ ██╗ ██╗██████╗ █████╗ ██╗ ██╗███████╗ █████╗ ███╗ ███╗██████╗
██╔══██╗╚██╗ ██╔╝██╔══██╗██╔══██╗██║ ██╔╝██╔════╝██╔══██╗████╗ ████║██╔══██╗
██████╔╝ ╚████╔╝ ██████╔╝███████║█████╔╝ ███████╗███████║██╔████╔██║██████╔╝
██╔═══╝ ╚██╔╝ ██╔══██╗██╔══██║██╔═██╗ ╚════██║██╔══██║██║╚██╔╝██║██╔═══╝
██║ ██║ ██║ ██║██║ ██║██║ ██╗███████║██║ ██║██║ ╚═╝ ██║██║
╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝
""")
return self.api.connect()
def close(self) -> None:
self._window_title_running = False
try:
emitter.emit("onBotStopping", self.api)
self.api.disconnect()
finally:
if (
self._window_title_thread is not None
and self._window_title_thread.is_alive()
and threading.current_thread() is not self._window_title_thread
):
self._window_title_thread.join(timeout=1.0)
emitter.shutdown(wait=True)