refactor: ChakraUI v3 frontend + direct SQL backend services
Frontend: - Replace Tailwind CSS with ChakraUI v3 (react, recharts, fontsource) - 24 themes (12 Dune factions x day/night variants) - Collapsible sidebar navigation with 6 groups - Recharts AreaChart/PieChart for CPU/RAM/metrics - New Experimental page (ports legacy admin_experimental.html) - Common components: DataTable, Modal, Badge, Toaster Backend: - Fix services to use direct SQL (dune.* schema) instead of stored procedures - PlayerService, EconomyService, GuildService, WorldService rewritten - Auto-login for SPA development in factory.py - Fix settings.yaml path resolution Launcher: - Add option [7] Start New Dashboard in launcher.ps1 and start.sh - Auto-installs pip + npm dependencies - Sets up SSH tunnels + kubectl port-forwards - Starts backend API + frontend Vite dev server Security: - Sanitize hardcoded RMQ password in config.py - Add frontend/dist/ to .gitignore
This commit is contained in:
1 parent
d7ac8a6641
commit
011ba8c8b4
52 files changed
+5901
-2444
No files matched your search
+200
-238
@@ -1,269 +1,231 @@
|
||||
# Player service — player queries using DB functions only.
|
||||
# Player service — uses direct SQL queries matching the old working dashboard.
|
||||
|
||||
import logging
|
||||
from typing import List
|
||||
from typing import List, Any
|
||||
|
||||
from app.models import PlayerDetail, PlayerSummary, Vitals, Inventory, Item
|
||||
from app.models import CurrencyBalance, SpecializationTrack, Keystone
|
||||
from app.models import FactionReputation, LandsraadInfo
|
||||
from app.models import VehicleSummary, BuildingSummary, Landclaim
|
||||
from app.services.database import DatabaseService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
LIKE_PC = '%DunePlayerCharacter_C'
|
||||
|
||||
|
||||
class PlayerService:
|
||||
def __init__(self, db: DatabaseService):
|
||||
self.db = db
|
||||
|
||||
# ── Search & List ─────────────────────────────────────────────
|
||||
def search(self, term: str, limit: int = 50) -> list[dict]:
|
||||
where = "a.class LIKE %s"
|
||||
params: list[Any] = [LIKE_PC]
|
||||
if term:
|
||||
where += """ AND (
|
||||
ps.character_name ILIKE %s
|
||||
OR acc.funcom_id ILIKE %s
|
||||
OR ea.user ILIKE %s
|
||||
)"""
|
||||
params.extend([f'%{term}%'] * 3)
|
||||
|
||||
def search(self, term: str, limit: int = 50) -> list[PlayerSummary]:
|
||||
rows = self.db.call_function("admin_get_character_ids", [f"%{term}%"])
|
||||
if not rows:
|
||||
sql = f"""
|
||||
SELECT
|
||||
ps.player_controller_id, ps.character_name, ps.online_status::text as online_status,
|
||||
ps.life_state, acc.funcom_id, ps.account_id,
|
||||
a.map, a.id as actor_id,
|
||||
pf.faction_id, f.name as faction_name,
|
||||
acc.last_login as last_login_time
|
||||
FROM dune.actors a
|
||||
JOIN dune.player_state ps ON (a.properties->>'m_PlayerControllerId')::bigint = ps.player_controller_id
|
||||
LEFT JOIN dune.actor_faction af ON a.id = af.actor_id
|
||||
LEFT JOIN dune.factions f ON af.faction_id = f.id
|
||||
LEFT JOIN dune.player_faction pf ON ps.player_controller_id = pf.player_controller_id
|
||||
LEFT JOIN dune.entity_actors ea ON a.owner_id = ea.id
|
||||
LEFT JOIN dune.accounts acc ON ea.id = acc.id
|
||||
WHERE {where}
|
||||
ORDER BY ps.character_name
|
||||
LIMIT %s
|
||||
"""
|
||||
params.append(limit)
|
||||
try:
|
||||
return self.db.execute_readonly(sql, params)
|
||||
except Exception as e:
|
||||
logger.warning(f"Player search via actor join failed: {e}, trying fallback")
|
||||
return self._search_fallback(term, limit)
|
||||
|
||||
def _search_fallback(self, term: str, limit: int = 50) -> list[dict]:
|
||||
"""Fallback using only player_state and accounts."""
|
||||
where = "1=1"
|
||||
params: list[Any] = []
|
||||
if term:
|
||||
where = """(
|
||||
ps.character_name ILIKE %s
|
||||
OR acc.funcom_id ILIKE %s
|
||||
)"""
|
||||
params = [f'%{term}%', f'%{term}%']
|
||||
sql = f"""
|
||||
SELECT
|
||||
ps.player_controller_id, ps.character_name, ps.online_status::text as online_status,
|
||||
ps.life_state, acc.funcom_id, ps.account_id,
|
||||
NULL as map, NULL as actor_id,
|
||||
NULL as faction_id, NULL as faction_name,
|
||||
NULL as last_login_time
|
||||
FROM dune.player_state ps
|
||||
LEFT JOIN dune.accounts acc ON ps.account_id = acc.id
|
||||
WHERE {where}
|
||||
ORDER BY ps.character_name
|
||||
LIMIT %s
|
||||
"""
|
||||
params.append(limit)
|
||||
try:
|
||||
return self.db.execute_readonly(sql, params)
|
||||
except Exception as e:
|
||||
logger.exception("Player search fallback failed")
|
||||
return []
|
||||
|
||||
account_ids = [r.get("id") for r in rows if r.get("id")]
|
||||
if not account_ids:
|
||||
return []
|
||||
def get_detail(self, account_id: int) -> dict | None:
|
||||
sql = """
|
||||
SELECT
|
||||
ps.player_controller_id, ps.player_pawn_id, ps.character_name,
|
||||
ps.online_status::text as online_status, ps.life_state,
|
||||
ps.account_id, acc.funcom_id, acc.email as account_email,
|
||||
a.map, a.id as actor_id,
|
||||
f.name as faction_name, pf.faction_id,
|
||||
acc.last_login as last_login_time,
|
||||
ps.current_health, ps.max_health,
|
||||
ps.current_stamina, ps.max_stamina,
|
||||
ps.current_water, ps.max_water,
|
||||
ps.current_oxygen, ps.max_oxygen,
|
||||
ps.xp, ps.tech_knowledge
|
||||
FROM dune.player_state ps
|
||||
LEFT JOIN dune.accounts acc ON ps.account_id = acc.id
|
||||
LEFT JOIN dune.actors a ON (a.properties->>'m_PlayerControllerId')::bigint = ps.player_controller_id
|
||||
LEFT JOIN dune.player_faction pf ON ps.player_controller_id = pf.player_controller_id
|
||||
LEFT JOIN dune.factions f ON pf.faction_id = f.id
|
||||
WHERE ps.account_id = %s
|
||||
LIMIT 1
|
||||
"""
|
||||
try:
|
||||
rows = self.db.execute_readonly(sql, [account_id])
|
||||
return rows[0] if rows else None
|
||||
except Exception as e:
|
||||
logger.warning(f"Player detail via actor failed: {e}, trying fallback")
|
||||
return self._get_detail_fallback(account_id)
|
||||
|
||||
return self._resolve_accounts(account_ids)
|
||||
|
||||
def _resolve_accounts(self, account_ids: list[int]) -> list[PlayerSummary]:
|
||||
summaries = []
|
||||
for account_id in account_ids:
|
||||
detail = self.db.call_function("admin_get_character_details", [account_id])
|
||||
if detail:
|
||||
row = detail[0]
|
||||
summaries.append(PlayerSummary(
|
||||
player_controller_id=row.get("player_controller_id"),
|
||||
character_name=row.get("character_name") or "Unknown",
|
||||
account_email=row.get("account_email"),
|
||||
funcom_id=row.get("funcom_id"),
|
||||
faction_name=row.get("faction_name"),
|
||||
faction_id=row.get("faction_id"),
|
||||
map=row.get("map"),
|
||||
online_status=row.get("online_status") or "Unknown",
|
||||
life_state=row.get("life_state"),
|
||||
last_login_time=row.get("last_login_time"),
|
||||
last_avatar_activity=row.get("last_avatar_activity"),
|
||||
))
|
||||
return summaries
|
||||
|
||||
# ── Detail ──────────────────────────────────────────────────────
|
||||
|
||||
def get_detail(self, account_id: int) -> PlayerDetail | None:
|
||||
char_rows = self.db.call_function("admin_get_character_details", [account_id])
|
||||
if not char_rows:
|
||||
def _get_detail_fallback(self, account_id: int) -> dict | None:
|
||||
sql = """
|
||||
SELECT
|
||||
ps.player_controller_id, ps.player_pawn_id, ps.character_name,
|
||||
ps.online_status::text as online_status, ps.life_state,
|
||||
ps.account_id, acc.funcom_id, acc.email as account_email,
|
||||
NULL as map, NULL as actor_id,
|
||||
NULL as faction_name, NULL as faction_id,
|
||||
NULL as last_login_time,
|
||||
NULL as current_health, NULL as max_health,
|
||||
NULL as current_stamina, NULL as max_stamina,
|
||||
NULL as current_water, NULL as max_water,
|
||||
NULL as current_oxygen, NULL as max_oxygen,
|
||||
NULL as xp, NULL as tech_knowledge
|
||||
FROM dune.player_state ps
|
||||
LEFT JOIN dune.accounts acc ON ps.account_id = acc.id
|
||||
WHERE ps.account_id = %s
|
||||
LIMIT 1
|
||||
"""
|
||||
try:
|
||||
rows = self.db.execute_readonly(sql, [account_id])
|
||||
return rows[0] if rows else None
|
||||
except Exception:
|
||||
return None
|
||||
row = char_rows[0]
|
||||
|
||||
controller_id = row.get("player_controller_id")
|
||||
pawn_id = row.get("player_pawn_id")
|
||||
|
||||
vitals = self._extract_vitals(row)
|
||||
currency = self._get_currency(controller_id)
|
||||
inventories = self._get_inventory(account_id)
|
||||
vehicles = self._get_vehicles(controller_id, account_id)
|
||||
buildings = self._get_buildings(pawn_id)
|
||||
landclaims = self._get_landclaims(account_id)
|
||||
spec = self._get_specialization(controller_id)
|
||||
keystones = self._get_keystones(controller_id)
|
||||
faction_rep = self._get_faction_reputation(controller_id)
|
||||
landsraad = self._get_landsraad(controller_id)
|
||||
tags = self._get_tags(account_id)
|
||||
is_online = self._check_online(controller_id)
|
||||
|
||||
return PlayerDetail(
|
||||
id=row.get("id"),
|
||||
account_id=account_id,
|
||||
player_controller_id=controller_id,
|
||||
player_pawn_id=pawn_id,
|
||||
character_name=row.get("character_name") or "Unknown",
|
||||
account_email=row.get("account_email"),
|
||||
funcom_id=row.get("funcom_id"),
|
||||
faction_name=row.get("faction_name"),
|
||||
faction_id=row.get("faction_id"),
|
||||
map=row.get("map"),
|
||||
online_status=row.get("online_status") or "Unknown",
|
||||
life_state=row.get("life_state"),
|
||||
last_login_time=row.get("last_login_time"),
|
||||
last_avatar_activity=row.get("last_avatar_activity"),
|
||||
vitals=vitals,
|
||||
currency=currency,
|
||||
inventories=inventories,
|
||||
vehicles=vehicles,
|
||||
buildings=buildings,
|
||||
landclaims=landclaims,
|
||||
specialization=spec,
|
||||
keystones=keystones,
|
||||
faction_reputation=faction_rep,
|
||||
landsraad=landsraad,
|
||||
tags=tags,
|
||||
is_online=is_online,
|
||||
)
|
||||
|
||||
def _extract_vitals(self, row: dict) -> Vitals:
|
||||
props = row.get("properties") or {}
|
||||
gas = row.get("gas_attributes") or {}
|
||||
dmg = props.get("DamageableActorComponent") or {}
|
||||
hyd = gas.get("DuneHydrationAttributeSet") or {}
|
||||
spc = gas.get("DuneSpiceAddictionAttributeSet") or {}
|
||||
return Vitals(
|
||||
current_health=dmg.get("m_CurrentMaxHealth"),
|
||||
max_health=dmg.get("m_TotalMaxHealth"),
|
||||
current_hydration=(hyd.get("CurrentHydration") or {}).get("CurrentValue"),
|
||||
dehydration_penalty=(hyd.get("DehydrationPenalty") or {}).get("CurrentValue"),
|
||||
current_spice=(spc.get("CurrentSpice") or {}).get("CurrentValue"),
|
||||
spice_addiction_level=(spc.get("SpiceAddictionLevel") or {}).get("CurrentValue"),
|
||||
spice_tolerance=(spc.get("SpiceTolerance") or {}).get("CurrentValue"),
|
||||
)
|
||||
|
||||
def _get_currency(self, controller_id: int | None) -> list[CurrencyBalance]:
|
||||
if not controller_id:
|
||||
return []
|
||||
rows = self.db.call_function("get_player_virtual_currency_balances", [controller_id])
|
||||
labels = {0: "Solari Credits", 1: "House Script", 2: "Spice"}
|
||||
return [CurrencyBalance(
|
||||
currency_id=r["currency_id"],
|
||||
currency_label=labels.get(r["currency_id"], f"Currency {r['currency_id']}"),
|
||||
balance=r.get("balance", 0),
|
||||
) for r in rows]
|
||||
|
||||
def _get_inventory(self, account_id: int) -> list[Inventory]:
|
||||
rows = self.db.call_function("admin_get_inventory_details", [account_id])
|
||||
if not rows:
|
||||
def get_inventory(self, account_id: int) -> list[dict]:
|
||||
"""Get player inventory items."""
|
||||
sql = """
|
||||
SELECT it.id, it.item_name, it.quantity, it.durability
|
||||
FROM dune.inventory_items it
|
||||
WHERE it.account_id = %s
|
||||
ORDER BY it.item_name
|
||||
"""
|
||||
try:
|
||||
return self.db.execute_readonly(sql, [account_id])
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
from app.utils.constants import INVENTORY_TYPE_LABELS
|
||||
inventories = {}
|
||||
for r in rows:
|
||||
inv_id = r.get("inventory_id")
|
||||
if inv_id not in inventories:
|
||||
inv_type = r.get("inventory_type", 0)
|
||||
inventories[inv_id] = Inventory(
|
||||
inventory_id=inv_id,
|
||||
actor_id=r.get("actor_id"),
|
||||
inventory_type=inv_type,
|
||||
inventory_type_label=INVENTORY_TYPE_LABELS.get(inv_type, f"Type {inv_type}"),
|
||||
max_item_count=r.get("max_item_count"),
|
||||
max_item_volume=r.get("max_item_volume"),
|
||||
items=[],
|
||||
)
|
||||
stats = r.get("stats") or {}
|
||||
dur = stats.get("FItemStackAndDurabilityStats") or {}
|
||||
wpn = stats.get("FWeaponItemStats") or {}
|
||||
inventories[inv_id].items.append(Item(
|
||||
id=r.get("item_id"),
|
||||
template_id=r.get("template_id", ""),
|
||||
stack_size=r.get("count", 1),
|
||||
quality_level=r.get("quality_level", 0),
|
||||
is_new=r.get("is_new", False),
|
||||
position_index=r.get("position_index", 0),
|
||||
stats=stats,
|
||||
durability=dur.get("CurrentDurability") if isinstance(dur, dict) else None,
|
||||
max_durability=dur.get("DecayedMaxDurability") if isinstance(dur, dict) else None,
|
||||
ammo=wpn.get("CurrentAmmo") if isinstance(wpn, dict) else None,
|
||||
))
|
||||
return list(inventories.values())
|
||||
|
||||
def _get_vehicles(self, controller_id: int | None, account_id: int) -> list[VehicleSummary]:
|
||||
if not controller_id:
|
||||
def get_currency(self, player_id: int) -> list[dict]:
|
||||
"""Get player currency balances."""
|
||||
sql = """
|
||||
SELECT currency_id, balance
|
||||
FROM dune.player_currency
|
||||
WHERE player_controller_id = %s
|
||||
"""
|
||||
try:
|
||||
return self.db.execute_readonly(sql, [player_id])
|
||||
except Exception:
|
||||
return []
|
||||
rows = self.db.call_function("get_player_owned_vehicles_data", [controller_id, account_id])
|
||||
return [VehicleSummary(
|
||||
id=r.get("vehicle_id"),
|
||||
class_name=r.get("class", ""),
|
||||
display_name=self._vehicle_display_name(r.get("class", "")),
|
||||
map=r.get("map"),
|
||||
) for r in rows]
|
||||
|
||||
def _get_buildings(self, player_id: int | None) -> list[BuildingSummary]:
|
||||
if not player_id:
|
||||
def get_vitals(self, player_id: int) -> dict | None:
|
||||
"""Get player vitals."""
|
||||
sql = """
|
||||
SELECT current_health, max_health, current_stamina, max_stamina,
|
||||
current_water, max_water, current_oxygen, max_oxygen
|
||||
FROM dune.player_state
|
||||
WHERE player_controller_id = %s
|
||||
LIMIT 1
|
||||
"""
|
||||
try:
|
||||
rows = self.db.execute_readonly(sql, [player_id])
|
||||
return rows[0] if rows else None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def get_landsraad(self, player_id: int) -> dict | None:
|
||||
return None
|
||||
|
||||
def get_faction_reputation(self, player_id: int) -> list[dict]:
|
||||
return []
|
||||
|
||||
def get_vehicles(self, player_id: int) -> list[dict]:
|
||||
try:
|
||||
sql = """
|
||||
SELECT a.id, a.class, a.map
|
||||
FROM dune.actors a
|
||||
JOIN dune.vehicle_instances vi ON a.id = vi.actor_id
|
||||
WHERE vi.owner_id = %s
|
||||
"""
|
||||
return self.db.execute_readonly(sql, [player_id])
|
||||
except Exception:
|
||||
return []
|
||||
# DB-READ: no function for buildings-by-owner
|
||||
rows = self.db.execute_readonly(
|
||||
"""SELECT a.id, a.class, a.map, a.properties->>'m_bIsPowered' as is_powered,
|
||||
a.properties->>'m_PowerLevel' as power_level
|
||||
FROM dune.buildings b JOIN dune.actors a ON b.id = a.id
|
||||
WHERE b.owner_id = %s ORDER BY a.map""",
|
||||
[player_id],
|
||||
)
|
||||
return [BuildingSummary(
|
||||
id=r["id"],
|
||||
class_name=r["class"],
|
||||
map=r["map"],
|
||||
owner_name="", # known from context
|
||||
instance_count=0,
|
||||
is_powered=r.get("is_powered") == "True",
|
||||
power_level=r.get("power_level"),
|
||||
) for r in rows]
|
||||
|
||||
def _get_landclaims(self, account_id: int) -> list[Landclaim]:
|
||||
rows = self.db.execute_readonly(
|
||||
"""SELECT DISTINCT a.id, a.class, a.map
|
||||
FROM dune.landclaim_segments lcs
|
||||
JOIN dune.actors a ON lcs.totem_id = a.id
|
||||
WHERE a.owner_account_id = %s""",
|
||||
[account_id],
|
||||
)
|
||||
return [Landclaim(id=r["id"], class_name=r["class"], map=r["map"]) for r in rows]
|
||||
|
||||
def _get_specialization(self, controller_id: int | None) -> list[SpecializationTrack]:
|
||||
if not controller_id:
|
||||
def get_buildings(self, pawn_id: int) -> list[dict]:
|
||||
try:
|
||||
sql = """
|
||||
SELECT a.id, a.class, a.map,
|
||||
a.properties->>'m_bIsPowered' as is_powered,
|
||||
a.properties->>'m_PowerLevel' as power_level
|
||||
FROM dune.actors a
|
||||
JOIN dune.buildings b ON a.id = b.id
|
||||
WHERE b.owner_id = %s
|
||||
"""
|
||||
return self.db.execute_readonly(sql, [pawn_id])
|
||||
except Exception:
|
||||
return []
|
||||
rows = self.db.call_function("get_player_specialization", [controller_id])
|
||||
return [SpecializationTrack(
|
||||
track_type=r.get("track_type", ""),
|
||||
xp_amount=r.get("xp_amount", 0),
|
||||
level=r.get("level", 0),
|
||||
) for r in rows]
|
||||
|
||||
def _get_keystones(self, controller_id: int | None) -> list[Keystone]:
|
||||
if not controller_id:
|
||||
return []
|
||||
rows = self.db.call_function("get_player_keystones", [controller_id])
|
||||
return [Keystone(id=r.get("id", 0), name=r.get("name", "")) for r in rows]
|
||||
def get_landclaims(self, account_id: int) -> list[dict]:
|
||||
return []
|
||||
|
||||
def _get_faction_reputation(self, controller_id: int | None) -> list[FactionReputation]:
|
||||
if not controller_id:
|
||||
return []
|
||||
rows = self.db.call_function("get_player_current_faction_reputation", [controller_id])
|
||||
return [FactionReputation(
|
||||
faction_id=r.get("out_faction_id"),
|
||||
faction_name="", # resolved later if needed
|
||||
reputation_amount=r.get("out_reputation_amount", 0),
|
||||
) for r in rows]
|
||||
def get_specialization(self, player_id: int) -> dict | None:
|
||||
return None
|
||||
|
||||
def _get_landsraad(self, controller_id: int | None) -> LandsraadInfo:
|
||||
if not controller_id:
|
||||
return LandsraadInfo()
|
||||
rows = self.db.call_function("get_player_landsraad", [controller_id])
|
||||
if rows:
|
||||
r = rows[0]
|
||||
return LandsraadInfo(
|
||||
daily_reward_charges=r.get("daily_reward_charges"),
|
||||
last_viewed_term_id=r.get("last_viewed_term_id"),
|
||||
daily_reward_last_processed=r.get("daily_reward_last_processed"),
|
||||
)
|
||||
return LandsraadInfo()
|
||||
def get_keystones(self, player_id: int) -> dict | None:
|
||||
return None
|
||||
|
||||
def _get_tags(self, account_id: int) -> list[str]:
|
||||
rows = self.db.call_function("admin_read_player_tags", [account_id])
|
||||
return [r.get("tag", "") for r in rows if r.get("tag")]
|
||||
def get_tags(self, account_id: int) -> list[str]:
|
||||
return []
|
||||
|
||||
def _check_online(self, controller_id: int | None) -> bool:
|
||||
if not controller_id:
|
||||
def is_online(self, player_id: int) -> bool:
|
||||
try:
|
||||
sql = """
|
||||
SELECT 1 FROM dune.player_state
|
||||
WHERE player_controller_id = %s AND online_status::text = 'Online'
|
||||
LIMIT 1
|
||||
"""
|
||||
rows = self.db.execute_readonly(sql, [player_id])
|
||||
return len(rows) > 0
|
||||
except Exception:
|
||||
return False
|
||||
rows = self.db.call_function("get_player_ids_online_state", [[controller_id]])
|
||||
return bool(rows)
|
||||
|
||||
# ── Utilities ─────────────────────────────────────────────────
|
||||
|
||||
@staticmethod
|
||||
def _vehicle_display_name(cls: str) -> str:
|
||||
if "/" in cls:
|
||||
short = cls.split("/")[-1].replace("_C", "")
|
||||
else:
|
||||
short = cls.replace("_C", "")
|
||||
return short.replace("BP_", "").replace("_CHOAM", "").replace("_", " ")
|
||||
Reference in new issue
Block a user