Test Failed
Push — dev ( 985fdf...79dec8 )
by Nico
14:06
created

User::getBuoys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Entity;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping\Column;
10
use Doctrine\ORM\Mapping\Entity;
11
use Doctrine\ORM\Mapping\GeneratedValue;
12
use Doctrine\ORM\Mapping\Id;
13
use Doctrine\ORM\Mapping\Index;
14
use Doctrine\ORM\Mapping\JoinColumn;
15
use Doctrine\ORM\Mapping\ManyToOne;
16
use Doctrine\ORM\Mapping\OneToMany;
17
use Doctrine\ORM\Mapping\OneToOne;
18
use Doctrine\ORM\Mapping\OrderBy;
19
use Doctrine\ORM\Mapping\Table;
20
use Stu\Component\Game\GameEnum;
21
use Stu\Component\Game\ModuleViewEnum;
22
use Stu\Component\Map\MapEnum;
23
use Stu\Component\Player\UserAwardEnum;
24
use Stu\Component\Player\UserCssClassEnum;
25
use Stu\Component\Player\UserRpgBehaviorEnum;
26
use Stu\Module\PlayerSetting\Lib\UserEnum;
27
use Stu\Module\PlayerSetting\Lib\UserSettingEnum;
28
29
#[Table(name: 'stu_user')]
30
#[Index(name: 'user_alliance_idx', columns: ['allys_id'])]
31
#[Entity(repositoryClass: 'Stu\Orm\Repository\UserRepository')]
32
class User implements UserInterface
33
{
34
    #[Id]
35
    #[Column(type: 'integer')]
36
    #[GeneratedValue(strategy: 'IDENTITY')]
37
    private int $id;
38
39
    #[Column(type: 'string')]
40
    private string $username = '';
41
42
    #[Column(type: 'string', length: 20)]
43
    private string $login = '';
44
45
    #[Column(type: 'string', length: 255)]
46
    private string $pass = '';
47
48
    #[Column(type: 'string', length: 6, nullable: true)]
49
    private ?string $sms_code = null;
50
51
    #[Column(type: 'string', length: 200)]
52
    private string $email = '';
53
54
    #[Column(type: 'string', length: 255, nullable: true)]
55
    private ?string $mobile = null;
56
57
    #[Column(type: 'integer', nullable: true)]
58
    private ?int $allys_id = null;
59
60
    #[Column(type: 'integer')]
61
    private int $race = 9;
62
63
    #[Column(type: 'smallint')]
64
    private int $state = UserEnum::USER_STATE_NEW;
65
66
    #[Column(type: 'integer')]
67
    private int $lastaction = 0;
68
69
    #[Column(type: 'integer')]
70
    private int $creation = 0;
71
72
    #[Column(type: 'integer')]
73
    private int $kn_lez = 0;
74
75
    #[Column(type: 'smallint')]
76
    private int $delmark = 0;
77
78
    #[Column(type: 'boolean')]
79
    private bool $vac_active = false;
80
81
    #[Column(type: 'integer')]
82
    private int $vac_request_date = 0;
83
84
    #[Column(type: 'text')]
85
    private string $description = '';
86
87
    #[Column(type: 'smallint')]
88
    private int $tick = 1;
89
90
    #[Column(type: 'smallint', nullable: true)]
91
    private ?int $maptype = MapEnum::MAPTYPE_INSERT;
0 ignored issues
show
introduced by
The private property $maptype is not used, and could be removed.
Loading history...
92
93
    #[Column(type: 'text')]
94
    private string $sessiondata = '';
95
96
    #[Column(type: 'string', length: 255)]
97
    private string $password_token = '';
98
99
    #[Column(type: 'integer')]
100
    private int $prestige = 0;
101
102
    #[Column(type: 'boolean', options: ['default' => false])]
103
    private bool $deals = false;
104
105
    #[Column(type: 'integer', nullable: true)]
106
    private ?int $last_boarding = null;
107
108
    #[ManyToOne(targetEntity: 'Alliance', inversedBy: 'members')]
109
    #[JoinColumn(name: 'allys_id', referencedColumnName: 'id')]
110
    private ?AllianceInterface $alliance = null;
111
112
    #[ManyToOne(targetEntity: 'Faction')]
113
    #[JoinColumn(name: 'race', referencedColumnName: 'id')]
114
    private FactionInterface $faction;
115
116
    #[OneToMany(targetEntity: 'Buoy', mappedBy: 'user')]
117
    private Collection $buoys;
118
119
    /**
120
     * @var ArrayCollection<int, UserAwardInterface>
121
     */
122
    #[OneToMany(targetEntity: 'UserAward', mappedBy: 'user', indexBy: 'award_id')]
123
    #[OrderBy(['award_id' => 'ASC'])]
124
    private Collection $awards;
125
126
    /**
127
     * @var ArrayCollection<int, ColonyInterface>
128
     */
129
    #[OneToMany(targetEntity: 'Colony', mappedBy: 'user', indexBy: 'id')]
130
    #[OrderBy(['colonies_classes_id' => 'ASC', 'id' => 'ASC'])]
131
    private Collection $colonies;
132
133
    /**
134
     * @var ArrayCollection<int, UserLayerInterface>
135
     */
136
    #[OneToMany(targetEntity: 'UserLayer', mappedBy: 'user', indexBy: 'layer_id')]
137
    private Collection $userLayers;
138
139
    #[OneToOne(targetEntity: 'UserLock', mappedBy: 'user')]
140
    private ?UserLockInterface $userLock = null;
141
142
    /**
143
     * @var ArrayCollection<string, UserSettingInterface>
144
     */
145
    #[OneToMany(targetEntity: 'UserSetting', mappedBy: 'user', indexBy: 'setting')]
146
    private Collection $settings;
147
148
    /**
149
     * @var ArrayCollection<int, UserCharactersInterface>
150
     */
151
    #[OneToMany(targetEntity: 'UserCharacters', mappedBy: 'user', cascade: ['persist'])]
152
    private Collection $characters;
153
154
    /** @var null|array<mixed> */
155
    private $sessiondataUnserialized;
156
157
    public function __construct()
158
    {
159
        $this->awards = new ArrayCollection();
160
        $this->colonies = new ArrayCollection();
161
        $this->userLayers = new ArrayCollection();
162
        $this->settings = new ArrayCollection();
163
        $this->characters = new ArrayCollection();
164
        $this->buoys = new ArrayCollection();
165
    }
166
167
    public function getId(): int
168
    {
169
        return $this->id;
170
    }
171
172
    public function getName(): string
173
    {
174
        //if UMODE active, add info to user name
175
        if ($this->isVacationRequestOldEnough()) {
176
            return $this->username . '[b][color=red] (UMODE)[/color][/b]';
177
        }
178
        return $this->username;
179
    }
180
181
    public function setUsername(string $username): UserInterface
182
    {
183
        $this->username = $username;
184
        return $this;
185
    }
186
187
    public function getLogin(): string
188
    {
189
        return $this->login;
190
    }
191
192
    public function setLogin(string $login): UserInterface
193
    {
194
        $this->login = $login;
195
        return $this;
196
    }
197
198
    public function getPassword(): string
199
    {
200
        return $this->pass;
201
    }
202
203
    public function setPassword(string $password): UserInterface
204
    {
205
        $this->pass = $password;
206
        return $this;
207
    }
208
209
    public function getSmsCode(): ?string
210
    {
211
        return $this->sms_code;
212
    }
213
214
    public function setSmsCode(?string $code): UserInterface
215
    {
216
        $this->sms_code = $code;
217
        return $this;
218
    }
219
220
    public function getEmail(): string
221
    {
222
        return $this->email;
223
    }
224
225
    public function setEmail(string $email): UserInterface
226
    {
227
        $this->email = $email;
228
        return $this;
229
    }
230
231
    public function getMobile(): ?string
232
    {
233
        return $this->mobile;
234
    }
235
236
    public function setMobile(?string $mobile): UserInterface
237
    {
238
        $this->mobile = $mobile;
239
        return $this;
240
    }
241
242
    public function getRgbCode(): string
243
    {
244
        $setting = $this->getSettings()->get(UserSettingEnum::RGB_CODE->value);
245
        if ($setting !== null) {
246
            return $setting->getValue();
247
        }
248
249
        return '';
250
    }
251
252
    public function getCss(): string
253
    {
254
        $setting = $this->getSettings()->get(UserSettingEnum::CSS_COLOR_SHEET->value);
255
        if ($setting !== null) {
256
            return $setting->getValue();
257
        }
258
259
        return UserCssClassEnum::BLACK->value;
260
    }
261
262
    public function getFactionId(): int
263
    {
264
        return $this->race;
265
    }
266
267
    public function setFaction(FactionInterface $faction): UserInterface
268
    {
269
        $this->faction = $faction;
270
        return $this;
271
    }
272
273
    public function getFaction(): FactionInterface
274
    {
275
        return $this->faction;
276
    }
277
278
    public function getAwards(): Collection
279
    {
280
        return $this->awards;
281
    }
282
283
    public function getColonies(): Collection
284
    {
285
        return $this->colonies;
286
    }
287
288
    public function hasColony(): bool
289
    {
290
        if (
291
            $this->getState() === UserEnum::USER_STATE_COLONIZATION_SHIP
292
            || $this->getState() === UserEnum::USER_STATE_UNCOLONIZED
293
        ) {
294
            return false;
295
        }
296
297
        return !$this->getColonies()->isEmpty();
298
    }
299
300
    public function getState(): int
301
    {
302
        return $this->state;
303
    }
304
305
    public function isLocked(): bool
306
    {
307
        return $this->getUserLock() !== null && $this->getUserLock()->getRemainingTicks() > 0;
308
    }
309
310
    public function getUserStateDescription(): string
311
    {
312
        if ($this->isLocked()) {
313
            return _('GESPERRT');
314
        }
315
        return UserEnum::getUserStateDescription($this->getState());
316
    }
317
318
    public function setState(int $state): UserInterface
319
    {
320
        $this->state = $state;
321
        return $this;
322
    }
323
324
    public function getAvatar(): string
325
    {
326
        $setting = $this->getSettings()->get(UserSettingEnum::AVATAR->value);
327
        if ($setting !== null) {
328
            return $setting->getValue();
329
        }
330
331
        return '';
332
    }
333
334
    public function isEmailNotification(): bool
335
    {
336
        $setting = $this->getSettings()->get(UserSettingEnum::EMAIL_NOTIFICATION->value);
337
        if ($setting !== null) {
338
            return (bool)$setting->getValue();
339
        }
340
341
        return false;
342
    }
343
344
    public function getLastaction(): int
345
    {
346
        return $this->lastaction;
347
    }
348
349
    public function setLastaction(int $lastaction): UserInterface
350
    {
351
        $this->lastaction = $lastaction;
352
        return $this;
353
    }
354
355
    public function getCreationDate(): int
356
    {
357
        return $this->creation;
358
    }
359
360
    public function setCreationDate(int $creationDate): UserInterface
361
    {
362
        $this->creation = $creationDate;
363
        return $this;
364
    }
365
366
    public function getKnMark(): int
367
    {
368
        return $this->kn_lez;
369
    }
370
371
    public function setKnMark(int $knMark): UserInterface
372
    {
373
        $this->kn_lez = $knMark;
374
        return $this;
375
    }
376
377
    public function getDeletionMark(): int
378
    {
379
        return $this->delmark;
380
    }
381
382
    public function setDeletionMark(int $deletionMark): UserInterface
383
    {
384
        $this->delmark = $deletionMark;
385
        return $this;
386
    }
387
388
    public function isVacationMode(): bool
389
    {
390
        return $this->vac_active;
391
    }
392
393
    public function setVacationMode(bool $vacationMode): UserInterface
394
    {
395
        $this->vac_active = $vacationMode;
396
        return $this;
397
    }
398
399
    public function getVacationRequestDate(): int
400
    {
401
        return $this->vac_request_date;
402
    }
403
404
    public function setVacationRequestDate(int $date): UserInterface
405
    {
406
        $this->vac_request_date = $date;
407
408
        return $this;
409
    }
410
411
    public function isVacationRequestOldEnough(): bool
412
    {
413
        return $this->isVacationMode() && (time() - $this->getVacationRequestDate() > UserEnum::VACATION_DELAY_IN_SECONDS);
414
    }
415
416
    public function isStorageNotification(): bool
417
    {
418
        $setting = $this->getSettings()->get(UserSettingEnum::STORAGE_NOTIFICATION->value);
419
        if ($setting !== null) {
420
            return (bool)$setting->getValue();
421
        }
422
423
        return false;
424
    }
425
426
    public function getDescription(): string
427
    {
428
        return $this->description;
429
    }
430
431
    public function setDescription(string $description): UserInterface
432
    {
433
        $this->description = $description;
434
        return $this;
435
    }
436
437
    public function isShowOnlineState(): bool
438
    {
439
        $setting = $this->getSettings()->get(UserSettingEnum::SHOW_ONLINE_STATUS->value);
440
        if ($setting !== null) {
441
            return (bool)$setting->getValue();
442
        }
443
444
        return false;
445
    }
446
447
    public function isShowPmReadReceipt(): bool
448
    {
449
        $setting = $this->getSettings()->get(UserSettingEnum::SHOW_PM_READ_RECEIPT->value);
450
        if ($setting !== null) {
451
            return (bool)$setting->getValue();
452
        }
453
454
        return false;
455
    }
456
457
    public function isSaveLogin(): bool
458
    {
459
        $setting = $this->getSettings()->get(UserSettingEnum::SAVE_LOGIN->value);
460
        if ($setting !== null) {
461
            return (bool)$setting->getValue();
462
        }
463
464
        return false;
465
    }
466
467
    public function getFleetFixedDefault(): bool
468
    {
469
        $setting = $this->getSettings()->get(UserSettingEnum::FLEET_FIXED_DEFAULT->value);
470
        if ($setting !== null) {
471
            return (bool)$setting->getValue();
472
        }
473
474
        return false;
475
    }
476
477
    public function getWarpsplitAutoCarryoverDefault(): bool
478
    {
479
        $setting = $this->getSettings()->get(UserSettingEnum::WARPSPLIT_AUTO_CARRYOVER_DEFAULT->value);
480
        if ($setting !== null) {
481
            return (bool)$setting->getValue();
482
        }
483
484
        return false;
485
    }
486
487
    public function getTick(): int
488
    {
489
        return $this->tick;
490
    }
491
492
    public function setTick(int $tick): UserInterface
493
    {
494
        $this->tick = $tick;
495
        return $this;
496
    }
497
498
    public function getUserLayers(): Collection
499
    {
500
        return $this->userLayers;
501
    }
502
503
    public function hasSeen(int $layerId): bool
504
    {
505
        return $this->getUserLayers()->containsKey($layerId);
506
    }
507
508
    public function hasExplored(int $layerId): bool
509
    {
510
        return $this->hasSeen($layerId) && $this->getUserLayers()->get($layerId)->isExplored();
511
    }
512
513
    public function getSettings(): Collection
514
    {
515
        return $this->settings;
516
    }
517
518
    public function getSessiondata(): string
519
    {
520
        return $this->sessiondata;
521
    }
522
523
    public function setSessiondata(string $sessiondata): UserInterface
524
    {
525
        $this->sessiondata = $sessiondata;
526
        $this->sessiondataUnserialized = null;
527
        return $this;
528
    }
529
530
    public function getPasswordToken(): string
531
    {
532
        return $this->password_token;
533
    }
534
535
    public function setPasswordToken(string $password_token): UserInterface
536
    {
537
        $this->password_token = $password_token;
538
        return $this;
539
    }
540
541
    public function getPrestige(): int
542
    {
543
        return $this->prestige;
544
    }
545
546
    public function setPrestige(int $prestige): UserInterface
547
    {
548
        $this->prestige = $prestige;
549
        return $this;
550
    }
551
552
    public function getDefaultView(): ModuleViewEnum
553
    {
554
        $setting = $this->getSettings()->get(UserSettingEnum::DEFAULT_VIEW->value);
555
        if ($setting !== null) {
556
            return ModuleViewEnum::from($setting->getValue());
557
        }
558
559
        return ModuleViewEnum::MAINDESK;
560
    }
561
562
    public function getRpgBehavior(): UserRpgBehaviorEnum
563
    {
564
        $setting = $this->getSettings()->get(UserSettingEnum::RPG_BEHAVIOR->value);
565
        if ($setting !== null) {
566
            return UserRpgBehaviorEnum::from((int)$setting->getValue());
567
        }
568
569
        return UserRpgBehaviorEnum::RPG_BEHAVIOR_NOT_SET;
570
    }
571
572
    public function getDeals(): bool
573
    {
574
        return $this->deals;
575
    }
576
577
    public function setDeals(bool $deals): UserInterface
578
    {
579
        $this->deals = $deals;
580
        return $this;
581
    }
582
583
    public function getLastBoarding(): ?int
584
    {
585
        return $this->last_boarding;
586
    }
587
588
    public function setLastBoarding(int $lastBoarding): UserInterface
589
    {
590
        $this->last_boarding = $lastBoarding;
591
        return $this;
592
    }
593
594
    public function isOnline(): bool
595
    {
596
        return !($this->getLastAction() < time() - GameEnum::USER_ONLINE_PERIOD);
597
    }
598
599
    public function getAlliance(): ?AllianceInterface
600
    {
601
        return $this->alliance;
602
    }
603
604
    public function setAlliance(?AllianceInterface $alliance): UserInterface
605
    {
606
        $this->alliance = $alliance;
607
        return $this;
608
    }
609
610
    public function setAllianceId(?int $allianceId): UserInterface
611
    {
612
        $this->allys_id = $allianceId;
613
        return $this;
614
    }
615
616
    public function getSessionDataUnserialized(): array
617
    {
618
        if ($this->sessiondataUnserialized === null) {
619
            $this->sessiondataUnserialized = unserialize($this->getSessionData());
620
            if (!is_array($this->sessiondataUnserialized)) {
621
                $this->sessiondataUnserialized = [];
622
            }
623
        }
624
        return $this->sessiondataUnserialized;
625
    }
626
627
    public function isContactable(): bool
628
    {
629
        return $this->getId() != UserEnum::USER_NOONE;
630
    }
631
632
    public function hasAward(int $awardId): bool
633
    {
634
        return $this->awards->containsKey($awardId) === true;
635 6
    }
636
637 6
    public function hasStationsNavigation(): bool
638
    {
639
        if ($this->isNpc()) {
640
            return true;
641
        }
642
643
        return $this->hasAward(UserAwardEnum::RESEARCHED_STATIONS);
644
    }
645
646
    public static function isUserNpc(int $userId): bool
647
    {
648
        return $userId < UserEnum::USER_FIRST_ID;
649
    }
650
651
    public function isNpc(): bool
652
    {
653
        return self::isUserNpc($this->getId());
654
    }
655
656
    public function getUserLock(): ?UserLockInterface
657
    {
658
        return $this->userLock;
659
    }
660
661
    public function __toString(): string
662
    {
663
        return sprintf('userName: %s', $this->getName());
664
    }
665
666
    public function hasTranslation(): bool
667
    {
668
        $text = $this->getDescription();
669
        return strpos($text, '[translate]') !== false && strpos($text, '[/translate]') !== false;
670
    }
671
672
    public function isShowPirateHistoryEntrys(): bool
673
    {
674
        $setting = $this->getSettings()->get(UserSettingEnum::SHOW_PIRATE_HISTORY_ENTRYS->value);
675
        if ($setting !== null) {
676
            return (bool)$setting->getValue();
677
        }
678
679
        return false;
680
    }
681
682
    public function getCharacters(): Collection
683
    {
684
        return $this->characters;
685
    }
686
687
    /**
688
     * @return Collection<BuoyInterface>
689
     */
690
    public function getBuoys(): Collection
691
    {
692
        return $this->buoys;
693
    }
694
}