Implemented User Points for teams #140

Merged
Hay1tsme merged 3 commits from :develop into develop 2024-06-29 04:04:14 +00:00
2 changed files with 45 additions and 7 deletions

View File

@ -720,9 +720,14 @@ class ChuniBase:
team_id = 65535 team_id = 65535
team_name = self.game_cfg.team.team_name team_name = self.game_cfg.team.team_name
team_rank = 0 team_rank = 0
team_user_point = 0
# Get user profile # Get user profile
profile = await self.data.profile.get_profile_data(data["userId"], self.version) profile = await self.data.profile.get_profile_data(data["userId"], self.version)
if profile is None:
return {"userId": data["userId"], "teamId": 0}
if profile and profile["teamId"]: if profile and profile["teamId"]:
# Get team by id # Get team by id
team = await self.data.profile.get_team_by_id(profile["teamId"]) team = await self.data.profile.get_team_by_id(profile["teamId"])
@ -731,7 +736,12 @@ class ChuniBase:
team_id = team["id"] team_id = team["id"]
team_name = team["teamName"] team_name = team["teamName"]
team_rank = await self.data.profile.get_team_rank(team["id"]) team_rank = await self.data.profile.get_team_rank(team["id"])
team_point = team["teamPoint"]
if team["userTeamPoint"] is not None and team["userTeamPoint"] is not "":
user_team_point_data = json.loads(team["userTeamPoint"])
for user_point_data in user_team_point_data:
if user_point_data["user"] == data["userId"]:
team_user_point = int(user_point_data["userPoint"])
# Don't return anything if no team name has been defined for defaults and there is no team set for the player # Don't return anything if no team name has been defined for defaults and there is no team set for the player
if not profile["teamId"] and team_name == "": if not profile["teamId"] and team_name == "":
return {"userId": data["userId"], "teamId": 0} return {"userId": data["userId"], "teamId": 0}
@ -741,11 +751,12 @@ class ChuniBase:
"teamId": team_id, "teamId": team_id,
"teamRank": team_rank, "teamRank": team_rank,
"teamName": team_name, "teamName": team_name,
"assaultTimeRate": 1, # TODO: Figure out assaultTime, which might be team point boost?
"userTeamPoint": { "userTeamPoint": {
"userId": data["userId"], "userId": data["userId"],
"teamId": team_id, "teamId": team_id,
"orderId": 1, "orderId": 0,
"teamPoint": 1, "teamPoint": team_user_point,
"aggrDate": data["playDate"], "aggrDate": data["playDate"],
}, },
} }

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",
) )
@ -705,12 +707,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)
@ -722,6 +748,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)