Duel Algorithms - Sylphian-Network/Sylphian-UserPets GitHub Wiki
The UserPets add-on includes multiple duel algorithms by default.
| Key | Label | Description | Class |
|---|---|---|---|
| default | Default (50/50) | Simple 50/50 chance, ignores all pet stats. | DefaultAlgorithm |
| care | Care-based | Winner more likely the pet with higher happiness and lower hunger/sleepiness stats. | CareBasedAlgorithm |
| exp | Exp-based | Weighted using level, experience, and care stats; higher experience increases chances. | ExpBasedAlgorithm |
| weight | Weighted | Weighted probability based on level and care stats. Higher-level pets with better care stats win more often. | WeightedAlgorithm |
If none of these are to your liking, or you would like to write your algorithm, heres how you do so.
Creating a duel algorithm inside Sylphian/UserPets
If you want to add a custom algorithm directly inside the add-on:
Step 1: Create your PHP class under:
src/addons/Sylphian/UserPets/Duel/Algorithms/
Step 2: Register it in AlgorithmRegistry.php:
$algorithms = [
new DefaultAlgorithm(),
new CareBasedAlgorithm(),
new ExpBasedAlgorithm(),
new WeightedAlgorithm(),
// Add your custom algorithm here
new NewAlgorithm(),
];
Creating a duel algorithm using a separate add-on
This is super easy to do, all you need to have is another add-on you can add an event listener to, and an algorithm.
Step 1: Create a algorithm
Basic algorithm
This here is an example of a basic algorithm. All it does is take $petB and determines it as the winner.
<?php
namespace Vendor\Addon\Duel\Algorithms;
use Sylphian\UserPets\Duel\Algorithms\DuelAlgorithmInterface;
class CheatAlgorithm implements DuelAlgorithmInterface
{
public function getKey(): string
{
return 'cheat';
}
public function getLabel(): string
{
return 'Cheat Algorithm';
}
public function calculateWinner(array $petA, array $petB): array
{
return $petB;
}
}
More advanced algorithm
Here you can add your more complex algorithm logic, e.g., based on pet stats:
<?php
namespace Vendor\Addon\Duel\Algorithms;
use Sylphian\UserPets\Duel\Algorithms\DuelAlgorithmInterface;
class StatBasedAlgorithm implements DuelAlgorithmInterface
{
public function getKey(): string
{
return 'stat_based';
}
public function getLabel(): string
{
return 'Stat-Based Algorithm';
}
public function calculateWinner(array $petA, array $petB): array
{
// Example logic: winner is pet with higher level + happiness score
$scoreA = ($petA['level'] ?? 0) + ($petA['happiness'] ?? 0);
$scoreB = ($petB['level'] ?? 0) + ($petB['happiness'] ?? 0);
return ($scoreA >= $scoreB) ? $petA : $petB;
}
}
Pet stats
$petX['level']$petX['experience']$petX['happiness']$petX['hunger']$petX['sleepiness']
Step 2: Create a Listener Class
This class is what your code event listener will use. The class would look something like this:
<?php
namespace Vendor\Addon\Listener;
use Vendor\Addon\Duel\Algorithms\CheatAlgorithm;
class Listener
{
public static function algorithms(array &$algorithms): void
{
$algorithms[] = new CheatAlgorithm();
}
}
Step 3: Create the code event listener
This is done in the admin control panel.
The areas that are required are:
- Listen to event:
sylphian_userpets_duel_algorithms - Execute callback:
Vendor\Addon\Listener\Listener::algorithms - Add-on:
Vendor/Addon