From 7b54eb5f1b8595aa826c18647c33d5e4bc03994a Mon Sep 17 00:00:00 2001
From: ninjasoturi <52629375+Ninjasoturi@users.noreply.github.com>
Date: Wed, 8 May 2024 20:58:00 +0300
Subject: [PATCH] New feature: enable/disable scheduled raid bosses Added the
ability to enable and disable scheduled raid bosses. Makes managing regional
raid bosses easier if using automatic updating of upcoming bosses.
---
VERSION | 2 +-
constants.php | 1 +
lang/language.json | 65 ++++++++++++++++++++++++++++++
logic/createRaidBossList.php | 1 +
logic/read_upcoming_bosses.php | 17 ++++++--
logic/resolve_raid_boss.php | 1 +
mods/delete_scheduled_entry.php | 37 -----------------
mods/edit_scheduled_entry.php | 70 +++++++++++++++++++++++++++++++++
mods/import_future_bosses.php | 2 +-
mods/pokedex_list_raids.php | 7 ++--
mods/update_bosses.php | 33 ++++++++++++++--
sql/0-pokemon-raid-bot.sql | 1 +
sql/upgrade/6.sql | 1 +
13 files changed, 188 insertions(+), 50 deletions(-)
delete mode 100644 mods/delete_scheduled_entry.php
create mode 100644 mods/edit_scheduled_entry.php
create mode 100644 sql/upgrade/6.sql
diff --git a/VERSION b/VERSION
index 7813681f..62f94575 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-5
\ No newline at end of file
+6
\ No newline at end of file
diff --git a/constants.php b/constants.php
index d01c501b..4c792b7d 100644
--- a/constants.php
+++ b/constants.php
@@ -151,6 +151,7 @@
defined('EMOJI_DISK') or define('EMOJI_DISK', iconv('UCS-4LE', 'UTF-8', pack('V', 0x1F4BE)));
defined('EMOJI_NEW') or define('EMOJI_NEW', iconv('UCS-4LE', 'UTF-8', pack('V', 0x1F195)));
defined('EMOJI_CLIPPY') or define('EMOJI_CLIPPY',iconv('UCS-4LE', 'UTF-8', pack('V', 0x1F4CE)));
+defined('EMOJI_DISABLED') or define('EMOJI_DISABLED',iconv('UCS-4LE', 'UTF-8', pack('V', 0x1F645)));
// Carriage return.
defined('CR') or define('CR', "\n");
diff --git a/lang/language.json b/lang/language.json
index a38de54b..d3de5135 100644
--- a/lang/language.json
+++ b/lang/language.json
@@ -480,6 +480,58 @@
"FI": "Poista",
"ES": "Borrar"
},
+ "disabled": {
+ "NL": "TRANSLATE",
+ "DE": "TRANSLATE",
+ "EN": "Disabled",
+ "IT": "TRANSLATE",
+ "PT-BR": "TRANSLATE",
+ "RU": "TRANSLATE",
+ "NO": "TRANSLATE",
+ "FR": "TRANSLATE",
+ "PL": "TRANSLATE",
+ "FI": "Pois käytöstä",
+ "ES": "TRANSLATE"
+ },
+ "enabled": {
+ "NL": "TRANSLATE",
+ "DE": "TRANSLATE",
+ "EN": "Enabled",
+ "IT": "TRANSLATE",
+ "PT-BR": "TRANSLATE",
+ "RU": "TRANSLATE",
+ "NO": "TRANSLATE",
+ "FR": "TRANSLATE",
+ "PL": "TRANSLATE",
+ "FI": "Käytössä",
+ "ES": "TRANSLATE"
+ },
+ "disable": {
+ "NL": "TRANSLATE",
+ "DE": "TRANSLATE",
+ "EN": "Disable",
+ "IT": "TRANSLATE",
+ "PT-BR": "TRANSLATE",
+ "RU": "TRANSLATE",
+ "NO": "TRANSLATE",
+ "FR": "TRANSLATE",
+ "PL": "TRANSLATE",
+ "FI": "Poista käytöstä",
+ "ES": "TRANSLATE"
+ },
+ "enable": {
+ "NL": "TRANSLATE",
+ "DE": "TRANSLATE",
+ "EN": "Enable",
+ "IT": "TRANSLATE",
+ "PT-BR": "TRANSLATE",
+ "RU": "TRANSLATE",
+ "NO": "TRANSLATE",
+ "FR": "TRANSLATE",
+ "PL": "TRANSLATE",
+ "FI": "Ota käyttöön",
+ "ES": "TRANSLATE"
+ },
"edit": {
"NL": "Bewerken",
"DE": "Bearbeiten",
@@ -1429,6 +1481,19 @@
"FI": "Haluatko poistaa tämän ajastuksen?",
"ES": "¿Quieres eliminar esta entrada programada?"
},
+ "edit_scheduled_entry": {
+ "NL": "TRANSLATE",
+ "DE": "TRANSLATE",
+ "EN": "Edit scheduled entry",
+ "IT": "TRANSLATE",
+ "PT-BR": "TRANSLATE",
+ "RU": "TRANSLATE",
+ "NO": "TRANSLATE",
+ "FR": "TRANSLATE",
+ "PL": "TRANSLATE",
+ "FI": "Muokkaa ajastusta",
+ "ES": "TRANSLATE"
+ },
"found_upcoming_bosses": {
"NL": "Volgende geplande raid bazen gevonden",
"DE": "Folgende bevorstehende Raid-Bosse gefunden",
diff --git a/logic/createRaidBossList.php b/logic/createRaidBossList.php
index 91cc8de7..c5f4a9c9 100644
--- a/logic/createRaidBossList.php
+++ b/logic/createRaidBossList.php
@@ -28,6 +28,7 @@ function createRaidBossList() {
FROM raid_bosses
WHERE raid_level IN ' . $levelList . '
AND date_end > DATE_SUB(NOW(), INTERVAL 1 HOUR)
+ AND disabled = 0
ORDER BY sameDay, date_start, date_end
');
$list = '';
diff --git a/logic/read_upcoming_bosses.php b/logic/read_upcoming_bosses.php
index 8fb9a95d..c8eda99b 100644
--- a/logic/read_upcoming_bosses.php
+++ b/logic/read_upcoming_bosses.php
@@ -3,11 +3,11 @@
require_once(LOGIC_PATH . '/curl_get_contents.php');
/**
* Read upcoming bosses from Pokebattlers API and return the results as a HTML formatted text list
- * @param bool $return_sql Return results in sql insert query instead of text list
+ * @param string $returnFormat Defines the format in which result are returned. sql, list or array
* @param array|bool $levelsToRead Array of raid levels to include in import. Otherwise use the levels set in constants.php
- * @return string
+ * @return string|array
*/
-function read_upcoming_bosses($return_sql = false, $levelsToRead = false) {
+function read_upcoming_bosses($returnFormat = 'list', $levelsToRead = false) {
global $pokebattler_import_future_tiers, $pokebattler_level_map, $pokebattler_pokemon_map;
$link = curl_get_contents('https://fight.pokebattler.com/raids');
$pb = json_decode($link, true);
@@ -17,6 +17,7 @@ function read_upcoming_bosses($return_sql = false, $levelsToRead = false) {
$standardTimezone = new dateTimeZone('UTC');
$count = 0;
$sql = $list = $prev_start = $prev_end = $prev_rl = '';
+ $returnArr = [];
foreach($pb['breakingNews'] as $news) {
if($news['type'] != 'RAID_TYPE_RAID') continue;
@@ -70,9 +71,17 @@ function read_upcoming_bosses($return_sql = false, $levelsToRead = false) {
}else {
$sql .= ',("'.$dex_id_form[0].'","'.$dex_id_form[1].'","'.$date_start.'","'.$date_end.'","'.$raid_level_id.'", 1)';
}
+ $returnArr[] = [
+ 'pokedex_id' => $dex_id_form[0],
+ 'pokemon_form_id' => $dex_id_form[1],
+ 'date_start' => $date_start,
+ 'date_end' => $date_end,
+ 'raid_level' => $raid_level_id,
+ ];
}
if($count > 0) $sql.=';';
- if($return_sql) return $sql;
+ if($returnFormat == 'sql') return $sql;
+ elseif($returnFormat == 'array') return $returnArr;
else return $list;
}
diff --git a/logic/resolve_raid_boss.php b/logic/resolve_raid_boss.php
index 8d964fb5..335ec78c 100644
--- a/logic/resolve_raid_boss.php
+++ b/logic/resolve_raid_boss.php
@@ -15,6 +15,7 @@ function resolve_raid_boss($pokemon, $pokemon_form, $spawn, $raid_level) {
FROM raid_bosses
WHERE raid_level = :raidLevel
AND scheduled = 1
+ AND disabled = 0
AND convert_tz(:spawn, "+00:00", :tzDiff) BETWEEN date_start AND date_end
', ['raidLevel' => $raid_level, 'spawn' => $spawn, 'tzDiff' => $tz_diff]);
// Return egg
diff --git a/mods/delete_scheduled_entry.php b/mods/delete_scheduled_entry.php
deleted file mode 100644
index dd7bf21d..00000000
--- a/mods/delete_scheduled_entry.php
+++ /dev/null
@@ -1,37 +0,0 @@
-accessCheck('pokedex');
-$id = $data['i'];
-
-if(isset($data['s']) && $data['s'] == 1) {
- my_query('DELETE FROM raid_bosses WHERE id = ?', [$id]);
- include(ROOT_PATH . '/mods/pokedex_list_raids.php');
- exit();
-}
-$query = my_query('SELECT pokedex_id, pokemon_form_id, date_start, date_end, raid_level FROM raid_bosses WHERE id = ? LIMIT 1', [$id]);
-$pokemon = $query->fetch();
-$msg = getTranslation('delete_scheduled_confirmation') . CR . CR;
-$msg .= $pokemon['date_start'] . ' - ' . $pokemon['date_end'] . ':' . CR;
-$msg .= getTranslation($pokemon['raid_level'] . 'stars') . ': ';
-$msg .= get_local_pokemon_name($pokemon['pokedex_id'], $pokemon['pokemon_form_id']);
-
-$keys[0][] = button(getTranslation('yes'), ['delete_scheduled_entry', 'i' => $id, 's' => 1]);
-$keys[0][] = button(getTranslation('no'), 'pokedex_list_raids');
-
-// Build callback message string.
-$callback_response = 'OK';
-
-// Telegram JSON array.
-$tg_json = array();
-
-// Answer callback.
-$tg_json[] = answerCallbackQuery($update['callback_query']['id'], $callback_response, true);
-
-// Edit message.
-$tg_json[] = edit_message($update, $msg, $keys, false, true);
-
-// Telegram multicurl request.
-curl_json_multi_request($tg_json);
diff --git a/mods/edit_scheduled_entry.php b/mods/edit_scheduled_entry.php
new file mode 100644
index 00000000..6ef5e98d
--- /dev/null
+++ b/mods/edit_scheduled_entry.php
@@ -0,0 +1,70 @@
+accessCheck('pokedex');
+$id = $data['i'];
+
+$query = my_query('SELECT pokedex_id, pokemon_form_id, date_start, date_end, raid_level, disabled FROM raid_bosses WHERE id = ? LIMIT 1', [$id]);
+$pokemon = $query->fetch();
+if(isset($data['s']) && $data['s'] == 1) {
+ my_query('UPDATE raid_bosses SET disabled = NOT disabled WHERE id = ?', [$id]);
+ $pokemon['disabled'] = ($pokemon['disabled'] == 1) ? 0 : 1;
+}
+if(isset($data['s']) && $data['s'] == 2) {
+ $msg = getTranslation('delete_scheduled_confirmation') . CR . CR;
+ $msg .= $pokemon['date_start'] . ' - ' . $pokemon['date_end'] . ':' . CR;
+ $msg .= getTranslation($pokemon['raid_level'] . 'stars') . ': ';
+ $msg .= get_local_pokemon_name($pokemon['pokedex_id'], $pokemon['pokemon_form_id']) . CR;
+ $msg .= '' . ($pokemon['disabled'] ? getTranslation('disabled') : getTranslation('enabled')) .'';
+ $keys[0][0] = button(getTranslation('yes'), ['edit_scheduled_entry', 'i' => $id, 's' => 3]);
+ $keys[0][1] = button(getTranslation('no'), ['edit_scheduled_entry', 'i' => $id]);
+ // Build callback message string.
+ $callback_response = 'OK';
+
+ // Telegram JSON array.
+ $tg_json = array();
+
+ // Answer callback.
+ $tg_json[] = answerCallbackQuery($update['callback_query']['id'], $callback_response, true);
+
+ // Edit message.
+ $tg_json[] = edit_message($update, $msg, $keys, false, true);
+
+ // Telegram multicurl request.
+ curl_json_multi_request($tg_json);
+ exit();
+}
+if(isset($data['s']) && $data['s'] == 3) {
+ my_query('DELETE FROM raid_bosses WHERE id = ?', [$id]);
+ include(ROOT_PATH . '/mods/pokedex_list_raids.php');
+ exit();
+}
+$msg = getTranslation('edit_scheduled_entry') . ':' . CR . CR;
+$msg .= EMOJI_CLOCK . SP . $pokemon['date_start'] . ' - ' . $pokemon['date_end'] . CR;
+$msg .= getTranslation($pokemon['raid_level'] . 'stars') . ': ';
+$msg .= get_local_pokemon_name($pokemon['pokedex_id'], $pokemon['pokemon_form_id']) . CR;
+$msg .= '' . ($pokemon['disabled'] ? getTranslation('disabled') : getTranslation('enabled')) .'';
+
+$keys[0][] = button(
+ ($pokemon['disabled'] ? getTranslation('enable') : getTranslation('disable')),
+ ['edit_scheduled_entry', 'i' => $id, 's' => 1]
+);
+$keys[1][] = button(getTranslation('delete'), ['edit_scheduled_entry', 'i' => $id, 's' => 2]);
+$keys[2][] = button(getTranslation('back'), 'pokedex_list_raids');
+
+// Build callback message string.
+$callback_response = 'OK';
+
+// Telegram JSON array.
+$tg_json = array();
+
+// Answer callback.
+$tg_json[] = answerCallbackQuery($update['callback_query']['id'], $callback_response, true);
+
+// Edit message.
+$tg_json[] = edit_message($update, $msg, $keys, false, true);
+
+// Telegram multicurl request.
+curl_json_multi_request($tg_json);
diff --git a/mods/import_future_bosses.php b/mods/import_future_bosses.php
index 35341716..25462d3d 100644
--- a/mods/import_future_bosses.php
+++ b/mods/import_future_bosses.php
@@ -14,7 +14,7 @@
if($action == '1') {
$sql = 'DELETE FROM raid_bosses WHERE scheduled = 1;';
- $sql .= read_upcoming_bosses(true);
+ $sql .= read_upcoming_bosses('sql');
$query = my_query($sql);
$msg = getTranslation('import_done');
$tg_json = array();
diff --git a/mods/pokedex_list_raids.php b/mods/pokedex_list_raids.php
index 75cc3f6d..e6dac581 100644
--- a/mods/pokedex_list_raids.php
+++ b/mods/pokedex_list_raids.php
@@ -17,7 +17,8 @@
raid_bosses.raid_level,
DATE_FORMAT(date_start, \'%e.%c. ' . getTranslation('raid_egg_opens_at') . ' %H:%i\') as date_start,
DATE_FORMAT(date_end, \'%e.%c. ' . getTranslation('raid_egg_opens_at') . ' %H:%i\') as date_end,
- raid_bosses.scheduled
+ raid_bosses.scheduled,
+ raid_bosses.disabled
FROM raid_bosses
LEFT JOIN pokemon
ON raid_bosses.pokedex_id = pokemon.pokedex_id
@@ -68,8 +69,8 @@
// Add button to edit pokemon.
if($pokemon['scheduled'] == 1) {
$keys[] = button(
- EMOJI_CLOCK . ' [' . $pokemon['raid_level'] . ']' . SP . $poke_name,
- ['delete_scheduled_entry', 'i' => $pokemon['id']]
+ EMOJI_CLOCK . ($pokemon['disabled'] == 1 ? EMOJI_DISABLED : '').' [' . $pokemon['raid_level'] . ']' . SP . $poke_name,
+ ['edit_scheduled_entry', 'i' => $pokemon['id']]
);
} else {
$keys[] = button('[' . $pokemon['raid_level'] . ']' . SP . $poke_name, ['pokedex_edit_pokemon', 'p' => $pokemon['pokedex_id'] . '-' . $pokemon['pokemon_form_id']]);
diff --git a/mods/update_bosses.php b/mods/update_bosses.php
index 346010a1..c69da62b 100644
--- a/mods/update_bosses.php
+++ b/mods/update_bosses.php
@@ -54,10 +54,35 @@
$sql = 'INSERT INTO raid_bosses (pokedex_id, pokemon_form_id, raid_level) VALUES ' . $sql_values . ';';
}elseif($levels == 'scheduled') {
require_once(LOGIC_PATH . '/read_upcoming_bosses.php');
- $data = read_upcoming_bosses(true, [5,6,7,8,10]);
- if(empty($data)) exit;
- $sql = 'DELETE FROM raid_bosses WHERE scheduled = 1;';
- $sql .= $data;
+ $upcoming = read_upcoming_bosses('array', [5,6,7,8,10]);
+ if(empty($upcoming)) exit;
+ $idArray = [];
+ $updateRows = '';
+ $updateCount = 0;
+ // Exclude existing entries from deletion
+ foreach($upcoming as $row) {
+ $data = my_query('
+ SELECT
+ id, pokedex_id, pokemon_form_id, date_start, date_end, raid_level
+ FROM raid_bosses
+ WHERE pokedex_id = :pokedex_id
+ AND pokemon_form_id = :pokemon_form_id
+ AND date_start = :date_start
+ AND date_end = :date_end
+ AND scheduled = 1'
+ ,['pokedex_id' => $row['pokedex_id'], 'pokemon_form_id' => $row['pokemon_form_id'], 'date_start'=>$row['date_start'], 'date_end'=>$row['date_end']]);
+ $result = $data->fetchAll(PDO::FETCH_COLUMN, 0);
+ if($data->rowCount() == 0) {
+ $updateRows .= '(\'' . implode("', '", $row) . '\', \'1\'),';
+ $updateCount++;
+ }else {
+ $idArray[] = $result[0];
+ }
+ }
+ $updateRows = rtrim($updateRows, ',');
+
+ $sql = 'DELETE FROM raid_bosses WHERE scheduled = 1 AND id NOT IN ('.implode(',', $idArray).'); ';
+ if($updateCount > 0) $sql .= 'INSERT INTO raid_bosses (pokedex_id, pokemon_form_id, date_start, date_end, raid_level, scheduled) VALUES ' . $updateRows.';';
}else {
info_log("Invalid argumens supplied to update_bosses!");
exit();
diff --git a/sql/0-pokemon-raid-bot.sql b/sql/0-pokemon-raid-bot.sql
index ee7ad515..9dd0601d 100644
--- a/sql/0-pokemon-raid-bot.sql
+++ b/sql/0-pokemon-raid-bot.sql
@@ -106,6 +106,7 @@ CREATE TABLE `raid_bosses` (
`date_end` datetime NOT NULL DEFAULT '2038-01-19 03:14:07',
`raid_level` TINYINT UNSIGNED DEFAULT NULL,
`scheduled` TINYINT(1) NULL DEFAULT 0,
+ `disabled` TINYINT(1) UNSIGNED DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `raids` (
diff --git a/sql/upgrade/6.sql b/sql/upgrade/6.sql
new file mode 100644
index 00000000..ef70b947
--- /dev/null
+++ b/sql/upgrade/6.sql
@@ -0,0 +1 @@
+ALTER TABLE `raid_bosses` ADD COLUMN IF NOT EXISTS `disabled` TINYINT(1) UNSIGNED DEFAULT 0 AFTER `scheduled`;