Skip to content

Commit 44edd9e

Browse files
committed
New version 3.0.4.0
ENHANCEMENTS: - Squadron credits added - /squadron credits added - Lua: dcsbot.addSquadronPoints() / dcsbot.getSquadronPoints() / dcsbot.listSquadrons() added - Lua: dcsbot.getUserRoles() added to retrieve the roles of the user in mission. - Competitive: auto-join to matches added, win-types added. CHANGES: - MissionStats: changed to dynamic event names - MizEdit: can work better with variables now, performance improved syntax support for lists/searches - People can only be a member of ONE squadron at a time now! - CreditSystem: Squadrons will receive points on kills now for each squadron member. - Competitive: new event onMatchFinished to be used in other plugins BUGFIXES: - Competitive: match finish check was not activated
1 parent db0c46d commit 44edd9e

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE TABLE IF NOT EXISTS squadron_credits (campaign_id INTEGER NOT NULL, squadron_id INTEGER PRIMARY KEY, points INTEGER NOT NULL DEFAULT 0);
2+
CREATE TABLE IF NOT EXISTS squadron_credits_log (id SERIAL PRIMARY KEY, campaign_id INTEGER NOT NULL, event TEXT NOT NULL, squadron_id INTEGER NOT NULL, old_points INTEGER NOT NULL, new_points INTEGER NOT NULL, player_ucid TEXT, remark TEXT, time TIMESTAMP DEFAULT (now() AT TIME ZONE 'utc'));

plugins/creditsystem/squadron.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from core import Server, utils
2+
from core.data.dataobject import DataObjectFactory, DataObject
3+
from core.services.registry import ServiceRegistry
4+
from core.utils import get_squadron
5+
from core.data.member import Member
6+
from core.data.const import Status
7+
from dataclasses import dataclass, field
8+
from services.servicebus import ServiceBus
9+
from typing import TYPE_CHECKING, Optional
10+
11+
if TYPE_CHECKING:
12+
from .player import CreditPlayer
13+
14+
15+
@dataclass
16+
@DataObjectFactory.register()
17+
class Squadron(DataObject):
18+
name: str
19+
server: Server
20+
players: list[Member] = field(compare=False, default_factory=list)
21+
_points: int = field(compare=False, default=-1)
22+
squadron_id: int = field(compare=False, init=False)
23+
24+
def __post_init__(self):
25+
super().__post_init__()
26+
squadron = get_squadron(self.node, name=self.name)
27+
if squadron:
28+
self.squadron_id = squadron['id']
29+
else:
30+
self.squadron_id = -1
31+
32+
@property
33+
def points(self) -> int:
34+
if self._points == -1:
35+
with self.pool.connection() as conn:
36+
campaign_id, _ = utils.get_running_campaign(self.node, self.server)
37+
if not campaign_id:
38+
return -1
39+
cursor = conn.execute("SELECT points FROM squadron_credits WHERE campaign_id = %s AND squadron_id = %s",
40+
(campaign_id, self.squadron_id))
41+
row = cursor.fetchone()
42+
if row:
43+
self._points = row[0]
44+
else:
45+
self.points = 0
46+
return self._points
47+
48+
@points.setter
49+
def points(self, p: int) -> None:
50+
self._points = p
51+
52+
campaign_id, _ = utils.get_running_campaign(self.node, self.server)
53+
if campaign_id:
54+
# persist points
55+
with self.pool.connection() as conn:
56+
with conn.transaction():
57+
conn.execute("""
58+
INSERT INTO squadron_credits (campaign_id, squadron_id, points)
59+
VALUES (%s, %s, %s)
60+
ON CONFLICT (squadron_id) DO UPDATE SET points = EXCLUDED.points
61+
""", (campaign_id, self.squadron_id, self._points))
62+
63+
# send points to all active DCS-servers
64+
bus = ServiceRegistry.get(ServiceBus)
65+
for server in bus.servers.values():
66+
if server.status in [Status.PAUSED, Status.RUNNING]:
67+
bus.loop.create_task(server.send_to_dcs({
68+
'command': 'updateSquadronPoints',
69+
'squadron': self.name,
70+
'points': self._points
71+
}))
72+
73+
def audit(self, event: str, points: int, remark: str, player: Optional["CreditPlayer"] = None):
74+
if points == 0:
75+
return
76+
campaign_id, _ = utils.get_running_campaign(self.node, self.server)
77+
if not campaign_id:
78+
return
79+
with self.pool.connection() as conn:
80+
with conn.transaction():
81+
conn.execute("""
82+
INSERT INTO squadron_credits_log (
83+
campaign_id,
84+
event,
85+
squadron_id,
86+
old_points,
87+
new_points,
88+
player_ucid,
89+
remark
90+
) VALUES (%s, %s, %s, %s, %s, %s, %s)
91+
""", (campaign_id, event, self.squadron_id, self._points - points, self._points,
92+
player.ucid or None, remark))

0 commit comments

Comments
 (0)