Skip to content

Commit

Permalink
IMPROVED: /list
Browse files Browse the repository at this point in the history
NEW: /listall

/list now displays up to 12 raids, if more raids are in the system it automatically switches to /listall which can be called independently too.

TODO:
Update readme
Update /help
  • Loading branch information
Chefkeks committed Feb 4, 2021
1 parent 24a266f commit 8f7ad53
Show file tree
Hide file tree
Showing 12 changed files with 384 additions and 97 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.035.1
2.1.035.2
133 changes: 65 additions & 68 deletions commands/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,87 +9,84 @@
// Check access.
bot_access_check($update, 'list');

// Build query.
$rs = my_query(
"
SELECT raids.*,
gyms.lat, gyms.lon, gyms.address, gyms.gym_name, gyms.ex_gym,
users.name
FROM raids
LEFT JOIN gyms
ON raids.gym_id = gyms.id
LEFT JOIN users
ON raids.user_id = users.user_id
WHERE raids.end_time>UTC_TIMESTAMP()
ORDER BY raids.end_time ASC LIMIT 20
"
);

// Count results.
$count = 0;
// Include
include(ROOT_PATH . '/logic/get_active_raids.php');

// Init text and keys.
$text = '';
$keys = [];

// Get raids.
while ($raid = $rs->fetch()) {
// Set text and keys.
$gym_name = $raid['gym_name'];
if(empty($gym_name)) {
$gym_name = '';
}
$raids = get_active_raids();

$text .= $gym_name . CR;
$raid_day = dt2date($raid['start_time']);
$now = utcnow();
$today = dt2date($now);
$start = dt2time($raid['start_time']);
$end = dt2time($raid['end_time']);
$text .= get_local_pokemon_name($raid['pokemon'], $raid['pokemon_form']) . SP . '-' . SP . (($raid_day == $today) ? '' : ($raid_day . ', ')) . $start . SP . getTranslation('to') . SP . $end . CR . CR;

// Split pokemon and form to get the pokedex id.
$pokedex_id = explode('-', $raid['pokemon'])[0];

// Pokemon is an egg?
$eggs = $GLOBALS['eggs'];
if(in_array($pokedex_id, $eggs)) {
$keys_text = EMOJI_EGG . SP . $gym_name;
} else {
$keys_text = $gym_name;
}
// Did we get any raids?
if(isset($raids[0]['r_active'])) {
debug_log($raids[0]['r_active'], 'Active raids:');

$keys[] = array(
'text' => $keys_text,
'callback_data' => $raid['id'] . ':raids_list:0'
);
// More raids as we like?
if($raids[0]['r_active'] > 12) {
// Forward to /listall
debug_log('Too much raids, forwarding to /listall');
include_once(ROOT_PATH . '/commands/listall.php');
exit();

// Counter++
$count = $count + 1;
}
// Just enough raids to display at once
} else {
//while ($raid = $rs->fetch()) {
foreach($raids as $raid) {
// Set text and keys.
$gym_name = $raid['gym_name'];
if(empty($gym_name)) {
$gym_name = '';
}

$text .= $gym_name . CR;
$raid_day = dt2date($raid['start_time']);
$now = utcnow();
$today = dt2date($now);
$start = dt2time($raid['start_time']);
$end = dt2time($raid['end_time']);
$text .= get_local_pokemon_name($raid['pokemon'], $raid['pokemon_form']) . SP . '-' . SP . (($raid_day == $today) ? '' : ($raid_day . ', ')) . $start . SP . getTranslation('to') . SP . $end . CR . CR;

// Split pokemon and form to get the pokedex id.
$pokedex_id = explode('-', $raid['pokemon'])[0];

// Pokemon is an egg?
$eggs = $GLOBALS['eggs'];
if(in_array($pokedex_id, $eggs)) {
$keys_text = EMOJI_EGG . SP . $gym_name;
} else {
$keys_text = $gym_name;
}

$keys[] = array(
'text' => $keys_text,
'callback_data' => $raid['id'] . ':raids_list:0'
);
}

// Get the inline key array.
$keys = inline_key_array($keys, 1);

// Add exit key.
$keys[] = [
[
'text' => getTranslation('abort'),
'callback_data' => '0:exit:0'
]
];

// Build message.
$msg = '<b>' . getTranslation('list_all_active_raids') . ':</b>' . CR;
$msg .= $text;
$msg .= '<b>' . getTranslation('select_gym_name') . '</b>' . CR;
}

// Set message.
if($count == 0) {
$msg = '<b>' . getTranslation('no_active_raids_found') . '</b>';
// No active raids
} else {
// Get the inline key array.
$keys = inline_key_array($keys, 1);

// Add exit key.
$keys[] = [
[
'text' => getTranslation('abort'),
'callback_data' => '0:exit:0'
]
];

// Build message.
$msg = '<b>' . getTranslation('list_all_active_raids') . ':</b>' . CR;
$msg .= $text;
$msg .= '<b>' . getTranslation('select_gym_name') . '</b>' . CR;
$msg = '<b>' . getTranslation('no_active_raids_found') . '</b>';
}

// Send message.
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

?>
22 changes: 22 additions & 0 deletions commands/listall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
// Write to log.
debug_log('LIST');

// For debug.
//debug_log($update);
//debug_log($data);

// Check access.
bot_access_check($update, 'list');

// Set message.
$msg = '<b>' . getTranslation('list_all_active_raids') . '</b>' . CR;
$msg .= '<b>' . getTranslation('select_gym_first_letter') . '</b>' . CR;

// Set keys.
$keys = raid_edit_gyms_first_letter_keys('list_by_gym');

// Send message.
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);

?>
2 changes: 1 addition & 1 deletion config/config.json.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"VERSION":"2.1.035.1",
"VERSION":"2.1.035.2",
"DB_HOST":"localhost",
"DB_NAME":"your_database_name",
"DB_USER":"your_database_user",
Expand Down
1 change: 0 additions & 1 deletion logic.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
include('logic/delete_trainerinfo.php');
include('logic/disable_raid_level.php');
include('logic/edit_pokedex_keys.php');
include('logic/get_active_raids.php');
include('logic/get_chat_title.php');
include('logic/get_formatted_pokemon_cp.php');
include('logic/get_gym_by_telegram_id.php');
Expand Down
12 changes: 7 additions & 5 deletions logic/get_active_raids.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
<?php
/**
* Get last 50 active raids.
* Get last 12 active raids.
* @return array
*/
function get_active_raids()
{
// Get last 50 active raids data.
// Get last 12 active raids data.
$rs = my_query(
"
SELECT raids.*,
gyms.lat, gyms.lon, gyms.address, gyms.gym_name, gyms.ex_gym, gyms.gym_note,
start_time, end_time,
TIME_FORMAT(TIMEDIFF(end_time, UTC_TIMESTAMP()) + INTERVAL 1 MINUTE, '%k:%i') AS t_left
TIME_FORMAT(TIMEDIFF(end_time, UTC_TIMESTAMP()) + INTERVAL 1 MINUTE, '%k:%i') AS t_left,
(SELECT COUNT(*) FROM raids WHERE end_time>UTC_TIMESTAMP()) AS r_active
FROM raids
LEFT JOIN gyms
ON raids.gym_id = gyms.id
WHERE end_time>UTC_TIMESTAMP()
ORDER BY end_time ASC LIMIT 50
ORDER BY end_time ASC
LIMIT 12
"
);

// Get the raids.
$raids = $rs->fetch();
$raids = $rs->fetchAll();

debug_log($raids);

Expand Down
38 changes: 38 additions & 0 deletions logic/get_raid_by_gym.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Get raid data by gym_id
* @param $gym_id
* @return array
*/
function get_raid_by_gym($gym_id)
{
// Get the raid data by gym_id.
$rs = my_query(
"
SELECT raids.*,
gyms.lat, gyms.lon, gyms.address, gyms.gym_name, gyms.ex_gym, gyms.gym_note,
users.name,
TIME_FORMAT(TIMEDIFF(end_time, UTC_TIMESTAMP()) + INTERVAL 1 MINUTE, '%k:%i') AS t_left,
TIMESTAMPDIFF(MINUTE,raids.start_time,raids.end_time) as t_duration
FROM raids
LEFT JOIN gyms
ON raids.gym_id = gyms.id
LEFT JOIN users
ON raids.user_id = users.user_id
WHERE end_time>UTC_TIMESTAMP()
AND gyms.id = {$gym_id}
"
);

// Get the row.
$raid = $rs->fetch();

// Inject raid level
$raid['level'] = get_raid_level($raid['pokemon'], $raid['pokemon_form']);

debug_log($raid);

return $raid;
}

?>
12 changes: 12 additions & 0 deletions logic/raid_edit_gym_keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ function raid_edit_gym_keys($first, $warn = true, $action = 'edit_raidlevel', $d
$arg = $gym['id'];
}

// List action to list only gyms with active raids, so always continue at the end
if ($action == 'list_raid') {
if ($gym['active_raid'] == 1) {
$keys[] = array(
'text' => $gym['gym_name'],
'callback_data' => $first . ':' . $action . ':' . $arg
);
}
// Continue always in case of list action
continue;
}

// Write to log.
// debug_log($gym);

Expand Down
78 changes: 57 additions & 21 deletions logic/raid_edit_gyms_first_letter_keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,64 @@ function raid_edit_gyms_first_letter_keys($action = 'raid_by_gym', $hidden = fal

// Case or not?
if(!empty($case)) {
// Get gyms from database
$rs = my_query(
"
SELECT CASE $case
ELSE UPPER(LEFT(gym_name, 1))
END AS first_letter
FROM gyms
WHERE show_gym = {$show_gym}
GROUP BY 1
ORDER BY gym_name
"
);
// List or other action?
if($action == 'list_by_gym') {
// Get gyms with active raids only from database
$rs = my_query(
"
SELECT CASE $case
ELSE UPPER(LEFT(gym_name, 1))
END AS first_letter
FROM raids
LEFT JOIN gyms
ON raids.gym_id = gyms.id
WHERE end_time>UTC_TIMESTAMP()
AND show_gym = {$show_gym}
GROUP BY 1
ORDER BY gym_name
"
);
} else {
// Get gyms from database
$rs = my_query(
"
SELECT CASE $case
ELSE UPPER(LEFT(gym_name, 1))
END AS first_letter
FROM gyms
WHERE show_gym = {$show_gym}
GROUP BY 1
ORDER BY gym_name
"
);
}
} else {
// Get gyms from database
$rs = my_query(
"
SELECT DISTINCT UPPER(SUBSTR(gym_name, 1, 1)) AS first_letter
FROM gyms
WHERE show_gym = {$show_gym}
ORDER BY 1
"
);
// List or other action?
if($action == 'list_by_gym') {
// Get gyms with active raids only from database
// Get gyms from database
$rs = my_query(
"
SELECT DISTINCT UPPER(SUBSTR(gym_name, 1, 1)) AS first_letter
FROM raids
LEFT JOIN gyms
ON raids.gym_id = gyms.id
WHERE end_time>UTC_TIMESTAMP()
AND show_gym = {$show_gym}
ORDER BY 1
"
);
} else {
// Get gyms from database
$rs = my_query(
"
SELECT DISTINCT UPPER(SUBSTR(gym_name, 1, 1)) AS first_letter
FROM gyms
WHERE show_gym = {$show_gym}
ORDER BY 1
"
);
}
}

// Init empty keys array.
Expand Down
Loading

0 comments on commit 8f7ad53

Please sign in to comment.