Skip to content

Commit

Permalink
wip start fight & end fight callback #2276
Browse files Browse the repository at this point in the history
  • Loading branch information
BastLast committed Jan 16, 2025
1 parent c72a80a commit e370936
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
73 changes: 73 additions & 0 deletions Core/src/commands/player/FightCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
import {BlockingConstants} from "../../../../Lib/src/constants/BlockingConstants";
import {InventorySlots} from "../../core/database/game/models/InventorySlot";
import {LogsReadRequests, RankedFightResult} from "../../core/database/logs/LogsReadRequests";
import {FightController} from "../../core/fights/FightController";
import {FightOvertimeBehavior} from "../../core/fights/FightOvertimeBehavior";
import {PlayerFighter} from "../../core/fights/fighter/PlayerFighter";
import {ClassDataController} from "../../data/Class";

type PlayerStats = {
classId: number,
Expand Down Expand Up @@ -54,6 +58,64 @@ async function getPlayerStats(player: Player): Promise<PlayerStats> {
};
}

/**
* Code that will be executed when a fight ends (except if the fight has a bug)
* @param fight
*/
async function fightEndCallback(fight: FightController): Promise<void> {
const fightLogId = await draftBotInstance.logsDatabase.logFight(fight);

const player1GameResult = fight.isADraw() ? EloGameResult.DRAW : fight.getWinner() === 0 ? EloGameResult.WIN : EloGameResult.LOSE;
const player2GameResult = player1GameResult === EloGameResult.DRAW ? EloGameResult.DRAW : player1GameResult === EloGameResult.WIN ? EloGameResult.LOSE : EloGameResult.WIN;

// Player variables
const player1 = await Players.getById((fight.fighters[0] as PlayerFighter).player.id);
const player2 = await Players.getById((fight.fighters[1] as PlayerFighter).player.id);

// Calculate elo
const player1KFactor = EloUtils.getKFactor(player1);
const player2KFactor = EloUtils.getKFactor(player2);
const player1NewRating = EloUtils.calculateNewRating(player1.gloryPoints, player2.gloryPoints, player1GameResult, player1KFactor);
const player2NewRating = EloUtils.calculateNewRating(player2.gloryPoints, player1.gloryPoints, player2GameResult, player2KFactor);

// Create embed
const embed = await createFightEndCallbackEmbed(fight,

Check failure on line 82 in Core/src/commands/player/FightCommand.ts

View workflow job for this annotation

GitHub Actions / eslint-core-module

Expected newline after '('
{
player: player1,
playerNewRating: player1NewRating,
playerKFactor: player1KFactor,
playerGameResult: player1GameResult
},
{
player: player2,
playerNewRating: player2NewRating,
playerKFactor: player2KFactor,
playerGameResult: player2GameResult
});

Check failure on line 94 in Core/src/commands/player/FightCommand.ts

View workflow job for this annotation

GitHub Actions / eslint-core-module

Expected newline before ')'

// Change glory and fightCountdown and save
await player1.setGloryPoints(player1NewRating, NumberChangeReason.FIGHT, fight.getFightView().channel, fight.getFightView().language, fightLogId);
player1.fightCountdown--;
if (player1.fightCountdown < 0) {
player1.fightCountdown = 0;
}
await player2.setGloryPoints(player2NewRating, NumberChangeReason.FIGHT, fight.getFightView().channel, fight.getFightView().language, fightLogId);
player2.fightCountdown--;
if (player2.fightCountdown < 0) {
player2.fightCountdown = 0;
}
await Promise.all([
player1.save(),
player2.save()
]);

await fight.getFightView().channel.send({
embeds: [
embed
]
});
}

/**
* Check if a BO3 is already finished (3 games played or 2 wins)
* @param bo3
Expand Down Expand Up @@ -135,7 +197,18 @@ export default class FightCommand {
if (!opponent) {
// Error message if no opponent found
}
const askingFighter = new PlayerFighter(player, ClassDataController.instance.getById(player.class));
await askingFighter.loadStats();
const incomingFighter = new PlayerFighter(opponent, ClassDataController.instance.getById(opponent.class));
await incomingFighter.loadStats();
// Start fight
const fightController = new FightController(
{fighter1: askingFighter, fighter2: incomingFighter},
FightOvertimeBehavior.END_FIGHT_DRAW,
context
);
fightController.setEndCallback(fightEndCallback);
await fightController.startFight();
}
else {
response.push(makePacket(CommandFightRefusePacketRes, {}));
Expand Down
8 changes: 2 additions & 6 deletions Core/src/core/fights/FightController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,16 @@ export class FightController {
fighter1: Fighter,
fighter2: Fighter
},
fightParameters: {
friendly: boolean,
overtimeBehavior: FightOvertimeBehavior
},
overtimeBehavior: FightOvertimeBehavior,
context: PacketContext
) {
this.fighters = [fighters.fighter1, fighters.fighter2];
this.fightInitiator = fighters.fighter1;
this.state = FightState.NOT_STARTED;
this.turn = 1;
this.friendly = fightParameters.friendly;
this._fightView = new FightView(context, this);
this.weather = new FightWeather();
this.overtimeBehavior = fightParameters.overtimeBehavior;
this.overtimeBehavior = overtimeBehavior;
}

public tryToExecuteFightAction(fightAction: FightAction, attacker: Fighter, defender: Fighter, turn: number): FightActionResult {
Expand Down

0 comments on commit e370936

Please sign in to comment.