Skip to content

Commit 35dbbbd

Browse files
committed
CHANGES:
- RestAPI: added /squadrons, squadron_members, /squadron_credits
1 parent fc44f54 commit 35dbbbd

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

plugins/restapi/commands.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import shutil
77
import uvicorn
88

9-
from core import Plugin, DEFAULT_TAG, Side
9+
from core import Plugin, DEFAULT_TAG, Side, DataObjectFactory, utils
1010
from datetime import datetime, timedelta, timezone
1111
from fastapi import FastAPI, APIRouter, Form
1212
from psycopg.rows import dict_row
13+
14+
from plugins.creditsystem.squadron import Squadron
1315
from services.bot import DCSServerBot
1416
from typing import Optional, Any
1517
from uvicorn import Config
@@ -33,13 +35,16 @@ def __init__(self, bot: DCSServerBot):
3335
prefix = cfg.get('prefix', '')
3436
self.router = APIRouter()
3537
self.router.add_api_route(prefix + "/servers", self.servers, methods=["GET"])
38+
self.router.add_api_route(prefix + "/squadrons", self.squadrons, methods=["GET"])
3639
self.router.add_api_route(prefix + "/topkills", self.topkills, methods=["GET"])
3740
self.router.add_api_route(prefix + "/topkdr", self.topkdr, methods=["GET"])
3841
self.router.add_api_route(prefix + "/trueskill", self.trueskill, methods=["GET"])
3942
self.router.add_api_route(prefix + "/getuser", self.getuser, methods=["POST"])
4043
self.router.add_api_route(prefix + "/missilepk", self.missilepk, methods=["POST"])
4144
self.router.add_api_route(prefix + "/stats", self.stats, methods=["POST"])
4245
self.router.add_api_route(prefix + "/credits", self.credits, methods=["POST"])
46+
self.router.add_api_route(prefix + "/squadron_members", self.squadron_members, methods=["POST"])
47+
self.router.add_api_route(prefix + "/squadron_credits", self.squadron_credits, methods=["POST"])
4348
self.router.add_api_route(prefix + "/linkme", self.linkme, methods=["POST"])
4449
self.app = app
4550
self.config = Config(app=self.app, host=cfg['listen'], port=cfg['port'], log_level=logging.ERROR,
@@ -100,6 +105,22 @@ async def servers(self):
100105
servers.append(data)
101106
return servers
102107

108+
async def squadrons(self):
109+
async with self.apool.connection() as conn:
110+
async with conn.cursor(row_factory=dict_row) as cursor:
111+
squadrons: list[dict] = []
112+
async for row in await cursor.execute("""
113+
SELECT * FROM squadrons ORDER BY name
114+
"""):
115+
squadrons.append({
116+
"name": row['name'],
117+
"description": row['description'],
118+
"image_url": row['image_url'],
119+
"locked": row['locked'],
120+
"role": self.bot.get_role(row['role']).name
121+
})
122+
return squadrons
123+
103124
async def topkills(self):
104125
async with self.apool.connection() as conn:
105126
async with conn.cursor(row_factory=dict_row) as cursor:
@@ -256,6 +277,36 @@ async def credits(self, nick: str = Form(default=None), date: str = Form(default
256277
""", (ucid, ))
257278
return await cursor.fetchone()
258279

280+
async def squadron_members(self, name: str = Form(default=None)):
281+
self.log.debug(f'Calling /squadron_members with name="{name}"')
282+
async with self.apool.connection() as conn:
283+
async with conn.cursor(row_factory=dict_row) as cursor:
284+
await cursor.execute("""
285+
SELECT p.name, DATE_TRUNC('second', p.last_seen) AS "date"
286+
FROM players p JOIN squadron_members sm ON sm.player_ucid = p.ucid
287+
JOIN squadrons s ON sm.squadron_id = s.id
288+
WHERE s.name = %s
289+
""", (name, ))
290+
return await cursor.fetchall()
291+
292+
async def squadron_credits(self, name: str = Form(default=None)):
293+
self.log.debug(f'Calling /squadron_members with name="{name}"')
294+
ret = []
295+
async with self.apool.connection() as conn:
296+
async for row in await conn.execute("""
297+
SELECT c.id, c.name
298+
FROM campaigns c
299+
LEFT OUTER JOIN squadron_credits s ON c.id = s.campaign_id
300+
JOIN squadrons s2 ON s.squadron_id = s2.id
301+
WHERE (now() AT TIME ZONE 'utc') BETWEEN c.start AND COALESCE(c.stop, now() AT TIME ZONE 'utc')
302+
AND s2.name = %s
303+
""", (name, )):
304+
squadron = utils.get_squadron(node=self.node, name=name)
305+
squadron_obj = DataObjectFactory().new(Squadron, node=self.node, name=squadron['name'],
306+
campaign_id=row[0])
307+
ret.append({"campaign": row[1], "credits": squadron_obj.points})
308+
return ret
309+
259310
async def linkme(self,
260311
discord_id: str = Form(..., description="Discord user ID (snowflake)", example="123456789012345678"),
261312
force: bool = Form(False, description="Force the operation", example=True)):

0 commit comments

Comments
 (0)