]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/TechniqueController.php
serverside tech init
[alttp.git] / app / Http / Controllers / TechniqueController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\Models\Technique;
6 use App\Models\TechniqueMap;
7 use App\Models\TechniqueTranslation;
8 use Illuminate\Database\Eloquent\Builder;
9 use Illuminate\Database\Eloquent\ModelNotFoundException;
10 use Illuminate\Http\Request;
11
12 class TechniqueController extends Controller
13 {
14
15         public function byType(Request $request, $type) {
16                 $techs = Technique::where('index', true)->where('type', '=', $type);
17                 $this->applyFilter($request, $techs);
18                 return $techs->get()->toJson();
19         }
20
21         public function byTypeAndName(Request $request, $type, $name) {
22                 $tech = Technique::where('type', '=', $type)->where('name', '=', $name)->firstOrFail();
23                 $this->authorize('view', $tech);
24                 $tech->load(['chapters', 'relations']);
25                 return $tech->toJson();
26         }
27
28         public function forMap($map) {
29                 $techs = TechniqueMap::with(['technique', 'technique.relations'])->where('map', '=', $map);
30
31                 return $techs->get()->toJson();
32         }
33
34         public function search(Request $request) {
35                 $techs = Technique::where('index', '=', 1);
36                 $this->applyFilter($request, $techs);
37                 return $techs->get()->toJson();
38         }
39
40         public function single(Request $request, Technique $tech) {
41                 $this->authorize('view', $tech);
42                 $tech->load(['chapters', 'relations']);
43                 return $tech->toJson();
44         }
45
46         public function update(Request $request, Technique $content) {
47                 $this->authorize('update', $content);
48
49                 $validatedData = $request->validate([
50                         'attribution' => 'string',
51                         'description' => 'string',
52                         'language' => 'string|in:de,en',
53                         'parent_id' => 'integer|exists:App\\Models\\Technique,id',
54                         'short' => 'string',
55                         'title' => 'string',
56                 ]);
57
58                 if ($validatedData['language'] == 'en') {
59                         $this->applyLocalizedValues($validatedData, $content);
60                         $content->save();
61                 } else {
62                         $translation = $this->getTranslation($content, $validatedData['language']);
63                         $this->applyLocalizedValues($validatedData, $translation);
64                         $translation->save();
65                 }
66
67                 $result = isset($validatedData['parent_id']) ? Technique::findOrFail($validatedData['parent_id']) : $content->fresh();
68                 $result->load(['chapters', 'relations']);
69                 return $result->toJson();
70         }
71
72         public function web(Request $request, $type, $name) {
73                 $tech = Technique::where('type', '=', $type)->where('name', '=', $name)->first();
74                 if ($tech) {
75                         return view('app')
76                                 ->with('title', $tech->getTranslatedProperty('title'))
77                                 ->with('description', $tech->getTranslatedProperty('short'));
78                 }
79                 $url_map = [
80                         'dungeon' => 'dungeons',
81                         'location' => 'locations',
82                         'mode' => 'modes',
83                         'ruleset' => 'rulesets',
84                         'tech' => 'tech',
85                 ];
86                 $tech = Technique::where('name', '=', $name)->whereIn('type', array_keys($url_map))->first();
87                 if ($tech && isset($url_map[$tech->type])) {
88                         return redirect('/'.$url_map[$tech->type].'/'.$tech->name);
89                 }
90                 throw new ModelNotFoundException();
91         }
92
93         private function applyLocalizedValues($validatedData, $content) {
94                 foreach (['attribution', 'description', 'short', 'title'] as $name) {
95                         if (isset($validatedData[$name])) {
96                                 $content->{$name} = $validatedData[$name];
97                         }
98                 }
99         }
100
101         private function getTranslation(Technique $content, $language) {
102                 foreach ($content->translations as $translation) {
103                         if ($translation->locale == $language) {
104                                 return $translation;
105                         }
106                 }
107                 $translation = new TechniqueTranslation();
108                 $translation->technique_id = $content->id;
109                 $translation->locale = $language;
110                 return $translation;
111         }
112
113         protected function applyFilter(Request $request, Builder $query) {
114                 $validatedData = $request->validate([
115                         'phrase' => 'string|nullable',
116                         'ruleset' => 'array|nullable',
117                         'ruleset.competitive' => 'boolean',
118                         'ruleset.mg' => 'boolean',
119                         'ruleset.nl' => 'boolean',
120                         'ruleset.owg' => 'boolean',
121                         'type' => 'string|nullable',
122                 ]);
123
124                 if (!empty($validatedData['type'])) {
125                         $query->where('type', '=', $validatedData['type']);
126                 }
127
128                 if (!empty($validatedData['phrase'])) {
129                         $search = $validatedData['phrase'];
130                         $query->where(function (Builder $query) use ($search) {
131                                 $query->where('title', 'LIKE', '%'.$search.'%')
132                                 ->orWhere('short', 'LIKE', '%'.$search.'%');
133                         });
134                 }
135
136                 if (isset($validatedData['ruleset'])) {
137                         $com = isset($validatedData['ruleset']['competitive']) && $validatedData['ruleset']['competitive'];
138                         $owg = isset($validatedData['ruleset']['owg']) && $validatedData['ruleset']['owg'];
139                         $mg = isset($validatedData['ruleset']['mg']) && $validatedData['ruleset']['mg'];
140                         $nl = isset($validatedData['ruleset']['nl']) && $validatedData['ruleset']['nl'];
141                         $any = $com || $owg || $mg || $nl;
142                         $all = $com && $owg && $mg && $nl;
143                         if ($any && !$all) {
144                                 $query->where(function(Builder $query) use ($com, $owg, $mg, $nl) {
145                                         $query->whereNull('rulesets');
146                                         if ($com) {
147                                                 $query->orWhere('rulesets->competitive', '=', true);
148                                         }
149                                         if ($owg) {
150                                                 $query->orWhere('rulesets->owg', '=', true);
151                                         }
152                                         if ($mg) {
153                                                 $query->orWhere('rulesets->mg', '=', true);
154                                         }
155                                         if ($nl) {
156                                                 $query->orWhere('rulesets->nl', '=', true);
157                                         }
158                                 });
159                         }
160                 }
161         }
162
163 }