-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from lanedirt/159-refactor-message-system-to-…
…support-dynamic-translations Refactor message system to support dynamic translations
- Loading branch information
Showing
26 changed files
with
744 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.