Skip to content

Commit

Permalink
✨ Fix the tracked users
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoberg committed May 3, 2024
1 parent 0c31de7 commit 42f732f
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,42 +127,49 @@ def fetch_vatsim_pilots():
data = response.json()
return {pilot['cid']: pilot for pilot in data['pilots']}

tracked_users = {
'908962': 'kRhqR59V3/Ekbq1dpCr+QV8xAXo='
}

def updatePilots(ts3conn, conn):
"""
Update pilot groups in Teamspeak based on active pilots from VATSIM.
Update pilot groups in Teamspeak based on active pilots from VATSIM that are tracked.
"""
active_pilots = fetch_vatsim_pilots() # Fetches current active pilots

# Fetch existing TS3 groups related to pilots
ts3_groups = {group["name"]: int(group["sgid"]) for group in ts3conn.servergrouplist() if "Pilot_" in group["name"]}

for cid, pilot in active_pilots.items():
pilot_callsign = pilot['callsign']
ts3uid = pilot['ts3uid'] # This assumes ts3uid is somehow retrieved or stored

group_name = f"Pilot_{pilot_callsign}"
if group_name not in ts3_groups:
# Create new group if not exists
resp = ts3conn.servergroupcopy(ssgid=sourceGroup, tsgid=0, name=group_name, type_=1)
ts3_groups[group_name] = int(resp.parsed[0]["sgid"])
if cid in tracked_users:
pilot_callsign = pilot['callsign']
ts3uid = tracked_users[cid]
group_name = f"Pilot_{pilot_callsign}"
if group_name not in ts3_groups:
# Create new group if not exists
resp = ts3conn.servergroupcopy(ssgid=sourceGroup, tsgid=0, name=group_name, type_=1)
ts3_groups[group_name] = int(resp.parsed[0]["sgid"])

try:
# Add the pilot to the respective group
dbid = ts3conn.clientgetdbidfromuid(cluid=ts3uid).parsed[0]["cldbid"]
ts3conn.servergroupaddclient(sgid=ts3_groups[group_name], cldbid=dbid)
logger.info(f"Added pilot {cid} ({pilot_callsign}) to {group_name}")
incrementUpdateCount()
except Exception as e:
logger.error(f"Failed to add/update pilot {cid} in TS3: {e}")
incrementFailCount()
try:
# Add the pilot to the respective group
dbid = ts3conn.clientgetdbidfromuid(cluid=ts3uid).parsed[0]["cldbid"]
ts3conn.servergroupaddclient(sgid=ts3_groups[group_name], cldbid=dbid)
logger.info(f"Added pilot {cid} ({pilot_callsign}) to {group_name}")
incrementUpdateCount()
except Exception as e:
logger.error(f"Failed to add/update pilot {cid} in TS3: {e}")
incrementFailCount()
else:
logger.info(f"Pilot {cid} not tracked. Skipping...")

# Cleanup old pilot groups if no longer active
for group_name, sgid in ts3_groups.items():
if group_name not in [f"Pilot_{p['callsign']}" for p in active_pilots.values()]:
if group_name not in [f"Pilot_{p['callsign']}" for p in active_pilots.values() if p['cid'] in tracked_users]:
ts3conn.servergroupdel(sgid=sgid, force=1)
logger.info(f"Deleted unused pilot group {group_name}")



### CONTROLLER BLOCK ###


Expand Down

0 comments on commit 42f732f

Please sign in to comment.