Server/project/src/services/PlayerService.ts

78 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-03-03 16:23:46 +01:00
import { inject, injectable } from "tsyringe";
import { IPmcData } from "../models/eft/common/IPmcData";
import { ILogger } from "../models/spt/utils/ILogger";
import { DatabaseServer } from "../servers/DatabaseServer";
import { TimeUtil } from "../utils/TimeUtil";
2023-03-03 16:23:46 +01:00
import { LocalisationService } from "./LocalisationService";
@injectable()
export class PlayerService
{
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("TimeUtil") protected timeUtil: TimeUtil,
2023-03-03 16:23:46 +01:00
@inject("LocalisationService") protected localisationService: LocalisationService,
@inject("DatabaseServer") protected databaseServer: DatabaseServer
)
{ }
/**
* Dupe of QuestHelper.rewardsSkillPoints()
* Add xp to a player skill
* @param pmcData Player profile
* @param skillName Name of skill to increment
* @param amount Amount of skill points to add to skill
* @param useSkillProgressRateMultipler Skills are multiplied by a value in globals, default is off to maintain compatibility with legacy code
2023-03-03 16:23:46 +01:00
*/
public incrementSkillLevel(pmcData: IPmcData, skillName: string, amount: number, useSkillProgressRateMultipler = false): void
2023-03-03 16:23:46 +01:00
{
if (!amount || amount < 0)
{
this.logger.error(this.localisationService.getText("player-attempt_to_increment_skill_with_negative_value", skillName));
return;
}
const profileSkill = pmcData.Skills.Common.find(skill => skill.Id === skillName);
if (!profileSkill)
{
this.logger.error(this.localisationService.getText("quest-no_skill_found", skillName));
return;
}
if (useSkillProgressRateMultipler)
{
const globals = this.databaseServer.getTables().globals;
const skillProgressRate = globals.config.SkillsSettings.SkillProgressRate;
amount = skillProgressRate * amount;
}
2023-03-03 16:23:46 +01:00
profileSkill.Progress += amount;
profileSkill.LastAccess = this.timeUtil.getTimestamp();
2023-03-03 16:23:46 +01:00
}
/**
* Get level of player
* @param pmcData Player profile
* @returns Level of player
2023-03-03 16:23:46 +01:00
*/
public calculateLevel(pmcData: IPmcData): number
{
let exp = 0;
for (const level in this.databaseServer.getTables().globals.config.exp.level.exp_table)
{
if (pmcData.Info.Experience < exp)
{
break;
}
pmcData.Info.Level = parseInt(level);
exp += this.databaseServer.getTables().globals.config.exp.level.exp_table[level].exp;
}
return pmcData.Info.Level;
}
}