Skip to content

Commit

Permalink
medic hits count for something and banners
Browse files Browse the repository at this point in the history
  • Loading branch information
spookybear0 committed May 14, 2024
1 parent 2315609 commit f01fad6
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 45 deletions.
36 changes: 0 additions & 36 deletions app.py

This file was deleted.

8 changes: 8 additions & 0 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
62 changes: 58 additions & 4 deletions assets/html/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@


{% block html_head %}
<style>
#banner_text {
width: 400px;
background: #262626;
border: 1px solid #999;
border-radius: 4px;
color: #fff;
height: 4em;
font-size: 1em;
padding: 0.5em;
}

#banner_type {
width: 200px;
background: #262626;
border: 1px solid #999;
border-radius: 4px;
color: #fff;
height: 4em;
font-size: 1em;
padding: 0.5em;
}
</style>

<script>
function getCookie() {
let cookieValue = null;
Expand Down Expand Up @@ -65,16 +89,46 @@
});
}

function set_banner() {
let text = document.getElementById("banner_text").value;
let type = document.getElementById("banner_type").value;

fetch("/admin/set_banner", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRFToken": getCookie("csrftoken")
},
body: JSON.stringify({
"text": text,
"type": type
})
});
}

</script>
{% endblock %}

{% block content %}

<h1 style="text-align: center;">Dashboard</h1>

<button onclick="recalculate_ratings()" class="button">Recalculate Ratings</button>
<button onclick="recalculate_sm5_ratings()" class="button">Recalculate SM5 Ratings</button>
<button onclick="recalculate_laserball_ratings()" class="button">Recalculate Laserball Ratings</button>
<button onclick="flush_cache()" class="button">Flush Cache</button>
<div style="display: flex; flex-direction: row; gap: 10px;">
<button onclick="recalculate_ratings()" class="button">Recalculate Ratings</button>
<button onclick="recalculate_sm5_ratings()" class="button">Recalculate SM5 Ratings</button>
<button onclick="recalculate_laserball_ratings()" class="button">Recalculate Laserball Ratings</button>
<button onclick="flush_cache()" class="button">Flush Cache</button>
</div>

<!-- Banner -->

<div style="display: flex; flex-direction: row; gap: 10px; margin-top: 5rem;">
<input type="text" id="banner_text" placeholder="Banner Text" id="banner_text">
<select id="banner_type" id="banner_type">
<option value="info">Info</option>
<option value="warning">Warning</option>
<option value="danger">Danger</option>
</select>
<button onclick="set_banner()" class="button">Set Banner</button>

{% endblock %}
8 changes: 8 additions & 0 deletions assets/html/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,14 @@
href="/logout">Logout</a></li>
{% endif %}
</ul>

<!--banner that can be chnaged in ctx.banner-->
{% if app.ctx.banner.text %}
<div class="banner" style="background-color: {{ app.ctx.banner_type_to_color(app.ctx.banner.type)}};">
<h1>{{ app.ctx.banner.text }}</h1>
</div>
{% endif %}

<div id="content">
{% block content %}{% endblock %}
</div>
Expand Down
14 changes: 14 additions & 0 deletions handlers/admin/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,29 @@ 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"})


@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"})


@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"})

Expand All @@ -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"})
15 changes: 10 additions & 5 deletions helpers/ratinghelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
5 changes: 5 additions & 0 deletions web.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from tortoise import Tortoise
from config import config
from helpers import cachehelper
import utils

TORTOISE_ORM = {
"connections": {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f01fad6

Please sign in to comment.