Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .ai/rules/downtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
paths:
- 'app/Livewire/Downtime/**'
---

# Downtime

## Downtime is gated by the character, written by the character's player, and one component in three places
downtime_activities has no visibility column. DowntimeActivity::scopeVisibleTo(user, role) is a whereIn over Entity::visibleTo(), so a row is read by whoever may read the character and a GM-only NPC's month never loads for a player. DowntimeActivityPolicy follows the character, not the author: a GM or entities.player_user_id may create, edit, and delete, the rule EntityPolicy::update() already spells. Downtime\Log renders on the character page (scoped to the character, form writes under it), the session page (scoped to the session, character picker), and /downtime (both pickers); do not fork it. The character picker's list is also the validation rule, so a posted id it never offered is an error rather than a write. The start date goes through the same calendar check as the session form and the end date is computed, never stored.
1 change: 1 addition & 0 deletions .ai/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Before planning or editing, find the row whose globs match the file's path and r
| app/Livewire/Decisions/** | .ai/rules/decisions.md |
| app/Support/Dice/** | .ai/rules/dice.md |
| app/Discord/** | .ai/rules/discord.md |
| app/Livewire/Downtime/** | .ai/rules/downtime.md |
| app/Support/Encounters/** | .ai/rules/encounters.md |
| app/Livewire/Entities/** | .ai/rules/entities.md |
| app/Events/** | .ai/rules/events.md |
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Slice 23 adds **a ceiling on a campaign's files**. `CAMPAIGN_STORAGE_MB`, 500 by

Slice 24 adds **the player screen**, for the television at the end of the table. `/screen` is one page with no sidebar and no controls that shows what the whole party may see and nothing else, whoever opened it: the turn order with health as a word, a handout full size, a map with the pins the party found, or the revealed clocks, with the clocks in a strip along the bottom the rest of the time. The GM chooses from a card on the Run screen, and putting a hidden handout up shows the party on the way. It changes on every open screen at once over the same channel as the table, and it falls back on its own: to the fight while one runs, then to the campaign's name and the party.

Slice 25 adds **downtime**, what each character did between sessions. A row is what they did, how many days it cost, an optional note in Markdown, the session it happened around, and, when the campaign has a calendar, the day in the world it began, from which the page prints the range it covered. A player writes their own PC's rows and a GM writes anyone's; whoever may see the character reads them, and there is no second switch. The same log sits on the character's page, on the session's page, and at `/downtime`, where the days are summed per character on every read. It travels in the export and lands on the character's page in the vault.

Your creatures travel in your export with the prose you wrote. A shipped creature never does — it is named in the file and nothing more, exactly as before. The CC BY notice follows the words rather than the table: a creature you wrote carries none, and a copy of a shipped one keeps the source and licence it came from, so the credit travels with the text.

## Local setup
Expand Down
27 changes: 27 additions & 0 deletions app/Actions/Campaigns/ExportCampaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\Combatant;
use App\Models\Decision;
use App\Models\DiceRoll;
use App\Models\DowntimeActivity;
use App\Models\Encounter;
use App\Models\Entity;
use App\Models\EntityBodyRevision;
Expand Down Expand Up @@ -97,6 +98,7 @@ class ExportCampaign
'game_sessions' => 'sessions',
'decisions' => 'decisions',
'ledger_entries' => 'ledger',
'downtime_activities' => 'downtime',
'reputation_changes' => 'reputation',
'stat_blocks' => 'stat_blocks',
'encounters' => 'encounters',
Expand Down Expand Up @@ -162,6 +164,7 @@ public function handle(Campaign $campaign): array
'clocks' => $this->clocks($campaign),
'decisions' => $this->decisions($campaign),
'ledger' => $this->ledger($campaign),
'downtime' => $this->downtime($campaign),
'reputation' => $this->reputation($campaign),
];
}
Expand Down Expand Up @@ -637,6 +640,30 @@ private function ledger(Campaign $campaign): iterable
]);
}

/**
* @return iterable<int, array<string, mixed>> A LazyCollection: it streams row by row.
*/
private function downtime(Campaign $campaign): iterable
{
return DowntimeActivity::query()
->withoutGlobalScopes()
->where('campaign_id', $campaign->id)
->orderBy('created_at')
->orderBy('id')
->cursor()
->map(fn (DowntimeActivity $activity) => [
'id' => $activity->id,
'entity_id' => $activity->entity_id,
'game_session_id' => $activity->game_session_id,
'activity' => $activity->activity,
'days' => $activity->days,
'notes' => $activity->notes,
'starts_on' => $activity->starts_on?->toArray(),
'created_at' => $activity->created_at?->toIso8601String(),
'updated_at' => $activity->updated_at?->toIso8601String(),
]);
}

/**
* @return iterable<int, array<string, mixed>> A LazyCollection: it streams row by row.
*/
Expand Down
27 changes: 27 additions & 0 deletions app/Actions/Campaigns/ImportCampaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Clock;
use App\Models\Combatant;
use App\Models\Decision;
use App\Models\DowntimeActivity;
use App\Models\Encounter;
use App\Models\Entity;
use App\Models\EntityBodyRevision;
Expand Down Expand Up @@ -93,6 +94,7 @@ public function handle(array $document, User $importer, array $restored = []): C
$this->decisions($document, $campaign, $importer, $ids);
$this->ledger($document, $campaign, $importer, $ids);
$this->reputation($document, $campaign, $importer, $ids);
$this->downtime($document, $campaign, $importer, $ids);
$this->screen($document, $campaign, $ids);
});

Expand Down Expand Up @@ -506,6 +508,31 @@ private function prepped(array $document, IdMap $ids): void
}
}

/**
* What each character did between sessions. The character and the session are
* remapped; the date travels as the triple; the importer is the author, because
* the file cannot say who was.
*
* @param array<string, mixed> $document
*/
private function downtime(array $document, Campaign $campaign, User $importer, IdMap $ids): void
{
foreach ($document['downtime'] as $row) {
$this->write(new DowntimeActivity, [
'id' => $ids->remember($row['id']),
'campaign_id' => $campaign->id,
'entity_id' => $ids->newFor($row['entity_id']),
'game_session_id' => $ids->newForNullable($row['game_session_id']),
'activity' => $row['activity'],
'days' => $row['days'],
'notes' => $row['notes'],
'starts_on' => $row['starts_on'],
'created_at' => $row['created_at'],
'created_by' => $importer->id,
]);
}
}

/**
* What the screen on the wall shows, written after the page it names exists. The
* focus travels as it is; the page is remapped like every other id.
Expand Down
52 changes: 52 additions & 0 deletions app/Actions/Campaigns/ReadCampaignFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\Models\Campaign;
use App\Models\Combatant;
use App\Models\Decision;
use App\Models\DowntimeActivity;
use App\Models\Encounter;
use App\Models\EntityRelation;
use App\Models\GameSession;
Expand Down Expand Up @@ -135,6 +136,7 @@ public function handle(string $json): ReadResult
'clocks' => $this->clocks($this->list($decoded, 'clocks')),
'decisions' => $this->decisions($this->list($decoded, 'decisions')),
'ledger' => $this->ledger($this->list($decoded, 'ledger')),
'downtime' => $this->downtime($this->list($decoded, 'downtime')),
'reputation' => $this->reputation($this->list($decoded, 'reputation')),
];

Expand Down Expand Up @@ -952,6 +954,49 @@ private function signedInteger(array $row, string $key): ?int
return is_string($value) && preg_match('/^-?\\d+$/', $value) === 1 ? (int) $value : null;
}

/**
* What each character did between sessions. A row is its character and its
* activity; one with neither is a row nothing can show. Absent from a file older
* than slice 25, and an empty list is what that means.
*
* @param list<array<string, mixed>> $rows
* @return list<array<string, mixed>>
*/
private function downtime(array $rows): array
{
$activities = [];

foreach ($rows as $index => $row) {
$id = $this->id($row, 'downtime', $index);

if ($id === null) {
continue;
}

$activity = $this->text($row, 'activity', DowntimeActivity::MAX_ACTIVITY_LENGTH);
$character = $this->reference($row, 'entity_id');

if ($activity === null || $character === null) {
continue;
}

$activities[] = [
'id' => $id,
'entity_id' => $character,
'game_session_id' => $this->reference($row, 'game_session_id'),
'activity' => $activity,
'days' => max(0, min(DowntimeActivity::MAX_DAYS, $this->integer($row, 'days') ?? 0)),
'notes' => $this->text($row, 'notes', DowntimeActivity::MAX_NOTES_LENGTH),
'starts_on' => $this->gameDate($row, 'starts_on'),
'created_at' => $this->text($row, 'created_at', 40),
];
}

$this->report->count('downtime', count($activities));

return $activities;
}

/**
* @param list<mixed> $rows
* @return list<array<string, mixed>>
Expand Down Expand Up @@ -1044,6 +1089,8 @@ private function checkReferences(array $document): void
$ledger = $document['ledger'];
/** @var list<array<string, mixed>> $reputation */
$reputation = $document['reputation'];
/** @var list<array<string, mixed>> $downtime */
$downtime = $document['downtime'];

// An arc is an entity of one type, so the reference is checked against the
// arcs the file carries rather than every page in it.
Expand Down Expand Up @@ -1120,6 +1167,11 @@ private function checkReferences(array $document): void
$this->mustResolve($change['game_session_id'], $this->sessionIds, 'session', 'a reputation change');
}

foreach ($downtime as $activity) {
$this->mustResolve($activity['entity_id'], $this->entityIds, 'character', 'a downtime row');
$this->mustResolve($activity['game_session_id'], $this->sessionIds, 'session', 'a downtime row');
}

$this->checkCycles($entities, 'parent_id', 'The pages in that file nest inside each other in a loop');
$this->checkCycles($tables, 'nested_table_id', 'The tables in that file nest inside each other in a loop');
}
Expand Down
18 changes: 17 additions & 1 deletion app/Actions/Campaigns/WriteCampaignMarkdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Campaign;
use App\Models\Decision;
use App\Models\DowntimeActivity;
use App\Models\Entity;
use App\Models\EntityRelation;
use App\Models\GameSession;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function handle(Campaign $campaign): array
$entities = Entity::withoutGlobalScopes()
->where('campaign_id', $campaign->id)
->whereNull('deleted_at')
->with(['tags', 'parent', 'arc', 'player', 'objectives', 'relations.target', 'incomingRelations.source', 'reputationChanges.gameSession'])
->with(['tags', 'parent', 'arc', 'player', 'objectives', 'relations.target', 'incomingRelations.source', 'reputationChanges.gameSession', 'downtimeActivities.gameSession'])
->orderBy('name')
->get();

Expand Down Expand Up @@ -242,6 +243,21 @@ private function entity(Entity $entity): string
->implode("\n");
}

// What the character did between sessions, oldest first, with the days, the
// range in the world when there is one, and the session it happened around.
if ($entity->isCharacter() && $entity->downtimeActivities->isNotEmpty()) {
$body[] = "## Downtime\n\n".$entity->downtimeActivities
->map(function (DowntimeActivity $activity): string {
$range = $this->reckoning === null ? null : $activity->range($this->reckoning);
$line = '- '.$activity->activity.', '.DowntimeActivity::dayCount($activity->days)
.($range !== null ? ', '.$range : '')
.($activity->gameSession !== null ? ' *('.$activity->gameSession->label().')*' : '');

return $activity->hasNotes() ? $line."\n ".$activity->notes : $line;
})
->implode("\n");
}

$body[] = $this->section('Rewards', $entity->rewards);
$body[] = $this->section('GM notes', $entity->dm_notes);

Expand Down
13 changes: 13 additions & 0 deletions app/Actions/Downtime/DeleteDowntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Actions\Downtime;

use App\Models\DowntimeActivity;

class DeleteDowntime
{
public function handle(DowntimeActivity $activity): void
{
$activity->delete();
}
}
33 changes: 33 additions & 0 deletions app/Actions/Downtime/RecordDowntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Actions\Downtime;

use App\Models\Campaign;
use App\Models\DowntimeActivity;
use App\Models\Entity;
use App\Models\GameSession;
use App\Models\User;
use App\Support\Reckoning\GameDate;

class RecordDowntime
{
/**
* One row. The parts arrive validated: the component checked the day count
* against the bounds and the date against the calendar before it got here.
*
* @param array{activity: string, days: int, notes?: string|null, session?: GameSession|null, starts_on?: GameDate|null} $parts
*/
public function handle(Campaign $campaign, User $actor, Entity $character, array $parts): DowntimeActivity
{
return DowntimeActivity::create([
'campaign_id' => $campaign->id,
'entity_id' => $character->id,
'game_session_id' => ($parts['session'] ?? null)?->id,
'activity' => trim($parts['activity']),
'days' => $parts['days'],
'notes' => filled($parts['notes'] ?? null) ? trim((string) $parts['notes']) : null,
'starts_on' => $parts['starts_on'] ?? null,
'created_by' => $actor->id,
]);
}
}
29 changes: 29 additions & 0 deletions app/Actions/Downtime/UpdateDowntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Actions\Downtime;

use App\Models\DowntimeActivity;
use App\Models\GameSession;
use App\Support\Reckoning\GameDate;

class UpdateDowntime
{
/**
* The same parts as RecordDowntime, on a row. The character never changes: a row
* written on the wrong character is deleted and written again.
*
* @param array{activity: string, days: int, notes?: string|null, session?: GameSession|null, starts_on?: GameDate|null} $parts
*/
public function handle(DowntimeActivity $activity, array $parts): DowntimeActivity
{
$activity->fill([
'game_session_id' => ($parts['session'] ?? null)?->id,
'activity' => trim($parts['activity']),
'days' => $parts['days'],
'notes' => filled($parts['notes'] ?? null) ? trim((string) $parts['notes']) : null,
'starts_on' => $parts['starts_on'] ?? null,
])->save();

return $activity;
}
}
65 changes: 65 additions & 0 deletions app/Livewire/Downtime/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Livewire\Downtime;

use App\Livewire\Concerns\InteractsWithCampaign;
use App\Models\Campaign;
use App\Models\DowntimeActivity;
use App\Models\Entity;
use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Livewire\Component;

/**
* The page around the log: a total per character, then the whole campaign's rows.
* Every member; what each reads is the log's own query, and the totals here go
* through the same gate, so a GM-only character's month is not in a player's sum.
*/
class Index extends Component
{
use InteractsWithCampaign;

public function mount(Campaign $campaign): void
{
$this->enterCampaign($campaign);
$this->authorize('viewAny', [DowntimeActivity::class, $campaign]);
}

public function render(): View
{
return view('livewire.downtime.index', [
'role' => $this->role(),
'totals' => $this->totals(),
])->title('Downtime');
}

/**
* Days per character, summed on every read over the rows this viewer may see,
* in name order. Two queries: the sums, then the names for the ids that came back.
*
* @return Collection<int, array{character: Entity, days: int}>
*/
private function totals(): Collection
{
/** @var User $user */
$user = auth()->user();

$sums = DowntimeActivity::query()
->visibleTo($user, $this->role())
->selectRaw('entity_id, sum(days) as total_days')
->groupBy('entity_id')
->pluck('total_days', 'entity_id');

if ($sums->isEmpty()) {
return collect();
}

return Entity::query()
->whereKey($sums->keys()->all())
->orderBy('name')
->get()
->map(fn (Entity $character) => ['character' => $character, 'days' => (int) $sums[$character->id]])
->values();
}
}
Loading
Loading