Skip to content

Commit

Permalink
Added a new type of kingdom log for those who loose their kingdom to …
Browse files Browse the repository at this point in the history
…the old man. Various fixes around kingdoms.
  • Loading branch information
AdamKyle committed Nov 20, 2023
1 parent da1fb8e commit 6ee7aae
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 15 deletions.
28 changes: 17 additions & 11 deletions app/Flare/Models/KingdomLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class KingdomLog extends Model {
'morale_loss',
'published',
'opened',
'additional_details',
'created_at',
];

Expand All @@ -40,17 +41,18 @@ class KingdomLog extends Model {
* @var array
*/
protected $casts = [
'units_sent' => 'array',
'units_survived' => 'array',
'old_buildings' => 'array',
'new_buildings' => 'array',
'old_units' => 'array',
'new_units' => 'array',
'published' => 'boolean',
'opened' => 'boolean',
'item_damage' => 'float',
'morale_loss' => 'float',
'status' => 'integer',
'units_sent' => 'array',
'units_survived' => 'array',
'old_buildings' => 'array',
'new_buildings' => 'array',
'old_units' => 'array',
'new_units' => 'array',
'additional_details' => 'array',
'published' => 'boolean',
'opened' => 'boolean',
'item_damage' => 'float',
'morale_loss' => 'float',
'status' => 'integer',
];

protected $appends = [
Expand Down Expand Up @@ -94,6 +96,10 @@ public function setNewUnitsUnitsAttribute($value) {
$this->attributes['new_units'] = json_encode($value);
}

public function setAdditionalDetailsAttribute($value) {
$this->attributes['additional_details'] = json_encode($value);
}

protected static function newFactory() {
return new KingdomLogFactory();
}
Expand Down
9 changes: 9 additions & 0 deletions app/Flare/Transformers/KingdomAttackLogsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function transform(KingdomLog $log): array {
'opened' => $log->opened,
'created_at' => $log->created_at->setTimezone(env('TIME_ZONE'))->format('Y-m-d H:m:s'),
'took_kingdom' => (new KingdomLogStatusValue($log->status))->tookKingdom(),
'additional_details' => $log->additional_details,
];
}

Expand Down Expand Up @@ -102,6 +103,10 @@ protected function isMyLog(KingdomLog $log): bool {
$character = $user->character;
}

if (is_null($log->to_kingdom_id)) {
return true;
}

$attackedKingdom = Kingdom::find($log->to_kingdom_id);


Expand Down Expand Up @@ -169,6 +174,10 @@ protected function getStatusName(int $status): string {
return 'Kingdom was taken';
}

if ($logStatus->overPopulated()) {
return 'Kingdom was overpopulated';
}

return 'Error. Unknown status';
}
}
13 changes: 12 additions & 1 deletion app/Flare/Values/KingdomLogStatusValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class KingdomLogStatusValue {
const KINGDOM_ATTACKED = 4;
const UNITS_RETURNING = 5;
const BOMBS_DROPPED = 6;
const OVER_POPULATED = 7;

/**
* @var string[] $values
Expand All @@ -29,7 +30,8 @@ class KingdomLogStatusValue {
self::LOST_KINGDOM => 3,
self::KINGDOM_ATTACKED => 4,
self::UNITS_RETURNING => 5,
self::BOMBS_DROPPED => 6
self::BOMBS_DROPPED => 6,
self::OVER_POPULATED => 7,
];

/**
Expand Down Expand Up @@ -110,4 +112,13 @@ public function unitsReturning(): bool {
public function bombsDropped(): bool {
return $this->value === self::BOMBS_DROPPED;
}

/**
* Were we overpopulated?
*
* @return bool
*/
public function overPopulated(): bool {
return $this->value === self::OVER_POPULATED;
}
}
3 changes: 2 additions & 1 deletion app/Game/Core/Traits/KingdomCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Game\Core\Traits;

use App\Flare\Values\MapNameValue;
use Cache;
use Illuminate\Database\Eloquent\Collection;
use App\Flare\Models\GameMap;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function rebuildCharacterKingdomCache(Character $character) {

$kingdoms = Kingdom::select('id', 'x_position', 'y_position', 'color', 'name')
->where('character_id', $character->id)
->where('game_map_id', $gameMap)
->where('game_map_id', $gameMap->id)
->get();

if ($kingdoms->isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion app/Game/Kingdoms/Handlers/TooMuchPopulationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ protected function destroyPlayerKingdom(): void {

$this->destroyKingdom($this->kingdom, $character);

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

event(new UpdateGlobalMap($character));
event(new AddKingdomToMap($character));
Expand Down
9 changes: 8 additions & 1 deletion app/Game/Kingdoms/Handlers/Traits/DestroyKingdom.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

use App\Flare\Models\Character;
use App\Flare\Models\Kingdom;
use App\Game\Core\Traits\KingdomCache;
use App\Game\Kingdoms\Events\AddKingdomToMap;
use App\Game\Kingdoms\Events\UpdateGlobalMap;
use App\Game\Kingdoms\Events\UpdateNPCKingdoms;
use App\Game\Messages\Events\GlobalMessageEvent;

Trait DestroyKingdom {

use KingdomCache;

/**
* Destroy a kingdom.
*
Expand Down Expand Up @@ -41,13 +44,17 @@ public function destroyKingdom(Kingdom $kingdom, ?Character $character = null):
));

if (!is_null($character)) {
$character = $character->refresh;
$character = $character->refresh();

$this->rebuildCharacterKingdomCache($character);

event(new UpdateGlobalMap($character));
event(new AddKingdomToMap($character));
}

if (is_null($character)) {
$this->rebuildCharacterKingdomCache($character);

event(new UpdateNPCKingdoms($gameMap));
}
}
Expand Down
40 changes: 40 additions & 0 deletions app/Game/Kingdoms/Service/KingdomUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Game\Kingdoms\Service;


use App\Flare\Models\KingdomLog;
use App\Flare\Values\KingdomLogStatusValue;
use App\Game\Kingdoms\Traits\CalculateMorale;
use Illuminate\Support\Facades\Mail;
use Facades\App\Flare\Values\UserOnlineValue;
Expand Down Expand Up @@ -121,6 +123,17 @@ public function updateKingdom(): void {
return;
}

$character = $this->kingdom->character;

$additionalLogData = [
'kingdom_data' => [
'x' => $this->kingdom->x_position,
'y' => $this->kingdom->y_position,
'name' => $this->kingdom->name,
'game_map_name' => $this->kingdom->gameMap->name,
]
];

if ($this->shouldGiveKingdomToNpc()) {
$this->giveKingdomsToNpcHandler->giveKingdomToNPC($this->kingdom);

Expand All @@ -138,11 +151,17 @@ public function updateKingdom(): void {
}

if ($this->isTheOldManAngry()) {

$this->tooMuchPopulationHandler->setKingdom($this->kingdom)->handleAngryNPC();

$kingdom = $this->tooMuchPopulationHandler->getKingdom();

if (is_null($kingdom)) {

$additionalLogData['kingdom_data']['reason'] = 'Your kingdom was over populated and you could not afford the 10,000 Gold per additional person over your max population.';

$this->createKingdomLog($character, $additionalLogData, KingdomLogStatusValue::OVER_POPULATED);

return;
}

Expand All @@ -168,6 +187,27 @@ public function updateKingdom(): void {
$this->updateKingdomProtectedUntil();
}

protected function createKingdomLog(Character $character, array $additionalData, int $status) {

$log = [
'to_kingdom_id' => null,
'from_kingdom_id' => null,
'status' => $status,
'published' => true,
'additional_details' => $additionalData,
'character_id' => $character->id,
'old_buildings' => [],
'new_buildings' => [],
'old_units' => [],
'new_units' => [],
'morale_loss' => 1.0,
];

KingdomLog::create($log);

$this->updateKingdom->updateKingdomLogs($character);
}

/**
* Update the protection status.
*
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('kingdom_logs', function (Blueprint $table) {
$table->json('additional_details')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('kingdom_logs', function (Blueprint $table) {
//
});
}
};
3 changes: 3 additions & 0 deletions resources/js/game/lib/game/kingdoms/kingdom-log-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ export default interface KingdomLogDetails {

took_kingdom: boolean;

// This can be anything that we want to send back.
additional_details: any;

}
41 changes: 41 additions & 0 deletions resources/js/game/sections/kingdoms/kingdom-log-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import clsx from "clsx";
import { formatNumber } from "../../lib/game/format-number";
import KingdomLogProps from "../../lib/game/kingdoms/types/kingdom-log-props";
import {BuildingLogDetails, UnitLogDetails} from "../../lib/game/kingdoms/kingdom-log-details";
import InfoAlert from "../../components/ui/alerts/simple-alerts/info-alert";

export default class KingdomLogDetails extends React.Component<KingdomLogProps, { }> {

Expand Down Expand Up @@ -143,6 +144,46 @@ export default class KingdomLogDetails extends React.Component<KingdomLogProps,
}

render() {
if (this.props.log.status === 'Kingdom was overpopulated') {
return (
<BasicCard>
<div className='text-right cursor-pointer text-red-500'>
<button onClick={this.props.close_details}><i className="fas fa-minus-circle"></i></button>
</div>
<div className='my-4'>
<h3 className='mb-4'>{this.props.log.status}</h3>
<p className='my-4 text-red-600 dark:text-red-500'>
You kingdom was overpopulated. The Old Man took it and demolished it.
</p>
<dl className='my-4'>
<dt>Kingdom Name</dt>
<dd>{this.props.log.additional_details.kingdom_data.name}</dd>
<dt>Kingdom Location</dt>
<dd>(X/Y) {this.props.log.additional_details.kingdom_data.x} / {this.props.log.additional_details.kingdom_data.y}</dd>
<dt>On Map</dt>
<dd>{this.props.log.additional_details.kingdom_data.game_map_name}</dd>
<dt>Reason</dt>
<dd>{this.props.log.additional_details.kingdom_data.reason}</dd>
</dl>
<InfoAlert additional_css={'my-4'}>
<h4>Over Population</h4>
<p className='my-4'>
Kingdoms can purchase additional population for recruiting large amount of units,
but one should becarfeul because if you have more then your max at the hourly reset
The Old Man will stomp around. he will attempt to:
</p>
<ul className='my-4 list-disc'>
<li className='ml-4'>Take the cost our of your gold bars</li>
<li className='ml-4'>If you have none, he will take it from your treasury.</li>
<li className='ml-4'>If you have none, he will take it from your own gold.</li>
<li className='ml-4'>If you have none, he will destroy the kingdom.</li>
</ul>
</InfoAlert>
</div>
</BasicCard>
)
}

return (
<BasicCard>
<div className='text-right cursor-pointer text-red-500'>
Expand Down

0 comments on commit 6ee7aae

Please sign in to comment.