Passed
Push — master ( 48bfaa...170129 )
by Nico
25:44
created

User::isUserNpc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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