feat: conquest: coins minted at the academy, noble slots, loyalty that noblemen lower, villages that change hands
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
f6c77163aa
commit
973025ae14
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Actions;
|
||||||
|
|
||||||
|
use App\Engine\Nobility;
|
||||||
|
use App\Engine\Simulation;
|
||||||
|
use App\Game\GameRuleViolation;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Models\Village;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
|
|
||||||
|
/** Mint coins at the village's academy; they belong to the player and pay for noble slots. */
|
||||||
|
final class MintCoins extends VillageAction
|
||||||
|
{
|
||||||
|
public function __invoke(User $user, int $villageId, int $count): Village
|
||||||
|
{
|
||||||
|
if ($count < 1) {
|
||||||
|
throw new GameRuleViolation('count', 'Mint at least one coin.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->mutate($user, $villageId, function (Simulation $sim, Village $village, CarbonImmutable $now) use ($count) {
|
||||||
|
if (! $village->buildings->contains('type', 'academy')) {
|
||||||
|
throw new GameRuleViolation('requires', 'Coins are minted at an academy.');
|
||||||
|
}
|
||||||
|
$sim->pay(Nobility::of($village)->rules()->coinCost($count));
|
||||||
|
$village->player->increment('coins', $count);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -85,7 +85,14 @@ final class Military
|
||||||
));
|
));
|
||||||
$byVillagers = (int) floor($this->sim->workforce()->undrafted() / $this->units->upkeep($unit));
|
$byVillagers = (int) floor($this->sim->workforce()->undrafted() / $this->units->upkeep($unit));
|
||||||
|
|
||||||
return max(0, min($byResources, $byVillagers));
|
$bySlots = $this->isNoble($unit) && $this->village->player ? Nobility::of($this->village)->freeSlots($this->village->player) : PHP_INT_MAX;
|
||||||
|
|
||||||
|
return max(0, min($byResources, $byVillagers, $bySlots));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isNoble(string $unit): bool
|
||||||
|
{
|
||||||
|
return (bool) $this->sim->config()->get("units.$unit.noble", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Seconds per unit at this building instance, from its level and staffing. */
|
/** Seconds per unit at this building instance, from its level and staffing. */
|
||||||
|
|
@ -108,6 +115,9 @@ final class Military
|
||||||
if ($this->missingRequirements($unit)) {
|
if ($this->missingRequirements($unit)) {
|
||||||
throw new GameRuleViolation('requires', 'Requirements not met.');
|
throw new GameRuleViolation('requires', 'Requirements not met.');
|
||||||
}
|
}
|
||||||
|
if ($this->isNoble($unit) && $count > Nobility::of($this->village)->freeSlots($this->village->player)) {
|
||||||
|
throw new GameRuleViolation('coins', 'Mint more coins for another nobleman.');
|
||||||
|
}
|
||||||
if ($count * $this->units->upkeep($unit) > $this->sim->workforce()->undrafted()) {
|
if ($count * $this->units->upkeep($unit) > $this->sim->workforce()->undrafted()) {
|
||||||
throw new GameRuleViolation('villagers', 'Not enough villagers to train that many.');
|
throw new GameRuleViolation('villagers', 'Not enough villagers to train that many.');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,115 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Engine;
|
||||||
|
|
||||||
|
use App\Game\Military\Conquest;
|
||||||
|
use App\Models\GameEvent;
|
||||||
|
use App\Models\Movement;
|
||||||
|
use App\Models\Player;
|
||||||
|
use App\Models\RecruitOrder;
|
||||||
|
use App\Models\UnitStack;
|
||||||
|
use App\Models\Village;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
|
|
||||||
|
/** Coins, noble slots, loyalty and handing a conquered village over (combat.conquest). */
|
||||||
|
final class Nobility
|
||||||
|
{
|
||||||
|
private function __construct(private readonly Conquest $rules) {}
|
||||||
|
|
||||||
|
public static function of(Village $village): self
|
||||||
|
{
|
||||||
|
return new self(new Conquest($village->world->gameConfig()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rules(): Conquest
|
||||||
|
{
|
||||||
|
return $this->rules;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The village's loyalty at $at, grown back since it last changed. */
|
||||||
|
public function loyalty(Village $village, CarbonImmutable $at): float
|
||||||
|
{
|
||||||
|
if (! $village->loyalty_at) {
|
||||||
|
return min((float) $village->loyalty, $this->rules->maxLoyalty());
|
||||||
|
}
|
||||||
|
$hours = ($at->getTimestampMs() - $village->loyalty_at->getTimestampMs()) / 3_600_000;
|
||||||
|
|
||||||
|
return $this->rules->loyalty((float) $village->loyalty, $hours);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Noble slots in use: noblemen standing anywhere, under way and in training, plus
|
||||||
|
* every village beyond the first.
|
||||||
|
*/
|
||||||
|
public function slotsUsed(Player $player): int
|
||||||
|
{
|
||||||
|
$nobles = $this->rules->nobleUnits();
|
||||||
|
if (! $nobles) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
$villages = Village::where('player_id', $player->id)->pluck('id');
|
||||||
|
$standing = (int) UnitStack::whereIn('home_village_id', $villages)->whereIn('unit', $nobles)->sum('count');
|
||||||
|
$moving = Movement::where('player_id', $player->id)->pluck('units')
|
||||||
|
->sum(fn (array $units) => array_sum(array_intersect_key($units, array_flip($nobles))));
|
||||||
|
$training = RecruitOrder::whereIn('village_id', $villages)->whereIn('unit', $nobles)->get()
|
||||||
|
->sum(fn (RecruitOrder $o) => $o->count - $o->done);
|
||||||
|
|
||||||
|
return $standing + $moving + $training + max(0, $villages->count() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Noble slots the player's coins pay for and doesn't use yet. */
|
||||||
|
public function freeSlots(Player $player): int
|
||||||
|
{
|
||||||
|
return max(0, Conquest::slots((int) $player->coins) - $this->slotsUsed($player));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The village goes to the attacker. Its queues end without refunds, the old owner's
|
||||||
|
* groups from or to it are lost, and so is its support standing elsewhere.
|
||||||
|
*
|
||||||
|
* @return list<int> villages whose owners should hear about it
|
||||||
|
*/
|
||||||
|
public function handOver(Village $village, int $playerId, CarbonImmutable $at): array
|
||||||
|
{
|
||||||
|
$touched = [];
|
||||||
|
|
||||||
|
foreach ($village->orders as $order) {
|
||||||
|
GameEvent::where('type', GameEvent::BUILD_ORDER_FINISHED)->where('subject_id', $order->id)->whereNull('processed_at')->delete();
|
||||||
|
$order->delete();
|
||||||
|
}
|
||||||
|
$village->setRelation('orders', collect());
|
||||||
|
foreach ($village->recruits as $order) {
|
||||||
|
GameEvent::where('type', GameEvent::RECRUIT_FINISHED)->where('subject_id', $order->id)->whereNull('processed_at')->delete();
|
||||||
|
$order->delete();
|
||||||
|
}
|
||||||
|
$village->setRelation('recruits', collect());
|
||||||
|
GameEvent::where('type', GameEvent::LEVY_ENDS)->where('village_id', $village->id)->whereNull('processed_at')->delete();
|
||||||
|
$village->militia = 0;
|
||||||
|
$village->levy_until = null;
|
||||||
|
$village->starving_since = null;
|
||||||
|
|
||||||
|
// The old owner's commands from here and troops on their way home here.
|
||||||
|
$lost = Movement::where(fn ($q) => $q->where('origin_id', $village->id)->whereIn('type', [Movement::ATTACK, Movement::SUPPORT]))
|
||||||
|
->orWhere(fn ($q) => $q->where('target_id', $village->id)->where('type', Movement::RETURN))
|
||||||
|
->get();
|
||||||
|
foreach ($lost as $movement) {
|
||||||
|
GameEvent::whereIn('type', [GameEvent::MOVEMENT_ARRIVES, GameEvent::MOVEMENT_SPOTTED])
|
||||||
|
->where('subject_id', $movement->id)->whereNull('processed_at')->delete();
|
||||||
|
$touched[] = $movement->target_id;
|
||||||
|
$movement->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Its support standing in other villages.
|
||||||
|
$away = UnitStack::where('home_village_id', $village->id)->where('village_id', '!=', $village->id)->get();
|
||||||
|
foreach ($away as $stack) {
|
||||||
|
$touched[] = $stack->village_id;
|
||||||
|
$stack->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
$village->player_id = $playerId;
|
||||||
|
$village->loyalty = $this->rules->afterConquest();
|
||||||
|
$village->loyalty_at = $at;
|
||||||
|
|
||||||
|
return array_values(array_unique($touched));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -144,6 +144,10 @@ final class Rally
|
||||||
|
|
||||||
$this->take($units);
|
$this->take($units);
|
||||||
$plan = $this->plan($target, $units, $mode, $now);
|
$plan = $this->plan($target, $units, $mode, $now);
|
||||||
|
$conquest = Nobility::of($this->village)->rules();
|
||||||
|
if ($conquest->maxDistance() > 0 && array_intersect_key($units, array_flip($conquest->nobleUnits())) && $plan['distance'] > $conquest->maxDistance()) {
|
||||||
|
throw new GameRuleViolation('too_far', 'Noblemen don\'t march that far.');
|
||||||
|
}
|
||||||
$warning = $this->travel->warningSeconds($plan['seconds'], $plan['distance'], $mode, self::of($target)->detection());
|
$warning = $this->travel->warningSeconds($plan['seconds'], $plan['distance'], $mode, self::of($target)->detection());
|
||||||
$movement = Movement::create([
|
$movement = Movement::create([
|
||||||
'world_id' => $this->village->world_id,
|
'world_id' => $this->village->world_id,
|
||||||
|
|
@ -323,9 +327,27 @@ final class Rally
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Surviving noblemen of a won attack lower loyalty; at 0 the village changes hands.
|
||||||
|
$loyalty = null;
|
||||||
|
$nobility = Nobility::of($this->village);
|
||||||
|
$nobles = array_sum(array_intersect_key($survivors, array_flip($nobility->rules()->nobleUnits())));
|
||||||
|
if ($battle?->attackerWon && $nobles > 0) {
|
||||||
|
$before = $nobility->loyalty($this->village, $at);
|
||||||
|
$after = $before;
|
||||||
|
for ($i = 0; $i < $nobles; $i++) {
|
||||||
|
$after -= $nobility->rules()->drop(mt_rand() / (mt_getrandmax() + 1));
|
||||||
|
}
|
||||||
|
$conquered = $after <= 0;
|
||||||
|
$this->village->loyalty = $conquered ? $nobility->rules()->afterConquest() : $after;
|
||||||
|
$this->village->loyalty_at = $at;
|
||||||
|
$loyalty = ['before' => (int) floor($before), 'after' => $conquered ? 0 : (int) floor($after), 'conquered' => $conquered];
|
||||||
|
}
|
||||||
|
$conquered = $loyalty['conquered'] ?? false;
|
||||||
|
|
||||||
$loot = ['wood' => 0, 'clay' => 0, 'iron' => 0];
|
$loot = ['wood' => 0, 'clay' => 0, 'iron' => 0];
|
||||||
$capacity = 0;
|
$capacity = 0;
|
||||||
if ($battle?->attackerWon) {
|
// Conquerors stay, so they carry nothing off.
|
||||||
|
if ($battle?->attackerWon && ! $conquered) {
|
||||||
$capacity = (new LootCalculator($config))->capacity($survivors);
|
$capacity = (new LootCalculator($config))->capacity($survivors);
|
||||||
$stock = ['wood' => $this->village->wood, 'clay' => $this->village->clay, 'iron' => $this->village->iron];
|
$stock = ['wood' => $this->village->wood, 'clay' => $this->village->clay, 'iron' => $this->village->iron];
|
||||||
$loot = LootCalculator::take($stock, $this->hidden(), $capacity);
|
$loot = LootCalculator::take($stock, $this->hidden(), $capacity);
|
||||||
|
|
@ -350,6 +372,7 @@ final class Rally
|
||||||
'building' => $hit,
|
'building' => $hit,
|
||||||
'loot' => $loot,
|
'loot' => $loot,
|
||||||
'capacity' => $capacity,
|
'capacity' => $capacity,
|
||||||
|
'loyalty' => $loyalty,
|
||||||
// What scouts that got through saw, after the battle.
|
// What scouts that got through saw, after the battle.
|
||||||
'scouting' => $scouts ? [
|
'scouting' => $scouts ? [
|
||||||
'sent' => array_sum($scouts),
|
'sent' => array_sum($scouts),
|
||||||
|
|
@ -372,9 +395,36 @@ final class Rally
|
||||||
}
|
}
|
||||||
Push::queue($movement->origin_id);
|
Push::queue($movement->origin_id);
|
||||||
|
|
||||||
|
if ($conquered) {
|
||||||
|
$this->conquer($movement, $survivors, $at);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
$survivors ? $this->turnBack($movement, $survivors, $loot, $at) : $movement->delete();
|
$survivors ? $this->turnBack($movement, $survivors, $loot, $at) : $movement->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attacker takes the village: one nobleman settles, the other survivors stay as
|
||||||
|
* support from the village they came from.
|
||||||
|
*
|
||||||
|
* @param array<string, int> $survivors
|
||||||
|
*/
|
||||||
|
private function conquer(Movement $movement, array $survivors, CarbonImmutable $at): void
|
||||||
|
{
|
||||||
|
$nobility = Nobility::of($this->village);
|
||||||
|
$noble = collect($nobility->rules()->nobleUnits())->first(fn (string $u) => ($survivors[$u] ?? 0) > 0);
|
||||||
|
$survivors[$noble]--;
|
||||||
|
$oldHome = $this->village->troops->filter(fn (UnitStack $s) => $s->home_village_id === $this->village->id);
|
||||||
|
foreach ($oldHome as $stack) {
|
||||||
|
$stack->delete();
|
||||||
|
}
|
||||||
|
$this->village->setRelation('troops', $this->village->troops->diff($oldHome)->values());
|
||||||
|
Push::queue(...$nobility->handOver($this->village, $movement->player_id, $at));
|
||||||
|
$this->put($movement->origin_id, array_filter($survivors));
|
||||||
|
$movement->delete();
|
||||||
|
Push::queue($this->village->id);
|
||||||
|
}
|
||||||
|
|
||||||
/** Support moves in: the host feeds it from now on. */
|
/** Support moves in: the host feeds it from now on. */
|
||||||
private function station(Movement $movement, CarbonImmutable $at): void
|
private function station(Movement $movement, CarbonImmutable $at): void
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -208,6 +208,9 @@ final class ConfigLint
|
||||||
$problems[] = "[$unit] must eat food (upkeep > 0), or armies grow without limit.";
|
$problems[] = "[$unit] must eat food (upkeep > 0), or armies grow without limit.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (max($config->get('combat.conquest.coin_cost', [0])) > $maxCap) {
|
||||||
|
$problems[] = 'A coin costs more than the largest storage holds.';
|
||||||
|
}
|
||||||
|
|
||||||
return $problems;
|
return $problems;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Game\Military;
|
||||||
|
|
||||||
|
use App\Game\WorldConfig;
|
||||||
|
|
||||||
|
/** Loyalty, its drop under noblemen and the coin slots that limit them (combat.conquest). */
|
||||||
|
final class Conquest
|
||||||
|
{
|
||||||
|
public function __construct(private readonly WorldConfig $config) {}
|
||||||
|
|
||||||
|
public function maxLoyalty(): float
|
||||||
|
{
|
||||||
|
return (float) $this->config->get('combat.conquest.loyalty.max', 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function afterConquest(): float
|
||||||
|
{
|
||||||
|
return (float) $this->config->get('combat.conquest.loyalty.after_conquest', 25);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Loyalty $hours after it was $stored, grown back at the world's speed, up to the maximum. */
|
||||||
|
public function loyalty(float $stored, float $hours): float
|
||||||
|
{
|
||||||
|
$rate = (float) $this->config->get('combat.conquest.loyalty.regen_per_hour', 1) * $this->config->speed();
|
||||||
|
|
||||||
|
return min($this->maxLoyalty(), $stored + $rate * max(0, $hours));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** One nobleman's drop for a draw in [0, 1). */
|
||||||
|
public function drop(float $roll): float
|
||||||
|
{
|
||||||
|
[$min, $max] = $this->config->get('combat.conquest.loyalty.drop', [20, 35]);
|
||||||
|
|
||||||
|
return round($min + ($max - $min) * $roll);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Noble slots that $coins pay for: the n-th slot needs n more coins. */
|
||||||
|
public static function slots(int $coins): int
|
||||||
|
{
|
||||||
|
return (int) floor((sqrt(8 * max(0, $coins) + 1) - 1) / 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Coins in total for $slots slots. */
|
||||||
|
public static function coinsFor(int $slots): int
|
||||||
|
{
|
||||||
|
return intdiv($slots * ($slots + 1), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array{wood: int, clay: int, iron: int} */
|
||||||
|
public function coinCost(int $count = 1): array
|
||||||
|
{
|
||||||
|
return array_map(fn ($n) => (int) $n * $count, $this->config->get('combat.conquest.coin_cost'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function maxDistance(): float
|
||||||
|
{
|
||||||
|
return (float) $this->config->get('combat.conquest.max_distance', 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return list<string> */
|
||||||
|
public function nobleUnits(): array
|
||||||
|
{
|
||||||
|
return array_keys(array_filter($this->config->get('units', []), fn (array $u) => $u['noble'] ?? false));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -24,6 +24,8 @@ class Village extends Model
|
||||||
'militia' => 'integer',
|
'militia' => 'integer',
|
||||||
'levy_until' => 'immutable_datetime',
|
'levy_until' => 'immutable_datetime',
|
||||||
'starving_since' => 'immutable_datetime',
|
'starving_since' => 'immutable_datetime',
|
||||||
|
'loyalty' => 'float',
|
||||||
|
'loyalty_at' => 'immutable_datetime',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,12 @@
|
||||||
namespace App\Presenters;
|
namespace App\Presenters;
|
||||||
|
|
||||||
use App\Engine\Military;
|
use App\Engine\Military;
|
||||||
|
use App\Engine\Nobility;
|
||||||
use App\Engine\Quests;
|
use App\Engine\Quests;
|
||||||
use App\Engine\Simulation;
|
use App\Engine\Simulation;
|
||||||
use App\Engine\Starvation;
|
use App\Engine\Starvation;
|
||||||
use App\Game\Map\Continent;
|
use App\Game\Map\Continent;
|
||||||
|
use App\Game\Military\Conquest;
|
||||||
use App\Models\Building;
|
use App\Models\Building;
|
||||||
use App\Models\BuildOrder;
|
use App\Models\BuildOrder;
|
||||||
use App\Models\RecruitOrder;
|
use App\Models\RecruitOrder;
|
||||||
|
|
@ -70,6 +72,11 @@ final class VillagePresenter
|
||||||
'movements' => RallyPresenter::movements($village, $now),
|
'movements' => RallyPresenter::movements($village, $now),
|
||||||
'support' => RallyPresenter::support($village),
|
'support' => RallyPresenter::support($village),
|
||||||
'rally' => RallyPresenter::rally($village, $now),
|
'rally' => RallyPresenter::rally($village, $now),
|
||||||
|
'loyalty' => (int) floor(Nobility::of($village)->loyalty($village, $now)),
|
||||||
|
'conquest' => self::conquest($village),
|
||||||
|
// The player's villages in this world, for the switcher.
|
||||||
|
'villages' => Village::where('player_id', $village->player_id)->orderBy('id')->get(['id', 'name', 'x', 'y', 'points'])
|
||||||
|
->map(fn (Village $v) => ['id' => $v->id, 'name' => $v->name, 'x' => $v->x, 'y' => $v->y, 'points' => $v->points])->all(),
|
||||||
'quests' => Quests::summary($village),
|
'quests' => Quests::summary($village),
|
||||||
'unread_reports' => Report::where('player_id', $village->player_id)->whereNull('read_at')->count(),
|
'unread_reports' => Report::where('player_id', $village->player_id)->whereNull('read_at')->count(),
|
||||||
'builders' => $sim->builders(),
|
'builders' => $sim->builders(),
|
||||||
|
|
@ -152,6 +159,24 @@ final class VillagePresenter
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Coins and noble slots of the player, and what a coin costs. */
|
||||||
|
private static function conquest(Village $village): array
|
||||||
|
{
|
||||||
|
$nobility = Nobility::of($village);
|
||||||
|
$coins = (int) ($village->player->coins ?? 0);
|
||||||
|
$slots = Conquest::slots($coins);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'coins' => $coins,
|
||||||
|
'slots' => $slots,
|
||||||
|
'used' => $village->player ? $nobility->slotsUsed($village->player) : 0,
|
||||||
|
// Coins still missing for the next slot.
|
||||||
|
'next_slot_coins' => Conquest::coinsFor($slots + 1) - $coins,
|
||||||
|
'coin_cost' => $nobility->rules()->coinCost(),
|
||||||
|
'max_distance' => $nobility->rules()->maxDistance(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
private static function iso(CarbonImmutable $at): string
|
private static function iso(CarbonImmutable $at): string
|
||||||
{
|
{
|
||||||
return $at->utc()->format('Y-m-d\TH:i:s.v\Z');
|
return $at->utc()->format('Y-m-d\TH:i:s.v\Z');
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ use App\Actions\CancelOrder;
|
||||||
use App\Actions\CancelRecruit;
|
use App\Actions\CancelRecruit;
|
||||||
use App\Actions\EndLevy;
|
use App\Actions\EndLevy;
|
||||||
use App\Actions\Levy;
|
use App\Actions\Levy;
|
||||||
|
use App\Actions\MintCoins;
|
||||||
use App\Actions\QueueBuild;
|
use App\Actions\QueueBuild;
|
||||||
use App\Actions\QueueDemolish;
|
use App\Actions\QueueDemolish;
|
||||||
use App\Actions\QueueUpgrade;
|
use App\Actions\QueueUpgrade;
|
||||||
|
|
@ -19,7 +20,7 @@ use App\Presenters\VillagePresenter;
|
||||||
use App\Websocket\GameController;
|
use App\Websocket\GameController;
|
||||||
use Closure;
|
use Closure;
|
||||||
|
|
||||||
/** village.show|build|upgrade|demolish|cancel|staff|recruit|cancelRecruit|levy|endLevy|rename. Each answers with the fresh village state. */
|
/** village.show|build|upgrade|demolish|cancel|staff|recruit|cancelRecruit|levy|endLevy|mint|rename. Each answers with the fresh village state. */
|
||||||
class VillageController extends GameController
|
class VillageController extends GameController
|
||||||
{
|
{
|
||||||
public function show()
|
public function show()
|
||||||
|
|
@ -78,6 +79,11 @@ class VillageController extends GameController
|
||||||
return $this->village(fn ($user, $id) => app(EndLevy::class)($user, $id));
|
return $this->village(fn ($user, $id) => app(EndLevy::class)($user, $id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function mint()
|
||||||
|
{
|
||||||
|
return $this->village(fn ($user, $id) => app(MintCoins::class)($user, $id, (int) request('count', 1)));
|
||||||
|
}
|
||||||
|
|
||||||
public function rename()
|
public function rename()
|
||||||
{
|
{
|
||||||
return $this->village(fn ($user, $id) => app(RenameVillage::class)($user, $id, (string) request('name')));
|
return $this->village(fn ($user, $id) => app(RenameVillage::class)($user, $id, (string) request('name')));
|
||||||
|
|
|
||||||
|
|
@ -38,4 +38,18 @@ return [
|
||||||
// New players can't be attacked by players for this long after joining (seconds at
|
// New players can't be attacked by players for this long after joining (seconds at
|
||||||
// world speed 1), and can't attack players themselves meanwhile; barbarians are fine.
|
// world speed 1), and can't attack players themselves meanwhile; barbarians are fine.
|
||||||
'beginner_protection' => 3 * 86400,
|
'beginner_protection' => 3 * 86400,
|
||||||
|
|
||||||
|
// Taking villages, like Die Stämme. Loyalty runs from 0 to max and grows back
|
||||||
|
// regen_per_hour × world speed. Every surviving nobleman of a won attack lowers it by
|
||||||
|
// an even draw between drop[0] and drop[1]; at 0 or below the village changes hands,
|
||||||
|
// one nobleman settles there, and loyalty starts again at after_conquest.
|
||||||
|
'conquest' => [
|
||||||
|
'loyalty' => ['max' => 100, 'regen_per_hour' => 1.0, 'drop' => [20, 35], 'after_conquest' => 25],
|
||||||
|
// Noblemen don't march further than this many fields (0: no limit).
|
||||||
|
'max_distance' => 100,
|
||||||
|
// Coins minted at the academy. Noble slots grow with coins: the n-th slot needs n
|
||||||
|
// more coins (1, 3, 6, 10, …). Each nobleman alive and each village beyond the
|
||||||
|
// first takes a slot.
|
||||||
|
'coin_cost' => ['wood' => 28000, 'clay' => 30000, 'iron' => 25000],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -115,6 +115,8 @@ return [
|
||||||
'class' => 'infantry',
|
'class' => 'infantry',
|
||||||
'speed' => 35,
|
'speed' => 35,
|
||||||
'carry' => 0,
|
'carry' => 0,
|
||||||
|
// Lowers loyalty when it survives a won attack (combat.conquest).
|
||||||
|
'noble' => true,
|
||||||
'requires' => [],
|
'requires' => [],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
// Loyalty as of loyalty_at; it grows back lazily (combat.conquest.loyalty).
|
||||||
|
Schema::table('villages', function (Blueprint $table) {
|
||||||
|
$table->decimal('loyalty', 6, 2)->default(100);
|
||||||
|
$table->dateTime('loyalty_at', 3)->nullable();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Coins minted at an academy; they pay for noble slots.
|
||||||
|
Schema::table('players', function (Blueprint $table) {
|
||||||
|
$table->unsignedInteger('coins')->default(0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('players', fn (Blueprint $table) => $table->dropColumn('coins'));
|
||||||
|
Schema::table('villages', fn (Blueprint $table) => $table->dropColumn(['loyalty', 'loyalty_at']));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,145 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Actions\JoinWorld;
|
||||||
|
use App\Actions\MintCoins;
|
||||||
|
use App\Actions\QueueUpgrade;
|
||||||
|
use App\Actions\Recruit;
|
||||||
|
use App\Actions\SendTroops;
|
||||||
|
use App\Engine\VillageFactory;
|
||||||
|
use App\Models\Movement;
|
||||||
|
use App\Models\Report;
|
||||||
|
use App\Models\UnitStack;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Models\Village;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
|
|
||||||
|
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
|
||||||
|
|
||||||
|
/** A village with a rally point, an academy and these troops at home. */
|
||||||
|
function nobleVillage(array $units = [], ?Village $village = null): Village
|
||||||
|
{
|
||||||
|
$village ??= startVillage();
|
||||||
|
$village->buildings()->create(['type' => 'rally_point', 'level' => 1]);
|
||||||
|
$village->buildings()->create(['type' => 'academy', 'level' => 1]);
|
||||||
|
foreach ($units as $unit => $count) {
|
||||||
|
$village->troops()->create(['home_village_id' => $village->id, 'unit' => $unit, 'count' => $count]);
|
||||||
|
}
|
||||||
|
$village->player->update(['coins' => 1]);
|
||||||
|
|
||||||
|
return $village->fresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
function barbarianAt(Village $near, int $dx, int $dy, float $loyalty = 100): Village
|
||||||
|
{
|
||||||
|
$barbarian = app(VillageFactory::class)->create($near->world, null, 'Barbarian village', $near->x + $dx, $near->y + $dy, ['headquarters' => 1, 'storage' => 1, 'farm' => 1]);
|
||||||
|
$barbarian->update(['loyalty' => $loyalty, 'loyalty_at' => now()]);
|
||||||
|
|
||||||
|
return $barbarian;
|
||||||
|
}
|
||||||
|
|
||||||
|
function attackWith(Village $from, Village $to, array $units): void
|
||||||
|
{
|
||||||
|
app(SendTroops::class)(owner($from), $from->id, 'attack', $to->x, $to->y, $units, null, null);
|
||||||
|
test()->travel(2)->hours();
|
||||||
|
test()->artisan('game:events --once')->assertSuccessful();
|
||||||
|
}
|
||||||
|
|
||||||
|
it('mints coins at an academy and turns them into noble slots', function () {
|
||||||
|
$village = startVillage();
|
||||||
|
$village->update(['wood' => 400000, 'clay' => 400000, 'iron' => 400000]);
|
||||||
|
expect(rule(fn () => app(MintCoins::class)(owner($village), $village->id, 1)))->toBe('requires');
|
||||||
|
|
||||||
|
$village->buildings()->create(['type' => 'academy', 'level' => 1]);
|
||||||
|
$village->buildings()->create(['type' => 'storage', 'level' => 30]);
|
||||||
|
$state = shown(app(MintCoins::class)(owner($village), $village->id, 3));
|
||||||
|
|
||||||
|
expect($state['conquest'])->toMatchArray(['coins' => 3, 'slots' => 2, 'used' => 0, 'next_slot_coins' => 3])
|
||||||
|
->and($state['resources']['wood'])->toBe(400000.0 - 3 * 28000);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('needs a free noble slot to train a nobleman', function () {
|
||||||
|
$village = nobleVillage();
|
||||||
|
$village->player->update(['coins' => 0]);
|
||||||
|
$village->buildings()->create(['type' => 'storage', 'level' => 30]);
|
||||||
|
$village->update(['wood' => 200000, 'clay' => 200000, 'iron' => 200000]);
|
||||||
|
$academy = $village->buildings()->where('type', 'academy')->first();
|
||||||
|
|
||||||
|
expect(rule(fn () => app(Recruit::class)(owner($village), $village->id, $academy->id, 'nobleman', 1)))->toBe('coins');
|
||||||
|
|
||||||
|
$village->player->update(['coins' => 1]);
|
||||||
|
app(Recruit::class)(owner($village), $village->id, $academy->id, 'nobleman', 1);
|
||||||
|
$state = shown($village);
|
||||||
|
expect($state['conquest']['used'])->toBe(1)
|
||||||
|
->and(collect($state['military'])->firstWhere('type', 'academy')['units'][0]['max'])->toBe(0)
|
||||||
|
->and(rule(fn () => app(Recruit::class)(owner($village), $village->id, $academy->id, 'nobleman', 1)))->toBe('coins');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('lowers loyalty with every surviving nobleman, and loyalty grows back', function () {
|
||||||
|
$village = nobleVillage(['spear' => 50, 'nobleman' => 1]);
|
||||||
|
$barbarian = barbarianAt($village, 2, 0);
|
||||||
|
|
||||||
|
attackWith($village, $barbarian, ['spear' => 50, 'nobleman' => 1]);
|
||||||
|
$barbarian->refresh();
|
||||||
|
$report = Report::where('player_id', $village->player_id)->first()->payload;
|
||||||
|
|
||||||
|
expect($barbarian->player_id)->toBeNull()
|
||||||
|
->and($report['loyalty']['before'])->toBe(100)
|
||||||
|
->and($report['loyalty']['after'])->toBeBetween(65, 80)
|
||||||
|
->and($report['loyalty']['conquered'])->toBeFalse()
|
||||||
|
->and((float) $barbarian->loyalty)->toEqual($report['loyalty']['after'] + 0.0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('conquers a village whose loyalty reaches zero', function () {
|
||||||
|
$village = nobleVillage(['spear' => 50, 'nobleman' => 1]);
|
||||||
|
$barbarian = barbarianAt($village, 2, 0, 10);
|
||||||
|
|
||||||
|
attackWith($village, $barbarian, ['spear' => 50, 'nobleman' => 1]);
|
||||||
|
$barbarian->refresh();
|
||||||
|
|
||||||
|
expect($barbarian->player_id)->toBe($village->player_id)
|
||||||
|
->and((float) $barbarian->loyalty)->toBe(25.0)
|
||||||
|
// The nobleman settled; the spearmen stay as support from home.
|
||||||
|
->and(UnitStack::where('village_id', $barbarian->id)->get(['home_village_id', 'unit', 'count'])->toArray())
|
||||||
|
->toBe([['home_village_id' => $village->id, 'unit' => 'spear', 'count' => 50]])
|
||||||
|
->and(Movement::count())->toBe(0)
|
||||||
|
->and(Report::where('player_id', $village->player_id)->first()->payload['loyalty']['conquered'])->toBeTrue()
|
||||||
|
->and(array_column(shown($village)['villages'], 'id'))->toBe([$village->id, $barbarian->id])
|
||||||
|
->and(shown($village)['conquest']['used'])->toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('takes a player village with its queues, and the old owner starts over', function () {
|
||||||
|
$victim = startVillage();
|
||||||
|
$victim->buildings()->create(['type' => 'rally_point', 'level' => 1]);
|
||||||
|
$victim->troops()->create(['home_village_id' => $victim->id, 'unit' => 'spear', 'count' => 3]);
|
||||||
|
$village = nobleVillage(['spear' => 100, 'nobleman' => 1]);
|
||||||
|
$this->travel(4)->days();
|
||||||
|
$victim->update(['loyalty' => 5, 'loyalty_at' => now()]);
|
||||||
|
|
||||||
|
// The victim has a build order and support standing in the attacker's village.
|
||||||
|
app(QueueUpgrade::class)(owner($victim), $victim->id, $victim->buildings()->where('type', 'storage')->value('id'));
|
||||||
|
$village->troops()->create(['home_village_id' => $victim->id, 'unit' => 'spear', 'count' => 2]);
|
||||||
|
$victim->troops()->where('unit', 'spear')->update(['count' => 1]);
|
||||||
|
|
||||||
|
app(SendTroops::class)(owner($village), $village->id, 'attack', $victim->x, $victim->y, ['spear' => 100, 'nobleman' => 1], null, null);
|
||||||
|
// Long enough for any spawn distance, short enough that loyalty stays below one drop.
|
||||||
|
$this->travel(12)->hours();
|
||||||
|
$this->artisan('game:events --once')->assertSuccessful();
|
||||||
|
$victim->refresh();
|
||||||
|
|
||||||
|
expect($victim->player_id)->toBe($village->player_id)
|
||||||
|
->and($victim->orders()->count())->toBe(0)
|
||||||
|
->and(UnitStack::where('home_village_id', $victim->id)->where('village_id', '!=', $victim->id)->count())->toBe(0)
|
||||||
|
->and(Report::where('type', Report::DEFENSE)->first()->payload['loyalty']['conquered'])->toBeTrue();
|
||||||
|
|
||||||
|
$loser = User::whereKeyNot(owner($village)->id)->first();
|
||||||
|
$fresh = app(JoinWorld::class)($loser, $victim->world_id);
|
||||||
|
expect($fresh->id)->not->toBe($victim->id)
|
||||||
|
->and($fresh->player->user_id)->toBe($loser->id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps noblemen within their marching range', function () {
|
||||||
|
$village = nobleVillage(['nobleman' => 1]);
|
||||||
|
$far = barbarianAt($village, 101, 0);
|
||||||
|
|
||||||
|
expect(rule(fn () => app(SendTroops::class)(owner($village), $village->id, 'attack', $far->x, $far->y, ['nobleman' => 1], null, null)))->toBe('too_far');
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Game\Military\Conquest;
|
||||||
|
|
||||||
|
it('grows loyalty back at world speed, up to the maximum', function () {
|
||||||
|
expect((new Conquest(gameConfig()))->loyalty(40, 10))->toBe(50.0)
|
||||||
|
->and((new Conquest(gameConfig(['world' => ['speed' => 5]])))->loyalty(40, 10))->toBe(90.0)
|
||||||
|
->and((new Conquest(gameConfig()))->loyalty(95, 10))->toBe(100.0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('drops loyalty by 20 to 35 per nobleman', function () {
|
||||||
|
$conquest = new Conquest(gameConfig());
|
||||||
|
|
||||||
|
expect($conquest->drop(0.0))->toBe(20.0)
|
||||||
|
->and($conquest->drop(0.5))->toBe(28.0)
|
||||||
|
->and($conquest->drop(0.9999))->toBe(35.0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gives the n-th noble slot for n more coins', function () {
|
||||||
|
expect(array_map(Conquest::slots(...), [0, 1, 2, 3, 5, 6, 10]))->toBe([0, 1, 1, 2, 2, 3, 4])
|
||||||
|
->and(Conquest::coinsFor(4))->toBe(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('knows the noblemen and what a coin costs', function () {
|
||||||
|
$conquest = new Conquest(gameConfig());
|
||||||
|
|
||||||
|
expect($conquest->nobleUnits())->toBe(['nobleman'])
|
||||||
|
->and($conquest->coinCost(2))->toBe(['wood' => 56000, 'clay' => 60000, 'iron' => 50000]);
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue