-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameServerController.php
More file actions
172 lines (154 loc) · 5.88 KB
/
GameServerController.php
File metadata and controls
172 lines (154 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace App\Http\Controllers\Dashboard\GameServer;
use Inertia\Inertia;
use App\Helpers\Token;
use App\Models\GameServer\GameServer;
use App\Http\Controllers\Controller;
use App\Http\Requests\Dashboard\GameServer\StoreRequest;
use App\Http\Requests\Dashboard\GameServer\UpdateRequest;
use App\Http\Requests\Dashboard\GameServer\DestroyRequest;
use App\Http\Requests\Dashboard\GameServer\DeleteSelectedRequest;
class GameServerController extends Controller
{
/**
* The number of game servers to return for pagination.
*
* @var int
*/
protected int $perPage = 8;
/**
* Display a listing of the game servers.
*/
public function index()
{
return inertia('Dashboard/GameServers/Index', [
'title' => 'dashboard.page.game-servers.title',
'gameServers' => GameServer::paginate($this->perPage)->through(fn ($gameServer) => [
'id' => $gameServer->id,
'name' => $gameServer->name,
'full_address' => $gameServer->full_address,
'created_at' => $gameServer->created_at->format('d.m.Y - H:i:s'),
'updated_at' => $gameServer->updated_at->format('d.m.Y - H:i:s'),
])
]);
}
/**
* Show the form for creating a new game server.
*/
public function create()
{
return inertia('Dashboard/GameServers/Create', [
'title' => 'Новый сервер',
'auth_token' => fn () => Token::generate(GameServer::MAX_AUTH_TOKEN_LENGTH, 'game_servers', 'auth_token')
]);
}
/**
* Store a newly created game server in storage.
*/
public function store(StoreRequest $request)
{
$gameServer = GameServer::create($request->validated());
return redirect()->route('dashboard.game-servers.index', [
'page' => GameServer::paginate($this->perPage)->lastPage()
])->with([
'status' => 'success',
'message' => "Сервер \"$gameServer->name\" добавлен."
]);
}
/**
* Display the specified game server.
*/
public function show(GameServer $gameServer)
{
$serverInfo = [
'name' => 'My Awesome Game Server',
'version' => '1.2.3',
'players_online' => 42,
'max_players' => 100,
'status' => 'Online',
'description' => 'This is a fantastic game server with lots of fun activities!',
'map_image' => 'https://via.placeholder.com/320x240', // Пример URL изображения карты
'map_name' => 'Desert Storm',
'server_ip' => '192.168.1.1',
'server_port' => '27015',
];
return inertia('Dashboard/GameServers/Show', [
'title' => "О сервере $gameServer->name",
'gameServer' => [
'id' => $gameServer->id,
'name' => $gameServer->name,
'ip' => $gameServer->ip,
'port' => $gameServer->port,
],
'punishmentReasons' => $gameServer->punishmentReasons->sortBy(function($punishmentReason) {
return $punishmentReason->id;
})->values()->map(function ($punishmentReason) {
return [
'id' => $punishmentReason->id,
'name' => $punishmentReason->name,
'time' => $punishmentReason->time_for_humans,
'created_at' => $punishmentReason->created_at->format('d.m.Y - H:i:s'),
'updated_at' => $punishmentReason->updated_at->format('d.m.Y - H:i:s'),
];
}),
'serverInfo' => $serverInfo,
]);
}
/**
* Show the form for editing the specified game server.
*/
public function edit(GameServer $gameServer)
{
return inertia('Dashboard/GameServers/Edit', [
'title' => "Редактирование сервера $gameServer->name",
'gameServer' => [
'id' => $gameServer->id,
'name' => $gameServer->name,
'ip' => $gameServer->ip,
'port' => $gameServer->port,
],
'auth_token' => Inertia::lazy(fn () => Token::generate(GameServer::MAX_AUTH_TOKEN_LENGTH, 'game_servers', 'auth_token')),
]);
}
/**
* Update the specified game server in storage.
*/
public function update(UpdateRequest $request, GameServer $gameServer)
{
$gameServer->update($request->validated());
return back()->with([
'status' => 'success',
'message' => "Информация о сервере \"$gameServer->name\" обновлена."
]);
}
/**
* Remove the specified game server from storage.
*/
public function destroy(DestroyRequest $request, GameServer $gameServer)
{
$validated = $request->validated();
$gameServer->delete();
$redirectToPage = min($validated['current_page'], GameServer::paginate($this->perPage)->lastPage());
return redirect()->route('dashboard.game-servers.index', [
'page' => $redirectToPage
])->with([
'status' => 'deleted',
'message' => "Сервер \"$gameServer->name\" удален."
]);
}
/**
* Delete selected game servers from storage.
*/
public function deleteSelected(DeleteSelectedRequest $request)
{
$validated = $request->validated();
GameServer::whereIn('id', $validated['ids'])->delete();
$redirectToPage = min($validated['current_page'], GameServer::paginate($this->perPage)->lastPage());
return redirect()->route('dashboard.game-servers.index', [
'page' => $redirectToPage
])->with([
'status' => 'deleted',
'message' => 'Выбранные серверы удалены.'
]);
}
}