Skip to content

Commit

Permalink
Handing out items to players for the winter event
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKyle committed Dec 14, 2024
1 parent 0fde918 commit 8797d79
Show file tree
Hide file tree
Showing 9 changed files with 334 additions and 20 deletions.
18 changes: 12 additions & 6 deletions app/Console/DevelopmentCommands/TestExploration.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,23 @@ public function handle(CharacterBuilderService $characterBuilder, ExplorationAut

$this->line('Starting explorations for 1 hour, using default attack type, killing the first surface monster ...');

$bar = $this->output->createProgressBar($characters > count());

$bar->start();

foreach ($characters as $character) {
$explorationAutomationService->beginAutomation($character, [
'selected_monster_id' => Monster::where('name', 'Sewer Rat')->first()->id,
'auto_attack_length' => 1,
'attack_type' => 'attack',
'move_down_the_list_every' => null,
]);

$bar->advance();
}

$bar->finish();

$this->line('Automations have been started.');
}

Expand All @@ -74,14 +82,13 @@ protected function getTheCharacters(CharacterBuilderService $characterBuilder, i
if ($numberOfCharacters > $characters->count()) {
$charactersToCreate = $numberOfCharacters - $characters->count();

$this->line('Creating character amount: '.$charactersToCreate);
$this->line('Creating character amount: ' . $charactersToCreate);

$this->createTheCharacters($characterBuilder, $charactersToCreate);

$this->line('Characters created.');

return $this->getTheCharacters($characterBuilder, $numberOfCharacters, $characterToIgnore);

}

return $characters->take($numberOfCharacters)->get();
Expand All @@ -102,7 +109,6 @@ protected function createTheCharacters(CharacterBuilderService $characterBuilder
$gameRace = GameRace::inRandomOrder()->first();

$this->createCharacter($characterBuilder, $user, $surfaceMap, $gameClass, $gameRace);

}
}

Expand All @@ -112,9 +118,9 @@ protected function createTheCharacters(CharacterBuilderService $characterBuilder
protected function createUser(): User
{
return User::create([
'email' => Str::random(8).'@test.com',
'email' => Str::random(8) . '@test.com',
'password' => Hash::make(Str::random(8)),
'ip_address' => '0.0.0.'.rand(1, 100),
'ip_address' => '0.0.0.' . rand(1, 100),
'last_logged_in' => now(),
'guide_enabled' => false,
]);
Expand All @@ -133,7 +139,7 @@ protected function createCharacter(CharacterBuilderService $characterBuilder, Us

$characterBuilder->setRace($race)
->setClass($class)
->createCharacter($user, $map, Str::random(4).str_replace(' ', '', $class->name))
->createCharacter($user, $map, Str::random(4) . str_replace(' ', '', $class->name))
->assignSkills()
->assignPassiveSkills()
->buildCharacterCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ protected function createPossibleEvent()
return;
}

if (RandomNumberGenerator::generateTrueRandomNumber(1000000) >= 1000000) {
if (RandomNumberGenerator::generateTrueRandomNumber(1000) >= 999) {
Event::create([
'type' => EventType::GOLD_MINES,
'started_at' => now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ protected function createPossibleEvent()
return;
}

if (RandomNumberGenerator::generateTrueRandomNumber(1000000) >= 1000000) {
if (RandomNumberGenerator::generateTrueRandomNumber(1000) >= 999) {
Event::create([
'type' => EventType::PURGATORY_SMITH_HOUSE,
'started_at' => now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private function createPossibleEvent()
return;
}

if (RandomNumberGenerator::generateTrueRandomNumber(1000000) >= 1000000) {
if (RandomNumberGenerator::generateTrueRandomNumber(1000) >= 999) {
Event::create([
'type' => EventType::THE_OLD_CHURCH,
'started_at' => now(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
<?php

namespace App\Game\BattleRewardProcessing\Jobs\Events;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Facades\App\Flare\RandomNumber\RandomNumberGenerator;
use App\Flare\Builders\RandomAffixGenerator;
use App\Flare\Builders\RandomItemDropBuilder;
use App\Flare\Models\Character;
use App\Flare\Models\Item;
use App\Flare\Models\ScheduledEvent;
use App\Flare\Values\ItemSpecialtyType;
use App\Flare\Values\RandomAffixDetails;
use App\Game\Events\Values\EventType;
use App\Game\Messages\Events\GlobalMessageEvent;
use App\Game\Messages\Events\ServerMessageEvent;

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

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

/**
* Handle the job
*
* @param RandomAffixGenerator $randomAffixGenerator
* @param RandomItemDropBuilder $randomItemDropBuilder
* @return void
*/
public function handle(RandomAffixGenerator $randomAffixGenerator, RandomItemDropBuilder $randomItemDropBuilder): void
{
$character = Character::find($this->characterId);
$scheduledEvent = ScheduledEvent::where('event_type', EventType::WINTER_EVENT)->where('currently_running', true)->first();

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

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

if (!$character->map->gameMap->mapType()->isTheIcePlane()) {
return;
}

if ($this->canHaveWinterGift()) {

if ($character->isInventoryFull()) {
event(new ServerMessageEvent($character->user, 'Mr. Whiskers could not give you a christmas gift. He is sad. You made a fluffy black cat sad because your inventory is full. Make some room for next time child.'));

return;
}

$typeOfGear = $this->getType();

$costOfAffixToGenerate = $this->getCostOfAffixToAttach();

if (is_null($typeOfGear) || ($costOfAffixToGenerate < RandomAffixDetails::LEGENDARY)) {
$itemToGive = $randomItemDropBuilder->generateItem(rand(0, 400));

$this->giveItemToPlayer($character, $itemToGive, 0);

return;
}

$itemToGive = $this->fetchItemForReward($typeOfGear);

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

$numberOfAffixesToAttach = $this->howManyAffixesToAttach();

$itemToGive = $this->attachAffixes($character, $randomAffixGenerator, $itemToGive, $costOfAffixToGenerate, $numberOfAffixesToAttach);

$this->giveItemToPlayer($character, $itemToGive, $costOfAffixToGenerate);
}
}

/**
* Can have winter gift?
*
* @return boolean
*/
private function canHaveWinterGift(): bool
{
return RandomNumberGenerator::generateTrueRandomNumber(100) > 65;
}

/**
* What type of gear do we generate?
*
* Null means base gear
*
* @return string|null
*/
private function getType(): ?string
{

$randomChance = RandomNumberGenerator::generateTrueRandomNumber(100);

if ($randomChance < 50) {
return null;
}

$specialtyTypesOfgear = [
ItemSpecialtyType::CORRUPTED_ICE,
ItemSpecialtyType::HELL_FORGED,
ItemSpecialtyType::PURGATORY_CHAINS,
ItemSpecialtyType::PIRATE_LORD_LEATHER,
ItemSpecialtyType::DELUSIONAL_SILVER,
ItemSpecialtyType::TWISTED_EARTH,
ItemSpecialtyType::FAITHLESS_PLATE,
ItemSpecialtyType::PIRATE_LORD_LEATHER,
];

return $specialtyTypesOfgear[rand(0, count($specialtyTypesOfgear) - 1)];
}

/**
* Fetch an item to reward
*
* @param string|null $type
* @return Item
*/
private function fetchItemForReward(?string $type): Item
{
return Item::where('specialty_type', $type)
->whereNull('item_prefix_id')
->whereNull('item_suffix_id')
->whereDoesntHave('appliedHolyStacks')
->whereNotIn('type', [
'artifact',
'trinket',
'quest',
])
->inRandomOrder()
->first();
}

/**
* Get cost of affix to attach.
*
* - Anything less then legendary will be a regular affix.
*
* @return integer
*/
private function getCostOfAffixToAttach(): int
{
$randomChance = RandomNumberGenerator::generateTrueRandomNumber(500);

if ($randomChance >= 450) {
return RandomAffixDetails::COSMIC;
}

if ($randomChance >= 375) {
return RandomAffixDetails::MYTHIC;
}

if ($randomChance >= 100) {
return RandomAffixDetails::LEGENDARY;
}

// Anything less then a legendary
return RandomAffixDetails::LEGENDARY - 1;
}

/**
* How many affixes should we attach?
*
* - 1 or 2
*
* @return integer
*/
private function howManyAffixesToAttach(): int
{
return rand(1, 100) > 75 ? 2 : 1;
}

/**
* Attach affixes to item
*
* @param Character $character
* @param RandomAffixGenerator $randomAffixGenerator
* @param Item $item
* @param integer $costOfAffix
* @param integer $numberOfAffixes
* @return Item
*/
private function attachAffixes(Character $character, RandomAffixGenerator $randomAffixGenerator, Item $item, int $costOfAffix, int $numberOfAffixes): Item
{
$item = $item->duplicate();

$randomAffixGenerator = $randomAffixGenerator->setCharacter($character)->setPaidAmount($costOfAffix);

if ($numberOfAffixes === 1) {
$whichSide = rand(1, 100) > 50 ? 'suffix' : 'prefix';

$item->update([
'item_' . $whichSide . '_id' => $randomAffixGenerator->generateAffix($whichSide)->id,
'is_mythic' => $costOfAffix === RandomAffixDetails::MYTHIC,
'is_cosmic' => $costOfAffix === RandomAffixDetails::COSMIC,
]);

return $item->refresh();
}

$item->update([
'item_prefix_id' => $randomAffixGenerator->generateAffix('prefix')->id,
'item_suffix_id' => $randomAffixGenerator->generateAffix('suffix')->id,
'is_mythic' => $costOfAffix === RandomAffixDetails::MYTHIC,
'is_cosmic' => $costOfAffix === RandomAffixDetails::COSMIC,
]);

return $item->refresh();
}

/**
* Give item to the player.
*
* - If cosmic announce to all of chat.
*
* @param Character $character
* @param Item $item
* @return void
*/
private function giveItemToPlayer(Character $character, Item $item, int $costOfAffix): void
{
$slot = $character->inventory->slots()->create([
'item_id' => $item->id,
]);

if ($item->is_cosmic) {
event(new GlobalMessageEvent('Holy crap! Mr. Whiskers gave a COSMIC item to: ' . $character->name . ' what a rare fine! Kill creatures in The Ice Plane to try and earn yours while The Winter Event is running! Free christmas gifts for all while slaughtering creatures down there. Exploration works too! Do not miss out!'));
}

$type = match (true) {
$costOfAffix === RandomAffixDetails::LEGENDARY => 'Unique',
$costOfAffix === RandomAffixDetails::MYTHIC => 'Mythical',
$costOfAffix === RandomAffixDetails::COSMIC => 'Comic',
default => 'Normal'
};

event(new ServerMessageEvent($character->user, 'Mr. Whiskers has given you an item child! How fun is that? You got this because it is christmas time. You ununwrap your gift only to recieve a (' . $type . '): ' . $item->affix_name, $slot->id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Game\BattleRewardProcessing\Jobs\BattleSecondaryRewardHandler;
use App\Game\BattleRewardProcessing\Jobs\BattleWeeklyFightHandler;
use App\Game\BattleRewardProcessing\Jobs\BattleXpHandler;
use App\Game\BattleRewardProcessing\Jobs\Events\WinterEventChristmasGiftHandler;

class BattleRewardService
{
Expand Down Expand Up @@ -54,5 +55,8 @@ public function handleBaseRewards($includeXp = true)
BattleLocationHandler::dispatch($this->characterId, $this->monsterId)->onQueue('battle_reward_location_handlers')->delay(now()->addSeconds(2));
BattleWeeklyFightHandler::dispatch($this->characterId, $this->monsterId)->onQueue('battle_reward_weekly_fights')->delay(now()->addSeconds(2));
BattleItemHandler::dispatch($this->characterId, $this->monsterId)->onQueue('battle_reward_item_handler')->delay(now()->addSeconds(2));

// For special Events:
WinterEventChristmasGiftHandler::dispatch($this->characterId)->onQueue('event_battle_reward')->delay(now()->addSeconds(2));
}
}
Loading

0 comments on commit 8797d79

Please sign in to comment.