]> git.localhorst.tv Git - alttp.git/blob - app/Models/Channel.php
simple guessing game
[alttp.git] / app / Models / Channel.php
1 <?php
2
3 namespace App\Models;
4
5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Database\Eloquent\Model;
7
8 class Channel extends Model
9 {
10         use HasFactory;
11
12         public function getCurrentEpisode() {
13                 return $this->episodes()
14                         ->where('start', '<', now()->subMinutes(10))
15                         ->orderBy('start', 'DESC')
16                         ->first();
17         }
18
19         public function hasActiveGuessing() {
20                 return !is_null($this->guessing_start);
21         }
22
23         public function isAcceptingGuesses() {
24                 return !is_null($this->guessing_start) && is_null($this->guessing_end);
25         }
26
27         public function startGuessing($type) {
28                 $this->guessing_type = $type;
29                 $this->guessing_start = now();
30                 $this->save();
31         }
32
33         public function stopGuessing() {
34                 $this->guessing_end = now();
35                 $this->save();
36         }
37
38         public function solveGuessing($solution) {
39                 $start = $this->guessing_start;
40                 $end = is_null($this->guessing_end) ? now() : $this->guessing_end;
41                 $guesses = $this->guesses()->whereBetween('created_at', [$start, $end])->orderBy('created_at', 'ASC')->get();
42                 $unique_guesses = [];
43                 foreach ($guesses as $guess) {
44                         $unique_guesses[$guess->uid] = $guess;
45                 }
46                 $candidates = [];
47                 foreach ($unique_guesses as $guess) {
48                         if ($guess->guess == $solution) {
49                                 $candidates[] = $guess;
50                         }
51                 }
52                 if (empty($candidates) && is_numeric($solution)) {
53                         $min_distance = null;
54                         foreach ($unique_guesses as $guess) {
55                                 $distance = abs(intval($guess->guess) - intval($solution));
56                                 if (is_null($min_distance) || $distance == $min_distance) {
57                                         $candidates[] = $guess;
58                                 } else if ($distance < $min_distance) {
59                                         $candidates = [$guess];
60                                         $min_distance = $distance;
61                                 }
62                         }
63                 }
64                 $winners = [];
65                 $first = true;
66                 foreach ($candidates as $candidate) {
67                         $score = $this->scoreGuessing($solution, $candidate->guess, $first);
68                         $winner = new GuessingWinner();
69                         $winner->channel()->associate($this);
70                         $winner->pod = $start;
71                         $winner->uid = $candidate->uid;
72                         $winner->uname = $candidate->uname;
73                         $winner->guess = $candidate->guess;
74                         $winner->solution = $solution;
75                         $winner->score = $score;
76                         $winner->save();
77                         $winners[] = $winner;
78                         $first = false;
79                 }
80                 return $winners;
81         }
82
83         public function clearGuessing() {
84                 $this->guessing_start = null;
85                 $this->guessing_end = null;
86                 $this->save();
87         }
88
89         public function registerGuess($uid, $uname, $guess) {
90                 $model = new GuessingGuess();
91                 $model->channel()->associate($this);
92                 $model->uid = $uid;
93                 $model->uname = $uname;
94                 $model->guess = $guess;
95                 $model->save();
96         }
97
98         public function scoreGuessing($solution, $guess, $first) {
99                 return 1;
100         }
101
102         public function isValidGuess($solution) {
103                 if ($this->guessing_type == 'gtbk') {
104                         $int_solution = intval($solution);
105                         return $int_solution > 0 && $int_solution < 23;
106                 }
107                 return false;
108         }
109
110         public function crews() {
111                 return $this->hasMany(ChannelCrew::class);
112         }
113
114         public function episodes() {
115                 return $this->belongsToMany(Episode::class)
116                         ->using(Restream::class)
117                         ->withPivot('accept_comms', 'accept_tracker');
118         }
119
120         public function guesses() {
121                 return $this->hasMany(GuessingGuess::class);
122         }
123
124         public function organization() {
125                 return $this->belongsTo(Organization::class);
126         }
127
128         protected $casts = [
129                 'chat' => 'boolean',
130                 'chat_commands' => 'array',
131                 'chat_settings' => 'array',
132                 'guessing_start' => 'datetime',
133                 'guessing_end' => 'datetime',
134                 'languages' => 'array',
135                 'join' => 'boolean',
136         ];
137
138         protected $hidden = [
139                 'created_at',
140                 'ext_id',
141                 'updated_at',
142         ];
143
144 }