Skip to content

Commit

Permalink
Merge pull request #163 from lanedirt/159-refactor-message-system-to-…
Browse files Browse the repository at this point in the history
…support-dynamic-translations

Refactor message system to support dynamic translations
  • Loading branch information
lanedirt authored May 17, 2024
2 parents b247311 + 53e40ce commit d5fdcfc
Show file tree
Hide file tree
Showing 26 changed files with 744 additions and 156 deletions.
82 changes: 82 additions & 0 deletions app/Factories/GameMessageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace OGame\Factories;

use Illuminate\Contracts\Container\BindingResolutionException;
use OGame\GameMessages\Abstracts\GameMessage;
use OGame\GameMessages\ColonyEstablished;
use OGame\GameMessages\FleetDeployment;
use OGame\GameMessages\FleetDeploymentWithResources;
use OGame\GameMessages\ReturnOfFleet;
use OGame\GameMessages\ReturnOfFleetWithResources;
use OGame\GameMessages\TransportArrived;
use OGame\GameMessages\TransportReceived;
use OGame\GameMessages\WelcomeMessage;

class GameMessageFactory
{
/**
* @var array<string ,string>
*/
private static array $gameMessageClasses = [
'welcome_message' => WelcomeMessage::class,
'return_of_fleet_with_resources' => ReturnOfFleetWithResources::class,
'return_of_fleet' => ReturnOfFleet::class,
'transport_arrived' => TransportArrived::class,
'transport_received' => TransportReceived::class,
'colony_established' => ColonyEstablished::class,
'fleet_deployment' => FleetDeployment::class,
'fleet_deployment_with_resources' => FleetDeploymentWithResources::class,
];

/**
* @return array<GameMessage>
* @throws BindingResolutionException
*/
public static function getAllGameMessages(): array
{
$gameMessages = [];
foreach (self::$gameMessageClasses as $id => $class) {
$gameMessages[$id] = app()->make($class);
}
return $gameMessages;
}

/**
* @param string $key
*
* @return GameMessage
* @throws BindingResolutionException
*/
public static function createGameMessage(string $key): GameMessage
{
if (!isset(self::$gameMessageClasses[$key])) {
throw new BindingResolutionException("GameMessage with key $key not found.");
}

return app()->make(self::$gameMessageClasses[$key]);
}

/**
* Get all class keys that have a certain tab and optionally subtab. This is for knowing which messages to display
* in the game messages page in what tab/subtab.
*
* @param string $tab
* @param string|null $subtab
* @return array<int, string>
* @throws BindingResolutionException
*/
public static function GetGameMessageKeysByTab(string $tab, ?string $subtab = null): array
{
$matchingKeys = [];

foreach (self::$gameMessageClasses as $id => $class) {
$gameMessage = app()->make($class);
if ($gameMessage->getTab() === $tab && ($subtab === null || $gameMessage->getSubtab() === $subtab)) {
$matchingKeys[] = $id;
}
}

return $matchingKeys;
}
}
126 changes: 126 additions & 0 deletions app/GameMessages/Abstracts/GameMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace OGame\GameMessages\Abstracts;

use OGame\Facades\AppUtil;

abstract class GameMessage
{
/**
* @var string The key of the message. This is used to identify the message in the language files.
*/
protected string $key;

/**
* @var array<string> The params that the message requires to be filled.
*/
protected array $params;

/**
* @var string The tab of the message. This is used to group messages in the game messages page.
*/
protected string $tab;

/**
* @var string The subtab of the message. This is used to group messages in the game messages page.
*/
protected string $subtab;

public function __construct()
{
$this->initialize();
}

/**
* Initialize the message with the key, params, tab and subtab.
*
* @return void
*/
abstract protected function initialize(): void;

/**
* Get the key of the message.
*
* @return string
*/
public function getKey(): string
{
return $this->key;
}

/**
* Returns the static sender of the message.
*
* @return string
*/
public function getFrom(): string
{
return __('t_messages.' . $this->key . '.from');
}

/**
* Returns the subject of the message.
*
* @return string
*/
public function getSubject(): string
{
return __('t_messages.' . $this->key. '.subject');
}

/**
* Get the body of the message filled with provided params.
*
* @param array<string,string> $params
* @return string
*/
public function getBody(array $params): string
{
// Check if all the params are provided by checking all individual param names.
foreach ($this->params as $param) {
if (!array_key_exists($param, $params)) {
// Replace param in message with "?undefined?" to indicate that the param is missing.
$params[$param] = '?undefined?';
}
}

// Certain reserved params such as resources should be formatted with number_format.
foreach ($params as $key => $value) {
if (in_array($key, ['metal', 'crystal', 'deuterium'])) {
$params[$key] = AppUtil::formatNumber((int)$value);
}
}

return __('t_messages.' . $this->key . '.body', $params);
}

/**
* Get the params that the message requires to be filled.
*
* @return array<int, string>
*/
public function getParams(): array
{
return $this->params;
}

/**
* Get the tab of the message. This is used to group messages in the game messages page.
*
* @return string
*/
public function getTab(): string
{
return $this->tab;
}

/**
* Get the subtab of the message. This is used to group messages in the game messages page.
*
* @return string
*/
public function getSubtab(): string
{
return $this->subtab;
}
}
16 changes: 16 additions & 0 deletions app/GameMessages/ColonyEstablished.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace OGame\GameMessages;

use OGame\GameMessages\Abstracts\GameMessage;

class ColonyEstablished extends GameMessage
{
protected function initialize(): void
{
$this->key = 'colony_established';
$this->params = ['coordinates'];
$this->tab = 'economy';
$this->subtab = 'economy';
}
}
16 changes: 16 additions & 0 deletions app/GameMessages/FleetDeployment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace OGame\GameMessages;

use OGame\GameMessages\Abstracts\GameMessage;

class FleetDeployment extends GameMessage
{
protected function initialize(): void
{
$this->key = 'fleet_deployment';
$this->params = ['from', 'to'];
$this->tab = 'fleets';
$this->subtab = 'other';
}
}
16 changes: 16 additions & 0 deletions app/GameMessages/FleetDeploymentWithResources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace OGame\GameMessages;

use OGame\GameMessages\Abstracts\GameMessage;

class FleetDeploymentWithResources extends GameMessage
{
protected function initialize(): void
{
$this->key = 'fleet_deployment_with_resources';
$this->params = ['from', 'to', 'metal', 'crystal', 'deuterium'];
$this->tab = 'fleets';
$this->subtab = 'other';
}
}
16 changes: 16 additions & 0 deletions app/GameMessages/ReturnOfFleet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace OGame\GameMessages;

use OGame\GameMessages\Abstracts\GameMessage;

class ReturnOfFleet extends GameMessage
{
protected function initialize(): void
{
$this->key = 'return_of_fleet';
$this->params = ['from', 'to'];
$this->tab = 'fleets';
$this->subtab = 'other';
}
}
16 changes: 16 additions & 0 deletions app/GameMessages/ReturnOfFleetWithResources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace OGame\GameMessages;

use OGame\GameMessages\Abstracts\GameMessage;

class ReturnOfFleetWithResources extends GameMessage
{
protected function initialize(): void
{
$this->key = 'return_of_fleet_with_resources';
$this->params = ['from', 'to', 'metal', 'crystal', 'deuterium'];
$this->tab = 'fleets';
$this->subtab = 'other';
}
}
16 changes: 16 additions & 0 deletions app/GameMessages/TransportArrived.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace OGame\GameMessages;

use OGame\GameMessages\Abstracts\GameMessage;

class TransportArrived extends GameMessage
{
protected function initialize(): void
{
$this->key = 'transport_arrived';
$this->params = ['from', 'to', 'metal', 'crystal', 'deuterium'];
$this->tab = 'fleets';
$this->subtab = 'transport';
}
}
16 changes: 16 additions & 0 deletions app/GameMessages/TransportReceived.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace OGame\GameMessages;

use OGame\GameMessages\Abstracts\GameMessage;

class TransportReceived extends GameMessage
{
protected function initialize(): void
{
$this->key = 'transport_received';
$this->params = ['from', 'to', 'metal', 'crystal', 'deuterium'];
$this->tab = 'fleets';
$this->subtab = 'other';
}
}
16 changes: 16 additions & 0 deletions app/GameMessages/WelcomeMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace OGame\GameMessages;

use OGame\GameMessages\Abstracts\GameMessage;

class WelcomeMessage extends GameMessage
{
protected function initialize(): void
{
$this->key = 'welcome_message';
$this->params = ['player'];
$this->tab = 'universe';
$this->subtab = 'universe';
}
}
21 changes: 12 additions & 9 deletions app/GameMissions/Abstracts/GameMission.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Carbon;
use OGame\Factories\PlanetServiceFactory;
use OGame\GameMessages\ReturnOfFleet;
use OGame\GameMessages\ReturnOfFleetWithResources;
use OGame\GameMissions\Models\MissionPossibleStatus;
use OGame\GameObjects\Models\UnitCollection;
use OGame\Models\FleetMission;
Expand Down Expand Up @@ -310,21 +312,22 @@ protected function sendFleetReturnMessage(FleetMission $mission, PlayerService $
}

if ($return_resources->sum() > 0) {
$body = __('t_messages.return_of_fleet', [
$params = [
'from' => $from,
'to' => '[planet]' . $mission->planet_id_to . '[/planet]',
'metal' => $mission->metal,
'crystal' => $mission->crystal,
'deuterium' => $mission->deuterium,
]);
'metal' => (string)$mission->metal,
'crystal' => (string)$mission->crystal,
'deuterium' => (string)$mission->deuterium,
];
$this->messageService->sendSystemMessageToPlayer($targetPlayer, ReturnOfFleetWithResources::class, $params);
} else {
$body = __('t_messages.return_of_fleet_no_goods', [
$params = [
'from' => $from,
'to' => '[planet]' . $mission->planet_id_to . '[/planet]',
]);
}
];
$this->messageService->sendSystemMessageToPlayer($targetPlayer, ReturnOfFleet::class, $params);

$this->messageService->sendMessageToPlayer($targetPlayer, 'Return of a fleet', $body, 'return_of_fleet');
}
}

/**
Expand Down
Loading

0 comments on commit d5fdcfc

Please sign in to comment.