Skip to content

Commit

Permalink
In API, handle ratings submission when game is friendly or non-arena (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shaldengeki authored Aug 25, 2024
1 parent c6ba4c3 commit 05fdacb
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 8 deletions.
25 changes: 23 additions & 2 deletions ark_nova_stats/api/gql/types/game_rating.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from typing import Any, Optional, Type

from google.protobuf.json_format import Parse
from google.protobuf.json_format import ParseDict
from graphql import (
GraphQLArgument,
GraphQLField,
Expand Down Expand Up @@ -98,7 +99,27 @@ def submit_game_ratings(
args: dict[str, Any],
) -> list[GameRatingModel]:
table_id = int(args["tableId"])
parsed_ratings = Parse(args["ratings"], GameRatings(), ignore_unknown_fields=True)
json_ratings = json.loads(args["ratings"])

# Normally, for Arena games, we expect the rating update field to be a map.
# Unfortunately, in the case of non-Arena games, BGA sends along a totally different type (a list).
# This breaks our parsing, so we have to delete the field entirely.
# Similar for friendly games!
if (
"data" in json_ratings
and "players_elo_rating_update" in json_ratings["data"]
and isinstance(json_ratings["data"]["players_elo_rating_update"], list)
):
del json_ratings["data"]["players_elo_rating_update"]

if (
"data" in json_ratings
and "players_arena_rating_update" in json_ratings["data"]
and isinstance(json_ratings["data"]["players_arena_rating_update"], list)
):
del json_ratings["data"]["players_arena_rating_update"]

parsed_ratings = ParseDict(json_ratings, GameRatings(), ignore_unknown_fields=True)

# id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
# bga_table_id: Mapped[int] = mapped_column(ForeignKey("game_logs.bga_table_id"))
Expand Down
63 changes: 57 additions & 6 deletions ark_nova_stats/api/tests/game_ratings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
from ark_nova_stats.api.tests.fixtures import app, client


def test_submit_game_ratings(client: FlaskClient) -> None:
def read_fixture(name: str) -> str:
r = Runfiles.Create()
sample_ratings_fixture = r.Rlocation(
"_main/ark_nova_stats/bga_log_parser/fixtures/sample_game_ratings.json"
)
with open(sample_ratings_fixture, "r") as sample_ratings_logfile:
ratings_fixture_content = sample_ratings_logfile.read().strip()
fixture_path = r.Rlocation(f"_main/ark_nova_stats/bga_log_parser/fixtures/{name}")
with open(fixture_path, "r") as fixture_file:
return fixture_file.read().strip()


def test_submit_game_ratings(client: FlaskClient) -> None:
ratings_fixture_content = read_fixture("sample_game_ratings.json")
response = client.post(
"/graphql",
json={
Expand Down Expand Up @@ -63,5 +64,55 @@ def test_submit_game_ratings(client: FlaskClient) -> None:
assert 2034 == data[1]["newArenaElo"]


def test_submit_non_arena_game_ratings(client: FlaskClient) -> None:
ratings_fixture_content = read_fixture("ratings_non_arena.json")
response = client.post(
"/graphql",
json={
"query": """
mutation($ratings: String!, $tableId: Int!) {
submitGameRatings(
ratings: $ratings,
tableId: $tableId,
) {
id
newElo
priorElo
newArenaElo
priorArenaElo
}
}
""",
"variables": {
"ratings": ratings_fixture_content,
"tableId": 1,
},
},
)
assert response.json.get(
"data", {}
), f"data field not set on response json: {response.json}"
assert response.json["data"].get(
"submitGameRatings", {}
), f"submitGameRatings field not set on response json: {response.json['data']}"

data = response.json["data"]["submitGameRatings"]
assert 2 == len(
data
), f"Two ratings should have been created: {response.json['data']}"

# rudaZz
assert 1890 == data[0]["priorElo"]
assert 1885 == data[0]["newElo"]
assert data[0]["priorArenaElo"] is None
assert data[0]["newArenaElo"] is None

# sorryimlikethis
assert 2112 == data[1]["priorElo"]
assert 2116 == data[1]["newElo"]
assert data[1]["priorArenaElo"] is None
assert data[1]["newArenaElo"] is None


if __name__ == "__main__":
sys.exit(pytest.main([__file__] + sys.argv[1:]))
134 changes: 134 additions & 0 deletions ark_nova_stats/bga_log_parser/fixtures/ratings_non_arena.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
{
"status": 1,
"data": {
"players_results": {
"86346298": {
"id": "86346298",
"name": "sorryimlikethis",
"rank": "1",
"score": "132",
"score_aux": "5",
"zombie": "0",
"tie": "0"
},
"94569606": {
"id": "94569606",
"name": "rudaZz",
"rank": "2",
"score": "114",
"score_aux": "5",
"zombie": "0",
"tie": "0"
}
},
"players_current_ratings": {
"86346298": {
"id": "86346298",
"elo": "2111.82",
"games_played": "1812"
},
"94569606": {
"id": "94569606",
"elo": "1889.56",
"games_played": "1023"
}
},
"players_elo_rating_update": {
"86346298": {
"player_id": 86346298,
"global_modifiers": {
"p_factor": 1,
"n_factor": 1,
"e_factor": 0.5
},
"duels": {
"94569606": {
"expected_result": 0.7823529737580205,
"duel_result": 1,
"k_factor": 20,
"elo_delta": 4.352940524839591,
"duel_modifiers": [
{
"value": 1,
"id": "T",
"name": "Teammate",
"type": "nullifier",
"description": "",
"description_args": {
"{perc}": {
"text": "-100%",
"properties": [
"highlight"
]
}
}
},
{
"value": 1,
"id": "S",
"name": "Streak Malus",
"type": "malus",
"description": "",
"description_args": []
}
],
"opponent_id": 94569606
}
},
"tot_elo_delta": 4.352940524839591,
"avg_elo_delta": 4.352940524839591,
"real_elo_delta": 4.35,
"elo_delta_adjust_desc": "",
"new_elo_rating": 2116.17
},
"94569606": {
"player_id": 94569606,
"global_modifiers": {
"p_factor": 1,
"n_factor": 1,
"e_factor": 0.5
},
"duels": {
"86346298": {
"expected_result": 0.2176470262419795,
"duel_result": 0,
"k_factor": 20,
"elo_delta": -4.352940524839591,
"duel_modifiers": [
{
"value": 1,
"id": "T",
"name": "Teammate",
"type": "nullifier",
"description": "",
"description_args": {
"{perc}": {
"text": "-100%",
"properties": [
"highlight"
]
}
}
},
{
"value": 1,
"id": "S",
"name": "Streak Malus",
"type": "malus",
"description": "",
"description_args": []
}
],
"opponent_id": 86346298
}
},
"tot_elo_delta": -4.352940524839591,
"avg_elo_delta": -4.352940524839591,
"real_elo_delta": -4.35,
"elo_delta_adjust_desc": "",
"new_elo_rating": 1885.21
}
},
"players_arena_rating_update": []
}
}

0 comments on commit 05fdacb

Please sign in to comment.