Skip to content

Commit

Permalink
Merge pull request pokepark#30 from Chefkeks/new-questbot
Browse files Browse the repository at this point in the history
  • Loading branch information
florianbecker committed Sep 14, 2019
1 parent 74482bc commit ca1787c
Show file tree
Hide file tree
Showing 82 changed files with 8,786 additions and 6,308 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/.idea
config.php
/lang/event.json
11 changes: 11 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Block access to all dot files
RedirectMatch 404 /\..*$

# Block access to .git files and folders
RedirectMatch 404 /\.git

# Block access to .json and .sql files
RedirectMatch 404 /*\.(json|sql)$

# Block access to specifc folders
RedirectMatch 404 /(access|config|custom|ddos|lang|sql|screens)/
499 changes: 399 additions & 100 deletions README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.9.256.3
4 changes: 4 additions & 0 deletions access/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
152 changes: 152 additions & 0 deletions commands/addstop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php
// Write to log.
debug_log('ADDSTOP()');

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

// Check access.
bot_access_check($update, 'pokestop-add');

// Get stop coords.
$input = trim(substr($update['message']['text'], 8));

// Count commas given in input.
$count = substr_count($input, ",");

// 1 comma as it should be?
// E.g. 52.5145434,13.3501189
if($count == 1) {
$lat_lon = explode(',', $input);
$lat = $lat_lon[0];
$lon = $lat_lon[1];

// Lat and lon with comma instead of dot?
// E.g. 52,5145434,13,3501189
} else if($count == 3) {
$lat_lon = explode(',', $input);
$lat = $lat_lon[0] . '.' . $lat_lon[1];
$lon = $lat_lon[2] . '.' . $lat_lon[3];
} else {
// Invalid input - send the message and exit.
$msg = getTranslation('invalid_input');
sendMessage($update['message']['chat']['id'], $msg);
exit();
}

// Set stop name.
$stop_name = '#' . $update['message']['from']['id'];

// Get address.
$addr = get_address($lat, $lon);
$address = format_address($addr);

// Insert / update stop.
try {

global $db;

// Build query to check if stop is already in database or not
$rs = my_query("
SELECT COUNT(*)
FROM pokestops
WHERE pokestop_name = '{$stop_name}'
");

$row = $rs->fetch_row();

// Pokestop already in database or new
if (empty($row['0'])) {
// insert stop in table.
debug_log('Pokestop not found in database pokestop list! Inserting pokestop "' . $stop_name . '" now.');
$query = '
INSERT INTO pokestops (pokestop_name, lat, lon, address)
VALUES (:stop_name, :lat, :lon, :address)
';
$msg = getTranslation('pokestop_added');
} else {
// Get stop by temporary name.
$stop = get_pokestop_by_telegram_id($stop_name);

// If stop is already in the database, make sure no quest is shared before continuing!
if($stop) {
debug_log('Pokestop found in the database! Checking for shared quest now!');
$stop_id = $stop['id'];

// Check for duplicate quest
$duplicate_id = 0;
$duplicate_id = quest_duplication_check($stop_id);

// Continue with stop creation
if($duplicate_id > 0) {
debug_log('Quest is shared for that stop!');
debug_log('Tell user to update the stop name and exit!');

// Show message that a quest is shared for that pokestop.
$quest_id = $duplicate_id;
$quest = get_quest($quest_id);

// Build message.
$msg = EMOJI_WARN . SP . getTranslation('quest_already_exists') . SP . EMOJI_WARN . CR . get_formatted_quest($quest);

// Tell user to update the stop name first to create another pokestop
$msg .= getTranslation('pokestopname_then_location');
$keys = [];

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

exit();
} else {
debug_log('No shared quest found! Continuing now ...');
}
} else {
// Set stop_id to 0
$stop_id = 0;
debug_log('No pokestop found in the database! Continuing now ...');
}

// Update pokestops table to reflect pokestop changes.
debug_log('Pokestop found in database pokestops list! Updating pokestop "' . $stop_name . '" now.');
$query = '
UPDATE pokestops
SET lat = :lat,
lon = :lon,
address = :address
WHERE pokestop_name = :stop_name
';
$msg = getTranslation('pokestop_updated');
}

$statement = $dbh->prepare($query);
$statement->bindValue(':stop_name', $stop_name, PDO::PARAM_STR);
$statement->bindValue(':lat', $lat, PDO::PARAM_STR);
$statement->bindValue(':lon', $lon, PDO::PARAM_STR);
$statement->bindValue(':address', $address, PDO::PARAM_STR);
$statement->execute();

// Get last insert id.
if (empty($row['0'])) {
$stop_id = $dbh->lastInsertId();
}

// Stop details.
if($stop_id > 0) {
$stop = get_pokestop($stop_id);
$msg .= CR . CR . get_pokestop_details($stop);
}
} catch (PDOException $exception) {

error_log($exception->getMessage());
$dbh = null;
exit();
}

// Set keys.
$keys = [];

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

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

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

// Check access.
bot_access_check($update, 'invasion-create');

// Add comment to invasion.
// Trim away everything before the first space
// Max. 48 chars.
$comment = $update['message']['text'];
$comment = trim(substr($comment, strpos($comment, ' ') + 1));
$comment = substr($comment,0,48);

// Get all quests of the day from database.
$rs = my_query(
"
SELECT invasions.*,
pokestops.pokestop_name, pokestops.lat, pokestops.lon, pokestops.address
FROM invasions
LEFT JOIN pokestops
ON invasions.pokestop_id = pokestops.id
WHERE invasions.end_time > UTC_TIMESTAMP()
ORDER BY invasions.id
"
);

// Init empty keys array.
$keys = array();

// Add key for quest
while ($invasions = $rs->fetch_assoc()) {
// Pokestop name.
$text = empty($invasions['pokestop_name']) ? getTranslation('unnamed_pokestop') : $invasions['pokestop_name'];

// Add buttons to delete invasions.
$keys[] = array(
'text' => $text,
'callback_data' => $invasions['id'] . ':crypto:' . $comment
);

}

// Keys array received?
if ($keys) {
// Set message.
$msg = '<b>' . getTranslation('add_this_info_to_invasion') . '</b>';
$msg .= CR . CR . getTranslation('info') . ': ' . '<b>' . $comment . '</b>';

// Add abort navigation key.
$keys = inline_key_array($keys, 2);
$nav_keys = [];
$nav_keys[] = universal_inner_key($keys, '0', 'exit', '0', getTranslation('abort'));

// Get the inline key array.
$keys[] = $nav_keys;
} else {
// Set message.
$msg = '<b>' . getTranslation('no_invasions_currently') . '</b>';

// Set empty keys.
$keys = [];
}

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

exit();
7 changes: 4 additions & 3 deletions commands/delete.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//debug_log($data);

// Check access.
bot_access_check($update, BOT_ACCESS);
bot_access_check($update, 'access-bot');

// Get all quests of the day from database.
$rs = my_query(
"
SELECT quests.*,
questlist.quest_type, questlist.quest_quantity, questlist.quest_action,
questlist.quest_event, questlist.quest_type, questlist.quest_quantity, questlist.quest_action, questlist.quest_pokedex_ids, questlist.quest_poketypes,
rewardlist.reward_type, rewardlist.reward_quantity,
pokestops.pokestop_name, pokestops.lat, pokestops.lon, pokestops.address
FROM quests
Expand All @@ -23,7 +23,8 @@
ON quests.quest_id = questlist.id
LEFT JOIN rewardlist
ON quests.reward_id = rewardlist.id
WHERE quest_date = CURDATE()
WHERE quest_date > UTC_DATE()
AND quest_date < UTC_DATE() + INTERVAL 1 DAY
ORDER BY quests.id
"
);
Expand Down
42 changes: 42 additions & 0 deletions commands/deletestop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
// Write to log.
debug_log('DELETESTOP()');

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

// Check access.
bot_access_check($update, 'pokestop-delete');

// Get pokestops by name.
// Trim away everything before the first space
$searchterm = $update['message']['text'];
$searchterm = trim(substr($searchterm, strpos($searchterm, ' ') + 1));

// Get all matching pokestops.
$keys = get_pokestop_list_keys($searchterm, 'stop_delete');

// Keys array received?
if (is_array($keys)) {
// Set message.
$msg = '<b>' . getTranslation('select_pokestop_to_delete') . '</b>';

// Add abort navigation key.
$nav_keys = [];
$nav_keys[] = universal_inner_key($keys, '0', 'exit', '0', getTranslation('abort'));

// Get the inline key array.
$keys[] = $nav_keys;
} else {
// Set message.
$msg = '<b>' . getTranslation('pokestops_not_found') . '</b>';

// Set empty keys.
$keys = [];
}

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

exit();
25 changes: 25 additions & 0 deletions commands/dex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
// Write to log.
debug_log('DEX()');

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

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

// Create keys array.
$keys = [];

// Get pokemon name.
$pokemon = trim(substr($update['message']['text'], 4));

// Set message.
$msg = '<b>' . getTranslation('pokemon') . ':</b>' . CR;
$msg .= get_dex_entry($pokemon);

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

exit();
33 changes: 33 additions & 0 deletions commands/event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
// Write to log.
debug_log('EVENT()');

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

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

// Get config name and value.
$input = trim(substr($update['message']['text'], 6));

// Set event name for current language and default language.
if(!empty($input)) {
$eventfile = BOT_LANG_PATH . '/event.json';
$data = '{"quest_event_9999":{"' . DEFAULT_LANGUAGE . '":"' . $input . '"}}';
file_put_contents($eventfile, $data);
$msg = getTranslation('event_saved') . CR . CR;
$msg .= getTranslation('event') . ': <b>' . $input . '</b>' . CR;
debug_log('Current event:' . $input);

// Tell user how to set config and what is allowed to be set by config.
} else {
$msg = '<b>' . getTranslation('event_name_missing') . '</b>';
debug_log('Unsupported request for submitting an event!');
}

// Send message.
sendMessage($update['message']['chat']['id'], $msg);

?>
Loading

0 comments on commit ca1787c

Please sign in to comment.