From 64cb91a454834ec810e3cd908cb4a26b25e8427d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 10 Mar 2022 22:15:20 +0100 Subject: [PATCH] Add elo calculations --- .../comms/handlers/FightEndsHandler.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java index 9b3e64a6..d64c9a22 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/FightEndsHandler.java @@ -20,17 +20,28 @@ package de.steamwar.bungeecore.comms.handlers; import com.google.common.io.ByteArrayDataInput; +import de.steamwar.bungeecore.ArenaMode; import de.steamwar.bungeecore.comms.SpigotHandler; import de.steamwar.bungeecore.comms.packets.FightEndsPacket; +import de.steamwar.bungeecore.sql.SchemElo; +import de.steamwar.bungeecore.sql.SchemNode; +import de.steamwar.bungeecore.sql.SchematicType; +import de.steamwar.bungeecore.sql.UserElo; import net.md_5.bungee.api.config.ServerInfo; public class FightEndsHandler implements SpigotHandler { + private int K = 20; + @Override public void handle(ByteArrayDataInput in, ServerInfo info) { FightEndsPacket fightEndsPacket = new FightEndsPacket(in); - double playerRatio = Math.min(fightEndsPacket.getBluePlayers().size(), fightEndsPacket.getRedPlayers().size()) / (double )Math.max(fightEndsPacket.getBluePlayers().size(), fightEndsPacket.getRedPlayers().size()); + if (!ArenaMode.getBySchemType(SchematicType.fromDB(fightEndsPacket.getGameMode())).isRanked()) { + return; + } + + double playerRatio = 1 - (Math.min(fightEndsPacket.getBluePlayers().size(), fightEndsPacket.getRedPlayers().size()) / (double )Math.max(fightEndsPacket.getBluePlayers().size(), fightEndsPacket.getRedPlayers().size())); if (playerRatio >= 0.4) { return; } @@ -44,5 +55,31 @@ public class FightEndsHandler implements SpigotHandler { blueResult = 0; } + int blueSchemOwner = SchemNode.getSchematicOwner(fightEndsPacket.getBlueSchem()); + int redSchemOwner = SchemNode.getSchematicOwner(fightEndsPacket.getRedSchem()); + + if ((blueSchemOwner == 0 && redSchemOwner != 0) || (blueSchemOwner != 0 && redSchemOwner == 0)) { + return; + } + + int blueSchemElo = SchemElo.getElo(fightEndsPacket.getBlueSchem()); + int redSchemElo = SchemElo.getElo(fightEndsPacket.getRedSchem()); + + double blueWinExpectation = 1 / (1 + Math.pow(10, (redSchemElo - blueSchemElo) / 600f)); + double redWinExpectation = 1 / (1 + Math.pow(10, (blueSchemElo - redSchemElo) / 600f)); + + SchemElo.setElo(fightEndsPacket.getBlueSchem(), (int) Math.round(blueSchemElo + K * (blueResult - blueWinExpectation))); + SchemElo.setElo(fightEndsPacket.getRedSchem(), (int) Math.round(redSchemElo + K * (1 - blueResult - redWinExpectation))); + + if (blueSchemOwner != 0 && redSchemOwner != 0) { + for (int bluePlayer : fightEndsPacket.getBluePlayers()) { + int playerElo = UserElo.getElo(bluePlayer, fightEndsPacket.getGameMode()); + UserElo.setElo(bluePlayer, fightEndsPacket.getGameMode(), (int) Math.round(playerElo + K * (blueResult - blueWinExpectation))); + } + for (int redPlayer : fightEndsPacket.getRedPlayers()) { + int playerElo = UserElo.getElo(redPlayer, fightEndsPacket.getGameMode()); + UserElo.setElo(redPlayer, fightEndsPacket.getGameMode(), (int) Math.round(playerElo + K * (1 - blueResult - redWinExpectation))); + } + } } }