Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add magic description #327

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/arena/CharacterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,10 @@ class CharacterService {

/**
* Функция получения новой магии
* @param {String} magicId идентификатор магии
* @param {Number} lvl уровень проученной магии
* @param magicId идентификатор магии
* @param lvl уровень проученной магии
*/
async learnMagic(magicId, lvl) {
async learnMagic(magicId: string, lvl: number) {
this.magics[magicId] = lvl;
// опасный тест
await this.saveToDb();
Expand Down
2 changes: 1 addition & 1 deletion src/arena/Constuructors/HealMagicConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export abstract class Heal extends AffectableAction {
const maxHp = target.stats.val('maxHp');
const curHp = target.stats.val('hp');

const allHeal = MiscService.randInt(hl.min, hl.max) * proc;
const allHeal = MiscService.randFloat(hl.min, hl.max) * proc;
const maxHeal = maxHp - curHp;
const healEffect = Math.min(maxHeal, allHeal);

Expand Down
2 changes: 1 addition & 1 deletion src/arena/Constuructors/PhysConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default abstract class PhysConstructor extends AffectableAction {
const { initiator, target } = this.params;

const { min, max } = initiator.stats.val('hit');
const hit = MiscService.randInt(min, max);
const hit = MiscService.randFloat(min, max);

const effect = this.applyResists(hit, target);
this.status.effect = floatNumber(effect * initiator.proc);
Expand Down
109 changes: 0 additions & 109 deletions src/arena/MagicService.js

This file was deleted.

91 changes: 91 additions & 0 deletions src/arena/MagicService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import arena from '@/arena';
import config from '@/arena/config';
import MiscService from '@/arena/MiscService';
import type { Prof } from '@/data/profs';
import type CharacterService from './CharacterService';
import type { Magic } from './Constuructors/MagicConstructor';

const chance = config.magic.learnChance;

function learnChance() {
return chance > MiscService.dice('1d100');
}

/**
* @class Сервис работы с магиями
*/
export default class MagicService {
static MAX_MAGIC_LVL = 3;

/**
* Изучение магии с шансом
* @param lvl круг проучиваемой магии
*/
static async learnMagic(character: CharacterService, lvl: number) {
if (lvl > character.bonus) {
throw Error('Не хватает бонусов');
}

const magicsToLearn = MagicService.getMagicsToLearn(character, lvl);
if (!magicsToLearn.length) {
throw Error('Нет магий для изучения');
}

const magic = magicsToLearn[MiscService.randInt(0, magicsToLearn.length)];

character.bonus -= lvl;
await character.save({ bonus: character.bonus });

if (!learnChance()) {
throw Error('Не удалось выучить. Удача не на твоей стороне');
}

const charMagicLvl = character.magics[magic.name] || 0;
await character.learnMagic(magic.name, charMagicLvl + 1);

return magic;
}

/**
* Возвращает доступные магии на данном круге для изучения
* @param lvl круг проучиваемой магии
*/
static getMagicsToLearn(character: CharacterService, lvl: number): Magic[] {
const magicsByLvl = MagicService.getMagicListByProf(character.prof, lvl);

return magicsByLvl.filter((magic) => {
if (character.magics[magic.name]) {
return character.magics[magic.name] < MagicService.MAX_MAGIC_LVL;
}
return true;
});
}

/**
* Показываем описание магии
* @param magId строка идентификатор магии
*/
static getMagicById(magic: string) {
return arena.magics[magic as keyof typeof arena['magics']];
}

/**
* Список доступных магий для профы на заданном круге
* @param prof профессия персонажа
* @param lvl круг магии
* @returns возвращает магии всех кругов если не передан круг
*/
static getMagicListByProf(prof: Prof, lvl?: number) {
return Object.values(arena.magics).filter((magic) => {
if (!lvl) {
return magic.profList.includes(prof);
}

return magic.lvl === lvl && magic.profList.includes(prof);
});
}

static getMagicListByIds(magics: string[]) {
return magics.map(MagicService.getMagicById);
}
}
16 changes: 13 additions & 3 deletions src/arena/MiscService.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,20 @@ const STORES = {
* @param {Number} max
* @return {Number} Рандомное floatNumber значение
*/
function randInt(min, max) {
function randFloat(min, max) {
const tempMax = +max;
const tempMin = +min;
return (Math.random() * (tempMax - tempMin) + tempMin);
}
/**
* Рандомное целое значние между min - max
* @param {Number} min
* @param {Number} max
* @return {Number} Рандомное floatNumber значение
*/
function randInt(min, max) {
return Math.floor(Math.random() * (+max - +min) + +min);
}

module.exports = {
weaponTypes: WEAPON_TYPES,
Expand All @@ -108,10 +117,10 @@ module.exports = {
const sec = diceStr.split('+');
if (sec.length === 2) {
const parts = sec[0].split('d');
result = randInt(parts[0], parts[1]) + Number(sec[1]);
result = randFloat(parts[0], parts[1]) + Number(sec[1]);
} else {
const parts = diceStr.split('d');
result = randInt(parts[0], parts[1]);
result = randFloat(parts[0], parts[1]);
}
return result;
}, /**
Expand All @@ -125,5 +134,6 @@ module.exports = {
const max = +part[1];
return Math.floor(Math.random() * (max - min + 1) + min);
},
randFloat,
randInt,
};
Loading
Loading