108 lines
4.0 KiB
Python
108 lines
4.0 KiB
Python
from data import data_api
|
|
from match_calculations import calculation
|
|
import json
|
|
import os
|
|
from wood import logger
|
|
|
|
|
|
point_inflation = 0.8 # => entspricht %
|
|
K_FACTOR = 40 # Die "Border" (Maximalpunkte) die ein Sieg gibt.
|
|
|
|
# Mach die DB abfrage für die Relevanten Daten. Von hier aus werden die "Aufgaben" und Daten dann an die kleineren Berechnungs Funktionen verteilt.
|
|
def calculate_match (match_id):
|
|
match_data = data_api.get_match_by_id(match_id)
|
|
|
|
if not match_data:
|
|
print("Fehler: Match nicht gefunden!")
|
|
return
|
|
|
|
# Laden und aufdröseln der Match Daten
|
|
p1_id = match_data['player1_id']
|
|
p2_id = match_data['player2_id']
|
|
p1_score = match_data['score_player1']
|
|
p2_score = match_data['score_player2']
|
|
system_name = match_data['gamesystem_name']
|
|
system_id = match_data['gamesystem_id']
|
|
|
|
match_is_draw = False
|
|
draw_diff = determine_draw_diff(system_id)
|
|
|
|
# ==========================================
|
|
# 1. IDENTIFIKATION
|
|
# ==========================================
|
|
# Draw
|
|
if -draw_diff <= (p1_score - p2_score) <= draw_diff:
|
|
winner_id, looser_id = p1_id, p2_id
|
|
winner_score, looser_score = p1_score, p2_score
|
|
match_is_draw = True
|
|
# P1 Winner
|
|
elif p1_score > p2_score:
|
|
winner_id, looser_id = p1_id, p2_id
|
|
winner_score, looser_score = p1_score, p2_score
|
|
# P2 Winner
|
|
else:
|
|
winner_id, looser_id = p2_id, p1_id
|
|
winner_score, looser_score = p2_score, p1_score
|
|
|
|
|
|
# ==========================================
|
|
# 2. DIE BERECHNUNG & ROUTING-TABELLE
|
|
# ==========================================
|
|
# Wir speichern die Ergebnisse direkt in einem temporären Dictionary,
|
|
# und nutzen die SPIELER-ID als Schlüsselwort!
|
|
elo_factor = calculation.calc_elo_factor(winner_id, looser_id, system_name)
|
|
base_change = int(calculation.calc_base_change(elo_factor, match_is_draw, K_FACTOR))
|
|
rust_factor = calculation.calc_rust_factor(winner_id, looser_id, system_id)
|
|
|
|
#winner
|
|
w_base = int(base_change)
|
|
w_khorne = int(calculation.wrath_of_khorne(winner_id, system_id))
|
|
|
|
#looser
|
|
l_base = int(base_change*point_inflation)
|
|
l_khorne = int(calculation.wrath_of_khorne(looser_id, system_id))
|
|
|
|
slaanesh = calculation.slaanesh_delight()
|
|
tzeentch = calculation.tzeentch_schemes(system_id, winner_score, looser_score)
|
|
|
|
# ==========================================
|
|
# 3. Daten Verpacken
|
|
# ==========================================
|
|
calc_results = {
|
|
"match_id" : match_id,
|
|
"elo_factor" : elo_factor,
|
|
"rust_factor" : rust_factor,
|
|
"point_inflation" : point_inflation,
|
|
|
|
"winner_id" : winner_id, # <-- String-Key für save_calculated_match
|
|
"looser_id" : looser_id, # <-- String-Key für save_calculated_match
|
|
|
|
winner_id: { # <-- Variable als Key (z.B. 42: {...})
|
|
"base" : w_base,
|
|
"khorne" : w_khorne,
|
|
"slaanesh" : slaanesh,
|
|
"tzeentch" : tzeentch,
|
|
"total" : int((w_base + w_khorne + slaanesh + tzeentch) * rust_factor),
|
|
},
|
|
looser_id: { # <-- Variable als Key (z.B. 7: {...})
|
|
"base" : -l_base,
|
|
"khorne" : l_khorne,
|
|
"slaanesh" : -slaanesh,
|
|
"tzeentch" : -tzeentch,
|
|
"total" : int((-l_base + l_khorne - slaanesh - tzeentch) * rust_factor),
|
|
}
|
|
}
|
|
|
|
logger.log(f"calc_match ID:{match_id}", f"Winner {data_api.get_player_name(winner_id)}: Base {w_base} + Khorne({w_khorne}) + Slaanesh({slaanesh}) + Tzeentch({tzeentch}) = {calc_results[winner_id]["total"]}")
|
|
logger.log(f"calc_match ID:{match_id}", f"Looser {data_api.get_player_name(looser_id)}: -Base({l_base}) + Khorne({l_khorne}) - Slaanesh({slaanesh}) - Tzeentch({tzeentch}) = {calc_results[looser_id]["total"]}")
|
|
data_api.save_calculated_match(calc_results)
|
|
|
|
|
|
def determine_draw_diff(sys_id):
|
|
draw_diff = 0
|
|
match sys_id:
|
|
case 1:
|
|
draw_diff = 3
|
|
case 2,3:
|
|
draw_diff = 1
|
|
return draw_diff |