Skip to content

Commit

Permalink
New raidbot, new core.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chefkeks committed Apr 26, 2019
1 parent 191073d commit c671fcc
Show file tree
Hide file tree
Showing 69 changed files with 4,262 additions and 7,072 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea
config.php
custom-constants.php
315 changes: 214 additions & 101 deletions README.md

Large diffs are not rendered by default.

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
138 changes: 138 additions & 0 deletions commands/addgym.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
// Write to log.
debug_log('ADDGYM()');

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

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

// Get gym name.
$input = trim(substr($update['message']['text'], 7));

// Count commans 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 gym name.
$gym_name = '#' . $update['message']['from']['id'];

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

// Insert / update gym.
try {

global $db;

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

$row = $rs->fetch_row();

// Gym already in database or new
if (empty($row['0'])) {
// insert gym in table.
debug_log('Gym not found in database gym list! Inserting gym "' . $gym_name . '" now.');
$query = '
INSERT INTO gyms (gym_name, lat, lon, address, show_gym)
VALUES (:gym_name, :lat, :lon, :address, 0)
';
$msg = getTranslation('gym_added');
} else {
// Get gym by temporary name.
$gym = get_gym_by_telegram_id($gym_name);

// If gym is already in the database, make sure no raid is active before continuing!
if($gym) {
debug_log('Gym found in the database! Checking for active raid now!');
$gym_id = $gym['id'];

// Check for duplicate raid
$duplicate_id = 0;
$duplicate_id = active_raid_duplication_check($gym_id);

// Continue with raid creation
if($duplicate_id > 0) {
debug_log('Active raid is in progress!');
debug_log('Tell user to update the gymname and exit!');

// Show message that a raid is active on that gym.
$raid_id = $duplicate_id;
$raid = get_raid($raid_id);

// Build message.
$msg = EMOJI_WARN . SP . getTranslation('raid_already_exists') . SP . EMOJI_WARN . CR . show_raid_poll_small($raid);

// Tell user to update the gymname first to create another raid by location
$msg .= getTranslation('gymname_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 active raid found! Continuing now ...');
}
} else {
// Set gym_id to 0
$gym_id = 0;
debug_log('No gym found in the database! Continuing now ...');
}

// Update gyms table to reflect gym changes.
debug_log('Gym found in database gym list! Updating gym "' . $gym_name . '" now.');
$query = '
UPDATE gyms
SET lat = :lat,
lon = :lon,
address = :address
WHERE gym_name = :gym_name
';
$msg = getTranslation('gym_updated');
}

$statement = $dbh->prepare($query);
$statement->bindValue(':gym_name', $gym_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();
} catch (PDOException $exception) {

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

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

?>
26 changes: 10 additions & 16 deletions commands/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
//debug_log($data);

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

// Get timezone.
$tz = TIMEZONE;
bot_access_check($update, 'delete');

// Count results.
$count = 0;
Expand All @@ -28,32 +25,29 @@
gyms.address ,
gyms.gym_name ,
gyms.ex_gym ,
users. NAME ,
UNIX_TIMESTAMP(start_time) AS ts_start ,
UNIX_TIMESTAMP(end_time) AS ts_end ,
UNIX_TIMESTAMP(NOW()) AS ts_now ,
UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(NOW()) AS t_left
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 > NOW()
AND raids.timezone = :timezone
raids.end_time > UTC_TIMESTAMP()
ORDER BY
raids.end_time ASC
LIMIT 20
';
$statement = $dbh->prepare( $query );
$statement->bindValue(':timezone', $tz, PDO::PARAM_STR);
$statement->bindValue(':now', $now, PDO::PARAM_STR);
$statement->execute();
while ($row = $statement->fetch()) {

// Set text and keys.
$text .= $row['gym_name'] . CR;
$raid_day = unix2tz($row['ts_start'], $row['timezone'], 'Y-m-d');
$today = unix2tz($row['ts_now'], $row['timezone'], 'Y-m-d');
$text .= get_local_pokemon_name($row['pokemon']) . SP . '' . SP . (($raid_day == $today) ? '' : ($raid_day . ', ')) . unix2tz($row['ts_start'], $row['timezone']) . SP . getTranslation('to') . SP . unix2tz($row['ts_end'], $row['timezone']) . CR . CR;
$now = utcnow();
$today = dt2date($now);
$raid_day = dt2date($row['start_time']);
$start = dt2time($row['start_time']);
$end = dt2time($row['end_time']);
$text .= get_local_pokemon_name($row['pokemon']) . SP . '' . SP . (($raid_day == $today) ? '' : ($raid_day . ', ')) . $start . SP . getTranslation('to') . SP . $end . CR . CR;
$keys[] = array(
'text' => $row['gym_name'],
'callback_data' => $row['id'] . ':raids_delete:0'
Expand Down
29 changes: 29 additions & 0 deletions commands/deletegym.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
// Write to log.
debug_log('DELETEGYM()');

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

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

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

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

// Add key for hidden gyms.
$h_keys = [];
$h_keys[] = universal_inner_key($h_keys, '0', 'gym_hidden_letter', 'gym_delete', getTranslation('hidden_gyms'));
$h_keys = inline_key_array($h_keys, 1);

// Merge keys.
$keys = array_merge($h_keys, $keys);

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

?>
4 changes: 2 additions & 2 deletions commands/exreport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//debug_log($update);
//debug_log($data);

// Check access - user must be admin!
bot_access_check($update, BOT_ADMINS);
// Check access.
bot_access_check($update, 'ex-report');

// Init empty keys array.
$keys = [];
Expand Down
56 changes: 20 additions & 36 deletions commands/gym.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,45 +1,29 @@
<?php
// Write to log.
debug_log('GYM()');
debug_log('GYM');

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

// Get gym name.
$gym_name = trim(substr($update['message']['text'], 4));
// Check access.
bot_access_check($update, 'gym-details');

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

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

// Add key for hidden gyms.
$h_keys = [];
$h_keys[] = universal_inner_key($h_keys, '0', 'gym_hidden_letter', 'gym_details', getTranslation('hidden_gyms'));
$h_keys = inline_key_array($h_keys, 1);

// Merge keys.
$keys = array_merge($h_keys, $keys);

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

// Write to log.
debug_log('Setting gym name to ' . $gym_name);

// Private chat type.
if ($update['message']['chat']['type'] == 'private') {

try {

// Update gym name in raid table.
$query = '
UPDATE gyms
SET gym_name = :gym_name, show_gym = 1
WHERE
gym_id = :gym_id
ORDER BY
id DESC
LIMIT 1
';
$statement = $dbh->prepare( $query );
$statement->bindValue(':gym_name', $gym_name, PDO::PARAM_STR);
$statement->bindValue(':gym_id', $update['message']['from']['id'], PDO::PARAM_INT);
$statement->execute();
}
catch (PDOException $exception) {

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

// Send the message.
sendMessage($update['message']['chat']['id'], getTranslation('gym_name_updated'));
}
?>
Loading

0 comments on commit c671fcc

Please sign in to comment.