Skip to content

Commit

Permalink
Added a way to calculate total skill xp in exploration
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKyle committed Dec 13, 2024
1 parent 3af6b83 commit afb0f80
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/Flare/Services/CharacterRewardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public function fetchXpForMonster(Monster $monster): int
}

// Get XP based on the skill in trainings training sacrificial amount, ie, give me back 85% of this xp.
$xp = $this->skillService->getXpWithSkillTrainingReduction($this->character, $xp);
$xp = $this->skillService->getCharacterXpWithSkillTrainingReduction($this->character, $xp);

$event = ScheduledEvent::where('event_type', EventType::FEEDBACK_EVENT)->where('currently_running', true)->first();

Expand Down
56 changes: 56 additions & 0 deletions app/Game/BattleRewardProcessing/Jobs/ExplorationSkillXpHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Game\BattleRewardProcessing\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Flare\Models\Character;
use App\Flare\Models\Monster;
use App\Flare\Services\CharacterRewardService;
use App\Game\Skills\Services\SkillService;

class ExplorationSkillXpHandler implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* @param integer $characterId
* @param integer $numberOfCreatures
* @param integer $xp
*/
public function __construct(private int $characterId, private int $xp) {}

/**
* Handle the job
*
* @param CharacterRewardService $characterRewardService
* @return void
*/
public function handle(SkillService $skillService): void
{
$character = Character::find($this->characterId);

if (is_null($character)) {
return;
}

$this->processSkillXpReward($character, $skillService);
}

/**
* Process Skill XP Reward
*
* @param Character $character
* @param Monster $monster
* @param CharacterRewardService $characterRewardService
* @return void
*/
private function processSkillXpReward(Character $character, SkillService $skillService): void
{

$skillService->setSkillInTraining($character)->giveXpToTrainingSkill($character, $this->xp);
}
}
19 changes: 17 additions & 2 deletions app/Game/Exploration/Jobs/Exploration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
use App\Flare\Values\MaxCurrenciesValue;
use App\Game\Battle\Events\UpdateCharacterStatus;
use App\Game\Battle\Handlers\BattleEventHandler;
use App\Game\BattleRewardProcessing\Jobs\ExplorationSkillXpHandler;
use App\Game\BattleRewardProcessing\Jobs\ExplorationXpHandler;
use App\Game\Character\Builders\AttackBuilders\CharacterCacheData;
use App\Game\Core\Events\UpdateCharacterCurrenciesEvent;
use App\Game\Exploration\Events\ExplorationLogUpdate;
use App\Game\Exploration\Events\ExplorationTimeOut;

use App\Game\Skills\Services\SkillService;

class Exploration implements ShouldQueue
{
Expand All @@ -31,6 +32,8 @@ class Exploration implements ShouldQueue

private CharacterRewardService $characterRewardService;

private SkillService $skillService;

private int $automationId;

private string $attackType;
Expand All @@ -49,11 +52,14 @@ public function handle(
MonsterPlayerFight $monsterPlayerFight,
BattleEventHandler $battleEventHandler,
CharacterCacheData $characterCacheData,
CharacterRewardService $characterRewardService
CharacterRewardService $characterRewardService,
SkillService $skillService
): void {

$this->characterRewardService = $characterRewardService;

$this->skillService = $skillService;

$automation = CharacterAutomation::where('character_id', $this->character->id)->where('id', $this->automationId)->first();

if ($this->shouldBail($automation)) {
Expand Down Expand Up @@ -125,15 +131,19 @@ private function encounter(MonsterPlayerFight $response, CharacterAutomation $au

$monster = Monster::find($params['selected_monster_id']);
$totalXpToReward = 0;
$totalSkillXpToReward = 0;
$characterRewardService = $this->characterRewardService->setCharacter($this->character);
$characterSkillService = $this->skillService->setSkillInTraining($this->character);

for ($i = 1; $i <= $enemies; $i++) {
$battleEventHandler->processMonsterDeath($this->character->id, $params['selected_monster_id'], false);

$totalXpToReward += $characterRewardService->fetchXpForMonster($monster);
$totalSkillXpToReward += $characterSkillService->getXpForSkillIntraining($this->character, $monster->xp);
}

ExplorationXpHandler::dispatch($this->character->id, $enemies, $totalXpToReward)->onQueue('exploration_battle_xp_reward')->delay(now()->addSeconds(2));
ExplorationSkillXpHandler::dispatch($this->character->id, $totalSkillXpToReward)->onQueue('exploration_battle_skill_xp_reward')->delay(now()->addSeconds(2));

$this->sendOutEventLogUpdate('The last of the enemies fall. Covered in blood, exhausted, you look around for any signs of more of their friends. The area is silent. "Another day, another battle.
We managed to survive." The Guide states as he walks from the shadows. The pair of you set off in search of the next adventure ...
Expand All @@ -156,17 +166,22 @@ private function canSurviveFight(MonsterPlayerFight $response, CharacterAutomati

$monster = Monster::find($params['selected_monster_id']);
$totalXpToReward = 0;
$totalSkillXpToReward = 0;
$characterRewardService = $this->characterRewardService->setCharacter($this->character);
$characterSkillService = $this->skillService->setSkillInTraining($this->character);


for ($i = 1; $i <= 10; $i++) {
if (!$this->fightAutomationMonster($response, $automation, $battleEventHandler, $params)) {
return false;
}

$totalXpToReward += $characterRewardService->fetchXpForMonster($monster);
$totalSkillXpToReward += $characterSkillService->getXpForSkillIntraining($this->character, $monster->xp);
}

ExplorationXpHandler::dispatch($this->character->id, 10, $totalXpToReward)->onQueue('exploration_battle_xp_reward')->delay(now()->addSeconds(2));
ExplorationSkillXpHandler::dispatch($this->character->id, $totalSkillXpToReward)->onQueue('exploration_battle_skill_xp_reward')->delay(now()->addSeconds(2));

Cache::put('can-character-survive-' . $this->character->id, true);

Expand Down
133 changes: 101 additions & 32 deletions app/Game/Skills/Services/SkillService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class SkillService
{
use ResponseBuilder;

private ?Skill $skillInTraining;

public function __construct(
private Manager $manager,
private BasicSkillsTransformer $basicSkillsTransformer,
Expand All @@ -30,6 +32,19 @@ public function __construct(
private BattleMessageHandler $battleMessageHandler,
) {}

/**
* Set the current skill in training
*
* @param Character $character
* @return SkillService
*/
public function setSkillInTraining(Character $character): SkillService
{
$this->skillInTraining = $character->skills->where('currently_training', true)->first();

return $this;
}

/**
* Gets the skills for a player.
*/
Expand Down Expand Up @@ -101,61 +116,82 @@ public function trainSkill(Character $character, int $skillId, float $xpPercenta
*/
public function assignXPToTrainingSkill(Character $character, int $xp): void
{
$skillInTraining = $character->skills->where('currently_training', true)->first();
$event = ScheduledEvent::where('event_type', EventType::FEEDBACK_EVENT)->where('currently_running', true)->first();

if (is_null($skillInTraining)) {
return;
}

if ($skillInTraining->level === $skillInTraining->baseSkill->max_level) {
if (is_null($this->skillInTraining)) {
return;
}

$skillXp = $xp + ($xp * $skillInTraining->xp_towards);
$skillXp = $skillXp + $skillXp * ($skillInTraining->skill_training_bonus + $character->map->gameMap->skill_training_bonus);
$skillXp += 5;
$skillXp = $this->getXpForSkillIntraining($character, $xp);

if (!is_null($event)) {
$skillXp += 150;
}

$newXp = $skillInTraining->xp + $skillXp;
$newXp = $this->skillInTraining->xp + $skillXp;

$skillInTraining->update(['xp' => $newXp]);
$skillInTraining = $skillInTraining->refresh();
$this->skillInTraining->update(['xp' => $newXp]);
$skillInTraining = $this->skillInTraining->refresh();

$this->battleMessageHandler->handleSkillXpUpdate($character->user, $skillInTraining->name, $skillXp);

while ($newXp >= $skillInTraining->xp_max) {
$newXp -= $skillInTraining->xp_max;
$this->handlePossibleLevelUpForSkill($skillInTraining, $newXp);
}

$skillInTraining = $this->levelUpSkill($skillInTraining);
/**
* Give a specific amount of xp to a skill in training
*
* @param Character $character
* @param integer $totalXpToGive
* @return void
*/
public function giveXpToTrainingSkill(Character $character, int $totalXpToGive): void
{
if (is_null($this->skillInTraining)) {
return;
}

if ($skillInTraining->level === $skillInTraining->baseSkill->max_level) {
$newXp = 0;
$newXp = $this->skillInTraining->xp + $totalXpToGive;

$skillInTraining->update(['xp' => $newXp]);
$this->skillInTraining->update(['xp' => $newXp]);
$skillInTraining = $this->skillInTraining->refresh();

$skillInTraining = $skillInTraining->refresh();
$this->battleMessageHandler->handleSkillXpUpdate($character->user, $skillInTraining->name, $totalXpToGive);

break;
}
$this->handlePossibleLevelUpForSkill($skillInTraining, $newXp);
}

$skillInTraining->update(['xp' => $newXp]);
/**
* Get the xp for the skill in training
*
* @param Character $character
* @param integer $xp
* @return integer
*/
public function getXpForSkillIntraining(Character $character, int $xp): int
{
$event = ScheduledEvent::where('event_type', EventType::FEEDBACK_EVENT)->where('currently_running', true)->first();

$skillInTraining = $skillInTraining->refresh();
if (is_null($this->skillInTraining)) {
return 0;
}

if ($newXp > 0) {
$skillInTraining->update(['xp' => $newXp]);
if ($this->skillInTraining->level === $$this->skillInTraining->baseSkill->max_level) {
return 0;
}

$skillXp = $xp + ($xp * $this->skillInTraining->xp_towards);
$skillXp = $skillXp + $skillXp * ($this->skillInTraining->skill_training_bonus + $character->map->gameMap->skill_training_bonus);
$skillXp += 5;

if (!is_null($event)) {
$skillXp += 150;
}
}

/**
* Get the XP after being reduced from any skill in training.
*
* @param Character $character
* @param integer $xp
* @return integer
*/
public function getXpWithSkillTrainingReduction(Character $character, int $xp): int
public function getCharacterXpWithSkillTrainingReduction(Character $character, int $xp): int
{
$skillInTraining = $character->skills->where('currently_training', true)->first();

Expand Down Expand Up @@ -220,13 +256,46 @@ public function assignXpToCraftingSkill(GameMap $gameMap, Skill $skill): void
$skill->update(['xp' => $newXp]);
}

/**
* Handle possibly leveling up the skill.
*
* @param Skill $skillInTraining
* @param integer $newXp
* @return void
*/
private function handlePossibleLevelUpForSkill(Skill $skillInTraining, int $newXp): void
{
while ($newXp >= $skillInTraining->xp_max) {
$newXp -= $skillInTraining->xp_max;

$skillInTraining = $this->levelUpSkill($skillInTraining);

if ($skillInTraining->level === $skillInTraining->baseSkill->max_level) {
$newXp = 0;

$skillInTraining->update(['xp' => $newXp]);

$skillInTraining = $skillInTraining->refresh();

break;
}

$skillInTraining->update(['xp' => $newXp]);

$skillInTraining = $skillInTraining->refresh();
}

if ($newXp > 0) {
$skillInTraining->update(['xp' => $newXp]);
}
}

/**
* Level a skill.
*
* @throws Exception
*/
protected function levelUpSkill(Skill $skill): Skill
private function levelUpSkill(Skill $skill): Skill
{
if ($skill->xp >= $skill->xp_max) {

Expand Down
8 changes: 8 additions & 0 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@
'database' => 5,
],

'exploration_battle_skill_xp_reward' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 5,
],

'battle_reward_currencies' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
Expand Down
Loading

0 comments on commit afb0f80

Please sign in to comment.