Fix openskill matches not saving properly
live RealDictCursor moment. fuck you python
This commit is contained in:
parent
20ab1d320a
commit
5363f9291a
82
main.py
82
main.py
|
@ -185,6 +185,30 @@ def create_match(request: CreateMatchRequest):
|
||||||
cursor.close()
|
cursor.close()
|
||||||
conn.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")
|
@app.post("/joinmatch")
|
||||||
def join_match(request: JoinMatchRequest):
|
def join_match(request: JoinMatchRequest):
|
||||||
|
@ -466,40 +490,6 @@ def get_latest_commit_hashes():
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"error": str(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")
|
@app.post("/joinmatch_2v2")
|
||||||
def join_match_2v2(request: JoinMatch2v2Request):
|
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):
|
async def end_four_match_openskill(request: EndFourMatch):
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
try:
|
try:
|
||||||
# Fetch player IDs and their current ratings
|
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"""
|
"""
|
||||||
SELECT player1_team1_uid, player2_team1_uid, player1_team2_uid, player2_team2_uid
|
SELECT player1_team1_uid, player2_team1_uid, player1_team2_uid, player2_team2_uid
|
||||||
|
@ -577,14 +566,18 @@ async def end_four_match_openskill(request: EndFourMatch):
|
||||||
(request.match_id,)
|
(request.match_id,)
|
||||||
)
|
)
|
||||||
match = cursor.fetchone()
|
match = cursor.fetchone()
|
||||||
|
print(match)
|
||||||
if not match:
|
if not match:
|
||||||
raise HTTPException(status_code=404, detail="Match not found")
|
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(
|
cursor.execute(
|
||||||
"""
|
"""
|
||||||
SELECT uid, trueskill_mu AS mu, trueskill_sigma AS sigma
|
SELECT uid, openskill_mu AS mu, openskill_sigma AS sigma
|
||||||
FROM users
|
FROM users
|
||||||
WHERE uid IN (%s, %s, %s, %s);
|
WHERE uid IN (%s, %s, %s, %s);
|
||||||
""",
|
""",
|
||||||
|
@ -595,7 +588,6 @@ async def end_four_match_openskill(request: EndFourMatch):
|
||||||
for row in cursor.fetchall()
|
for row in cursor.fetchall()
|
||||||
}
|
}
|
||||||
|
|
||||||
# Default ratings for any missing players
|
|
||||||
default_rating = model.rating(mu=25, sigma=8.333)
|
default_rating = model.rating(mu=25, sigma=8.333)
|
||||||
players = {
|
players = {
|
||||||
player1_team1_uid: players.get(player1_team1_uid, default_rating),
|
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),
|
player2_team2_uid: players.get(player2_team2_uid, default_rating),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Calculate team scores
|
|
||||||
team1_score = request.player1_team1_score + request.player2_team1_score
|
team1_score = request.player1_team1_score + request.player2_team1_score
|
||||||
team2_score = request.player1_team2_score + request.player2_team2_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]
|
ranks = [0, 1] if team1_score > team2_score else [1, 0]
|
||||||
|
|
||||||
# Update ratings using OpenSkill
|
|
||||||
updated_ratings = model.rate(
|
updated_ratings = model.rate(
|
||||||
[[players[player1_team1_uid], players[player2_team1_uid]],
|
[[players[player1_team1_uid], players[player2_team1_uid]],
|
||||||
[players[player1_team2_uid], players[player2_team2_uid]]],
|
[players[player1_team2_uid], players[player2_team2_uid]]],
|
||||||
ranks=ranks
|
ranks=ranks
|
||||||
)
|
)
|
||||||
|
|
||||||
# Flatten the results for database updates
|
|
||||||
updates = [
|
updates = [
|
||||||
(uid, rating.mu, rating.sigma)
|
(uid, rating.mu, rating.sigma)
|
||||||
for uid, rating in zip(
|
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:
|
for uid, new_mu, new_sigma in updates:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"""
|
"""
|
||||||
UPDATE users
|
UPDATE users
|
||||||
SET trueskill_mu = %s, trueskill_sigma = %s
|
SET openskill_mu = %s, openskill_sigma = %s
|
||||||
WHERE uid = %s;
|
WHERE uid = %s;
|
||||||
""",
|
""",
|
||||||
(new_mu, new_sigma, uid)
|
(new_mu, new_sigma, uid)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Update match results
|
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"""
|
"""
|
||||||
UPDATE matches_2v2
|
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_team1_score, request.player2_team1_score,
|
||||||
request.player1_team2_score, request.player2_team2_score,
|
request.player1_team2_score, request.player2_team2_score,
|
||||||
team1_score, 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)
|
request.match_id)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -666,5 +652,3 @@ async def end_four_match_openskill(request: EndFourMatch):
|
||||||
cursor.close()
|
cursor.close()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
'''
|
|
||||||
|
|
Loading…
Reference in a new issue