Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 32 additions & 57 deletions src/Entity/Game.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Inline Games - Telegram Bot (@inlinegamesbot)
*
Expand Down Expand Up @@ -178,15 +179,13 @@ public function handleAction(string $action)
}

$this->languages = Language::list();

if (isset($this->data['settings']['language']) && $language = $this->data['settings']['language']) {
Language::set($language);
Utilities::debugPrint('Set language: ' . $language);
Utilities::debugPrint('Set language: ' . $this->languages);
if ($this->getUser('host')) {
$this->applyHostLanguage();
} else {
Language::set(Language::getDefaultLanguage());
}


if (empty($this->data) && $action !== 'newAction') {
Utilities::debugPrint('Empty game data found with action expecting it to not be');
$action = "handleEmptyData";
Expand Down Expand Up @@ -342,7 +341,6 @@ protected function editMessage(string $text, InlineKeyboard $reply_markup, bool
$this->data['settings']['use_old_mentions'] = true;

if ($this->saveData($this->data)) {
//if (preg_match_all("/<a\shref=\"tg:\/\/user\?id=(.*)\">.*<\/a>/Us", $text, $matches)) {
if (preg_match_all('/<a\\shref="tg:\\/\\/user\\?id=(.*)">.*<\\/a>/Us', $text, $matches)) {
for ($i = 0, $iMax = count($matches[1]); $i < $iMax; $i++) {
if ($matches[1][$i] == $this->getUserId('host')) {
Expand Down Expand Up @@ -474,6 +472,7 @@ protected function newAction(): ServerResponse
$this->data['game_data'] = null;

if ($this->saveData($this->data)) {
$this->applyHostLanguage();
return $this->editMessage(__('{PLAYER_HOST} is waiting for opponent to join...', ['{PLAYER_HOST}' => $this->getUserMention('host')]) . PHP_EOL . __('Press {BUTTON} button to join.', ['{BUTTON}' => '<b>\'' . __('Join') . '\'</b>']), $this->getReplyMarkup('lobby'));
}

Expand Down Expand Up @@ -620,6 +619,7 @@ protected function quitAction(): ServerResponse
$this->data['players']['guest'] = null;

if ($this->saveData($this->data)) {
$this->applyHostLanguage();
return $this->editMessage(__('{PLAYER} quit...', ['{PLAYER}' => $currentUserMention]) . PHP_EOL . __("{PLAYER_HOST} is now the host.", ['{PLAYER_HOST}' => $this->getUserMention('host')]) . PHP_EOL . __("{PLAYER_HOST} is waiting for opponent to join...", ['{PLAYER_HOST}' => $this->getUserMention('host')]) . PHP_EOL . __("Press {BUTTON} button to join.", ['{BUTTON}' => '<b>\'' . __('Join') . '\'</b>']), $this->getReplyMarkup('lobby'));
}

Expand Down Expand Up @@ -745,9 +745,30 @@ protected function gameAction(): ServerResponse

return $this->answerCallbackQuery();
}
protected function applyHostLanguage(): void
{
// Fallback
$host_lang = 'en';

if (!empty($this->data['players']['host']['language_code'])) {
$host_lang = $this->data['players']['host']['language_code'];

// Normalize (e.g. en-GB → en)
if (strpos($host_lang, '-') !== false) {
$host_lang = explode('-', $host_lang)[0];
}
}

// Save and apply
$this->data['game_data']['settings']['language'] = $host_lang;
$this->saveData($this->data);
Language::set($host_lang);

Utilities::debugPrint('Game language applied: ' . $host_lang);
}

/**
* Change language
* Change language based on host’s Telegram language_code
*
* @return ServerResponse
*
Expand All @@ -761,35 +782,11 @@ protected function languageAction(): ServerResponse
return $this->answerCallbackQuery(__("You're not the host!"), true);
}

$current_languge = Language::getCurrentLanguage();
$selected_language = $this->languages[0];

$picknext = false;
foreach ($this->languages as $language) {
if ($picknext) {
$selected_language = $language;
break;
}

if ($language === $current_languge) {
$picknext = true;
}
}

$this->data['settings']['language'] = $selected_language;

if ($this->saveData($this->data)) {
Utilities::debugPrint('Set language: ' . $selected_language);
Language::set($selected_language);
}

if ($this->getUser('host') && $this->getUser('guest')) {
return $this->editMessage(__('{PLAYER_GUEST} joined...', ['{PLAYER_GUEST}' => $this->getUserMention('guest')]) . PHP_EOL . __('Waiting for {PLAYER} to start...', ['{PLAYER}' => $this->getUserMention('host')]) . PHP_EOL . __('Press {BUTTON} button to start.', ['{BUTTON}' => '<b>\'' . __('Play') . '\'</b>']), $this->getReplyMarkup('pregame'));
}

return $this->editMessage(__('{PLAYER_HOST} is waiting for opponent to join...', ['{PLAYER_HOST}' => $this->getUserMention('host')]) . PHP_EOL . __('Press {BUTTON} button to join.', ['{BUTTON}' => '<b>\'' . __('Join') . '\'</b>']), $this->getReplyMarkup('lobby'));
$this->applyHostLanguage();
return $this->answerCallbackQuery(__("Language set to host’s language."), true);
}


/**
* Game empty keyboard
*
Expand Down Expand Up @@ -820,17 +817,6 @@ protected function lobbyKeyboard(): array
{
$inline_keyboard = [];

if (count($this->languages) > 1) {
$inline_keyboard[] = [
new InlineKeyboardButton(
[
'text' => ucfirst(locale_get_display_language(Language::getCurrentLanguage(), Language::getCurrentLanguage())),
'callback_data' => static::getCode() . ";language",
]
),
];
}

$inline_keyboard[] = [
new InlineKeyboardButton(
[
Expand Down Expand Up @@ -868,17 +854,6 @@ protected function pregameKeyboard(): array
),
];

if (count($this->languages) > 1) {
$inline_keyboard[] = [
new InlineKeyboardButton(
[
'text' => ucfirst(locale_get_display_language(Language::getCurrentLanguage(), Language::getCurrentLanguage())),
'callback_data' => static::getCode() . ";language",
]
),
];
}

$inline_keyboard[] = [
new InlineKeyboardButton(
[
Expand Down Expand Up @@ -1012,7 +987,7 @@ protected function boardPrint(array $board): void
}

$line = str_replace('_won', '*', $line);

$board_out .= $line . '|' . PHP_EOL;
$board_out .= str_repeat(' ---', $this->max_x) . PHP_EOL;
}
Expand Down
Loading