Skip to content

Commit

Permalink
Merge pull request #615 from aiarena/lint-and-bot-file-api-update-rem…
Browse files Browse the repository at this point in the history
…erge

REMERGE - Linting, bot file api and python version update
  • Loading branch information
lladdy authored Sep 8, 2023
2 parents 52d72d7 + a5dbff1 commit d1d28f4
Show file tree
Hide file tree
Showing 201 changed files with 6,639 additions and 4,302 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.7
FROM python:3.10

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
Expand Down
67 changes: 41 additions & 26 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,23 @@ on:
branches: [ master, staging ]

jobs:
build:
linters:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
python ./pip/pip-install.py --python=python --pip=pip
- uses: pre-commit/[email protected]

test:
runs-on: ubuntu-latest
services:
postgres:
Expand All @@ -32,32 +47,32 @@ jobs:
strategy:
max-parallel: 4
matrix:
python-version: [3.7]
python-version: [ '3.10' ]

steps:
- name: Checkout repository and submodules
uses: actions/checkout@v2
with:
submodules: recursive
- uses: shogo82148/actions-setup-redis@v1
with:
redis-version: '6.x'
- run: redis-cli ping
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
python ./pip/pip-install.py --python=python --pip=pip
- name: Grant aiarena user full access
env:
- name: Checkout repository and submodules
uses: actions/checkout@v3
with:
submodules: recursive
- uses: shogo82148/actions-setup-redis@v1
with:
redis-version: '6.x'
- run: redis-cli ping
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
python ./pip/pip-install.py --python=python --pip=pip
- name: Grant aiarena user full access
env:
PORT: ${{ job.services.postgres.ports[5432] }}
run: |
PGPASSWORD="aiarena" psql -Uaiarena --host="127.0.0.1" --port "$PORT" -c "GRANT ALL PRIVILEGES ON DATABASE aiarena TO aiarena;";
- name: Run Tests
env:
run: |
PGPASSWORD="aiarena" psql -Uaiarena --host="127.0.0.1" --port "$PORT" -c "GRANT ALL PRIVILEGES ON DATABASE aiarena TO aiarena;";
- name: Run Tests
env:
NOFAKE_REDIS: 1
run: |
python manage.py test --noinput
run: |
python manage.py test --noinput
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
repos:
- repo: local
hooks:
- id: black
name: Black
entry: black --check
types: [python]
language: python
- id: ruff
name: ruff
entry: ruff
types: [python]
language: python
2 changes: 1 addition & 1 deletion aiarena/api/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class ApiConfig(AppConfig):
name = 'aiarena.api'
name = "aiarena.api"
60 changes: 42 additions & 18 deletions aiarena/api/arenaclient/ac_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,29 @@

from aiarena.core.api.competitions import Competitions


if TYPE_CHECKING:
from aiarena.core.models import ArenaClient

import logging

from constance import config
from django.db import transaction, connection
from django.db import connection, transaction
from django.db.models import F
from django.db.models.signals import pre_save

from aiarena.api.arenaclient.exceptions import LadderDisabled, NotEnoughAvailableBots, MaxActiveRounds, NoMaps, \
CompetitionPaused, CompetitionClosing
from constance import config

from aiarena.api.arenaclient.exceptions import (
CompetitionClosing,
CompetitionPaused,
LadderDisabled,
MaxActiveRounds,
NoMaps,
NotEnoughAvailableBots,
)
from aiarena.core.api import Matches
from aiarena.core.models import Match, Competition
from django.db.models.signals import pre_save
from aiarena.core.models import Competition, Match


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -50,7 +59,13 @@ def next_competition_match(arenaclient: ArenaClient):
if Competitions.check_has_matches_to_play_and_apply_locks(competition):
try:
return Matches.start_next_match_for_competition(arenaclient, competition)
except (NoMaps, NotEnoughAvailableBots, MaxActiveRounds, CompetitionPaused, CompetitionClosing) as e:
except (
NoMaps,
NotEnoughAvailableBots,
MaxActiveRounds,
CompetitionPaused,
CompetitionClosing,
) as e:
logger.debug(f"Skipping competition {id}: {e}")
continue

Expand All @@ -69,14 +84,16 @@ def next_match(arenaclient: ArenaClient) -> Match:
try:
if config.REISSUE_UNFINISHED_MATCHES:
# Check for any unfinished matches assigned to this user. If any are present, return that.
unfinished_matches = Match.objects.only('id', 'map') \
.filter(started__isnull=False, assigned_to=arenaclient,
result__isnull=True).order_by(F('round_id').asc())
unfinished_matches = (
Match.objects.only("id", "map")
.filter(started__isnull=False, assigned_to=arenaclient, result__isnull=True)
.order_by(F("round_id").asc())
)
if unfinished_matches.count() > 0:
return unfinished_matches[0] # todo: re-set started time?
# Trying a new match
return ACCoordinator.next_new_match(arenaclient)
except Exception as e:
except Exception:
logger.exception("Exception while processing request for match.")
raise
else:
Expand All @@ -91,11 +108,12 @@ def _get_competition_priority_order():
:return:
"""

competition_priority_order = cache.get('competition_priority_order')
competition_priority_order = cache.get("competition_priority_order")
if not competition_priority_order:
with connection.cursor() as cursor:
# I don't know why but for some reason CTEs didn't work so here; have a massive query.
cursor.execute("""
cursor.execute(
"""
select perc_active.competition_id
from (select competition_id,
competition_participations.competition_participations_cnt / cast(total_active_cnt as float) as perc_active
Expand Down Expand Up @@ -135,10 +153,14 @@ def _get_competition_priority_order():
group by competition_id) as recent_matches) as perc_recent_matches
on perc_recent_matches.competition_id = perc_active.competition_id
order by COALESCE(perc_recent_matches, 0) - perc_active
""")
"""
)
competition_priority_order = [row[0] for row in cursor.fetchall()] # return competition ids
cache.set('competition_priority_order', competition_priority_order,
config.COMPETITION_PRIORITY_ORDER_CACHE_TIME)
cache.set(
"competition_priority_order",
competition_priority_order,
config.COMPETITION_PRIORITY_ORDER_CACHE_TIME,
)

return competition_priority_order

Expand All @@ -148,5 +170,7 @@ def post_save_competition(sender, instance, **kwargs):
# if it's not a new instance...
if instance.id is not None:
previous = Competition.objects.get(id=instance.id)
if previous.status != instance.status and cache.has_key('competition_priority_order'):
cache.delete('competition_priority_order') # if the status changed, bust our competition_priority_order cache
if previous.status != instance.status and cache.has_key("competition_priority_order"):
cache.delete(
"competition_priority_order"
) # if the status changed, bust our competition_priority_order cache
2 changes: 1 addition & 1 deletion aiarena/api/arenaclient/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class ArenaClientConfig(AppConfig):
name = 'aiarena.api.arenaclient'
name = "aiarena.api.arenaclient"
19 changes: 10 additions & 9 deletions aiarena/api/arenaclient/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,36 @@

class NoMaps(Exception):
def __init__(self):
super().__init__('There are no active maps available for a match.')
super().__init__("There are no active maps available for a match.")


class NotEnoughAvailableBots(Exception):
def __init__(self):
super().__init__('Not enough available bots for a match. Wait until more bots become available.')
super().__init__("Not enough available bots for a match. Wait until more bots become available.")


class MaxActiveRounds(Exception):
def __init__(self):
super().__init__('This competition has reached it\'s maximum active rounds.')
super().__init__("This competition has reached it's maximum active rounds.")


class LadderDisabled(APIException):
status_code = 200
default_detail = 'The ladder is currently disabled.'
default_code = 'ladder_disabled'
default_detail = "The ladder is currently disabled."
default_code = "ladder_disabled"


class CompetitionPaused(Exception):
def __init__(self):
super().__init__('This competition is paused.')
super().__init__("This competition is paused.")


class CompetitionClosing(Exception):
def __init__(self):
super().__init__('This competition is closing.')
super().__init__("This competition is closing.")


class NoGameForClient(APIException):
status_code = 200
default_detail = 'No game available for client.'
default_code = 'no_game_available'
default_detail = "No game available for client."
default_code = "no_game_available"
Loading

0 comments on commit d1d28f4

Please sign in to comment.