public function addBackground(RulebookBackground $background, $amount, $description = null) { $query = CharacterBackground::character($this->character_id)->version($this->version)->where('background_id', $background->id); if ($description) { $query = $query->where('description', $description); } else { $query = $query->whereNull('description'); } $backgroundRecord = $query->first(); if ($backgroundRecord) { $rankDifference = $amount - $backgroundRecord->amount; if ($rankDifference < 0) { if ($backgroundRecord->definition->name == "Generation") { $generationCost = [1, 2, 4, 8, 16]; $backgroundRecord->lost_points += $generationCost[$backgroundRecord->amount - 1] - $generationCost[$amount - 1]; } else { $backgroundRecord->lost_points += abs($rankDifference); } } else { if ($rankDifference > 0 && $this->editingAsStoryteller()) { if ($backgroundRecord->definition->name == "Generation") { $generationCost = [1, 2, 4, 8, 16]; $backgroundRecord->free_points += $generationCost[$amount - 1] - $generationCost[$backgroundRecord->amount - 1]; } else { $backgroundRecord->free_points += $rankDifference; } } } } else { $backgroundRecord = $this->createNewRecord("CharacterBackground"); $backgroundRecord->background_id = $background->id; if ($description) { $backgroundRecord->description = $description; } if ($this->editingAsStoryteller()) { if ($background->name == "Generation") { $generationCost = [1, 2, 4, 8, 16]; $backgroundRecord->free_points += $generationCost[$amount - 1]; } else { $backgroundRecord->free_points += $amount; } } } $backgroundRecord->amount = $amount; $backgroundRecord->save(); $this->touchedRecords["CharacterBackground"][] = $backgroundRecord->id; }
public function backgrounds($version = -1, $ignoreRankZeros = false) { if ($version == -1) { $version = $this->activeVersion(); } if ($ignoreRankZeros) { return CharacterBackground::character($this->id)->version($version); } return CharacterBackground::character($this->id)->where('amount', '>', 0)->version($version); }