aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-03-16 14:54:38 +0100
committerlonkaars <l.leblansch@gmail.com>2021-03-16 14:54:38 +0100
commit03bad7b1d65394eff846f411aa5c49982843b100 (patch)
treea0553a51f2e2947ba64013ea52e6410238efc399 /api
parent447926f7825e1995a52fc7315493e416b83e50f2 (diff)
~rating
Diffstat (limited to 'api')
-rw-r--r--api/game/info.py6
-rw-r--r--api/rating.py26
-rw-r--r--api/user/info.py4
3 files changed, 30 insertions, 6 deletions
diff --git a/api/game/info.py b/api/game/info.py
index 64812d8..a58edde 100644
--- a/api/game/info.py
+++ b/api/game/info.py
@@ -3,13 +3,9 @@ from functools import reduce
from db import cursor
from auth.login_token import token_login
from user.info import format_user
+from rating import outcome
from ruleset import resolve_ruleset
-def outcome(outcome_str, player_1):
- outcome_int = { "w": 1, "l": -1, "d": 0 }[outcome_str]
- if not player_1: outcome_int *= -1
- return { 1: "w", -1: "l", 0: "d" }[outcome_int]
-
def format_game(game_id, user_id = None):
game = cursor.execute("select " + ", ".join([
"game_id", # 0
diff --git a/api/rating.py b/api/rating.py
new file mode 100644
index 0000000..e05645f
--- /dev/null
+++ b/api/rating.py
@@ -0,0 +1,26 @@
+from db import cursor
+
+def outcome(outcome_str, player_1):
+ outcome_int = { "w": 1, "l": -1, "d": 0 }[outcome_str]
+ if not player_1: outcome_int *= -1
+ return { 1: "w", -1: "l", 0: "d" }[outcome_int]
+
+def rating_v1(won_games): # python is a garbage language
+ return abs(won_games) ** (1 / 2.2) * 23 * (1, -1)[won_games < 0]
+
+def get_all_games(user_id):
+ return cursor.execute("select player_1_id, player_2_id, outcome " + \
+ "from games " + \
+ "where (player_1_id = ? or player_2_id = ?) " + \
+ "and status = \"finished\" or status = \"resign\"", [user_id, user_id]).fetchall()
+
+def get_rating(user_id):
+ score = 400
+ games = get_all_games(user_id)
+ mapped_games = [game if game[0] == user_id else (game[1], game[0], outcome(game[2], False)) for game in games]
+ counted_opponents = {}
+ for game in mapped_games:
+ counted_opponents |= {game[1]: (counted_opponents.get(game[1]) or 0) + { "w": 1, "l": -1, "d": 0 }[game[2]]}
+ for opponent in counted_opponents:
+ score += rating_v1(counted_opponents.get(opponent))
+ return int(score)
diff --git a/api/user/info.py b/api/user/info.py
index b60593e..a9594aa 100644
--- a/api/user/info.py
+++ b/api/user/info.py
@@ -1,6 +1,7 @@
from flask import Blueprint, request
from db import cursor
from auth.login_token import token_login
+from rating import get_rating
import json
def valid_user_id(user_id):
@@ -38,7 +39,8 @@ def format_user(user_id, viewer = ''):
"registered": user[3],
"avatar": user[4],
"status": user[5],
- "friends": count_friends(user_id)
+ "friends": count_friends(user_id),
+ "rating": get_rating(user_id),
}
if viewer:
formatted_user["relation"] = get_relation_to(viewer, user_id)