Added team user points

This commit is contained in:
EmmyHeart 2024-05-13 08:45:19 +00:00
parent c96c9257a6
commit 50e0dde7de

View File

@ -1,3 +1,4 @@
import json
from typing import Dict, List, Optional from typing import Dict, List, Optional
from sqlalchemy import Table, Column, UniqueConstraint, and_ from sqlalchemy import Table, Column, UniqueConstraint, and_
from sqlalchemy.types import Integer, String, Boolean, JSON, BigInteger from sqlalchemy.types import Integer, String, Boolean, JSON, BigInteger
@ -389,6 +390,7 @@ team = Table(
Column("id", Integer, primary_key=True, nullable=False), Column("id", Integer, primary_key=True, nullable=False),
Column("teamName", String(255)), Column("teamName", String(255)),
Column("teamPoint", Integer), Column("teamPoint", Integer),
Column("userTeamPoint", JSON),
mysql_charset="utf8mb4", mysql_charset="utf8mb4",
) )
@ -693,12 +695,36 @@ class ChuniProfileData(BaseData):
# Return the rank if found, or a default rank otherwise # Return the rank if found, or a default rank otherwise
return rank if rank is not None else 0 return rank if rank is not None else 0
# RIP scaled team ranking. Gone, but forgotten async def update_team(self, team_id: int, team_data: Dict, user_id: str, user_point_delta: int) -> bool:
# def get_team_rank_scaled(self, team_id: int) -> int: # Update the team data
async def update_team(self, team_id: int, team_data: Dict) -> bool:
team_data["id"] = team_id team_data["id"] = team_id
existing_team = self.get_team_by_id(team_id)
if existing_team is None or "userTeamPoint" not in existing_team:
self.logger.warn(
f"update_team: Failed to update team! team id: {team_id}. Existing team data not found."
)
return False
user_team_point_data = []
if existing_team["userTeamPoint"] is not None and existing_team["userTeamPoint"] is not "":
user_team_point_data = json.loads(existing_team["userTeamPoint"])
updated = False
# Try to find the user in the existing data and update their points
for user_point_data in user_team_point_data:
if user_point_data["user"] == user_id:
user_point_data["userPoint"] = str(int(user_point_delta))
updated = True
break
# If the user was not found, add them to the data with the new points
if not updated:
user_team_point_data.append({"user": user_id, "userPoint": str(user_point_delta)})
# Update the team's userTeamPoint field in the team data
team_data["userTeamPoint"] = json.dumps(user_team_point_data)
# Update the team in the database
sql = insert(team).values(**team_data) sql = insert(team).values(**team_data)
conflict = sql.on_duplicate_key_update(**team_data) conflict = sql.on_duplicate_key_update(**team_data)
@ -710,6 +736,7 @@ class ChuniProfileData(BaseData):
) )
return False return False
return True return True
async def get_rival(self, rival_id: int) -> Optional[Row]: async def get_rival(self, rival_id: int) -> Optional[Row]:
sql = select(profile).where(profile.c.user == rival_id) sql = select(profile).where(profile.c.user == rival_id)
result = await self.execute(sql) result = await self.execute(sql)