Skip to content

Commit 6f87c59

Browse files
committed
Fix Fauna command so it does not depend on action names being fixed
1 parent 78d0599 commit 6f87c59

File tree

2 files changed

+34
-42
lines changed

2 files changed

+34
-42
lines changed

commands/FaunaCommand.php

+33-42
Original file line numberDiff line numberDiff line change
@@ -152,46 +152,20 @@ private static function getStatusMessage(User $sender, $config)
152152

153153
$doors = json_decode($doors->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
154154

155-
$icons = [
156-
'locked' => '🔒',
157-
'unlocked' => '🔓',
158-
'unknown' => '',
159-
'open' => '🚪',
160-
];
161-
162-
$markup = [
163-
'label' => static fn(string $text) => new InlineKeyboardButton([
164-
'text' => $text,
165-
'callback_data' => static::buildCbData(),
166-
]),
167-
'lock' => static fn(string $doorId) => new InlineKeyboardButton([
168-
'text' => $icons['locked'] . ' Lock',
169-
'callback_data' => static::buildCbData([
170-
'action' => 'lock',
171-
'doorId' => $doorId,
172-
]),
173-
]),
174-
'open' => static fn(string $doorId) => new InlineKeyboardButton([
175-
'text' => $icons['open'] . ' Open',
176-
'callback_data' => static::buildCbData([
177-
'action' => 'open',
178-
'doorId' => $doorId,
179-
]),
180-
]),
181-
'unlock' => static fn(string $doorId) => new InlineKeyboardButton([
182-
'text' => $icons['unlocked'] . ' Unlock',
183-
'callback_data' => static::buildCbData([
184-
'action' => 'unlock',
185-
'doorId' => $doorId,
186-
]),
187-
]),
188-
];
189-
190155
$kb = new InlineKeyboard([
191156
'inline_keyboard' => array_merge(...array_map(static fn(array $door) => [[
192-
$markup['label']($door['name'])
157+
new InlineKeyboardButton([
158+
'text' => $door['name'],
159+
'callback_data' => static::buildCbData(),
160+
]),
193161
], array_map(static fn(string $action) =>
194-
$markup[$action]($door['id']),
162+
new InlineKeyboardButton([
163+
'text' => static::getLabel($action),
164+
'callback_data' => static::buildCbData([
165+
'action' => $action,
166+
'doorId' => $door['id'],
167+
]),
168+
]),
195169
$door['supported_actions'])], $doors)),
196170
]);
197171

@@ -201,6 +175,24 @@ private static function getStatusMessage(User $sender, $config)
201175
];
202176
}
203177

178+
private static function getLabel(string $action) {
179+
$icons = [
180+
'lock' => '🔒',
181+
'unlock' => '🔓',
182+
'open' => '🚪',
183+
];
184+
185+
$separators = " \t\r\n\f\v-_";
186+
$description = ucwords($action, $separators);
187+
$baseAction = explode($separators, $action)[0];
188+
189+
if (!array_key_exists($baseAction, $icons)) {
190+
return $description;
191+
}
192+
193+
return $icons[$baseAction] . ' ' . $description;
194+
}
195+
204196
private static function getState(User $sender)
205197
{
206198
return file_get_contents(static::getFilename($sender, 'state'));
@@ -344,13 +336,12 @@ public static function processCallback(User $sender, Message $message, array $da
344336

345337
$action = $data['action'];
346338

347-
if (!in_array($action, ['open', 'lock', 'unlock'])) {
339+
if (!ctype_alnum(str_replace(['-', '_'], '', $action))) {
348340
return [
349341
'text' => 'Unknown command ' . $action,
350342
];
351343
}
352344

353-
// action is open, lock or unlock
354345
if (!array_key_exists('doorId', $data)) {
355346
return [
356347
'text' => 'No door ID provided',
@@ -408,7 +399,7 @@ public function execute()
408399

409400
[
410401
$action,
411-
$firstArgument,
402+
$doorId,
412403
] = explode(' ', $text);
413404

414405
// Self deauth
@@ -421,7 +412,7 @@ public function execute()
421412
]));
422413
}
423414

424-
if (!in_array($action, ['open', 'lock', 'unlock'])) {
415+
if (!ctype_alnum(str_replace(['-', '_'], '', $action))) {
425416
return Request::sendMessage(array_merge($replyData, [
426417
'text' => 'Invalid command',
427418
]));
@@ -435,7 +426,7 @@ public function execute()
435426

436427
$replyData['reply_to_message_id'] = $chat_id < 0 ? $response->getResult()->getMessageId() : null;
437428

438-
$result = static::executeCommand($sender, $action, $firstArgument, $config);
429+
$result = static::executeCommand($sender, $action, $doorId, $config);
439430

440431
if ($result !== true) {
441432
return Request::sendMessage(array_merge($replyData, $result));

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"nesbot/carbon": "^2.0.0",
66
"monolog/monolog": "^1.24",
77
"ext-json": "*",
8+
"ext-ctype": "*",
89
"ext-curl": "*",
910
"user890104/rcon-client": "dev-master"
1011
},

0 commit comments

Comments
 (0)