Skip to content

Commit 37bc75a

Browse files
committed
CHANGES:
- Changed the order of SQLs in player to prepare for foreign keys - Changes the order of the players embed to sort by player name BUGFIXES: - Monitoring: warning was still sent even if alarm pct was reached.
1 parent 7792948 commit 37bc75a

File tree

5 files changed

+74
-68
lines changed

5 files changed

+74
-68
lines changed

core/data/player.py

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -59,49 +59,50 @@ def __post_init__(self):
5959
lock_time = self.server.locals.get('coalitions', {}).get('lock_time', '1 day')
6060
with self.pool.connection() as conn:
6161
with conn.transaction():
62-
with closing(conn.cursor()) as cursor:
63-
cursor.execute(f"""
64-
SELECT DISTINCT p.discord_id, CASE WHEN b.ucid IS NOT NULL THEN TRUE ELSE FALSE END AS banned,
65-
p.manual, c.coalition,
66-
CASE WHEN w.player_ucid IS NOT NULL THEN TRUE ELSE FALSE END AS watchlict, p.vip
67-
FROM players p LEFT OUTER JOIN bans b ON p.ucid = b.ucid
68-
LEFT OUTER JOIN coalitions c
69-
ON p.ucid = c.player_ucid
70-
AND c.server_name = %s
71-
AND c.coalition_join > (NOW() AT TIME ZONE 'UTC' - interval '{lock_time}')
72-
LEFT OUTER JOIN watchlist w ON p.ucid = w.player_ucid
73-
WHERE p.ucid = %s
74-
AND COALESCE(b.banned_until, (now() AT TIME ZONE 'utc')) >= (now() AT TIME ZONE 'utc')
75-
""", (self.server.name, self.ucid))
76-
# existing member found?
77-
if cursor.rowcount == 1:
78-
row = cursor.fetchone()
79-
self._member = self.bot.get_member_by_ucid(self.ucid)
80-
if self._member:
81-
# special handling for discord-less bots
82-
if isinstance(self._member, discord.Member):
83-
self._verified = row[2]
84-
else:
85-
self._verified = True
86-
self.banned = row[1]
87-
if row[3]:
88-
self.coalition = Coalition(row[3])
89-
self._watchlist = row[4]
90-
self._vip = row[5]
91-
else:
92-
rules = self.server.locals.get('rules')
93-
if rules:
94-
cursor.execute("""
95-
INSERT INTO messages (sender, player_ucid, message, ack)
96-
VALUES (%s, %s, %s, %s)
97-
""", (self.server.locals.get('server_user', 'Admin'), self.ucid, rules,
98-
self.server.locals.get('accept_rules_on_join', False)))
99-
100-
cursor.execute("""
101-
INSERT INTO players (ucid, discord_id, name, last_seen)
102-
VALUES (%s, -1, %s, (now() AT TIME ZONE 'utc'))
103-
ON CONFLICT (ucid) DO UPDATE SET name=excluded.name, last_seen=excluded.last_seen
104-
""", (self.ucid, self.name))
62+
# add new players to the database
63+
conn.execute("""
64+
INSERT INTO players (ucid, discord_id, name, last_seen)
65+
VALUES (%s, -1, %s, (now() AT TIME ZONE 'utc'))
66+
ON CONFLICT (ucid) DO UPDATE SET name=excluded.name, last_seen=excluded.last_seen
67+
""", (self.ucid, self.name))
68+
# get the player information
69+
cursor = conn.execute(f"""
70+
SELECT DISTINCT p.discord_id, CASE WHEN b.ucid IS NOT NULL THEN TRUE ELSE FALSE END AS banned,
71+
p.manual, c.coalition,
72+
CASE WHEN w.player_ucid IS NOT NULL THEN TRUE ELSE FALSE END AS watchlict, p.vip
73+
FROM players p LEFT OUTER JOIN bans b ON p.ucid = b.ucid
74+
LEFT OUTER JOIN coalitions c
75+
ON p.ucid = c.player_ucid
76+
AND c.server_name = %s
77+
AND c.coalition_join > (NOW() AT TIME ZONE 'UTC' - interval '{lock_time}')
78+
LEFT OUTER JOIN watchlist w ON p.ucid = w.player_ucid
79+
WHERE p.ucid = %s
80+
AND COALESCE(b.banned_until, (now() AT TIME ZONE 'utc')) >= (now() AT TIME ZONE 'utc')
81+
""", (self.server.name, self.ucid))
82+
# existing member found?
83+
if cursor.rowcount == 1:
84+
row = cursor.fetchone()
85+
self._member = self.bot.get_member_by_ucid(self.ucid)
86+
if self._member:
87+
# special handling for discord-less bots
88+
if isinstance(self._member, discord.Member):
89+
self._verified = row[2]
90+
else:
91+
self._verified = True
92+
self.banned = row[1]
93+
if row[3]:
94+
self.coalition = Coalition(row[3])
95+
self._watchlist = row[4]
96+
self._vip = row[5]
97+
else:
98+
rules = self.server.locals.get('rules')
99+
if rules:
100+
cursor.execute("""
101+
INSERT INTO messages (sender, player_ucid, message, ack)
102+
VALUES (%s, %s, %s, %s)
103+
""", (self.server.locals.get('server_user', 'Admin'), self.ucid, rules,
104+
self.server.locals.get('accept_rules_on_join', False)))
105+
105106
# if automatch is enabled, try to match the user
106107
if not self.member and self.bot.locals.get('automatch', False):
107108
discord_user = self.bot.match_user({"ucid": self.ucid, "name": self.name})

core/report/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ async def render(self, *args, **kwargs) -> ReportEnv:
420420
msg = f"Exception while processing report {self.filename}!"
421421
if self.server:
422422
msg += f' for server {self.server.name}'
423-
self.log.error(msg)
423+
self.log.error(msg, exc_info=True)
424424
raise
425425
finally:
426426
if env and env.filename:

core/services/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def proxy(_func: Callable[..., Any] | None = None, *, timeout: float = 60):
4242
async def my_fancy_method(self, server: Server, *args, **kwargs) -> Any:
4343
...
4444
45-
This will call my_fancy_method on the remote node, if the server is remote, and on the local node, if it is not.
45+
This will call my_fancy_method on the remote node if the server is remote, and on the local node, if it is not.
4646
"""
4747
def decorator(original_function: Callable[..., Any]):
4848
@wraps(original_function)

plugins/mission/players.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from core import report, Server, Side, Coalition
2+
from plugins.srs.commands import SRS
3+
from typing import cast
24

35

46
class Main(report.EmbedElement):
@@ -10,12 +12,13 @@ async def render(self, server: Server, sides: list[Coalition]):
1012
Side.RED: {"names": [], "units": [], "SRS": []},
1113
Side.NEUTRAL: {"names": [], "units": [], "SRS": []}
1214
}
13-
srs_plugin = self.bot.cogs.get('SRS', None)
15+
srs_plugin = cast(SRS, self.bot.cogs.get('SRS', None))
1416
if srs_plugin:
1517
srs_users = srs_plugin.eventlistener.srs_users.get(server.name, {})
1618
else:
1719
srs_users = {}
18-
for player in players:
20+
players_sorted = sorted(players, key=lambda p: p.display_name)
21+
for player in players_sorted:
1922
sides[player.side]['names'].append(player.display_name)
2023
if player.side != Side.SPECTATOR:
2124
unit = player.unit_type

services/monitoring/service.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -350,27 +350,29 @@ async def drive_check(self):
350350
total, free = utils.get_drive_space(drive)
351351
warn_pct = config['warn'] / 100
352352
alert_pct = config['alert'] / 100
353-
if (free < total * warn_pct) and not self.space_warning_sent[drive]:
354-
message = config['message'].format(
355-
drive=drive,
356-
pct=config['warn'],
357-
bytes_free=self.convert_bytes(free),
358-
bytes_total=self.convert_bytes(total)
359-
)
360-
self.log.warning(message)
361-
await self.node.audit(message)
362-
self.space_warning_sent[drive] = True
363-
if (free < total * alert_pct) and not self.space_alert_sent[drive]:
364-
message = config['message'].format(
365-
drive=drive,
366-
pct=config['alert'],
367-
bytes_free=self.convert_bytes(free),
368-
bytes_total=self.convert_bytes(total)
369-
)
370-
self.log.error(message)
371-
await self.send_alert(title=f"Your DCS drive on node {self.node.name} is running out of space!",
372-
message=message)
373-
self.space_alert_sent[drive] = True
353+
if free < total * alert_pct:
354+
if not self.space_alert_sent[drive]:
355+
message = config['message'].format(
356+
drive=drive,
357+
pct=config['alert'],
358+
bytes_free=self.convert_bytes(free),
359+
bytes_total=self.convert_bytes(total)
360+
)
361+
self.log.error(message)
362+
await self.send_alert(title=f"Your DCS drive on node {self.node.name} is running out of space!",
363+
message=message)
364+
self.space_alert_sent[drive] = True
365+
elif free < total * warn_pct:
366+
if not self.space_warning_sent[drive]:
367+
message = config['message'].format(
368+
drive=drive,
369+
pct=config['warn'],
370+
bytes_free=self.convert_bytes(free),
371+
bytes_total=self.convert_bytes(total)
372+
)
373+
self.log.warning(message)
374+
await self.node.audit(message)
375+
self.space_warning_sent[drive] = True
374376

375377
@tasks.loop(minutes=1.0)
376378
async def monitoring(self):

0 commit comments

Comments
 (0)