Skip to content

Commit

Permalink
#151 Dust shield (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyvg authored Nov 14, 2023
1 parent 7f36e53 commit 9eff5f2
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/arena/Constuructors/MagicConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ export abstract class Magic {
* @param initiator Объект кастера
*/
getExp({ initiator } = this.params): void {
const exp = Math.round(this.baseExp * initiator.proc);
const exp = this.calculateExp(this.status.effect || 0, this.baseExp);

this.status.exp = exp;
initiator.stats.up('exp', this.baseExp);
initiator.stats.up('exp', exp);
}

calculateExp(effect: number, baseExp = 0) {
return Math.round(baseExp * this.params.initiator.proc);
}

/**
Expand Down
26 changes: 23 additions & 3 deletions src/arena/Constuructors/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
import type { Breaks, FailArgs, SuccessArgs } from '../types';
import type {
Breaks, FailArgs, SuccessArgs,
} from '../types';

export const isSuccessResult = (result: SuccessArgs | FailArgs): result is SuccessArgs => {
type Result = SuccessArgs | FailArgs;
type SuccessDamageResult<T = Result> = T extends { dmg: number } ? T : never;

export const isSuccessResult = (result: Result): result is SuccessArgs => {
return !('message' in result);
};

export const isSuccessDamageResult = (result: SuccessArgs | FailArgs) => {
export const isSuccessDamageResult = (result: Result): result is SuccessDamageResult => {
if (isSuccessResult(result)) {
return ('dmg' in result);
}

return false;
};

export const isPhysicalDamageResult = (result: Result): result is SuccessDamageResult => {
console.log(result);
if (isSuccessDamageResult(result)) {
return result.dmgType === 'physical';
}

return false;
};

export const findByTarget = (target: string) => {
return (result: Result) => {
return result.target === target;
};
};

export const handleCastError = (error: unknown, onActionError: (error: Breaks) => void) => {
if (error instanceof Error) {
console.error(error);
Expand Down
4 changes: 4 additions & 0 deletions src/arena/GameService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ export default class GameService {
this.history.addHistoryForRound(item, this.round.count);
}

getLastRoundResults() {
return this.history.getHistoryForRound(this.round.count - 1);
}

getRoundResults() {
return this.history.getHistoryForRound(this.round.count);
}
Expand Down
2 changes: 1 addition & 1 deletion src/arena/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default {
[
'smallAura',
'magicArmor', 'strongAura',
'dust_shield', 'mediumAura',
'dustShield', 'mediumAura',
'stoneSkin',
'magic_wall'], // ???
'cats_claw',
Expand Down
60 changes: 60 additions & 0 deletions src/arena/magics/dustShield.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import _ from 'lodash';
import { LongMagic } from '../Constuructors/LongMagicConstructor';
import { isPhysicalDamageResult, findByTarget } from '../Constuructors/utils';
import type GameService from '../GameService';
import type { Player } from '../PlayersService';

const minProtect = 5;
const maxProtect = 20;

class DustShield extends LongMagic {
constructor() {
super({
name: 'dustShield',
displayName: 'Щит праха',
desc: 'Щит из праха повышает защиту кастера на величину, результат деления количества повреждений предыдущего раунда на количество атакеров, но не больше 20 и не меньше 5.',
cost: 14,
baseExp: 8,
costType: 'mp',
lvl: 4,
orderType: 'self',
aoeType: 'target',
magType: 'good',
chance: [100, 100, 100],
profList: ['m'],
effect: ['1d3', '1d5', '1d7'],
});
}

run(initiator: Player, target: Player, game: GameService): void {
const protect = this.calculateProtect(initiator, target, game);

target.stats.up('pdef', _.clamp(protect, minProtect, maxProtect));
}

runLong(initiator: Player, target: Player, game: GameService): void {
this.run(initiator, target, game);
}

calculateExp(effect: number, baseExp = 0): number {
return Math.round(baseExp * effect);
}

calculateProtect(initiator: Player, target: Player, game: GameService) {
const effect = this.effectVal({ initiator, target, game });

const results = game.getLastRoundResults();
const physicalDamageResults = results
.filter(isPhysicalDamageResult)
.filter(findByTarget(target.nick));

const damageTaken = physicalDamageResults.reduce((sum, { dmg }) => sum + dmg, 0);
if (damageTaken) {
return effect + (damageTaken / physicalDamageResults.length) || 0;
}

return effect;
}
}

export default new DustShield();
1 change: 1 addition & 0 deletions src/arena/magics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export { default as fireBall } from './fireBall';
export { default as physicalSadness } from './physicalSadness';
export { default as bodySpirit } from './bodySpirit';
export { default as blight } from './blight';
export { default as dustShield } from './dustShield';

0 comments on commit 9eff5f2

Please sign in to comment.