Fix openskill matches not saving properly
live RealDictCursor moment. fuck you python
This commit is contained in:
parent
20ab1d320a
commit
5363f9291a
84
main.py
84
main.py
|
@ -185,6 +185,30 @@ def create_match(request: CreateMatchRequest):
|
|||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
@app.post("/creatematch_2v2")
|
||||
def create_match_2v2(request: CreateMatchRequest):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
player1_uid = get_uid_by_token(request.token)
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO matches_2v2 (player1_team1_uid)
|
||||
VALUES (%s)
|
||||
RETURNING match_id;
|
||||
""",
|
||||
(player1_uid,)
|
||||
)
|
||||
match_id = cursor.fetchone()["match_id"]
|
||||
conn.commit()
|
||||
return {"match_id": match_id}
|
||||
except Exception as e:
|
||||
conn.rollback()
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
@app.post("/joinmatch")
|
||||
def join_match(request: JoinMatchRequest):
|
||||
|
@ -466,40 +490,6 @@ def get_latest_commit_hashes():
|
|||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
'''
|
||||
## EXPERIMENTAL: 4 player match TS calculation
|
||||
import trueskill
|
||||
ts = trueskill.TrueSkill(mu=25.0, sigma=8.333, beta=4.166, tau=0.083, draw_probability=0.1)
|
||||
|
||||
|
||||
@app.post("/creatematch_2v2")
|
||||
def create_match_2v2(request: CreateMatchRequest):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
player1_uid = get_uid_by_token(request.token)
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO matches_2v2 (player1_team1_uid)
|
||||
VALUES (%s)
|
||||
RETURNING match_id;
|
||||
""",
|
||||
(player1_uid,)
|
||||
)
|
||||
match_id = cursor.fetchone()["match_id"]
|
||||
conn.commit()
|
||||
return {"match_id": match_id}
|
||||
except Exception as e:
|
||||
conn.rollback()
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
class JoinMatch2v2Request(BaseModel):
|
||||
token: str
|
||||
match_id: int
|
||||
slot: str # e.g., "player1_team1", "player2_team1", "player1_team2", "player2_team2"
|
||||
|
||||
@app.post("/joinmatch_2v2")
|
||||
def join_match_2v2(request: JoinMatch2v2Request):
|
||||
|
@ -562,12 +552,11 @@ def join_match_2v2(request: JoinMatch2v2Request):
|
|||
|
||||
|
||||
|
||||
@app.post("/endfour_openskill")
|
||||
@app.post("/endfour")
|
||||
async def end_four_match_openskill(request: EndFourMatch):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
# Fetch player IDs and their current ratings
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT player1_team1_uid, player2_team1_uid, player1_team2_uid, player2_team2_uid
|
||||
|
@ -576,15 +565,19 @@ async def end_four_match_openskill(request: EndFourMatch):
|
|||
""",
|
||||
(request.match_id,)
|
||||
)
|
||||
match = cursor.fetchone()
|
||||
match = cursor.fetchone()
|
||||
print(match)
|
||||
if not match:
|
||||
raise HTTPException(status_code=404, detail="Match not found")
|
||||
|
||||
player1_team1_uid, player2_team1_uid, player1_team2_uid, player2_team2_uid = match
|
||||
player1_team1_uid = match['player1_team1_uid']
|
||||
player2_team1_uid = match['player2_team1_uid']
|
||||
player1_team2_uid = match['player1_team2_uid']
|
||||
player2_team2_uid = match['player2_team2_uid']
|
||||
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT uid, trueskill_mu AS mu, trueskill_sigma AS sigma
|
||||
SELECT uid, openskill_mu AS mu, openskill_sigma AS sigma
|
||||
FROM users
|
||||
WHERE uid IN (%s, %s, %s, %s);
|
||||
""",
|
||||
|
@ -595,7 +588,6 @@ async def end_four_match_openskill(request: EndFourMatch):
|
|||
for row in cursor.fetchall()
|
||||
}
|
||||
|
||||
# Default ratings for any missing players
|
||||
default_rating = model.rating(mu=25, sigma=8.333)
|
||||
players = {
|
||||
player1_team1_uid: players.get(player1_team1_uid, default_rating),
|
||||
|
@ -604,21 +596,17 @@ async def end_four_match_openskill(request: EndFourMatch):
|
|||
player2_team2_uid: players.get(player2_team2_uid, default_rating),
|
||||
}
|
||||
|
||||
# Calculate team scores
|
||||
team1_score = request.player1_team1_score + request.player2_team1_score
|
||||
team2_score = request.player1_team2_score + request.player2_team2_score
|
||||
|
||||
# Assign ranks based on scores
|
||||
ranks = [0, 1] if team1_score > team2_score else [1, 0]
|
||||
|
||||
# Update ratings using OpenSkill
|
||||
updated_ratings = model.rate(
|
||||
[[players[player1_team1_uid], players[player2_team1_uid]],
|
||||
[players[player1_team2_uid], players[player2_team2_uid]]],
|
||||
ranks=ranks
|
||||
)
|
||||
|
||||
# Flatten the results for database updates
|
||||
updates = [
|
||||
(uid, rating.mu, rating.sigma)
|
||||
for uid, rating in zip(
|
||||
|
@ -627,18 +615,16 @@ async def end_four_match_openskill(request: EndFourMatch):
|
|||
)
|
||||
]
|
||||
|
||||
# Update user ratings in the database
|
||||
for uid, new_mu, new_sigma in updates:
|
||||
cursor.execute(
|
||||
"""
|
||||
UPDATE users
|
||||
SET trueskill_mu = %s, trueskill_sigma = %s
|
||||
SET openskill_mu = %s, openskill_sigma = %s
|
||||
WHERE uid = %s;
|
||||
""",
|
||||
(new_mu, new_sigma, uid)
|
||||
)
|
||||
|
||||
# Update match results
|
||||
cursor.execute(
|
||||
"""
|
||||
UPDATE matches_2v2
|
||||
|
@ -651,7 +637,7 @@ async def end_four_match_openskill(request: EndFourMatch):
|
|||
(request.player1_team1_score, request.player2_team1_score,
|
||||
request.player1_team2_score, request.player2_team2_score,
|
||||
team1_score, team2_score,
|
||||
1 if team1_score > team2_score else 2, # Determine the winner team
|
||||
1 if team1_score > team2_score else 2,
|
||||
request.match_id)
|
||||
)
|
||||
|
||||
|
@ -666,5 +652,3 @@ async def end_four_match_openskill(request: EndFourMatch):
|
|||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
'''
|
||||
|
|
Loading…
Reference in a new issue