Skip to content

Commit

Permalink
Finished tests for the new battle messages
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKyle committed Dec 13, 2024
1 parent 3158add commit 3af6b83
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 30 deletions.
2 changes: 2 additions & 0 deletions app/Flare/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class User extends Authenticatable
'show_xp_for_class_masteries',
'show_xp_for_class_ranks',
'show_xp_for_equipped_class_specials',
'show_faction_loyalty_xp_gain',
'show_skill_xp_per_kill',
'show_item_skill_kill_count',
'show_gold_per_kill',
Expand Down Expand Up @@ -98,6 +99,7 @@ class User extends Authenticatable
'show_xp_for_class_masteries' => 'boolean',
'show_xp_for_class_ranks' => 'boolean',
'show_xp_for_equipped_class_specials' => 'boolean',
'show_faction_loyalty_xp_gain' => 'boolean',
'show_skill_xp_per_kill' => 'boolean',
'show_item_skill_kill_count' => 'boolean',
'show_gold_per_kill' => 'boolean',
Expand Down
56 changes: 26 additions & 30 deletions app/Game/BattleRewardProcessing/Handlers/BattleMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public function handleMessageForExplorationXp(User $user, int $numberOfCreatures
/**
* Handle message for gaining xp for faction loyalty
*
* @param User $user
* @param integer $totalXp
* @param integer $newFameLevel
* @param string $npcName
* @param User $user.
* @return void
*/
public function handleFactionLoyaltyXp(User $user, int $totalXp, int $newFameLevel, string $npcName): void
Expand All @@ -53,11 +50,11 @@ public function handleFactionLoyaltyXp(User $user, int $totalXp, int $newFameLev
return;
}

if (!$user->show_xp_per_kill) {
if (!$user->show_faction_loyalty_xp_gain) {
return;
}

$message = 'For gaining a new fame level (' . $newFameLevel . ') for helping: ' . $npcName . ' with their tasks you were rewarded with: ' . number_format($totalXp) . ' XP!';
$message = 'For gaining a new fame level (' . $newFameLevel . ') for helping: ' . $npcName . ' with their tasks you were rewarded with: ' . number_format($totalXp) . ' XP.';

ServerMessageHandler::sendBasicMessage($user, $message);
}
Expand All @@ -83,7 +80,7 @@ public function handleFactionPointGain(User $user, int $numberOfPointsToGain, in

$neededPoints = $maxPointsNeeded - $currentPoints;

$message = 'You gained: ' . number_format($numberOfPointsToGain) . ' Faction Points, which puts you at: ' . number_format($currentPoints) . '. You neex ' . number_format($neededPoints) . ' more points to gain a new level!';
$message = 'You gained: ' . number_format($numberOfPointsToGain) . ' Faction Points, which puts you at: ' . number_format($currentPoints) . ' points. You need: ' . number_format($neededPoints) . ' more points to gain a new level!';

ServerMessageHandler::sendBasicMessage($user, $message);
}
Expand All @@ -106,20 +103,18 @@ public function handleFactionPointGain(User $user, int $numberOfPointsToGain, in
*/
public function handleCurrencyGainMessage(User $user, CurrenciesMessageTypes $currencyType, int $currencyGain, int $newTotal): void
{

if (!UserOnlineValue::isOnline($user)) {
return;
}

$shouldShowMessage = match ($currencyType) {
CurrenciesMessageTypes::GOLD => $user->show_gold_per_kill,
CurrenciesMessageTypes::GOLD_DUST => $user->show_gold_dust_per_kill,
CurrenciesMessageTypes::SHARDS => $user->show_shards_per_kill,
CurrenciesMessageTypes::COPPER_COINS => $user->show_copper_coins_per_kill,
default => false,
};
$showMessageFlags = [
CurrenciesMessageTypes::GOLD->value => $user->show_gold_per_kill,
CurrenciesMessageTypes::GOLD_DUST->value => $user->show_gold_dust_per_kill,
CurrenciesMessageTypes::SHARDS->value => $user->show_shards_per_kill,
CurrenciesMessageTypes::COPPER_COINS->value => $user->show_copper_coins_per_kill,
];

if (!$shouldShowMessage) {
if (!$showMessageFlags[$currencyType->value]) {
return;
}

Expand All @@ -131,6 +126,7 @@ public function handleCurrencyGainMessage(User $user, CurrenciesMessageTypes $cu
);
}


/**
* Uhandles messages for class ranks when they gain XP.
*
Expand All @@ -146,7 +142,6 @@ public function handleCurrencyGainMessage(User $user, CurrenciesMessageTypes $cu
* @param integer $currentXp
* @param string|null $weaponMastery
* @param string|null $classspecial
* @throws InvalidArgumentException
* @return void
*/
public function handleClassRankMessage(User $user, ClassRanksMessageTypes $classRanksMessageTypes, string $className, int $xpGiven, int $currentXp, string $weaponMastery = null, string $classspecial = null): void
Expand All @@ -155,27 +150,28 @@ public function handleClassRankMessage(User $user, ClassRanksMessageTypes $class
return;
}

$shouldShowMessage = match ($classRanksMessageTypes) {
ClassRanksMessageTypes::XP_FOR_CLASS_MASTERIES => $user->show_xp_for_class_masteries,
ClassRanksMessageTypes::XP_FOR_CLASS_RANKS => $user->show_xp_for_class_ranks,
ClassRanksMessageTypes::XP_FOR_EQUIPPED_CLASS_SPECIALS => $user->show_xp_for_equipped_class_specials,
default => false,
};
$showMessageFlags = [
ClassRanksMessageTypes::XP_FOR_CLASS_MASTERIES->value => $user->show_xp_for_class_masteries,
ClassRanksMessageTypes::XP_FOR_CLASS_RANKS->value => $user->show_xp_for_class_ranks,
ClassRanksMessageTypes::XP_FOR_EQUIPPED_CLASS_SPECIALS->value => $user->show_xp_for_equipped_class_specials,
];

if (!$shouldShowMessage) {
if (!$showMessageFlags[$classRanksMessageTypes->getValue()]) {
return;
}

$message = match ($classRanksMessageTypes) {
ClassRanksMessageTypes::XP_FOR_CLASS_MASTERIES => 'Your class: ' . $className . ' has gained experience in a weapon mastery: ' . $weaponMastery . ' of: ' . number_format($xpGiven) . ' XP and now has a total of: ' . number_format($currentXp) . ' XP.',
ClassRanksMessageTypes::XP_FOR_CLASS_RANKS => 'Your class rank: ' . $className . ' has gained experience of: ' . number_format($xpGiven) . ' XP and now has a total of: ' . number_format($currentXp) . ' XP.',
ClassRanksMessageTypes::XP_FOR_EQUIPPED_CLASS_SPECIALS => 'Your class rank: ' . $className . ' has gained experience in a specialty ypu have equipped: ' . $classspecial . ' of: ' . number_format($xpGiven) . ' XP and now has a total of: ' . number_format($currentXp) . ' XP.',
default => throw new InvalidArgument('Invalid message type was given ...'),
};
$messages = [
ClassRanksMessageTypes::XP_FOR_CLASS_MASTERIES->value => 'Your class: ' . $className . ' has gained experience in a weapon mastery: ' . $weaponMastery . ' of: ' . number_format($xpGiven) . ' XP and now has a total of: ' . number_format($currentXp) . ' XP.',
ClassRanksMessageTypes::XP_FOR_CLASS_RANKS->value => 'Your class rank: ' . $className . ' has gained experience of: ' . number_format($xpGiven) . ' XP and now has a total of: ' . number_format($currentXp) . ' XP.',
ClassRanksMessageTypes::XP_FOR_EQUIPPED_CLASS_SPECIALS->value => 'Your class rank: ' . $className . ' has gained experience in a specialty you have equipped: ' . $classspecial . ' of: ' . number_format($xpGiven) . ' XP and now has a total of: ' . number_format($currentXp) . ' XP.',
];

$message = $messages[$classRanksMessageTypes->getValue()];

ServerMessageHandler::sendBasicMessage($user, $message);
}


/**
* Handle when an item skills kill count increases.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('show_faction_loyalty_xp_gain')->default(true);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use App\Game\BattleRewardProcessing\Handlers\BattleMessageHandler;
use App\Game\Messages\Events\ServerMessageEvent;
use App\Game\Messages\Types\ClassRanksMessageTypes;
use App\Game\Messages\Types\CurrenciesMessageTypes;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Tests\Setup\Character\CharacterFactory;
Expand Down Expand Up @@ -63,6 +65,204 @@ public function testHandleXPForExplorationMessageWhenUserLoggedInAndEnabledSetti
});
}

public function testHandleFactionLoyaltyXpMessageWhenNotLoggedIn()
{
$user = $this->createCharacterWithUserAttributes()->getCharacter()->user;

$this->battleMessageHandler->handleFactionLoyaltyXp($user, 10, 1, 'npc name');

Event::assertNotDispatched(ServerMessageEvent::class);
}

public function testHandleFactionLoyaltyXpMessageWhenLoggedInAndSettingIsTurnedOff()
{
$user = $this->createCharacterWithUserAttributes([
'show_faction_loyalty_xp_gain' => false
], true)->getCharacter()->user;

$this->battleMessageHandler->handleFactionLoyaltyXp($user, 10, 1, 'npc name');

Event::assertNotDispatched(ServerMessageEvent::class);
}

public function testHandleFactionLoyaltyXpMessageWhenLoggedInAndSettingIsrnabled()
{
$user = $this->createCharacterWithUserAttributes([
'show_faction_loyalty_xp_gain' => true
], true)->getCharacter()->user;

$this->battleMessageHandler->handleFactionLoyaltyXp($user, 10, 1, 'npc name');

Event::assertDispatched(function (ServerMessageEvent $event) {
return $event->message = 'For gaining a new fame level (1) for helping: npc name with their tasks you were rewarded with: 10 XP.';
});
}

public function testHandleFactionPointGainWhenNotLoggedIn()
{
$user = $this->createCharacterWithUserAttributes()->getCharacter()->user;

$this->battleMessageHandler->handleFactionPointGain($user, 10, 10, 100);

Event::assertNotDispatched(ServerMessageEvent::class);
}

public function testHandleFactionPointGainWhenLoggedInAndSettingIsDisabled()
{
$user = $this->createCharacterWithUserAttributes([
'show_faction_point_message' => false
], true)->getCharacter()->user;

$this->battleMessageHandler->handleFactionPointGain($user, 10, 10, 100);

Event::assertNotDispatched(ServerMessageEvent::class);
}

public function testHandleFactionPointGainWhenLoggedInAndSettingIsEnabled()
{
$user = $this->createCharacterWithUserAttributes([
'show_faction_point_message' => true
], true)->getCharacter()->user;

$this->battleMessageHandler->handleFactionPointGain($user, 10, 10, 100);

Event::assertDispatched(function (ServerMessageEvent $event) {
return $event->message = 'You gained: 10 Faction Points, which puts you at: 10 points. You need: 90 more points to gain a new level!';
});
}

public function testHandleCurrencyGainMessageAndNotLoggedIn()
{
$user = $this->createCharacterWithUserAttributes([])->getCharacter()->user;

$this->battleMessageHandler->handleCurrencyGainMessage($user, CurrenciesMessageTypes::COPPER_COINS, 10, 10);

Event::assertNotDispatched(ServerMessageEvent::class);
}

public function testHandleCurrencyGainMessageAndLoggedInWithSettingDisabled()
{
$user = $this->createCharacterWithUserAttributes([
'show_copper_coins_per_kill' => false
], true)->getCharacter()->user;

$this->battleMessageHandler->handleCurrencyGainMessage($user, CurrenciesMessageTypes::COPPER_COINS, 10, 10);

Event::assertNotDispatched(ServerMessageEvent::class);
}

public function testHandleCurrencyGainMessageAndNotLoggedInWithSettingEnabled()
{
$user = $this->createCharacterWithUserAttributes([
'show_copper_coins_per_kill' => true
], true)->getCharacter()->user;

$this->battleMessageHandler->handleCurrencyGainMessage($user, CurrenciesMessageTypes::COPPER_COINS, 10, 10);

Event::assertDispatched(function (ServerMessageEvent $event) {
return $event->message = 'You gained: 10 Copper Coins! Your new total is: 10.';
});
}

public function testHandleClassRankMessageWhileLoggedOut()
{
$user = $this->createCharacterWithUserAttributes([])->getCharacter()->user;

$this->battleMessageHandler->handleClassRankMessage($user, ClassRanksMessageTypes::XP_FOR_CLASS_MASTERIES, 'Sample', 10, 10, 'Staves');

Event::assertNotDispatched(ServerMessageEvent::class);
}

public function testHandleClassRankMessageWhileLoggedInAndSettingIsDisabled()
{
$user = $this->createCharacterWithUserAttributes([
'show_xp_for_class_masteries' => false
], true)->getCharacter()->user;

$this->battleMessageHandler->handleClassRankMessage($user, ClassRanksMessageTypes::XP_FOR_CLASS_MASTERIES, 'Sample', 10, 10, 'Staves');

Event::assertNotDispatched(ServerMessageEvent::class);
}

public function testHandleClassRankMessageWhileLoggedinAndSettingIsEnabled()
{
$user = $this->createCharacterWithUserAttributes([
'show_xp_for_class_masteries' => true
], true)->getCharacter()->user;

$this->battleMessageHandler->handleClassRankMessage($user, ClassRanksMessageTypes::XP_FOR_CLASS_MASTERIES, 'Sample', 10, 10, 'Staves');

Event::assertDispatched(function (ServerMessageEvent $event) {
return $event->message = 'Your class: Sample has gained experience in a weapon mastery: Staves of: 10 XP and now has a total of: 10 XP.';
});
}

public function testHandleItemKillCountMessageWhenLoggedOut()
{
$user = $this->createCharacterWithUserAttributes([])->getCharacter()->user;

$this->battleMessageHandler->handleItemKillCountMessage($user, 'item', 'skill name', 1, 100);

Event::assertNotDispatched(ServerMessageEvent::class);
}

public function testHandleItemKillCountMessageWhenLoggedInAndSettingIsDisabled()
{
$user = $this->createCharacterWithUserAttributes([
'show_item_skill_kill_count' => false
], true)->getCharacter()->user;

$this->battleMessageHandler->handleItemKillCountMessage($user, 'item', 'skill name', 1, 100);

Event::assertNotDispatched(ServerMessageEvent::class);
}

public function testHandleItemKillCountMessageWhenLoggedInAndSettingIsEnabled()
{
$user = $this->createCharacterWithUserAttributes([
'show_item_skill_kill_count' => true
], true)->getCharacter()->user;

$this->battleMessageHandler->handleItemKillCountMessage($user, 'item', 'skill name', 1, 100);

Event::assertDispatched(function (ServerMessageEvent $event) {
return $event->message = 'A item skill: skill name Attached to an item: item has gained one point towards its kill count and is now at: 1 points out of: 100. Only: 99 points left to go!';
});
}

public function testHandleSkillMessageWhenLoggedOut()
{
$user = $this->createCharacterWithUserAttributes([])->getCharacter()->user;

$this->battleMessageHandler->handleSkillXpUpdate($user, 'skill', 100);

Event::assertNotDispatched(ServerMessageEvent::class);
}

public function testHandleSkillMessageWhenLoggedInAndSettingDisabled()
{
$user = $this->createCharacterWithUserAttributes([
'show_skill_xp_per_kill' => false
], true)->getCharacter()->user;

$this->battleMessageHandler->handleSkillXpUpdate($user, 'skill', 100);

Event::assertNotDispatched(ServerMessageEvent::class);
}

public function testHandleSkillMessageWhenLoggedInAndSettingEnabled()
{
$user = $this->createCharacterWithUserAttributes([
'show_skill_xp_per_kill' => true
], true)->getCharacter()->user;

$this->battleMessageHandler->handleSkillXpUpdate($user, 'skill', 100);

Event::assertDispatched(function (ServerMessageEvent $event) {
return $event->message = 'Your skill: skill has gained: 100 XP! Killing is the key to gaining skill experience child! kill more!';
});
}

private function createCharacterWithUserAttributes(array $attributes = [], bool $createSesion = false): CharacterFactory
{
$characterFactory = (new CharacterFactory())->setAttributesOnUserForCreation($attributes)->createBaseCharacter()->givePlayerLocation();
Expand Down

0 comments on commit 3af6b83

Please sign in to comment.