public function buildRulebook($owner = -1)
 {
     //Build rulebook or retrieve it from the cache.
     $out = Cache::remember('rulebook', 24 * 60, function () {
         $baseRulebook = [];
         $baseRulebook['sects'] = $this->expandSects(RulebookSect::get());
         $baseRulebook['clans'] = $this->expandClans(RulebookClan::get());
         $baseRulebook['rituals'] = $this->sortByGroups(RulebookRitual::whereNull('owner')->get(), "rituals");
         $baseRulebook['backgrounds'] = $this->sortByGroups(RulebookBackground::orderBy('name')->get(), "backgrounds");
         $baseRulebook['disciplines'] = $this->expandDisciplines(RulebookDiscipline::orderBy('name')->get());
         $baseRulebook['natures'] = RulebookNature::get();
         $baseRulebook['abilities'] = $this->sortByGroups(RulebookAbility::where('owner', null)->orderBy('name')->get(), "abilities");
         $baseRulebook["paths"] = $this->expandPaths(RulebookPath::get());
         $baseRulebook["derangements"] = RulebookDerangement::get();
         $baseRulebook['merits'] = $this->sortByGroups(RulebookMerit::orderBy("cost")->get(), "merits");
         $baseRulebook['flaws'] = $this->sortByGroups(RulebookFlaw::orderBy("cost")->get(), "flaws");
         return $baseRulebook;
     });
     if ($owner != -1) {
         $out['custom_abilities'] = RulebookAbility::where('owner', $owner)->get();
         $out['elder_powers'] = RulebookElderPower::where('owner_id', $owner)->get();
         $out['combo_disciplines'] = RulebookComboDiscipline::where('owner_id', $owner)->get();
         $out['custom_rituals'] = RulebookRitual::where('owner', $owner)->get();
     }
     return Response::json($out);
 }
         if (isset($path)) {
             $discipline = RulebookDisciplinePath::find($path);
         }
         if ($diff != 0) {
             $order = $diff > 0 ? [$r1, $r2] : [$r2, $r1];
             for ($i = $order[0]; $i < $order[1]; $i++) {
                 $diff_array = $diff > 0 ? [null, $discipline->ranks()->get()[$i]->name . " (" . $discipline->name . " " . ($i + 1) . ")"] : [$discipline->ranks()->get()[$i]->name . " (" . $discipline->name . " " . ($i + 1) . ")", null];
                 print_change("", $diff_array, ["Purchased .", "[change]", "Removed ."]);
             }
         }
     }
 }
 if ($key == "elderPowers" && sizeof($value) > 0) {
     echo "<h5>Elder Powers</h5>";
     foreach ($value as $k => $v) {
         print_change("", $v["character_id"][0] == null ? [null, RulebookElderPower::find($k)->name] : [RulebookElderPower::find($k)->name, null], ["Purchased .", "[change]", "Removed ."]);
     }
 }
 if ($key == "comboDisciplines" && sizeof($value) > 0) {
     echo "<h5>Combo Disciplines</h5>";
     foreach ($value as $k => $v) {
         print_change("", $v["character_id"][0] == null ? [RulebookComboDiscipline::find($k)->name, null] : [null, RulebookComboDiscipline::find($k)->name], ["Purchased .", "[change]", "Removed ."]);
     }
 }
 if ($key == "path") {
     echo "<h5>Path</h5>";
     $path = $character->path($character->latestVersion()->version)->first();
     if ($path) {
         $path = $path->definition;
         print_change("", @$value["path_id"], ["Began on .", "Changed from  -> .", "Removed path (Is this a mistake?)."], function ($val) {
             return RulebookPath::find($val)->name;
 public function addElderPower($elderData)
 {
     $elderRecord = $this->findOneWhere("CharacterElderPower", "elder_id", $elderData["id"]);
     if (!$elderRecord) {
         $elderDefinition = RulebookElderPower::firstOrCreate(['owner_id' => $this->character_id, 'discipline_id' => $elderData['discipline'], 'name' => $elderData['name'], 'description' => $elderData['description']]);
         $elderRecord = $this->createNewRecord("CharacterElderPower");
         $elderRecord->elder_id = $elderDefinition->id;
         if ($this->editingAsStoryteller()) {
             $elderRecord->free_points = 12;
         }
         $elderRecord->save();
     }
     $this->touchedRecords["CharacterElderPower"][] = $elderRecord->id;
 }