Repository navigation
Expand file tree
/
Copy pathPlayerStatsPerGameTable.php
More file actions
204 lines (170 loc) · 7.08 KB
/
Copy pathPlayerStatsPerGameTable.php
File metadata and controls
204 lines (170 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
declare(strict_types=1);
namespace App\Model\Table;
use ArrayObject;
use Cake\Event\EventInterface;
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* PlayerStatsPerGame Model
*
* @property \App\Model\Table\GamesTable&\Cake\ORM\Association\BelongsTo $Games
* @property \App\Model\Table\PlayersTable&\Cake\ORM\Association\BelongsTo $Players
*
* @method \App\Model\Entity\PlayerStatsPerGame newEmptyEntity()
* @method \App\Model\Entity\PlayerStatsPerGame newEntity(array $data, array $options = [])
* @method array<\App\Model\Entity\PlayerStatsPerGame> newEntities(array $data, array $options = [])
* @method \App\Model\Entity\PlayerStatsPerGame get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
* @method \App\Model\Entity\PlayerStatsPerGame findOrCreate($search, ?callable $callback = null, array $options = [])
* @method \App\Model\Entity\PlayerStatsPerGame patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method array<\App\Model\Entity\PlayerStatsPerGame> patchEntities(iterable $entities, array $data, array $options = [])
* @method \App\Model\Entity\PlayerStatsPerGame|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
* @method \App\Model\Entity\PlayerStatsPerGame saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
* @method iterable<\App\Model\Entity\PlayerStatsPerGame>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\PlayerStatsPerGame>|false saveMany(iterable $entities, array $options = [])
* @method iterable<\App\Model\Entity\PlayerStatsPerGame>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\PlayerStatsPerGame> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<\App\Model\Entity\PlayerStatsPerGame>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\PlayerStatsPerGame>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<\App\Model\Entity\PlayerStatsPerGame>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\PlayerStatsPerGame> deleteManyOrFail(iterable $entities, array $options = [])
*/
class PlayerStatsPerGameTable extends Table
{
/** Played less than this (minutes on a team) = not counted as a game played */
public const MIN_MINUTES = 3;
/**
* SQL counting a player's games played: short appearances (under
* MIN_MINUTES) are left out; they still count for every other total.
*/
public static function countedGamesSql(string $alias = 'PlayerStatsPerGame'): string
{
return "COUNT(CASE WHEN $alias.minutes_played IS NULL OR $alias.minutes_played >= " . self::MIN_MINUTES . ' THEN 1 END)';
}
/**
* Initialize method
*
* @param array<string, mixed> $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('player_stats_per_game');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->belongsTo('Games', [
'foreignKey' => 'game_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Players', [
'foreignKey' => 'player_id',
'joinType' => 'INNER',
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->uuid('game_id')
->notEmptyString('game_id');
$validator
->uuid('player_id')
->notEmptyString('player_id');
$validator
->integer('kills')
->allowEmptyString('kills');
$validator
->integer('teamkills')
->allowEmptyString('teamkills');
$validator
->integer('deaths')
->allowEmptyString('deaths');
$validator
->integer('headshot')
->allowEmptyString('headshot');
$validator
->integer('busted')
->allowEmptyString('busted');
$validator
->integer('shredded')
->allowEmptyString('shredded');
$validator
->integer('peppered')
->allowEmptyString('peppered');
$validator
->integer('sprayed')
->allowEmptyString('sprayed');
$validator
->integer('punctured')
->allowEmptyString('punctured');
$validator
->integer('splattered')
->allowEmptyString('splattered');
$validator
->integer('slashed')
->allowEmptyString('slashed');
$validator
->integer('gibbed')
->allowEmptyString('gibbed');
$validator
->integer('suicided')
->allowEmptyString('suicided');
$validator
->integer('longest_streak')
->allowEmptyString('longest_streak');
$validator
->integer('picked_off')
->allowEmptyString('picked_off');
$validator
->integer('stole_the_flag')
->allowEmptyString('stole_the_flag');
$validator
->integer('lost_the_flag')
->allowEmptyString('lost_the_flag');
$validator
->integer('returned_the_flag')
->allowEmptyString('returned_the_flag');
$validator
->integer('scored_with_the_flag')
->allowEmptyString('scored_with_the_flag');
$validator
->numeric('kd_ratio')
->allowEmptyString('kd_ratio');
$validator
->numeric('total_score')
->allowEmptyString('total_score');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->isUnique(['game_id', 'player_id']), ['errorField' => 'game_id']);
$rules->add($rules->existsIn(['game_id'], 'Games'), ['errorField' => 'game_id']);
$rules->add($rules->existsIn(['player_id'], 'Players'), ['errorField' => 'player_id']);
return $rules;
}
/**
* Stats of games marked inaccurate (games.inaccurate) never count:
* every find - direct, contained or joined - drops them. Pass
* applyOptions(['includeInaccurate' => true]) to see them anyway.
*/
public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options, bool $primary): void
{
if (!empty($options['includeInaccurate'])) {
return;
}
$query->where([
$this->aliasField('game_id') . ' NOT IN (SELECT id FROM games WHERE inaccurate = 1)',
]);
}
}