From f01fad63053f1463d90ca4625a2044853e688fb2 Mon Sep 17 00:00:00 2001
From: SpookyBear0 <50897638+spookybear0@users.noreply.github.com>
Date: Mon, 13 May 2024 19:24:12 -0700
Subject: [PATCH] medic hits count for something and banners
---
app.py | 36 ---------------------
assets/css/app.css | 8 +++++
assets/html/admin/index.html | 62 +++++++++++++++++++++++++++++++++---
assets/html/base.html | 8 +++++
handlers/admin/index.py | 14 ++++++++
helpers/ratinghelper.py | 15 ++++++---
utils.py | 7 ++++
web.py | 5 +++
8 files changed, 110 insertions(+), 45 deletions(-)
delete mode 100644 app.py
diff --git a/app.py b/app.py
deleted file mode 100644
index 876ee96..0000000
--- a/app.py
+++ /dev/null
@@ -1,36 +0,0 @@
-import asyncio
-import os
-import sys
-import threading
-
-import webview
-from sanic.server.async_server import AsyncioServer
-
-import router
-from mysql import MySQLPool
-from shared import app
-
-path = os.path.dirname(os.path.abspath(__file__))
-os.chdir(path)
-sys.path.append(path)
-
-
-async def main():
- router.add_all_routes(app)
- app.static("assets", "assets", name="assets")
-
- app.ctx.sql = await MySQLPool.connect_with_config()
- server: AsyncioServer = await app.create_server(host="localhost", port=8000, return_asyncio_server=True)
-
- await server.startup()
- await server.serve_forever()
-
-
-if __name__ == "__main__":
- loop = asyncio.new_event_loop()
- asyncio.set_event_loop(loop)
-
- threading.Thread(target=lambda: loop.run_until_complete(main()), daemon=True).start()
-
- webview.create_window("Test", "http://localhost:8000/")
- webview.start(http_server=True)
diff --git a/assets/css/app.css b/assets/css/app.css
index e0377c7..529f141 100644
--- a/assets/css/app.css
+++ b/assets/css/app.css
@@ -651,4 +651,12 @@ dropdown menu
a {
color: #2fa4e7;
+}
+
+.banner {
+ background-color: #4da6ff;
+ color: #fff;
+ padding: 0.01rem;
+ text-align: center;
+ font-size: 0.5rem;
}
\ No newline at end of file
diff --git a/assets/html/admin/index.html b/assets/html/admin/index.html
index b1e26e8..2029429 100644
--- a/assets/html/admin/index.html
+++ b/assets/html/admin/index.html
@@ -3,6 +3,30 @@
{% block html_head %}
+
+
{% endblock %}
@@ -72,9 +113,22 @@
+
+
+
{% endblock %}
\ No newline at end of file
diff --git a/assets/html/base.html b/assets/html/base.html
index a97a90d..be5415b 100644
--- a/assets/html/base.html
+++ b/assets/html/base.html
@@ -283,6 +283,14 @@
href="/logout">Logout
{% endif %}
+
+
+{% if app.ctx.banner.text %}
+
+
{{ app.ctx.banner.text }}
+
+{% endif %}
+
{% block content %}{% endblock %}
diff --git a/handlers/admin/index.py b/handlers/admin/index.py
index c71c20a..be0afb2 100644
--- a/handlers/admin/index.py
+++ b/handlers/admin/index.py
@@ -20,7 +20,9 @@ async def admin_index(request: Request) -> str:
@app.post("/admin/recalculate_ratings")
@admin_only
async def admin_recalculate_ratings(request: Request) -> str:
+ request.app.ctx.banner["text"] = "Rating recalculation in progress, stats may be inaccurate"
asyncio.create_task(recalculate_ratings(), name="Recalculate Ratings")
+ request.app.ctx.banner["text"] = None
return response.json({"status": "ok"})
@@ -28,7 +30,9 @@ async def admin_recalculate_ratings(request: Request) -> str:
@app.post("/admin/recalculate_sm5_ratings")
@admin_only
async def admin_recalculate_sm5_ratings(request: Request) -> str:
+ request.app.ctx.banner["text"] = "Rating recalculation in progress, stats may be inaccurate"
asyncio.create_task(recalculate_sm5_ratings(), name="Recalculate SM5 Ratings")
+ request.app.ctx.banner["text"] = None
return response.json({"status": "ok"})
@@ -36,7 +40,9 @@ async def admin_recalculate_sm5_ratings(request: Request) -> str:
@app.post("/admin/recalculate_laserball_ratings")
@admin_only
async def admin_recalculate_lb_ratings(request: Request) -> str:
+ request.app.ctx.banner["text"] = "Rating recalculation in progress, stats may be inaccurate"
asyncio.create_task(recalculate_laserball_ratings(), name="Recalculate Laserball Ratings")
+ request.app.ctx.banner["text"] = None
return response.json({"status": "ok"})
@@ -49,3 +55,11 @@ async def admin_flush_cache(request: Request) -> str:
logger.info("Cache flushed")
return response.json({"status": "ok"})
+
+@app.post("/admin/set_banner")
+@admin_only
+async def admin_set_banner(request: Request) -> str:
+ request.app.ctx.banner["text"] = request.json.get("text") or None
+ request.app.ctx.banner["type"] = request.json.get("type") or None
+
+ return response.json({"status": "ok"})
\ No newline at end of file
diff --git a/helpers/ratinghelper.py b/helpers/ratinghelper.py
index d0ead4c..8ac1823 100644
--- a/helpers/ratinghelper.py
+++ b/helpers/ratinghelper.py
@@ -11,7 +11,7 @@
from db.laserball import LaserballGame
from db.player import Player
from db.sm5 import SM5Game
-from db.types import EventType, GameType, Team
+from db.types import EventType, GameType, Team, IntRole
from helpers import userhelper
# CONSTANTS
@@ -118,10 +118,15 @@ async def update_sm5_ratings(game: SM5Game) -> bool:
target_elo = Rating(target_player.sm5_mu, target_player.sm5_sigma)
out = model.rate([[shooter_elo], [target_elo]], ranks=[0, 1])
- shooter_player.sm5_mu += (out[0][0].mu - shooter_player.sm5_mu) * 0.1
+ weight_mu = 0.1 # default for damage and downed events
+ if target.role == IntRole.MEDIC:
+ weight_mu = 0.2 # medic events are weighted more heavily
+
+
+ shooter_player.sm5_mu += (out[0][0].mu - shooter_player.sm5_mu) * weight_mu
shooter_player.sm5_sigma += (out[0][0].sigma - shooter_player.sm5_sigma) * 0.1
- target_player.sm5_mu += (out[1][0].mu - target_player.sm5_mu) * 0.1
+ target_player.sm5_mu += (out[1][0].mu - target_player.sm5_mu) * weight_mu
target_player.sm5_sigma += (out[1][0].sigma - target_player.sm5_sigma) * 0.1
await shooter_player.save()
@@ -137,10 +142,10 @@ async def update_sm5_ratings(game: SM5Game) -> bool:
out = model.rate([[shooter_elo], [target_elo]], ranks=[0, 1])
- shooter_player.sm5_mu += (out[0][0].mu - shooter_player.sm5_mu) * 0.15
+ shooter_player.sm5_mu += (out[0][0].mu - shooter_player.sm5_mu) * 0.25
shooter_player.sm5_sigma += (out[0][0].sigma - shooter_player.sm5_sigma) * 0.1
- target_player.sm5_mu += (out[1][0].mu - target_player.sm5_mu) * 0.15
+ target_player.sm5_mu += (out[1][0].mu - target_player.sm5_mu) * 0.25
target_player.sm5_sigma += (out[1][0].sigma - target_player.sm5_sigma) * 0.1
await shooter_player.save()
diff --git a/utils.py b/utils.py
index 0dbc9b2..03beeae 100644
--- a/utils.py
+++ b/utils.py
@@ -60,3 +60,10 @@ async def wrapper(request: Request, *args, **kwargs) -> Union[response.HTTPRespo
def is_admin(request: Request) -> bool:
return request.ctx.session.get("permissions", 0) == Permission.ADMIN
+
+def banner_type_to_color(type: str) -> str:
+ return {
+ "info": "#4da6ff",
+ "warning": "#cfa602",
+ "danger": "#ff4d4d",
+ }.get(type, "#4da6ff")
\ No newline at end of file
diff --git a/web.py b/web.py
index 4ee526a..267d211 100644
--- a/web.py
+++ b/web.py
@@ -44,6 +44,7 @@
from tortoise import Tortoise
from config import config
from helpers import cachehelper
+import utils
TORTOISE_ORM = {
"connections": {
@@ -79,6 +80,8 @@ async def setup_app(app, loop) -> None:
Start the server in a production environment.
"""
app.ctx.sql = await MySQLPool.connect_with_config()
+ app.ctx.banner = {"text": None, "type": None}
+ app.ctx.banner_type_to_color = utils.banner_type_to_color
await Tortoise.init(
config=TORTOISE_ORM
@@ -93,6 +96,8 @@ async def main() -> None:
Start the server in a development/nonprod environment.
"""
app.ctx.sql = await MySQLPool.connect_with_config()
+ app.ctx.banner = {"text": "Rating recalculation in progress, stats may be inaccurate", "type": None}
+ app.ctx.banner_type_to_color = utils.banner_type_to_color
await Tortoise.init(
config=TORTOISE_ORM