Skip to content

Commit 525ed4a

Browse files
Huxinatorg5bot
authored andcommitted
more npc log
1 parent 9299c30 commit 525ed4a

17 files changed

+259
-37
lines changed

src/Migrations/Sqlite/Version20250613142059.php renamed to src/Migrations/Sqlite/Version20250614091853.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Auto-generated Migration: Please modify to your needs!
1212
*/
13-
final class Version20250613142059 extends AbstractMigration
13+
final class Version20250614091853 extends AbstractMigration
1414
{
1515
public function getDescription(): string
1616
{
@@ -891,7 +891,7 @@ public function up(Schema $schema): void
891891
CREATE INDEX note_user_idx ON stu_notes (user_id)
892892
SQL);
893893
$this->addSql(<<<'SQL'
894-
CREATE TABLE stu_npc_log (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, text CLOB NOT NULL, date INTEGER NOT NULL, source_user_id INTEGER DEFAULT NULL)
894+
CREATE TABLE stu_npc_log (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, text CLOB NOT NULL, date INTEGER NOT NULL, source_user_id INTEGER DEFAULT NULL, faction_id INTEGER DEFAULT NULL)
895895
SQL);
896896
$this->addSql(<<<'SQL'
897897
CREATE TABLE stu_opened_advent_door (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, user_id INTEGER NOT NULL, day INTEGER NOT NULL, year INTEGER NOT NULL, time INTEGER NOT NULL)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stu\Migrations\Pgsql;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
11+
final class Version20250614091852_NPCLOGFACTION extends AbstractMigration
12+
{
13+
public function getDescription(): string
14+
{
15+
return 'Adds faction_id to npc_log';
16+
}
17+
18+
public function up(Schema $schema): void
19+
{
20+
$this->addSql(<<<'SQL'
21+
ALTER TABLE stu_npc_log ADD faction_id INT DEFAULT NULL
22+
SQL);
23+
}
24+
25+
public function down(Schema $schema): void
26+
{
27+
$this->addSql(<<<'SQL'
28+
ALTER TABLE stu_npc_log DROP faction_id
29+
SQL);
30+
}
31+
}

src/Module/Game/Action/Transfer/Transfer.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,23 @@
2323
use Stu\Module\Control\TargetLink;
2424
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
2525
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
26+
use Stu\Orm\Entity\ColonyInterface;
27+
use Stu\Orm\Repository\NPCLogRepositoryInterface;
28+
use Stu\Orm\Repository\MapRepositoryInterface;
29+
2630

2731
final class Transfer implements ActionControllerInterface
2832
{
2933
public const string ACTION_IDENTIFIER = 'B_TRANSFER';
3034

35+
public const int INACTIV_TIME = 60 * 60 * 48; // 48 hours
36+
3137
public function __construct(
3238
private PrivateMessageSenderInterface $privateMessageSender,
3339
private TransferInformationFactoryInterface $transferInformationFactory,
34-
private InteractionCheckerBuilderFactoryInterface $interactionCheckerBuilderFactory
40+
private InteractionCheckerBuilderFactoryInterface $interactionCheckerBuilderFactory,
41+
private NPCLogRepositoryInterface $npcLogRepository,
42+
private MapRepositoryInterface $mapRepository
3543
) {}
3644

3745
#[Override]
@@ -99,6 +107,31 @@ public function handle(GameControllerInterface $game): void
99107
$informations
100108
);
101109

110+
if ($transferInformation->getTargetType() === TransferEntityTypeEnum::COLONY && !$isUnload) {
111+
$targetEntity = $transferInformation->getTargetWrapper()->get();
112+
if ($targetEntity instanceof ColonyInterface) {
113+
$targetUser = $target->getUser();
114+
$sourceUser = $source->getUser();
115+
if ($targetUser !== null && $sourceUser !== null && $this->mapRepository->isAdminRegionUserRegion($target->getLocation()->getId(), $targetUser->getFactionId())) {
116+
$userstring = $sourceUser->getName() . '(' . $sourceUser->getId() . ') -> ' . $targetUser->getName() . '(' . $targetUser->getId() . ')';
117+
if ($targetUser->getLastaction() < time() - self::INACTIV_TIME) {
118+
$lastactivestring = ' | Lastaction: ' . date('d.m.Y H:i:s', $targetUser->getLastaction());
119+
} else {
120+
$lastactivestring = '';
121+
}
122+
$text = $informations->getInformationsAsString() . ' | ' . $userstring . ' | ' . $target->getLocation()->getSectorString() . $lastactivestring;
123+
124+
$this->createEntry(
125+
$text,
126+
$sourceUser->getId(),
127+
$targetUser->getFactionId()
128+
);
129+
}
130+
}
131+
}
132+
133+
134+
102135
$this->privateMessageSender->send(
103136
$transferInformation->getSourceWrapper()->getUser()->getId(),
104137
$transferInformation->getTargetWrapper()->getUser()->getId(),
@@ -164,6 +197,20 @@ private function getTransferStrategy(TransferTypeEnum $transferType): TransferSt
164197
return $transferStrategy;
165198
}
166199

200+
private function createEntry(
201+
string $text,
202+
int $UserId,
203+
int $factionId
204+
): void {
205+
$entry = $this->npcLogRepository->prototype();
206+
$entry->setText($text);
207+
$entry->setSourceUserId($UserId);
208+
$entry->setDate(time());
209+
$entry->setFactionId($factionId);
210+
211+
$this->npcLogRepository->save($entry);
212+
}
213+
167214
#[Override]
168215
public function performSessionCheck(): bool
169216
{

src/Module/NPC/View/NPCLog/NPCLog.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ final class NPCLog implements ViewControllerInterface
1313
{
1414
public const string VIEW_IDENTIFIER = 'SHOW_NPC_LOG';
1515

16-
public function __construct(private NPCLogRepositoryInterface $npclogRepository)
17-
{
18-
}
16+
public function __construct(private NPCLogRepositoryInterface $npclogRepository) {}
1917

2018
#[Override]
2119
public function handle(GameControllerInterface $game): void
@@ -28,13 +26,21 @@ public function handle(GameControllerInterface $game): void
2826
_('NPC Log')
2927
);
3028

31-
$logs = $this->npclogRepository->findBy([], ['id' => 'DESC'], 100);
29+
$normalLogs = $this->npclogRepository->findBy(
30+
['faction_id' => null],
31+
['id' => 'DESC'],
32+
100
33+
);
34+
35+
$factionLogs = $this->npclogRepository->findBy(
36+
['faction_id' => $game->getUser()->getFactionId()],
37+
['id' => 'DESC'],
38+
100
39+
);
3240

3341
$game->setTemplateFile('html/npc/npclog.twig');
3442
$game->setPageTitle(_('NPC Log'));
35-
$game->setTemplateVar(
36-
'LIST',
37-
$logs
38-
);
43+
$game->setTemplateVar('NORMAL_LOGS', $normalLogs);
44+
$game->setTemplateVar('FACTION_LOGS', $factionLogs);
3945
}
4046
}

src/Orm/Entity/NPCLog.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class NPCLog implements NPCLogInterface
3030
#[Column(type: 'integer', nullable: true)]
3131
private ?int $source_user_id = 0;
3232

33+
#[Column(type: 'integer', nullable: true)]
34+
private ?int $faction_id = null;
35+
3336

3437

3538
#[Override]
@@ -79,4 +82,18 @@ public function setSourceUserId(int $sourceuserId): NPCLogInterface
7982

8083
return $this;
8184
}
85+
86+
#[Override]
87+
public function getFactionId(): ?int
88+
{
89+
return $this->faction_id;
90+
}
91+
92+
#[Override]
93+
public function setFactionId(?int $factionId): NPCLogInterface
94+
{
95+
$this->faction_id = $factionId;
96+
97+
return $this;
98+
}
8299
}

src/Orm/Entity/NPCLogInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ public function setDate(int $date): NPCLogInterface;
1717
public function getSourceUserId(): ?int;
1818

1919
public function setSourceUserId(int $userId): NPCLogInterface;
20+
21+
public function getFactionId(): ?int;
22+
23+
public function setFactionId(?int $factionId): NPCLogInterface;
2024
}

src/Orm/Repository/MapRepository.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use Stu\Orm\Entity\Map;
1919
use Stu\Orm\Entity\MapInterface;
2020
use Stu\Orm\Entity\UserInterface;
21+
use Stu\Orm\Entity\MapRegionSettlement;
22+
use Stu\Orm\Entity\StarSystemMap;
23+
2124

2225
/**
2326
* @extends EntityRepository<Map>
@@ -845,4 +848,30 @@ public function getUniqueInfluenceAreaIds(int $layerId): array
845848

846849
return array_map('intval', array_column($query->getResult(), 'influence_area_id'));
847850
}
851+
852+
#[Override]
853+
public function isAdminRegionUserRegion(int $locationId, int $factionId): bool
854+
{
855+
$result = $this->getEntityManager()
856+
->createQuery(
857+
sprintf(
858+
'SELECT COUNT(mrs.id) FROM %s m
859+
JOIN %s ssm WITH m.systems_id = ssm.systems_id
860+
JOIN %s mrs WITH m.admin_region_id = mrs.region_id
861+
WHERE ssm.id = :locationId
862+
AND mrs.faction_id = :factionId
863+
AND m.admin_region_id IS NOT NULL',
864+
Map::class,
865+
StarSystemMap::class,
866+
MapRegionSettlement::class
867+
)
868+
)
869+
->setParameters([
870+
'locationId' => $locationId,
871+
'factionId' => $factionId
872+
])
873+
->getSingleScalarResult();
874+
875+
return $result > 0;
876+
}
848877
}

src/Orm/Repository/MapRepositoryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,6 @@ public function getShipSubspaceLayerData(PanelBoundaries $boundaries, int $shipI
128128

129129
/** @return array<int> */
130130
public function getUniqueInfluenceAreaIds(int $layerId): array;
131+
132+
public function isAdminRegionUserRegion(int $locationId, int $factionId): bool;
131133
}

src/Public/static/js/npc.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function initNpcLogData() {
2+
var logContainer = document.getElementById('npcLogContainer');
3+
if (logContainer) {
4+
window.normalLogsData = logContainer.getAttribute('data-normal-logs');
5+
window.factionLogsData = logContainer.getAttribute('data-faction-logs');
6+
}
7+
}
8+
9+
function switchNpcLog(logType) {
10+
var normalBtn = document.getElementById('npcLogBtn');
11+
var factionBtn = document.getElementById('factionLogBtn');
12+
var logTitle = document.getElementById('logTitle');
13+
var logTableBody = document.getElementById('logTableBody');
14+
var noLogsMessage = document.getElementById('noLogsMessage');
15+
16+
if (logType === 'faction') {
17+
normalBtn.style.backgroundColor = '';
18+
factionBtn.style.backgroundColor = '#3c3c3c';
19+
logTitle.innerHTML = 'Fraktions-Log';
20+
21+
if (window.factionLogsData && window.factionLogsData.trim().length > 0) {
22+
logTableBody.innerHTML = window.factionLogsData;
23+
logTableBody.parentNode.style.display = 'table';
24+
noLogsMessage.style.display = 'none';
25+
} else {
26+
logTableBody.parentNode.style.display = 'none';
27+
noLogsMessage.style.display = 'block';
28+
noLogsMessage.innerHTML = 'Keine Fraktions-Logs vorhanden';
29+
}
30+
} else {
31+
normalBtn.style.backgroundColor = '#3c3c3c';
32+
factionBtn.style.backgroundColor = '';
33+
logTitle.innerHTML = 'NPC Log';
34+
35+
if (window.normalLogsData && window.normalLogsData.trim().length > 0) {
36+
logTableBody.innerHTML = window.normalLogsData;
37+
logTableBody.parentNode.style.display = 'table';
38+
noLogsMessage.style.display = 'none';
39+
} else {
40+
logTableBody.parentNode.style.display = 'none';
41+
noLogsMessage.style.display = 'block';
42+
noLogsMessage.innerHTML = 'Keine NPC-Logs vorhanden';
43+
}
44+
}
45+
}
46+
47+
document.addEventListener('DOMContentLoaded', function () {
48+
initNpcLogData();
49+
});

src/html/npc/npclog.twig

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,38 @@
22

33
{% block body %}
44
{% include 'html/breadcrumb.twig' %}
5-
<div class="tcal">
6-
{% if LIST|length > 0 %}
7-
<table>
8-
<thead>
9-
<tr>
10-
<th>Text</th>
11-
<th style="width:120px">Datum</th>
12-
</tr>
13-
</thead>
14-
<tbody>
15-
{% for listEntry in LIST %}
16-
<tr>
17-
<td>{{ listEntry.getText()|bbcode }}</td>
18-
<td>{{ listEntry.getDate()|stuDateTime }}</td>
19-
</tr>
20-
{% endfor %}
21-
</tbody>
22-
</table>
23-
{% else %}
24-
<p>Keine Logs vorhanden</p>
25-
{% endif %}
5+
<div class="tcal" id="npcLogContainer"
6+
data-normal-logs="{% for listEntry in NORMAL_LOGS %}<tr><td>{{ listEntry.getText()|bbcode|e('html_attr') }}</td><td>{{ listEntry.getDate()|stuDateTime }}</td></tr>{% endfor %}"
7+
data-faction-logs="{% for listEntry in FACTION_LOGS %}<tr><td>{{ listEntry.getText()|bbcode|e('html_attr') }}</td><td>{{ listEntry.getDate()|stuDateTime }}</td></tr>{% endfor %}">
8+
9+
<div style="margin-bottom: 15px;">
10+
<a href="#" onclick="switchNpcLog('normal'); return false;" id="npcLogBtn" class="linkbutton"
11+
style="background-color: #3c3c3c;">NPC Log</a>
12+
{% if FACTION_LOGS|length > 0 %}
13+
<a href="#" onclick="switchNpcLog('faction'); return false;" id="factionLogBtn"
14+
class="linkbutton">Fraktions-Log</a>
15+
{% endif %}
16+
</div>
17+
18+
<h3 id="logTitle">NPC Log</h3>
19+
20+
<table id="logTable" {% if NORMAL_LOGS|length==0 %}style="display: none;" {% endif %}>
21+
<thead>
22+
<tr>
23+
<th>Text</th>
24+
<th style="width:120px">Datum</th>
25+
</tr>
26+
</thead>
27+
<tbody id="logTableBody">
28+
{% for listEntry in NORMAL_LOGS %}
29+
<tr>
30+
<td>{{ listEntry.getText()|bbcode }}</td>
31+
<td>{{ listEntry.getDate()|stuDateTime }}</td>
32+
</tr>
33+
{% endfor %}
34+
</tbody>
35+
</table>
36+
37+
<p id="noLogsMessage" {% if NORMAL_LOGS|length> 0 %}style="display: none;"{% endif %}>Keine NPC-Logs vorhanden</p>
2638
</div>
27-
{% endblock %}
39+
{% endblock %}

0 commit comments

Comments
 (0)