Skip to content

Commit

Permalink
New feature: enable/disable scheduled raid bosses
Browse files Browse the repository at this point in the history
Added the ability to enable and disable scheduled raid bosses.
Makes managing regional raid bosses easier if using automatic updating of upcoming bosses.
  • Loading branch information
Ninjasoturi committed May 8, 2024
1 parent 217220e commit 7b54eb5
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 50 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5
6
1 change: 1 addition & 0 deletions constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
65 changes: 65 additions & 0 deletions lang/language.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions logic/createRaidBossList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
17 changes: 13 additions & 4 deletions logic/read_upcoming_bosses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions logic/resolve_raid_boss.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 0 additions & 37 deletions mods/delete_scheduled_entry.php

This file was deleted.

70 changes: 70 additions & 0 deletions mods/edit_scheduled_entry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
// Write to log.
debug_log('edit_scheduled_entry()');

// Check access.
$botUser->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 .= '<b>' . ($pokemon['disabled'] ? getTranslation('disabled') : getTranslation('enabled')) .'</b>';
$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 .= '<b>' . ($pokemon['disabled'] ? getTranslation('disabled') : getTranslation('enabled')) .'</b>';

$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);
2 changes: 1 addition & 1 deletion mods/import_future_bosses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions mods/pokedex_list_raids.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']]);
Expand Down
33 changes: 29 additions & 4 deletions mods/update_bosses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions sql/0-pokemon-raid-bot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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` (
Expand Down
1 change: 1 addition & 0 deletions sql/upgrade/6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `raid_bosses` ADD COLUMN IF NOT EXISTS `disabled` TINYINT(1) UNSIGNED DEFAULT 0 AFTER `scheduled`;

0 comments on commit 7b54eb5

Please sign in to comment.