Skip to content

Commit

Permalink
Affects v2 (#309)
Browse files Browse the repository at this point in the history
* Rewrite PhysConstructor to TS

* Affects v2
  • Loading branch information
kyvg authored Feb 15, 2024
1 parent 49296d3 commit 136c6d0
Show file tree
Hide file tree
Showing 67 changed files with 1,106 additions and 1,490 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
camelcase: 0,
'lines-between-class-members': ["error", "always", {exceptAfterSingleLine: true}],
'no-void': ['error', { allowAsStatement: true }],
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/no-useless-constructor': 'warn',
'@typescript-eslint/no-var-requires': 0,
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: "^_"}],
Expand Down
3 changes: 3 additions & 0 deletions src/arena/BattleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ function getTargetKeyboard(charId: string, game: Game, action: string) {
if (orderType === 'enemy') {
return !game.isPlayersAlly(player, target);
}
if (orderType === 'team') {
return game.isPlayersAlly(player, target);
}

return !orders.some((order) => target.id === order.target && action === order.action);
})
Expand Down
47 changes: 47 additions & 0 deletions src/arena/Constuructors/AffectableAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { BaseAction } from './BaseAction';
import type { PostAffect } from './interfaces/PostAffect';
import type { PreAffect } from './interfaces/PreAffect';
import type { SuccessArgs } from './types';

export abstract class AffectableAction extends BaseAction {
private preAffects: PreAffect[] = [];
private postAffects: PostAffect[] = [];
private affects: SuccessArgs[] = [];

registerPreAffects(preAffects: PreAffect[]) {
this.preAffects = preAffects;
}

registerPostAffects(postAffects: PostAffect[]) {
this.postAffects = postAffects;
}

checkPreAffects(params = this.params, status = this.status) {
this.preAffects.forEach((preAffect) => {
preAffect.preAffect(params, status);
});
}

checkPostAffects(params = this.params, status = this.status) {
this.postAffects.forEach((preAffect) => {
const result = preAffect.postAffect(params, status);
if (!result) {
return;
}

const normalizedResult = Array.isArray(result) ? result : [result];

this.affects.push(...normalizedResult);
});
}

getAffects() {
return this.affects;
}

reset() {
super.reset();

this.affects = [];
}
}
34 changes: 18 additions & 16 deletions src/arena/Constuructors/AoeDmgMagicConstructor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { floatNumber } from '../../utils/floatNumber';
import { DmgMagic } from './DmgMagicConstructor';
import type { BaseNext, DamageType, ExpArr } from './types';
import type {
BaseNext, DamageType, ExpArr, SuccessArgs,
} from './types';

export type AoeDmgMagicNext = BaseNext & {
actionType: 'aoe-dmg-magic'
Expand All @@ -14,7 +16,7 @@ export abstract class AoeDmgMagic extends DmgMagic {
status: {
exp: number;
expArr: ExpArr;
hit: number;
effect: number;
};

getExp({ initiator, target, game } = this.params): void {
Expand Down Expand Up @@ -46,31 +48,31 @@ export abstract class AoeDmgMagic extends DmgMagic {
});
}

resetStatus(): void {
reset(): void {
super.reset();

this.status = {
exp: 0,
hit: 0,
effect: 0,
expArr: [],
};
}

/**
* Магия прошла удачно
* @param initiator объект персонажаы
* @param target объект цели магии
* @todo тут нужен вывод требуемых параметров
*/
next(): void {
const { game, target } = this.params;
const args: AoeDmgMagicNext = {
...this.getNextArgs(),
getSuccessResult({ initiator, target } = this.params): SuccessArgs {
const result: AoeDmgMagicNext = {
exp: this.status.exp,
action: this.displayName,
target: target.nick,
initiator: initiator.nick,
actionType: 'aoe-dmg-magic',
expArr: this.status.expArr,
dmg: floatNumber(this.status.hit),
dmg: floatNumber(this.status.effect),
hp: target.stats.val('hp'),
dmgType: this.dmgType,
};

game.recordOrderResult(args);
this.reset();

return result;
}
}
25 changes: 25 additions & 0 deletions src/arena/Constuructors/BaseAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type GameService from '@/arena/GameService';
import type { Player } from '@/arena/PlayersService';

export abstract class BaseAction {
params: {
initiator: Player;
target: Player;
game: GameService
};

status = { effect: 0, exp: 0 };

abstract cast(initiator: Player, target: Player, game: GameService)

/**
* @param initiator объект персонажа
* @param target объект персонажа
* @param game Объект игры для доступа ко всему
*/
abstract run(initiator: Player, target: Player, game: GameService)

reset() {
this.status = { effect: 0, exp: 0 };
}
}
45 changes: 20 additions & 25 deletions src/arena/Constuructors/DmgMagicConstructor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { floatNumber } from '../../utils/floatNumber';
import type { Player } from '../PlayersService';
import { Magic, MagicArgs } from './MagicConstructor';
import type { BaseNext, DamageType } from './types';
import type { MagicArgs } from './MagicConstructor';
import { Magic } from './MagicConstructor';
import type { BaseNext, DamageType, SuccessArgs } from './types';

export type DmgMagicNext = BaseNext & {
actionType: 'dmg-magic'
Expand All @@ -20,11 +21,6 @@ export interface DmgMagic extends DmgMagicArgs, Magic {
* Общий конструктор не длительных магий
*/
export abstract class DmgMagic extends Magic {
status: {
exp: number;
hit: number;
};

/**
* Создание магии
*/
Expand All @@ -40,7 +36,7 @@ export abstract class DmgMagic extends Magic {
const effect = this.getEffectVal({ initiator, target, game });
const modifiedEffect = this.modifyEffect(effect, { initiator, target, game });

this.status.hit = modifiedEffect;
this.status.effect = modifiedEffect;

return modifiedEffect;
}
Expand Down Expand Up @@ -76,11 +72,11 @@ export abstract class DmgMagic extends Magic {
* кол-во единиц за использование магии
* Если кастеру хватило mp/en продолжаем,если нет, то возвращаем false
*/
getExp({ initiator, target, game } = this.params): void {
if (game.isPlayersAlly(initiator, target) && !initiator.flags.isGlitched) {
getExp({ initiator, target } = this.params): void {
if (initiator.isAlly(target) && !initiator.flags.isGlitched) {
this.status.exp = 0;
} else {
const dmgExp = this.calculateExp(this.status.hit, this.baseExp);
const dmgExp = this.calculateExp(this.status.effect, this.baseExp);
this.status.exp = dmgExp;

initiator.stats.up('exp', dmgExp);
Expand All @@ -91,29 +87,28 @@ export abstract class DmgMagic extends Magic {
return Math.round(hit * 8) + baseExp;
}

resetStatus(): void {
reset(): void {
this.status = {
exp: 0,
hit: 0,
effect: 0,
};
}

/**
* Магия прошла удачно
* @param initiator объект персонажаы
* @param target объект цели магии
* @todo тут нужен вывод требуемых параметров
*/
next(): void {
const { game, target } = this.params;
const args: DmgMagicNext = {
...this.getNextArgs(),
getSuccessResult({ initiator, target } = this.params): SuccessArgs {
const result: DmgMagicNext = {
exp: this.status.exp,
action: this.displayName,
actionType: 'dmg-magic',
dmg: floatNumber(this.status.hit),
target: target.nick,
initiator: initiator.nick,
msg: this.customMessage?.bind(this),
dmg: floatNumber(this.status.effect),
hp: target.stats.val('hp'),
dmgType: this.dmgType,
};

game.recordOrderResult(args);
this.reset();

return result;
}
}
2 changes: 2 additions & 0 deletions src/arena/Constuructors/FlagsConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class FlagsConstructor {
isShielded = 0;
isParalysed = false;
isDisarmed = false;
isLightShielded: Flag[] = [];

/**
* Обнуление флагов
Expand All @@ -35,5 +36,6 @@ export default class FlagsConstructor {
this.isParry = 0;
this.isDisarmed = false;
this.isShielded = 0;
this.isLightShielded = [];
}
}
36 changes: 15 additions & 21 deletions src/arena/Constuructors/HealMagicConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { floatNumber } from '../../utils/floatNumber';
import type Game from '../GameService';
import MiscService from '../MiscService';
import type { Player } from '../PlayersService';
import { AffectableAction } from './AffectableAction';
import type {
BaseNext, Breaks, BreaksMessage, CustomMessage, ExpArr, OrderType,
BaseNext, BreaksMessage, CustomMessage, ExpArr, FailArgs, OrderType, SuccessArgs,
} from './types';
import { handleCastError } from './utils';

export type HealNext = Omit<BaseNext, 'exp'> & {
actionType: 'heal';
Expand Down Expand Up @@ -32,19 +34,9 @@ export interface Heal extends HealArgs, CustomMessage {
/**
* Heal Class
*/
export abstract class Heal {
params!: {
initiator: Player;
target: Player;
game: Game;
};

status = {
exp: 0,
val: 0,
};

export abstract class Heal extends AffectableAction {
constructor(params: HealArgs) {
super();
Object.assign(this, params);
}

Expand All @@ -62,6 +54,7 @@ export abstract class Heal {
};

try {
this.checkPreAffects();
this.run(initiator, target, game);
// Получение экспы за хил следует вынести в отдельный action следующий
// за самим хилом, дабы выдать exp всем хиллерам после формирования
Expand All @@ -70,12 +63,12 @@ export abstract class Heal {
// this.backToLife();
this.next();
} catch (e) {
game.recordOrderResult(e);
handleCastError(e, (reason) => {
game.recordOrderResult(this.breaks(reason));
});
}
}

abstract run(initiator: Player, target: Player, game: Game): void;

/**
* Функция выполняет проверку, является ли хил "воскресившим", т.е если
* цель до выполнения лечения имела статус "isDead", а после хила имеет хп > 0
Expand All @@ -86,14 +79,15 @@ export abstract class Heal {
/**
* @param obj
*/
breaks(message: BreaksMessage): Breaks {
breaks(reason: BreaksMessage | SuccessArgs | SuccessArgs[]): FailArgs {
const { target, initiator } = this.params;
return {
message,
reason,
target: target.nick,
initiator: initiator.nick,
actionType: 'heal',
action: this.displayName,
weapon: initiator.weapon.item,
};
}

Expand All @@ -117,7 +111,7 @@ export abstract class Heal {

getExp(initiator: Player, target: Player, game: Game): number {
if (game.isPlayersAlly(initiator, target)) {
const healEffect = this.status.val;
const healEffect = this.status.effect;
const exp = Math.round(healEffect * 10);
initiator.stats.up('exp', exp);
return exp;
Expand All @@ -141,15 +135,15 @@ export abstract class Heal {
id: initiator.id,
name: initiator.nick,
exp: this.status.exp,
val: this.status.val,
val: this.status.effect,
};
const args: HealNext = {
expArr: [exp],
action: this.displayName,
actionType: 'heal',
target: target.nick,
initiator: initiator.nick,
effect: this.status.val,
effect: this.status.effect,
hp: target.stats.val('hp'),
};
game.recordOrderResult(args);
Expand Down
Loading

0 comments on commit 136c6d0

Please sign in to comment.