Skip to content

Commit

Permalink
Started refactoring messages to use enums and match instead of string…
Browse files Browse the repository at this point in the history
…s and switch
  • Loading branch information
AdamKyle committed Dec 5, 2024
1 parent 1aad2d0 commit 38b63a9
Show file tree
Hide file tree
Showing 47 changed files with 618 additions and 217 deletions.
5 changes: 3 additions & 2 deletions app/Admin/Jobs/AssignNewKingdomBuildingsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Flare\Models\GameBuilding;
use App\Flare\Models\Kingdom;
use App\Game\Messages\Types\KingdomMessageTypes;
use Facades\App\Game\Messages\Handlers\ServerMessageHandler;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down Expand Up @@ -56,9 +57,9 @@ public function handle()
if (! is_null($kingdom->character)) {
$user = $kingdom->character->user;

$message = 'Kingdom: '.$kingdom->name.' gained a new building: '.$this->gameBuilding->name;
$message = 'Kingdom: ' . $kingdom->name . ' gained a new building: ' . $this->gameBuilding->name;

ServerMessageHandler::handleMessage($user, 'new_building', $message);
ServerMessageHandler::handleMessage($user, KingdomMessageTypes::NEW_BUILDING, $message);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions app/Admin/Jobs/UpdateKingdomBuildings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Flare\Models\GameBuilding;
use App\Flare\Models\Kingdom;
use App\Flare\Models\KingdomBuilding;
use App\Game\Messages\Types\KingdomMessageTypes;
use Facades\App\Flare\Values\UserOnlineValue;
use Facades\App\Game\Messages\Handlers\ServerMessageHandler;
use Illuminate\Bus\Queueable;
Expand Down Expand Up @@ -61,10 +62,10 @@ public function handle(): void
if (! is_null($kingdom->character)) {
$user = $kingdom->character->user;

$message = 'Kingdom: '.$kingdom->name.' gained a new building: '.$this->gameBuilding->name;
$message = 'Kingdom: ' . $kingdom->name . ' gained a new building: ' . $this->gameBuilding->name;

if (UserOnlineValue::isOnline($user)) {
ServerMessageHandler::handleMessage($user, 'new_building', $message);
ServerMessageHandler::handleMessage($user, KingdomMessageTypes::NEW_BUILDING, $message);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions app/Admin/Services/ItemAffixService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Flare\Models\MarketBoard;
use App\Flare\Models\MarketHistory;
use App\Game\Core\Events\UpdateTopBarEvent;
use App\Game\Messages\Types\AdminMessageTypes;
use Facades\App\Game\Messages\Handlers\ServerMessageHandler;
use Illuminate\Database\Eloquent\Collection;

Expand Down Expand Up @@ -47,7 +48,7 @@ public function cleanRequestData(array $params): array
*/
public function deleteAffix(ItemAffix $affix): void
{
$column = 'item_'.$affix->type.'_id';
$column = 'item_' . $affix->type . '_id';
$name = $affix->name;
$itemsWithThisAffix = Item::where($column, $affix->id)->get();

Expand Down Expand Up @@ -120,9 +121,9 @@ protected function handleSlots(Collection $slots, ItemAffix $affix, string $name

$character = $character->refresh();

$forMessages = $name.' has been removed from one or more of your items. You have been compensated the amount of: '.$affix->cost;
$forMessages = $name . ' has been removed from one or more of your items. You have been compensated the amount of: ' . $affix->cost;

ServerMessageHandler::handleMessage($character->user, 'deleted_affix', $forMessages);
ServerMessageHandler::handleMessage($character->user, AdminMessageTypes::DELETED_AFFIX, $forMessages);

event(new UpdateTopBarEvent($character));
}
Expand Down
11 changes: 6 additions & 5 deletions app/Admin/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Flare\Models\User;
use App\Game\Core\Events\UpdateTopBarEvent;
use App\Game\Messages\Events\GlobalMessageEvent;
use App\Game\Messages\Types\CharacterMessageTypes;
use Carbon\Carbon;
use Facades\App\Game\Messages\Handlers\ServerMessageHandler;

Expand Down Expand Up @@ -86,9 +87,9 @@ public function silence(User $user, int $silenceFor): void

$user = $user->refresh();

$message = 'The creator has silenced you until: '.$canSpeakAgainAt->format('Y-m-d H:i:s').' ('.(int) $silenceFor.' Minutes server time) Making accounts to get around this is a bannable offense.';
$message = 'The creator has silenced you until: ' . $canSpeakAgainAt->format('Y-m-d H:i:s') . ' (' . (int) $silenceFor . ' Minutes server time) Making accounts to get around this is a bannable offense.';

ServerMessageHandler::handleMessage($user, 'silenced', $message);
ServerMessageHandler::handleMessage($user, CharacterMessageTypes::SILENCED, $message);

event(new UpdateTopBarEvent($user->character));

Expand All @@ -113,7 +114,7 @@ public function forceNameChange(User $user): void
*/
public function broadCastAdminMessage(User $user): void
{
$message = $user->character->name.' Sees the sky open and lightening comes hurtling down, striking the earth - cracking the air for miles around! They have been smitten by the hand of The Creator!';
$message = $user->character->name . ' Sees the sky open and lightening comes hurtling down, striking the earth - cracking the air for miles around! They have been smitten by the hand of The Creator!';

event(new GlobalMessageEvent($message));
}
Expand All @@ -127,8 +128,8 @@ public function sendUserMail(User $user, ?Carbon $unBanAt = null): void
{
event(new RefreshUserScreenEvent($user));

$unBannedAt = ! is_null($unBanAt) ? $unBanAt->format('l jS \\of F Y h:i:s A').' '.$unBanAt->timezoneName.'.' : 'Forever.';
$message = 'You have been banned until: '.$unBannedAt.' For the reason of: '.$user->banned_reason;
$unBannedAt = ! is_null($unBanAt) ? $unBanAt->format('l jS \\of F Y h:i:s A') . ' ' . $unBanAt->timezoneName . '.' : 'Forever.';
$message = 'You have been banned until: ' . $unBannedAt . ' For the reason of: ' . $user->banned_reason;

BanEmail::dispatch($user, $message)->delay(now()->addMinutes(1));
}
Expand Down
3 changes: 2 additions & 1 deletion app/Flare/Jobs/UpdateSilencedUserJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Admin\Events\UpdateAdminChatEvent;
use App\Flare\Models\User;
use App\Game\Core\Events\UpdateTopBarEvent;
use App\Game\Messages\Types\CharacterMessageTypes;
use Facades\App\Game\Messages\Handlers\ServerMessageHandler;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function handle(): void

$user = $this->user->refresh(0);

ServerMessageHandler::handleMessage($user, 'silenced', $forMessage);
ServerMessageHandler::handleMessage($user, CharacterMessageTypes::SILENCED, $forMessage);

event(new UpdateTopBarEvent($user->character));

Expand Down
109 changes: 77 additions & 32 deletions app/Flare/Services/CharacterRewardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use App\Game\Core\Services\CharacterService;
use App\Game\Events\Values\EventType;
use App\Game\Messages\Events\ServerMessageEvent;
use App\Game\Messages\Types\CharacterMessageTypes;
use App\Game\Messages\Types\MessageType;
use App\Game\Skills\Services\SkillService;
use Facades\App\Flare\Calculators\XPCalculator;
use Facades\App\Game\Messages\Handlers\ServerMessageHandler;
Expand Down Expand Up @@ -64,7 +66,7 @@ public function __construct(
/**
* Set the character.
*
* @return $this
* @return CharacterRewardService
*/
public function setCharacter(Character $character): CharacterRewardService
{
Expand All @@ -82,17 +84,7 @@ public function distributeCharacterXP(Monster $monster): CharacterRewardService
{
$this->distributeXP($monster);

if ($this->character->xp >= $this->character->xp_next) {
$leftOverXP = $this->character->xp - $this->character->xp_next;

if ($leftOverXP > 0) {
$this->handleLevelUps($leftOverXP);
}

if ($leftOverXP <= 0) {
$this->handleCharacterLevelUp(0);
}
}
$this->handleLevelUp();

if (! $this->character->isLoggedIn()) {
event(new UpdateTopBarEvent($this->character->refresh()));
Expand All @@ -101,6 +93,26 @@ public function distributeCharacterXP(Monster $monster): CharacterRewardService
return $this;
}

/**
* Distribute a specific amount of XP
*
* @param integer $xp
* @return CharacterRewardService
*/
public function distributeSpecifiedXp(int $xp): CharacterRewardService
{

$this->character->update([
'xp' => $xp,
]);

$this->character = $this->character->refresh();

$this->handleLevelUp();

return $this;
}

/**
* Distribute Skill Xp
*
Expand Down Expand Up @@ -189,10 +201,33 @@ public function currencyEventReward(Monster $monster): CharacterRewardService
return $this;
}

/**
* Handle possible level up.
*
* Takes into account XP over flow.
*
* @param Character $character
* @return void
*/
private function handleLevelUp(): void
{
if ($this->character->xp >= $this->character->xp_next) {
$leftOverXP = $this->character->xp - $this->character->xp_next;

if ($leftOverXP > 0) {
$this->handleMultipleLevelUps($leftOverXP);
}

if ($leftOverXP <= 0) {
$this->handleCharacterLevelUp(0);
}
}
}

/**
* Handle instances where we could have multiple level ups.
*/
private function handleLevelUps(int $leftOverXP, bool $shouldBuildCache = false): void
private function handleMultipleLevelUps(int $leftOverXP, bool $shouldBuildCache = false): void
{

$this->handleCharacterLevelUp($leftOverXP, $shouldBuildCache);
Expand All @@ -201,11 +236,11 @@ private function handleLevelUps(int $leftOverXP, bool $shouldBuildCache = false)
$leftOverXP = $this->character->xp - $this->character->xp_next;

if ($leftOverXP > 0) {
$this->handleLevelUps($leftOverXP, false);
$this->handleMultipleLevelUps($leftOverXP, false);
}

if ($leftOverXP <= 0) {
$this->handleLevelUps(0, true);
$this->handleMultipleLevelUps(0, true);
}
}

Expand Down Expand Up @@ -239,23 +274,43 @@ public function handleCharacterLevelUp(int $leftOverXP, bool $shouldBuildCache =
$this->updateCharacterStats($character);
}

ServerMessageHandler::handleMessage($character->user, 'level_up', $character->level);
ServerMessageHandler::handleMessage($character->user, CharacterMessageTypes::LEVEL_UP, $character->level);
}

/**
* Assigns XP to the character.
*
* @return void
*
* @throws Exception
*/
private function distributeXP(Monster $monster)
private function distributeXP(Monster $monster): void
{

$xp = $this->fetchXpForMonster($monster);

$this->character->update([
'xp' => $xp,
]);

$this->character = $this->character->refresh();
}

/**
* Fetch the xp for the monster
*
* - Can return 0 if we cannot gain xp.
* - Can return 0 if the xp we would gain is 0.
* - Takes into account skills in training
* - Takes into account Xp Bonuses such as items (Alchemy and quest)
*
* @param Monster $monster
* @return integer
*/
public function fetchXpForMonster(Monster $monster): int
{
$addBonus = true;

if (!$this->characterXpService->canCharacterGainXP($this->character)) {
return;
return 0;
}

// Reduce The XP from the monster if needed.
Expand Down Expand Up @@ -287,20 +342,10 @@ private function distributeXP(Monster $monster)
}

if ($xp === 0) {
return;
return 0;
}

$xp = $this->getXpWithBonuses($xp);

$characterXp = (int) $this->character->xp;

$xp = $characterXp + $xp;

$this->character->update([
'xp' => $xp,
]);

$this->character = $this->character->refresh();
return $this->getXpWithBonuses($xp);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions app/Flare/Services/DailyGoldDustService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Flare\Values\MaxCurrenciesValue;
use App\Game\Core\Events\UpdateTopBarEvent;
use App\Game\Messages\Events\GlobalMessageEvent;
use App\Game\Messages\Types\LotteryMessageType;
use Facades\App\Game\Messages\Handlers\ServerMessageHandler;
use Illuminate\Support\Facades\Cache;

Expand All @@ -32,7 +33,7 @@ public function handleRegularDailyGoldDust(Character $character): void

$character = $character->refresh();

ServerMessageHandler::handleMessage($character->user, 'daily_lottery', number_format($amount));
ServerMessageHandler::handleMessageWithNewValue($character->user, LotteryMessageType::DAILY_LOTTERY, number_format($amount), number_format($character->gold_dust));

event(new UpdateTopBarEvent($character));
}
Expand All @@ -44,7 +45,8 @@ public function handleWonDailyLottery(Character $character): void
{

if (! Cache::has('daily-gold-dust-lottery-won')) {
event(new GlobalMessageEvent($character->name.' has won the daily Gold Dust Lottery!
event(new GlobalMessageEvent(
$character->name . ' has won the daily Gold Dust Lottery!
(Gold Dust is used in Alchemy and Quests - See Help section -> Click Help I\'m stuck, and see currencies under: Character Information -> Currencies)'
));

Expand All @@ -63,7 +65,7 @@ public function handleWonDailyLottery(Character $character): void
Cache::put('daily-gold-dust-lottery-won', $character->id);
}

ServerMessageHandler::handleMessage($character->user, 'lotto_max', number_format(self::LOTTO_MAX));
ServerMessageHandler::handleMessageWithNewValue($character->user, LotteryMessageType::LOTTO_MAX, number_format(self::LOTTO_MAX), number_format($character->gold_dust));

event(new UpdateTopBarEvent($character));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function fetchCelestialFight(Character $character, CelestialFight $celest
public function attack(CelestialFightRequest $request, Character $character, CelestialFight $celestialFight)
{
if ($character->is_dead) {
broadcast(new ServerMessageEvent($character->user, 'You are dead and cannot participate.'));
event(new ServerMessageEvent($character->user, 'You are dead and cannot participate.'));

return response()->json([], 200);
}
Expand Down
26 changes: 10 additions & 16 deletions app/Game/Battle/Handlers/BattleEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Game\Battle\Events\AttackTimeOutEvent;
use App\Game\Battle\Events\CharacterRevive;
use App\Game\Battle\Events\UpdateCharacterStatus;
use App\Game\BattleRewardProcessing\Jobs\BattleAttackHandler;
use App\Game\BattleRewardProcessing\Services\BattleRewardService;
use App\Game\BattleRewardProcessing\Services\SecondaryRewardService;
use App\Game\BattleRewardProcessing\Services\WeeklyBattleService;
Expand Down Expand Up @@ -57,25 +58,18 @@ public function processDeadCharacter(Character $character, ?Monster $monster = n
}

/**
* Process the fact the monster has died.
* Processes what we should do when the monster dies.
*
* @throws Exception
* - Handles rewarding the player
*
* @param integer $characterId
* @param integer $monsterId
* @param boolean $includeXp
* @return void
*/
public function processMonsterDeath(int $characterId, int $monsterId): void
public function processMonsterDeath(int $characterId, int $monsterId, bool $includeXp = true): void
{
$monster = Monster::find($monsterId);

$character = Character::find($characterId);

if (is_null($monster)) {
Log::error('Missing Monster for id: ' . $monsterId);

return;
}

$this->battleRewardService->setUp($character->id, $monster->id)->handleBaseRewards();

$this->secondaryRewardService->handleSecondaryRewards($character);
$this->battleRewardService->setUp($characterId, $monsterId)->handleBaseRewards($includeXp);
}

/**
Expand Down
Loading

0 comments on commit 38b63a9

Please sign in to comment.