]> git.localhorst.tv Git - alttp.git/blob - app/Models/Channel.php
some guessing game fixes
[alttp.git] / app / Models / Channel.php
1 <?php
2
3 namespace App\Models;
4
5 use Illuminate\Broadcasting\Channel as PublicChannel;
6 use Illuminate\Broadcasting\PrivateChannel;
7 use Illuminate\Database\Eloquent\BroadcastsEvents;
8 use Illuminate\Database\Eloquent\Factories\HasFactory;
9 use Illuminate\Database\Eloquent\Model;
10 use Illuminate\Support\Arr;
11
12 class Channel extends Model {
13
14         use BroadcastsEvents;
15         use HasFactory;
16
17         public function broadcastOn($event) {
18                 $channels = [
19                         new PrivateChannel('Channel.'.$this->id),
20                 ];
21                 if (!empty($this->access_key)) {
22                         $channels[] = new PublicChannel('ChannelKey.'.$this->access_key);
23                 }
24                 return $channels;
25         }
26
27         public function getCurrentEpisode() {
28                 return $this->episodes()
29                         ->where('start', '<', now()->subMinutes(10))
30                         ->orderBy('start', 'DESC')
31                         ->first();
32         }
33
34         public function getGuessingLeaderboard() {
35                 $query = $this->winners()
36                         ->selectRaw('(select t2.uname from guessing_winners t2 where t2.uid = guessing_winners.uid order by created_at desc limit 1) as name, sum(score) as score')
37                         ->groupBy('uid')
38                         ->orderBy('score', 'desc')
39                         ->limit(10);
40                 $type = $this->getGuessingSetting('leaderboard_type', 'all');
41                 if ($type == 'month') {
42                         $query->where('created_at', '>=', now()->startOfMonth());
43                 } else if ($type == 'year') {
44                         $query->where('created_at', '>=', now()->startOfYear());
45                 } else if (is_numeric($type)) {
46                         $query->where('created_at', '>=', now()->sub($type, 'days'));
47                 }
48                 return $query->get();
49         }
50
51         public function hasActiveGuessing() {
52                 return !is_null($this->guessing_start);
53         }
54
55         public function isAcceptingGuesses() {
56                 return !is_null($this->guessing_start) && is_null($this->guessing_end);
57         }
58
59         public function startGuessing($type) {
60                 $this->guessing_type = $type;
61                 $this->guessing_start = now();
62                 $this->save();
63         }
64
65         public function stopGuessing() {
66                 $this->guessing_end = now();
67                 $this->save();
68         }
69
70         public function getGuessingSetting($name, $default = null) {
71                 if (empty($this->guessing_settings) ||
72                         empty($this->guessing_type) ||
73                         !array_key_exists($this->guessing_type, $this->guessing_settings) ||
74                         !array_key_exists($name, $this->guessing_settings[$this->guessing_type])
75                 ) {
76                         return $default;
77                 }
78                 return $this->guessing_settings[$this->guessing_type][$name];
79         }
80
81         public function solveGuessing($solution) {
82                 $start = $this->guessing_start;
83                 $end = is_null($this->guessing_end) ? now() : $this->guessing_end;
84                 $guesses = $this->guesses()->whereBetween('created_at', [$start, $end])->orderBy('created_at', 'ASC')->get();
85                 $unique_guesses = [];
86                 foreach ($guesses as $guess) {
87                         $unique_guesses[$guess->uid] = $guess;
88                 }
89                 $candidates = [];
90                 foreach ($unique_guesses as $guess) {
91                         if ($guess->guess == $solution) {
92                                 $candidates[] = $guess;
93                         }
94                 }
95                 if (empty($candidates) && is_numeric($solution)) {
96                         $min_distance = null;
97                         foreach ($unique_guesses as $guess) {
98                                 $distance = abs(intval($guess->guess) - intval($solution));
99                                 if (is_null($min_distance) || $distance == $min_distance) {
100                                         $candidates[] = $guess;
101                                         if (is_null($min_distance)) {
102                                                 $min_distance = $distance;
103                                         }
104                                 } else if ($distance < $min_distance) {
105                                         $candidates = [$guess];
106                                         $min_distance = $distance;
107                                 }
108                         }
109                 }
110                 $winners = [];
111                 $first = true;
112                 foreach ($candidates as $candidate) {
113                         $score = $this->scoreGuessing($solution, $candidate->guess, $first);
114                         $winner = new GuessingWinner();
115                         $winner->channel()->associate($this);
116                         $winner->pod = $start;
117                         $winner->uid = $candidate->uid;
118                         $winner->uname = $candidate->uname;
119                         $winner->guess = $candidate->guess;
120                         $winner->solution = $solution;
121                         $winner->score = $score;
122                         $winner->save();
123                         $winners[] = $winner;
124                         $first = false;
125                 }
126                 return $winners;
127         }
128
129         public function clearGuessing() {
130                 $this->guessing_start = null;
131                 $this->guessing_end = null;
132                 $this->save();
133         }
134
135         public function transformGuess($original) {
136                 $transformed = trim($original);
137                 if ($this->guessing_type == 'gtbk') {
138                         $transformed = str_replace(['roodyo1Gtbigkey'], ['2'], $transformed);
139                         $transformed = str_ireplace(['vier 4Head'], ['4'], $transformed);
140                 }
141                 return $transformed;
142         }
143
144         public function registerGuess($uid, $uname, $guess) {
145                 $model = new GuessingGuess();
146                 $model->channel()->associate($this);
147                 $model->uid = $uid;
148                 $model->uname = $uname;
149                 $model->guess = $this->transformGuess($guess);
150                 $model->save();
151         }
152
153         public function scoreGuessing($solution, $guess, $first) {
154                 $transformed = $this->transformGuess($guess);
155                 if ($transformed == $solution) {
156                         if ($first) {
157                                 return $this->getGuessingSetting('points_exact_first', 1);
158                         }
159                         return $this->getGuessingSetting('points_exact_other', 1);
160                 }
161                 $distance = abs(intval($transformed) - intval($solution));
162                 if ($distance <= $this->getGuessingSetting('points_close_max', 3)) {
163                         if ($first) {
164                                 return $this->getGuessingSetting('points_close_first', 1);
165                         }
166                         return $this->getGuessingSetting('points_close_other', 1);
167                 }
168                 return 0;
169         }
170
171         public function isValidGuess($solution) {
172                 $transformed = $this->transformGuess($solution);
173                 if ($this->guessing_type == 'gtbk') {
174                         $int_solution = intval($transformed);
175                         return is_numeric($transformed) && $int_solution > 0 && $int_solution < 23;
176                 }
177                 return false;
178         }
179
180         public function listGuessingWinners($winners) {
181                 $names = [];
182                 $distance = 0;
183                 foreach ($winners as $winner) {
184                         if ($winner->score > 0) {
185                                 $names[] = $winner->uname;
186                                 $distance = abs(intval($winner->guess) - intval($winner->solution));
187                         }
188                 }
189                 $msg = '';
190                 if (empty($names)) {
191                         $msg = $this->getGuessingSetting('no_winners_message');
192                 } else {
193                         $msg = $this->getGuessingSetting($distance ? 'close_winners_message' : 'winners_message', $this->getGuessingSetting('winners_message'));
194                         $msg = str_replace(['{distance}', '{names}'], [$distance, $this->listAnd($names)], $msg);
195                 }
196                 return $msg;
197         }
198
199         public function listAnd($entries) {
200                 $lang = empty($this->languages) ? 'en' : $this->languages[0];
201                 if ($lang == 'de') {
202                         return Arr::join($entries, ', ', ' und ');
203                 }
204                 return Arr::join($entries, ', ', ' and ');
205         }
206
207         public function crews() {
208                 return $this->hasMany(ChannelCrew::class);
209         }
210
211         public function episodes() {
212                 return $this->belongsToMany(Episode::class)
213                         ->using(Restream::class)
214                         ->withPivot('accept_comms', 'accept_tracker');
215         }
216
217         public function guesses() {
218                 return $this->hasMany(GuessingGuess::class);
219         }
220
221         public function organization() {
222                 return $this->belongsTo(Organization::class);
223         }
224
225         public function winners() {
226                 return $this->hasMany(GuessingWinner::class);
227         }
228
229         protected $casts = [
230                 'chat' => 'boolean',
231                 'chat_commands' => 'array',
232                 'chat_settings' => 'array',
233                 'guessing_end' => 'datetime',
234                 'guessing_settings' => 'array',
235                 'guessing_start' => 'datetime',
236                 'languages' => 'array',
237                 'join' => 'boolean',
238         ];
239
240         protected $hidden = [
241                 'access_key',
242                 'chat',
243                 'chat_commands',
244                 'chat_settings',
245                 'created_at',
246                 'ext_id',
247                 'guessing_settings',
248                 'join',
249                 'twitch_chat',
250                 'updated_at',
251         ];
252
253 }