Passed
Push — dev ( 04bdae...5ade2a )
by Janko
28:32
created

User::getAvatar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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