Passed
Push — dev ( b851e4...f48036 )
by Janko
48:24 queued 21:52
created

User::isProtectedAgainstPirates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 13
ccs 0
cts 8
cp 0
crap 12
rs 10
c 0
b 0
f 0
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
    /**
117
     * @var ArrayCollection<int, BuoyInterface>
118
     */
119
    #[OneToMany(targetEntity: 'Buoy', mappedBy: 'user')]
120
    private Collection $buoys;
121
122
    /**
123
     * @var ArrayCollection<int, UserAwardInterface>
124
     */
125
    #[OneToMany(targetEntity: 'UserAward', mappedBy: 'user', indexBy: 'award_id')]
126
    #[OrderBy(['award_id' => 'ASC'])]
127
    private Collection $awards;
128
129
    /**
130
     * @var ArrayCollection<int, ColonyInterface>
131
     */
132
    #[OneToMany(targetEntity: 'Colony', mappedBy: 'user', indexBy: 'id')]
133
    #[OrderBy(['colonies_classes_id' => 'ASC', 'id' => 'ASC'])]
134
    private Collection $colonies;
135
136
    /**
137
     * @var ArrayCollection<int, UserLayerInterface>
138
     */
139
    #[OneToMany(targetEntity: 'UserLayer', mappedBy: 'user', indexBy: 'layer_id')]
140
    private Collection $userLayers;
141
142
    #[OneToOne(targetEntity: 'UserLock', mappedBy: 'user')]
143
    private ?UserLockInterface $userLock = null;
144
145
    /**
146
     * @var ArrayCollection<string, UserSettingInterface>
147
     */
148
    #[OneToMany(targetEntity: 'UserSetting', mappedBy: 'user', indexBy: 'setting')]
149
    private Collection $settings;
150
151
    /**
152
     * @var ArrayCollection<int, UserCharactersInterface>
153
     */
154
    #[OneToMany(targetEntity: 'UserCharacters', mappedBy: 'user', cascade: ['persist'])]
155
    private Collection $characters;
156
157
    #[OneToOne(targetEntity: 'PirateWrath', mappedBy: 'user')]
158
    private ?PirateWrathInterface $pirateWrath = null;
159
160
    /** @var null|array<mixed> */
161
    private $sessiondataUnserialized;
162
163
    public function __construct()
164
    {
165
        $this->awards = new ArrayCollection();
166
        $this->colonies = new ArrayCollection();
167
        $this->userLayers = new ArrayCollection();
168
        $this->settings = new ArrayCollection();
169
        $this->characters = new ArrayCollection();
170
        $this->buoys = new ArrayCollection();
171
    }
172
173
    public function getId(): int
174
    {
175
        return $this->id;
176
    }
177
178
    public function getName(): string
179
    {
180
        //if UMODE active, add info to user name
181
        if ($this->isVacationRequestOldEnough()) {
182
            return $this->username . '[b][color=red] (UMODE)[/color][/b]';
183
        }
184
        return $this->username;
185
    }
186
187
    public function setUsername(string $username): UserInterface
188
    {
189
        $this->username = $username;
190
        return $this;
191
    }
192
193
    public function getLogin(): string
194
    {
195
        return $this->login;
196
    }
197
198
    public function setLogin(string $login): UserInterface
199
    {
200
        $this->login = $login;
201
        return $this;
202
    }
203
204
    public function getPassword(): string
205
    {
206
        return $this->pass;
207
    }
208
209
    public function setPassword(string $password): UserInterface
210
    {
211
        $this->pass = $password;
212
        return $this;
213
    }
214
215
    public function getSmsCode(): ?string
216
    {
217
        return $this->sms_code;
218
    }
219
220
    public function setSmsCode(?string $code): UserInterface
221
    {
222
        $this->sms_code = $code;
223
        return $this;
224
    }
225
226
    public function getEmail(): string
227
    {
228
        return $this->email;
229
    }
230
231
    public function setEmail(string $email): UserInterface
232
    {
233
        $this->email = $email;
234
        return $this;
235
    }
236
237
    public function getMobile(): ?string
238
    {
239
        return $this->mobile;
240
    }
241
242
    public function setMobile(?string $mobile): UserInterface
243
    {
244
        $this->mobile = $mobile;
245
        return $this;
246
    }
247
248
    public function getRgbCode(): string
249
    {
250
        $setting = $this->getSettings()->get(UserSettingEnum::RGB_CODE->value);
251
        if ($setting !== null) {
252
            return $setting->getValue();
253
        }
254
255
        return '';
256
    }
257
258
    public function getCss(): string
259
    {
260
        $setting = $this->getSettings()->get(UserSettingEnum::CSS_COLOR_SHEET->value);
261
        if ($setting !== null) {
262
            return $setting->getValue();
263
        }
264
265
        return UserCssClassEnum::BLACK->value;
266
    }
267
268
    public function getFactionId(): int
269
    {
270
        return $this->race;
271
    }
272
273
    public function setFaction(FactionInterface $faction): UserInterface
274
    {
275
        $this->faction = $faction;
276
        return $this;
277
    }
278
279
    public function getFaction(): FactionInterface
280
    {
281
        return $this->faction;
282
    }
283
284
    public function getAwards(): Collection
285
    {
286
        return $this->awards;
287
    }
288
289
    public function getColonies(): Collection
290
    {
291
        return $this->colonies;
292
    }
293
294
    public function hasColony(): bool
295
    {
296
        if (
297
            $this->getState() === UserEnum::USER_STATE_COLONIZATION_SHIP
298
            || $this->getState() === UserEnum::USER_STATE_UNCOLONIZED
299
        ) {
300
            return false;
301
        }
302
303
        return !$this->getColonies()->isEmpty();
304
    }
305
306
    public function getState(): int
307
    {
308
        return $this->state;
309
    }
310
311
    public function isLocked(): bool
312
    {
313
        return $this->getUserLock() !== null && $this->getUserLock()->getRemainingTicks() > 0;
314
    }
315
316
    public function getUserStateDescription(): string
317
    {
318
        if ($this->isLocked()) {
319
            return _('GESPERRT');
320
        }
321
        return UserEnum::getUserStateDescription($this->getState());
322
    }
323
324
    public function setState(int $state): UserInterface
325
    {
326
        $this->state = $state;
327
        return $this;
328
    }
329
330
    public function getAvatar(): string
331
    {
332
        $setting = $this->getSettings()->get(UserSettingEnum::AVATAR->value);
333
        if ($setting !== null) {
334
            return $setting->getValue();
335
        }
336
337
        return '';
338
    }
339
340
    public function isEmailNotification(): bool
341
    {
342
        $setting = $this->getSettings()->get(UserSettingEnum::EMAIL_NOTIFICATION->value);
343
        if ($setting !== null) {
344
            return (bool)$setting->getValue();
345
        }
346
347
        return false;
348
    }
349
350
    public function getLastaction(): int
351
    {
352
        return $this->lastaction;
353
    }
354
355
    public function setLastaction(int $lastaction): UserInterface
356
    {
357
        $this->lastaction = $lastaction;
358
        return $this;
359
    }
360
361
    public function getCreationDate(): int
362
    {
363
        return $this->creation;
364
    }
365
366
    public function setCreationDate(int $creationDate): UserInterface
367
    {
368
        $this->creation = $creationDate;
369
        return $this;
370
    }
371
372
    public function getKnMark(): int
373
    {
374
        return $this->kn_lez;
375
    }
376
377
    public function setKnMark(int $knMark): UserInterface
378
    {
379
        $this->kn_lez = $knMark;
380
        return $this;
381
    }
382
383
    public function getDeletionMark(): int
384
    {
385
        return $this->delmark;
386
    }
387
388
    public function setDeletionMark(int $deletionMark): UserInterface
389
    {
390
        $this->delmark = $deletionMark;
391
        return $this;
392
    }
393
394
    public function isVacationMode(): bool
395
    {
396
        return $this->vac_active;
397
    }
398
399
    public function setVacationMode(bool $vacationMode): UserInterface
400
    {
401
        $this->vac_active = $vacationMode;
402
        return $this;
403
    }
404
405
    public function getVacationRequestDate(): int
406
    {
407
        return $this->vac_request_date;
408
    }
409
410
    public function setVacationRequestDate(int $date): UserInterface
411
    {
412
        $this->vac_request_date = $date;
413
414
        return $this;
415
    }
416
417
    public function isVacationRequestOldEnough(): bool
418
    {
419
        return $this->isVacationMode() && (time() - $this->getVacationRequestDate() > UserEnum::VACATION_DELAY_IN_SECONDS);
420
    }
421
422
    public function isStorageNotification(): bool
423
    {
424
        $setting = $this->getSettings()->get(UserSettingEnum::STORAGE_NOTIFICATION->value);
425
        if ($setting !== null) {
426
            return (bool)$setting->getValue();
427
        }
428
429
        return false;
430
    }
431
432
    public function getDescription(): string
433
    {
434
        return $this->description;
435
    }
436
437
    public function setDescription(string $description): UserInterface
438
    {
439
        $this->description = $description;
440
        return $this;
441
    }
442
443
    public function isShowOnlineState(): bool
444
    {
445
        $setting = $this->getSettings()->get(UserSettingEnum::SHOW_ONLINE_STATUS->value);
446
        if ($setting !== null) {
447
            return (bool)$setting->getValue();
448
        }
449
450
        return false;
451
    }
452
453
    public function isShowPmReadReceipt(): bool
454
    {
455
        $setting = $this->getSettings()->get(UserSettingEnum::SHOW_PM_READ_RECEIPT->value);
456
        if ($setting !== null) {
457
            return (bool)$setting->getValue();
458
        }
459
460
        return false;
461
    }
462
463
    public function isSaveLogin(): bool
464
    {
465
        $setting = $this->getSettings()->get(UserSettingEnum::SAVE_LOGIN->value);
466
        if ($setting !== null) {
467
            return (bool)$setting->getValue();
468
        }
469
470
        return false;
471
    }
472
473
    public function getFleetFixedDefault(): bool
474
    {
475
        $setting = $this->getSettings()->get(UserSettingEnum::FLEET_FIXED_DEFAULT->value);
476
        if ($setting !== null) {
477
            return (bool)$setting->getValue();
478
        }
479
480
        return false;
481
    }
482
483
    public function getWarpsplitAutoCarryoverDefault(): bool
484
    {
485
        $setting = $this->getSettings()->get(UserSettingEnum::WARPSPLIT_AUTO_CARRYOVER_DEFAULT->value);
486
        if ($setting !== null) {
487
            return (bool)$setting->getValue();
488
        }
489
490
        return false;
491
    }
492
493
    public function getTick(): int
494
    {
495
        return $this->tick;
496
    }
497
498
    public function setTick(int $tick): UserInterface
499
    {
500
        $this->tick = $tick;
501
        return $this;
502
    }
503
504
    public function getUserLayers(): Collection
505
    {
506
        return $this->userLayers;
507
    }
508
509
    public function hasSeen(int $layerId): bool
510
    {
511
        return $this->getUserLayers()->containsKey($layerId);
512
    }
513
514
    public function hasExplored(int $layerId): bool
515
    {
516
        return $this->hasSeen($layerId) && $this->getUserLayers()->get($layerId)->isExplored();
517
    }
518
519
    public function getSettings(): Collection
520
    {
521
        return $this->settings;
522
    }
523
524
    public function getSessiondata(): string
525
    {
526
        return $this->sessiondata;
527
    }
528
529
    public function setSessiondata(string $sessiondata): UserInterface
530
    {
531
        $this->sessiondata = $sessiondata;
532
        $this->sessiondataUnserialized = null;
533
        return $this;
534
    }
535
536
    public function getPasswordToken(): string
537
    {
538
        return $this->password_token;
539
    }
540
541
    public function setPasswordToken(string $password_token): UserInterface
542
    {
543
        $this->password_token = $password_token;
544
        return $this;
545
    }
546
547
    public function getPrestige(): int
548
    {
549
        return $this->prestige;
550
    }
551
552
    public function setPrestige(int $prestige): UserInterface
553
    {
554
        $this->prestige = $prestige;
555
        return $this;
556
    }
557
558
    public function getDefaultView(): ModuleViewEnum
559
    {
560
        $setting = $this->getSettings()->get(UserSettingEnum::DEFAULT_VIEW->value);
561
        if ($setting !== null) {
562
            return ModuleViewEnum::from($setting->getValue());
563
        }
564
565
        return ModuleViewEnum::MAINDESK;
566
    }
567
568
    public function getRpgBehavior(): UserRpgBehaviorEnum
569
    {
570
        $setting = $this->getSettings()->get(UserSettingEnum::RPG_BEHAVIOR->value);
571
        if ($setting !== null) {
572
            return UserRpgBehaviorEnum::from((int)$setting->getValue());
573
        }
574
575
        return UserRpgBehaviorEnum::RPG_BEHAVIOR_NOT_SET;
576
    }
577
578
    public function getDeals(): bool
579
    {
580
        return $this->deals;
581
    }
582
583
    public function setDeals(bool $deals): UserInterface
584
    {
585
        $this->deals = $deals;
586
        return $this;
587
    }
588
589
    public function getLastBoarding(): ?int
590
    {
591
        return $this->last_boarding;
592
    }
593
594
    public function setLastBoarding(int $lastBoarding): UserInterface
595
    {
596
        $this->last_boarding = $lastBoarding;
597
        return $this;
598
    }
599
600
    public function isOnline(): bool
601
    {
602
        return !($this->getLastAction() < time() - GameEnum::USER_ONLINE_PERIOD);
603
    }
604
605
    public function getAlliance(): ?AllianceInterface
606
    {
607
        return $this->alliance;
608
    }
609
610
    public function setAlliance(?AllianceInterface $alliance): UserInterface
611
    {
612
        $this->alliance = $alliance;
613
        return $this;
614
    }
615
616
    public function setAllianceId(?int $allianceId): UserInterface
617
    {
618
        $this->allys_id = $allianceId;
619
        return $this;
620
    }
621
622
    public function getSessionDataUnserialized(): array
623
    {
624
        if ($this->sessiondataUnserialized === null) {
625
            $this->sessiondataUnserialized = unserialize($this->getSessionData());
626
            if (!is_array($this->sessiondataUnserialized)) {
627
                $this->sessiondataUnserialized = [];
628
            }
629
        }
630
        return $this->sessiondataUnserialized;
631
    }
632
633
    public function isContactable(): bool
634
    {
635
        return $this->getId() != UserEnum::USER_NOONE;
636
    }
637
638
    public function hasAward(int $awardId): bool
639
    {
640
        return $this->awards->containsKey($awardId) === true;
641
    }
642
643
    public function hasStationsNavigation(): bool
644
    {
645
        if ($this->isNpc()) {
646
            return true;
647
        }
648
649
        return $this->hasAward(UserAwardEnum::RESEARCHED_STATIONS);
650
    }
651
652 6
    public static function isUserNpc(int $userId): bool
653
    {
654 6
        return $userId < UserEnum::USER_FIRST_ID;
655
    }
656
657
    public function isNpc(): bool
658
    {
659
        return self::isUserNpc($this->getId());
660
    }
661
662
    public function getUserLock(): ?UserLockInterface
663
    {
664
        return $this->userLock;
665
    }
666
667
    public function __toString(): string
668
    {
669
        return sprintf('userName: %s', $this->getName());
670
    }
671
672
    public function hasTranslation(): bool
673
    {
674
        $text = $this->getDescription();
675
        return strpos($text, '[translate]') !== false && strpos($text, '[/translate]') !== false;
676
    }
677
678
    public function isShowPirateHistoryEntrys(): bool
679
    {
680
        $setting = $this->getSettings()->get(UserSettingEnum::SHOW_PIRATE_HISTORY_ENTRYS->value);
681
        if ($setting !== null) {
682
            return (bool)$setting->getValue();
683
        }
684
685
        return false;
686
    }
687
688
    public function getCharacters(): Collection
689
    {
690
        return $this->characters;
691
    }
692
693
    /**
694
     * @return Collection<int, BuoyInterface>
695
     */
696
    public function getBuoys(): Collection
697
    {
698
        return $this->buoys;
699
    }
700
701
    public function getPirateWrath(): ?PirateWrathInterface
702
    {
703
        return $this->pirateWrath;
704
    }
705
706
    public function setPirateWrath(PirateWrathInterface $wrath): UserInterface
707
    {
708
        $this->pirateWrath = $wrath;
709
710
        return $this;
711
    }
712
713
    public function isProtectedAgainstPirates(): bool
714
    {
715
        $pirateWrath = $this->pirateWrath;
716
        if ($pirateWrath === null) {
717
            return false;
718
        }
719
720
        $timeout = $pirateWrath->getProtectionTimeout();
721
        if ($timeout === null) {
722
            return false;
723
        }
724
725
        return $timeout > time();
726
    }
727
}
728