Skip to content

Commit 55d6f94

Browse files
committed
Merge branch 'develop'
2 parents c114380 + 7813c19 commit 55d6f94

7 files changed

Lines changed: 146 additions & 9 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace App\Command;
4+
5+
use App\Repository\GameServerRepository;
6+
use App\Service\GameServerOperations;
7+
use Doctrine\ORM\EntityManagerInterface;
8+
use App\Service\Connection;
9+
use App\Service\LogService;
10+
use Symfony\Component\Console\Attribute\AsCommand;
11+
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Input\InputArgument;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
16+
#[AsCommand(
17+
name: 'cron:server:custom',
18+
description: 'Run the custom command of the game server',
19+
)]
20+
class CronServerCustomCommand extends Command
21+
{
22+
#@var GameServerRepository
23+
private $gameServerRepository;
24+
25+
#@var GameServerOperations
26+
private $gameOperations;
27+
28+
#@var EntityManagerInterface
29+
private $em;
30+
31+
#@var Connection
32+
private $connection;
33+
34+
#@var LogService
35+
private $logService;
36+
37+
#@param GameServerRepository
38+
#@param GameServerOperations
39+
#@param EntityManagerInterface
40+
#@param Connection
41+
public function __construct(
42+
GameServerRepository $gameServerRepository,
43+
GameServerOperations $gameOperations,
44+
EntityManagerInterface $em,
45+
Connection $connection,
46+
LogService $logService
47+
)
48+
{
49+
$this->gameServerRepository = $gameServerRepository;
50+
$this->gameOperations = $gameOperations;
51+
$this->em = $em;
52+
$this->connection = $connection;
53+
$this->logService = $logService;
54+
55+
parent::__construct();
56+
}
57+
58+
protected function configure(): void
59+
{
60+
$this->addArgument('id', InputArgument::REQUIRED, 'id of game server');
61+
}
62+
63+
protected function execute(InputInterface $input, OutputInterface $output): int
64+
{
65+
$id = $input->getArgument('id');
66+
$game = $this->gameServerRepository->findById($id);
67+
68+
if (null === $game) {
69+
$output->writeln('Game server not found');
70+
71+
return Command::FAILURE;
72+
}
73+
74+
if (null === $game->getCommandCustomInternal()) {
75+
$output->writeln('No custom command set');
76+
77+
return Command::FAILURE;
78+
}
79+
80+
$connection = $this->connection->getConnection($game->getServer());
81+
if (null === $connection) {
82+
$output->writeln('Failed to connect to server');
83+
84+
return Command::FAILURE;
85+
}
86+
87+
$name = $this->gameOperations->getGameServerNameScreen($game);
88+
$cmd = $game->getCommandCustomInternal();
89+
90+
$command = "screen -S $name -X stuff \"$cmd\"`echo -ne '\015'`";
91+
92+
$output->writeln('Custom command sended!');
93+
$response = $this->connection->sendCommand($connection, $command);
94+
sleep(10);
95+
96+
if (false === $response) {
97+
$output->writeln('Failed to send custom command on game server');
98+
99+
return Command::FAILURE;
100+
}
101+
102+
return Command::SUCCESS;
103+
}
104+
}

src/Controller/GameServerController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public function cron(Request $request, GameServer $game): Response
127127
$crons = [
128128
"start" => $crontabRepository->findJobByRegex("/".$name."_start_/"),
129129
"stop" => $crontabRepository->findJobByRegex("/".$name."_stop_/"),
130-
"update" => $crontabRepository->findJobByRegex("/".$name."_update_/")
130+
"update" => $crontabRepository->findJobByRegex("/".$name."_update_/"),
131+
"custom" => $crontabRepository->findJobByRegex("/".$name."_custom_/")
131132
];
132133

133134
$form = $this->createForm(CronType::class);

src/Entity/GameServer.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ class GameServer
3131
private $name;
3232

3333
#[ORM\Column(type: 'string', length: 255)]
34-
private $commandStart;
34+
private string $commandStart;
3535

3636
#[ORM\Column(type: 'string', length: 255, nullable: true)]
37-
private $commandUpdate;
37+
private ?string $commandUpdate;
3838

3939
#[ORM\Column(type: 'string', length: 255)]
40-
private $commandStop;
40+
private string $commandStop;
41+
42+
#[ORM\Column(length: 255, nullable: true)]
43+
private ?string $commandCustomInternal = null;
4144

4245
#[ORM\Column(type: 'string', length: 255)]
4346
private $path;
@@ -65,9 +68,9 @@ public function __construct()
6568
{
6669
$this->stateType = 0;
6770
$this->createdAt = new DateTimeImmutable();
68-
$this->users = new ArrayCollection();
69-
$this->logs = new ArrayCollection();
70-
$this->cronjobs = new ArrayCollection();
71+
$this->users = new ArrayCollection();
72+
$this->logs = new ArrayCollection();
73+
$this->cronjobs = new ArrayCollection();
7174
}
7275

7376
public function getId(): ?int
@@ -123,6 +126,18 @@ public function setCommandStop(string $commandStop): self
123126
return $this;
124127
}
125128

129+
public function getCommandCustomInternal(): ?string
130+
{
131+
return $this->commandCustomInternal;
132+
}
133+
134+
public function setCommandCustomInternal(?string $commandCustomInternal): self
135+
{
136+
$this->commandCustomInternal = $commandCustomInternal;
137+
138+
return $this;
139+
}
140+
126141
public function getPath(): string
127142
{
128143
return $this->path;

src/Form/CronType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
2222
"Start command" => "start",
2323
"Stop command" => "stop",
2424
"Update command" => "update",
25+
"Custom command" => "custom"
2526
]
2627
])
2728
->add('periodicity', TextType::class, [

src/Form/GameServerType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
1818
->add('commandStart')
1919
->add('commandStop')
2020
->add('commandUpdate')
21+
->add('commandCustomInternal')
2122
->add('path')
2223
->add('server', EntityType::class, [
2324
"class" => Server::class,

templates/game/_form.html.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
{{ form_widget(form.commandStop, {'attr': {'class': 'form-control mb-4'}}) }}
1818
{{ form_label(form.commandUpdate, "Update command (optional)", {'attr': {'class': 'form-label'}}) }}
1919
{{ form_widget(form.commandUpdate, {'attr': {'class': 'form-control mb-4'}}) }}
20+
{{ form_label(form.commandCustomInternal, "Custom command internal (optional)", {'attr': {'class': 'form-label'}}) }}
21+
{{ form_widget(form.commandCustomInternal, {'attr': {'class': 'form-control mb-4'}}) }}
2022
</div>
2123
</div>
2224

templates/game/crons.html.twig

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<ul class="list-group">
3535
{% for cron in crons['stop'] %}
3636
<li class="list-group-item">
37-
Start command with cron : {{ cron.minutes ~ ' ' ~ cron.hours ~ ' ' ~ cron.dayOfMonth ~ ' ' ~ cron.months ~ ' ' ~ cron.dayOfWeek }}
37+
Stop command with cron : {{ cron.minutes ~ ' ' ~ cron.hours ~ ' ' ~ cron.dayOfMonth ~ ' ' ~ cron.months ~ ' ' ~ cron.dayOfWeek }}
3838
<form method="post" action="{{ path('game_cron_delete', {'id': game.id}) }}" onsubmit="return confirm('Are you sure you want to remove this cronjob?');">
3939
<input type="hidden" name="_token" value="{{ csrf_token('cron' ~ game.id) }}">
4040
<input type="hidden" name="cronjob" value="{{ cron.comments }}">
@@ -47,7 +47,20 @@
4747
<ul class="list-group">
4848
{% for cron in crons['update'] %}
4949
<li class="list-group-item">
50-
Start command with cron : {{ cron.minutes ~ ' ' ~ cron.hours ~ ' ' ~ cron.dayOfMonth ~ ' ' ~ cron.months ~ ' ' ~ cron.dayOfWeek }}
50+
Update command with cron : {{ cron.minutes ~ ' ' ~ cron.hours ~ ' ' ~ cron.dayOfMonth ~ ' ' ~ cron.months ~ ' ' ~ cron.dayOfWeek }}
51+
<form method="post" action="{{ path('game_cron_delete', {'id': game.id}) }}" onsubmit="return confirm('Are you sure you want to remove this cronjob?');">
52+
<input type="hidden" name="_token" value="{{ csrf_token('cron' ~ game.id) }}">
53+
<input type="hidden" name="cronjob" value="{{ cron.comments }}">
54+
<button class="btn btn-danger">Remove</button>
55+
</form>
56+
</li>
57+
{% endfor %}
58+
</ul>
59+
60+
<ul class="list-group">
61+
{% for cron in crons['custom'] %}
62+
<li class="list-group-item">
63+
Custom command with cron : {{ cron.minutes ~ ' ' ~ cron.hours ~ ' ' ~ cron.dayOfMonth ~ ' ' ~ cron.months ~ ' ' ~ cron.dayOfWeek }}
5164
<form method="post" action="{{ path('game_cron_delete', {'id': game.id}) }}" onsubmit="return confirm('Are you sure you want to remove this cronjob?');">
5265
<input type="hidden" name="_token" value="{{ csrf_token('cron' ~ game.id) }}">
5366
<input type="hidden" name="cronjob" value="{{ cron.comments }}">

0 commit comments

Comments
 (0)