Completed
Push — master ( d89e8d...a4e309 )
by Vladimir
04:11
created

Map::setRandomlyGenerated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file contains functionality relating to a BZFS world file
4
 *
5
 * @package    BZiON\Models
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
/**
10
 * A BZFlag server map
11
 * @package    BZiON\Models
12
 */
13
class Map extends AvatarModel implements NamedModel
14
{
15
    /**
16
     * The name of the map
17
     * @var string
18
     */
19
    protected $name;
20
21
    /**
22
     * A description of the map
23
     * @var string
24
     */
25
    protected $description;
26
27
    /**
28
     * The world size of the map
29
     * @var int|null
30
     */
31
    protected $world_size;
32
33
    /**
34
     * Whether or not the map is randomly generated each match
35
     * @var bool
36
     */
37
    protected $randomly_generated;
38
39
    /**
40
     * The number of a shots this map has
41
     * @var int
42
     */
43
    protected $shot_count;
44
45
    /**
46
     * Whether or not this map has ricochet
47
     * @var bool
48
     */
49
    protected $ricochet;
50
51
    /**
52
     * Whether or not this map has jumping
53
     * @var bool
54
     */
55
    protected $jumping;
56
57
    /**
58
     * The game mode this map supports
59
     *
60
     * @see Map::GAME_MODE_CTF
61
     * @see Map::GAME_MODE_AHOD
62
     *
63
     * @var int
64
     */
65
    protected $game_mode;
66
67
    /**
68
     * The status of the map
69
     * @var string
70
     */
71
    protected $status;
72
73
    /**
74
     * The name of the database table used for queries
75
     */
76
    const TABLE = "maps";
77
78
    /**
79
     * The location where avatars will be stored
80
     */
81
    const AVATAR_LOCATION = "/web/assets/imgs/avatars/maps/";
82
83
    const CREATE_PERMISSION = Permission::ADD_MAP;
84
    const EDIT_PERMISSION = Permission::EDIT_MAP;
85
    const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_MAP;
86
    const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_MAP;
87 21
88
    const GAME_MODE_CTF = 1;
89 21
    const GAME_MODE_AHOD = 2;
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    protected function assignResult($map)
95
    {
96
        $this->name = $map['name'];
97
        $this->description = $map['description'];
98
        $this->world_size = $map['world_size'];
99
        $this->randomly_generated = $map['randomly_generated'];
100
        $this->alias = $map['alias'];
101
        $this->avatar = $map['avatar'];
102
        $this->shot_count = $map['shot_count'];
103
        $this->ricochet = $map['ricochet'];
104
        $this->jumping = $map['jumping'];
105
        $this->game_mode = $map['game_mode'];
106
        $this->status = $map['status'];
107
    }
108
109
    /**
110
     * Add a new map
111
     *
112
     * @param string      $name        The name of the map
113
     * @param string|null $alias       The custom API-friendly alias of the map
114
     * @param string      $description The description of the map
115
     * @param string|null $avatar      An image of the map
116
     * @param string      $status      The status of the map (active, hidden, disabled or deleted)
117
     *
118
     * @return static
119
     */
120
    public static function addMap($name, $alias = null, $description = '', $avatar = null, $status = 'active')
121
    {
122
        return self::create(array(
123
            'name'        => $name,
124
            'alias'       => $alias,
125
            'description' => $description,
126
            'avatar'      => $avatar,
127
            'status'      => $status
128
        ));
129
    }
130
131
    /**
132
     * Get the name of the map
133
     *
134
     * @return string
135
     */
136
    public function getName()
137
    {
138
        return $this->name;
139
    }
140
141
    /**
142
     * Get the description of the map
143
     *
144
     * @return string
145
     */
146
    public function getDescription()
147
    {
148
        return $this->description;
149 1
    }
150
151 1
    /**
152 1
     * Get the world size of the map
153
     *
154
     * @return int|null
155
     */
156
    public function getWorldSize()
157
    {
158
        return $this->world_size;
159
    }
160
161
    /**
162
     * Get the number of shots this map has
163
     *
164
     * @return int
165
     */
166
    public function getShotCount()
167
    {
168
        return $this->shot_count;
169
    }
170
171
    /**
172
     * Get the game mode supported by this map
173
     *
174
     * @see Map::GAME_MODE_CTF
175
     * @see Map::GAME_MODE_AHOD
176
     *
177
     * @return int
178
     */
179
    public function getGameMode()
180
    {
181
        return (int)$this->game_mode;
182
    }
183
184
    /**
185
     * Get whether or not the map is randomly generated each match
186
     *
187
     * @return bool
188
     */
189
    public function isRandomlyGenerated()
190
    {
191
        return (bool)$this->randomly_generated;
192
    }
193
194
    /**
195
     * Get whether or not ricochet is enabled
196
     *
197
     * @return bool
198
     */
199
    public function isRicochetEnabled()
200
    {
201
        return (bool)$this->ricochet;
202
    }
203
204
    /**
205
     * Get whether or not jumping is enabled
206
     *
207
     * @return bool
208
     */
209
    public function isJumpingEnabled()
210
    {
211
        return (bool)$this->jumping;
212
    }
213
214
    /**
215
     * Set the name of the map
216
     *
217
     * @param string $name The new name
218
     * @return self
219
     */
220
    public function setName($name)
221
    {
222
        return $this->updateProperty($this->name, 'name', $name);
223
    }
224
225
    /**
226
     * Set the description of the map
227
     *
228
     * @param string $description The new description
229
     * @return self
230
     */
231
    public function setDescription($description)
232
    {
233
        return $this->updateProperty($this->description, 'description', $description);
234
    }
235
236
    /**
237
     * Set the world size of this map
238
     *
239
     * @param int $world_size
240
     *
241
     * @return Map
242
     */
243
    public function setWorldSize($world_size)
244
    {
245
        return $this->updateProperty($this->world_size, 'world_size', $world_size);
246
    }
247
248
    /**
249
     * Set the number of shots this map has
250
     *
251
     * @param int $shot_count
252
     *
253
     * @return self
254
     */
255
    public function setShotCount($shot_count)
256
    {
257
        return $this->updateProperty($this->shot_count, 'shot_count', $shot_count);
258
    }
259
260
    /**
261
     * Set whether or not this map is randomly generated
262
     *
263
     * @param bool $randomly_generated
264
     *
265
     * @return Map
266
     */
267
    public function setRandomlyGenerated($randomly_generated)
268
    {
269
        return $this->updateProperty($this->randomly_generated, 'randomly_generated', $randomly_generated);
270
    }
271
272
    /**
273
     * Set whether or not this map supports ricochet
274
     *
275
     * @param bool $ricochet
276
     *
277
     * @return self
278
     */
279
    public function setRicochetEnabled($ricochet)
280
    {
281
        return $this->updateProperty($this->ricochet, 'ricochet', $ricochet);
282
    }
283
284
    /**
285
     * Set whether or not this map supports jumping
286
     *
287
     * @param bool $jumping
288
     *
289
     * @return self
290
     */
291
    public function setJumpingEnabled($jumping)
292
    {
293
        return $this->updateProperty($this->jumping, 'jumping', $jumping);
294
    }
295
296
    /**
297
     * Set the game mode for this map
298
     *
299
     * @param int $game_mode
300
     *
301
     * @see Map::GAME_MODE_CTF
302
     * @see Map::GAME_MODE_AHOD
303
     *
304
     * @return Map
305
     */
306
    public function setGameMode($game_mode)
307
    {
308
        return $this->updateProperty($this->game_mode, 'game_mode', $game_mode);
309
    }
310
311
    /**
312
     * Get the number of matches played on this map
313
     *
314
     * @return int
315
     */
316
    public function countMatches()
317
    {
318
        return Match::getQueryBuilder()
319
            ->active()
320
            ->where('map')->is($this)
321
            ->count();
322
    }
323
324
    /**
325
     * Get a query builder for news
326
     * @return QueryBuilder
327
     */
328 View Code Duplication
    public static function getQueryBuilder()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
329
    {
330
        return new QueryBuilder('Map', array(
331
            'columns' => array(
332
                'name'   => 'name',
333
                'status' => 'status'
334
            ),
335
            'name' => 'name'
336
        ));
337
    }
338
}
339