Skip to content

Commit 69a1bab

Browse files
committed
extract logic from colony entity
1 parent da50167 commit 69a1bab

File tree

2 files changed

+113
-109
lines changed

2 files changed

+113
-109
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace Stu\Component\Colony\Trait;
4+
5+
use Stu\Component\Game\TimeConstants;
6+
7+
trait ColonyRotationTrait
8+
{
9+
public function getTwilightZone(int $timestamp): int
10+
{
11+
if (array_key_exists($timestamp, $this->twilightZones)) {
12+
return $this->twilightZones[$timestamp];
13+
}
14+
15+
$twilightZone = 0;
16+
17+
$width = $this->getSurfaceWidth();
18+
$rotationTime = $this->getRotationTime();
19+
$colonyTimeSeconds = $this->getColonyTimeSeconds($timestamp);
20+
21+
if ($this->getDayTimePrefix($timestamp) == 1) {
22+
$scaled = floor((((100 / ($rotationTime * 0.125)) * ($colonyTimeSeconds - $rotationTime * 0.25)) / 100) * $width);
23+
if ($scaled == 0) {
24+
$twilightZone = - (($width) - 1);
25+
} elseif ((int) - (($width) - ceil($scaled)) == 0) {
26+
$twilightZone = -1;
27+
} else {
28+
$twilightZone = (int) - (($width) - $scaled);
29+
}
30+
}
31+
if ($this->getDayTimePrefix($timestamp) == 2) {
32+
$twilightZone = $width;
33+
}
34+
if ($this->getDayTimePrefix($timestamp) == 3) {
35+
$scaled = floor((((100 / ($rotationTime * 0.125)) * ($colonyTimeSeconds - $rotationTime * 0.75)) / 100) * $width);
36+
$twilightZone = (int) ($width - $scaled);
37+
}
38+
if ($this->getDayTimePrefix($timestamp) == 4) {
39+
$twilightZone = 0;
40+
}
41+
42+
$this->twilightZones[$timestamp] = $twilightZone;
43+
44+
return $twilightZone;
45+
}
46+
47+
public function getRotationTime(): int
48+
{
49+
return (int) (TimeConstants::ONE_DAY_IN_SECONDS * $this->getRotationFactor() / 100);
50+
}
51+
52+
public function getColonyTimeHour(int $timestamp): ?string
53+
{
54+
$rotationTime = $this->getRotationTime();
55+
56+
return sprintf("%02d", (int) floor(($rotationTime / 3600) * ($this->getColonyTimeSeconds($timestamp) / $rotationTime)));
57+
}
58+
59+
public function getColonyTimeMinute(int $timestamp): ?string
60+
{
61+
$rotationTime = $this->getRotationTime();
62+
63+
return sprintf("%02d", (int) floor(60 * (($rotationTime / 3600) * ($this->getColonyTimeSeconds($timestamp) / $rotationTime) - ((int) $this->getColonyTimeHour($timestamp)))));
64+
}
65+
66+
private function getColonyTimeSeconds(int $timestamp): int
67+
{
68+
return $timestamp % $this->getRotationTime();
69+
}
70+
71+
public function getDayTimePrefix(int $timestamp): ?int
72+
{
73+
$daytimeprefix = null;
74+
$daypercent = (int) (($this->getColonyTimeSeconds($timestamp) / $this->getRotationTime()) * 100);
75+
if ($daypercent > 25 && $daypercent <= 37.5) {
76+
$daytimeprefix = 1; //Sonnenaufgang
77+
}
78+
if ($daypercent > 37.5 && $daypercent <= 75) {
79+
$daytimeprefix = 2; //Tag
80+
}
81+
if ($daypercent > 75 && $daypercent <= 87.5) {
82+
$daytimeprefix = 3; //Sonnenuntergang
83+
}
84+
if ($daypercent > 87.5 || $daypercent <= 25) {
85+
$daytimeprefix = 4; //Nacht
86+
}
87+
return $daytimeprefix;
88+
}
89+
90+
public function getDayTimeName(int $timestamp): ?string
91+
{
92+
$daytimename = null;
93+
if ($this->getDayTimePrefix($timestamp) == 1) {
94+
$daytimename = 'Morgen';
95+
}
96+
97+
if ($this->getDayTimePrefix($timestamp) == 2) {
98+
$daytimename = 'Tag';
99+
}
100+
101+
if ($this->getDayTimePrefix($timestamp) == 3) {
102+
$daytimename = 'Abend';
103+
}
104+
105+
if ($this->getDayTimePrefix($timestamp) == 4) {
106+
$daytimename = 'Nacht';
107+
}
108+
return $daytimename;
109+
}
110+
}

src/Orm/Entity/Colony.php

Lines changed: 3 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
use Doctrine\ORM\Mapping\Table;
2020
use Override;
2121
use Stu\Component\Colony\ColonyMenuEnum;
22+
use Stu\Component\Colony\Trait\ColonyRotationTrait;
2223
use Stu\Component\Game\ModuleEnum;
23-
use Stu\Component\Game\TimeConstants;
2424
use Stu\Lib\Colony\PlanetFieldHostTypeEnum;
2525
use Stu\Lib\Transfer\CommodityTransfer;
2626
use Stu\Lib\Transfer\TransferEntityTypeEnum;
@@ -35,6 +35,8 @@
3535
#[Entity(repositoryClass: ColonyRepository::class)]
3636
class Colony implements ColonyInterface
3737
{
38+
use ColonyRotationTrait;
39+
3840
#[Id]
3941
#[Column(type: 'integer')]
4042
#[GeneratedValue(strategy: 'IDENTITY')]
@@ -380,45 +382,6 @@ public function setShields(?int $shields): ColonyInterface
380382
return $this;
381383
}
382384

383-
#[Override]
384-
public function getTwilightZone(int $timestamp): int
385-
{
386-
if (array_key_exists($timestamp, $this->twilightZones)) {
387-
return $this->twilightZones[$timestamp];
388-
}
389-
390-
$twilightZone = 0;
391-
392-
$width = $this->getSurfaceWidth();
393-
$rotationTime = $this->getRotationTime();
394-
$colonyTimeSeconds = $this->getColonyTimeSeconds($timestamp);
395-
396-
if ($this->getDayTimePrefix($timestamp) == 1) {
397-
$scaled = floor((((100 / ($rotationTime * 0.125)) * ($colonyTimeSeconds - $rotationTime * 0.25)) / 100) * $width);
398-
if ($scaled == 0) {
399-
$twilightZone = - (($width) - 1);
400-
} elseif ((int) - (($width) - ceil($scaled)) == 0) {
401-
$twilightZone = -1;
402-
} else {
403-
$twilightZone = (int) - (($width) - $scaled);
404-
}
405-
}
406-
if ($this->getDayTimePrefix($timestamp) == 2) {
407-
$twilightZone = $width;
408-
}
409-
if ($this->getDayTimePrefix($timestamp) == 3) {
410-
$scaled = floor((((100 / ($rotationTime * 0.125)) * ($colonyTimeSeconds - $rotationTime * 0.75)) / 100) * $width);
411-
$twilightZone = (int) ($width - $scaled);
412-
}
413-
if ($this->getDayTimePrefix($timestamp) == 4) {
414-
$twilightZone = 0;
415-
}
416-
417-
$this->twilightZones[$timestamp] = $twilightZone;
418-
419-
return $twilightZone;
420-
}
421-
422385
#[Override]
423386
public function getShieldFrequency(): ?int
424387
{
@@ -459,75 +422,6 @@ public function setRotationFactor(int $rotationFactor): ColonyInterface
459422
return $this;
460423
}
461424

462-
#[Override]
463-
public function getRotationTime(): int
464-
{
465-
return (int) (TimeConstants::ONE_DAY_IN_SECONDS * $this->getRotationFactor() / 100);
466-
}
467-
468-
public function getColonyTimeSeconds(int $timestamp): int
469-
{
470-
return $timestamp % $this->getRotationTime();
471-
}
472-
473-
#[Override]
474-
public function getColonyTimeHour(int $timestamp): ?string
475-
{
476-
$rotationTime = $this->getRotationTime();
477-
478-
return sprintf("%02d", (int) floor(($rotationTime / 3600) * ($this->getColonyTimeSeconds($timestamp) / $rotationTime)));
479-
}
480-
481-
#[Override]
482-
public function getColonyTimeMinute(int $timestamp): ?string
483-
{
484-
$rotationTime = $this->getRotationTime();
485-
486-
return sprintf("%02d", (int) floor(60 * (($rotationTime / 3600) * ($this->getColonyTimeSeconds($timestamp) / $rotationTime) - ((int) $this->getColonyTimeHour($timestamp)))));
487-
}
488-
489-
#[Override]
490-
public function getDayTimePrefix(int $timestamp): ?int
491-
{
492-
$daytimeprefix = null;
493-
$daypercent = (int) (($this->getColonyTimeSeconds($timestamp) / $this->getRotationTime()) * 100);
494-
if ($daypercent > 25 && $daypercent <= 37.5) {
495-
$daytimeprefix = 1; //Sonnenaufgang
496-
}
497-
if ($daypercent > 37.5 && $daypercent <= 75) {
498-
$daytimeprefix = 2; //Tag
499-
}
500-
if ($daypercent > 75 && $daypercent <= 87.5) {
501-
$daytimeprefix = 3; //Sonnenuntergang
502-
}
503-
if ($daypercent > 87.5 || $daypercent <= 25) {
504-
$daytimeprefix = 4; //Nacht
505-
}
506-
return $daytimeprefix;
507-
}
508-
509-
#[Override]
510-
public function getDayTimeName(int $timestamp): ?string
511-
{
512-
$daytimename = null;
513-
if ($this->getDayTimePrefix($timestamp) == 1) {
514-
$daytimename = 'Morgen';
515-
}
516-
517-
if ($this->getDayTimePrefix($timestamp) == 2) {
518-
$daytimename = 'Tag';
519-
}
520-
521-
if ($this->getDayTimePrefix($timestamp) == 3) {
522-
$daytimename = 'Abend';
523-
}
524-
525-
if ($this->getDayTimePrefix($timestamp) == 4) {
526-
$daytimename = 'Nacht';
527-
}
528-
return $daytimename;
529-
}
530-
531425
#[Override]
532426
public function getSurfaceWidth(): int
533427
{

0 commit comments

Comments
 (0)