]> git.localhorst.tv Git - alttp.git/blob - app/Models/Channel.php
guessing game controls
[alttp.git] / app / Models / Channel.php
1 <?php
2
3 namespace App\Models;
4
5 use Illuminate\Broadcasting\PrivateChannel;
6 use Illuminate\Database\Eloquent\BroadcastsEvents;
7 use Illuminate\Database\Eloquent\Factories\HasFactory;
8 use Illuminate\Database\Eloquent\Model;
9 use Illuminate\Support\Arr;
10
11 class Channel extends Model {
12
13         use BroadcastsEvents;
14         use HasFactory;
15
16         public function broadcastOn($event) {
17                 $channels = [
18                         new PrivateChannel('Channel.'.$this->id),
19                 ];
20                 return $channels;
21         }
22
23         public function getCurrentEpisode() {
24                 return $this->episodes()
25                         ->where('start', '<', now()->subMinutes(10))
26                         ->orderBy('start', 'DESC')
27                         ->first();
28         }
29
30         public function getGuessingLeaderboard() {
31                 return $this->winners()->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')->groupBy('uid')->orderBy('score', 'desc')->limit(10)->get();
32         }
33
34         public function hasActiveGuessing() {
35                 return !is_null($this->guessing_start);
36         }
37
38         public function isAcceptingGuesses() {
39                 return !is_null($this->guessing_start) && is_null($this->guessing_end);
40         }
41
42         public function startGuessing($type) {
43                 $this->guessing_type = $type;
44                 $this->guessing_start = now();
45                 $this->save();
46         }
47
48         public function stopGuessing() {
49                 $this->guessing_end = now();
50                 $this->save();
51         }
52
53         public function getGuessingSetting($name, $default = null) {
54                 if (empty($this->guessing_settings) ||
55                         empty($this->guessing_type) ||
56                         !array_key_exists($this->guessing_type, $this->guessing_settings) ||
57                         !array_key_exists($name, $this->guessing_settings[$this->guessing_type])
58                 ) {
59                         return $default;
60                 }
61                 return $this->guessing_settings[$this->guessing_type][$name];
62         }
63
64         public function solveGuessing($solution) {
65                 $start = $this->guessing_start;
66                 $end = is_null($this->guessing_end) ? now() : $this->guessing_end;
67                 $guesses = $this->guesses()->whereBetween('created_at', [$start, $end])->orderBy('created_at', 'ASC')->get();
68                 $unique_guesses = [];
69                 foreach ($guesses as $guess) {
70                         $unique_guesses[$guess->uid] = $guess;
71                 }
72                 $candidates = [];
73                 foreach ($unique_guesses as $guess) {
74                         if ($guess->guess == $solution) {
75                                 $candidates[] = $guess;
76                         }
77                 }
78                 if (empty($candidates) && is_numeric($solution)) {
79                         $min_distance = null;
80                         foreach ($unique_guesses as $guess) {
81                                 $distance = abs(intval($guess->guess) - intval($solution));
82                                 if (is_null($min_distance) || $distance == $min_distance) {
83                                         $candidates[] = $guess;
84                                 } else if ($distance < $min_distance) {
85                                         $candidates = [$guess];
86                                         $min_distance = $distance;
87                                 }
88                         }
89                 }
90                 $winners = [];
91                 $first = true;
92                 foreach ($candidates as $candidate) {
93                         $score = $this->scoreGuessing($solution, $candidate->guess, $first);
94                         $winner = new GuessingWinner();
95                         $winner->channel()->associate($this);
96                         $winner->pod = $start;
97                         $winner->uid = $candidate->uid;
98                         $winner->uname = $candidate->uname;
99                         $winner->guess = $candidate->guess;
100                         $winner->solution = $solution;
101                         $winner->score = $score;
102                         $winner->save();
103                         $winners[] = $winner;
104                         $first = false;
105                 }
106                 return $winners;
107         }
108
109         public function clearGuessing() {
110                 $this->guessing_start = null;
111                 $this->guessing_end = null;
112                 $this->save();
113         }
114
115         public function registerGuess($uid, $uname, $guess) {
116                 $model = new GuessingGuess();
117                 $model->channel()->associate($this);
118                 $model->uid = $uid;
119                 $model->uname = $uname;
120                 $model->guess = $guess;
121                 $model->save();
122         }
123
124         public function scoreGuessing($solution, $guess, $first) {
125                 if ($guess == $solution) {
126                         if ($first) {
127                                 return $this->getGuessingSetting('points_exact_first', 1);
128                         }
129                         return $this->getGuessingSetting('points_exact_other', 1);
130                 }
131                 $distance = abs(intval($guess) - intval($solution));
132                 if ($distance <= $this->getGuessingSetting('points_close_max', 3)) {
133                         if ($first) {
134                                 return $this->getGuessingSetting('points_close_first', 1);
135                         }
136                         return $this->getGuessingSetting('points_close_other', 1);
137                 }
138                 return 0;
139         }
140
141         public function isValidGuess($solution) {
142                 if ($this->guessing_type == 'gtbk') {
143                         $int_solution = intval($solution);
144                         return $int_solution > 0 && $int_solution < 23;
145                 }
146                 return false;
147         }
148
149         public function listAnd($entries) {
150                 $lang = empty($this->languages) ? 'en' : $this->languages[0];
151                 if ($lang == 'de') {
152                         return Arr::join($entries, ', ', ' und ');
153                 }
154                 return Arr::join($entries, ', ', ' and ');
155         }
156
157         public function crews() {
158                 return $this->hasMany(ChannelCrew::class);
159         }
160
161         public function episodes() {
162                 return $this->belongsToMany(Episode::class)
163                         ->using(Restream::class)
164                         ->withPivot('accept_comms', 'accept_tracker');
165         }
166
167         public function guesses() {
168                 return $this->hasMany(GuessingGuess::class);
169         }
170
171         public function organization() {
172                 return $this->belongsTo(Organization::class);
173         }
174
175         public function winners() {
176                 return $this->hasMany(GuessingWinner::class);
177         }
178
179         protected $casts = [
180                 'chat' => 'boolean',
181                 'chat_commands' => 'array',
182                 'chat_settings' => 'array',
183                 'guessing_settings' => 'array',
184                 'guessing_start' => 'datetime',
185                 'guessing_end' => 'datetime',
186                 'languages' => 'array',
187                 'join' => 'boolean',
188         ];
189
190         protected $hidden = [
191                 'created_at',
192                 'ext_id',
193                 'updated_at',
194         ];
195
196 }