Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

Commit

Permalink
!roll can now accept strings as possible roll values
Browse files Browse the repository at this point in the history
  • Loading branch information
bigwheels16 committed Jan 9, 2014
1 parent 8695efa commit 4171e77
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 35 deletions.
89 changes: 60 additions & 29 deletions modules/HELPBOT_MODULE/RandomController.class.php
Expand Up @@ -48,6 +48,21 @@ class RandomController {
/** @Inject */
public $text;

/** @Inject */
public $util;

/** @Inject */
public $settingManager;

/**
* @Setting("time_between_rolls")
* @Description("How much time is required between rolls from the same person")
* @Visibility("edit")
* @Type("time")
* @Options("10s;30s;60s;90s")
*/
public $defaultTimeBetweenRolls = "30s";

/**
* This handler is called on bot startup.
* @Setup
Expand Down Expand Up @@ -102,7 +117,7 @@ public function randomCommand($message, $channel, $sender, $sendto, $args) {
* @Matches("/^roll ([0-9]+)$/i")
* @Matches("/^roll ([0-9]+) ([0-9]+)$/i")
*/
public function rollSingleCommand($message, $channel, $sender, $sendto, $args) {
public function rollNumericCommand($message, $channel, $sender, $sendto, $args) {
if (count($args) == 3) {
$min = $args[1];
$max = $args[2];
Expand All @@ -114,37 +129,55 @@ public function rollSingleCommand($message, $channel, $sender, $sendto, $args) {
if ($min >= $max) {
$msg = "The first number cannot be higher than or equal to the second number.";
} else {
$row = $this->db->queryRow("SELECT * FROM roll WHERE `name` = ? AND `time` >= ? LIMIT 1", $sender, time() - 30);
$timeBetweenRolls = $this->settingManager->get('time_between_rolls');
$row = $this->db->queryRow("SELECT * FROM roll WHERE `name` = ? AND `time` >= ? LIMIT 1", $sender, time() - $timeBetweenRolls);
if ($row === null) {
$num = rand($min, $max);
$this->db->exec("INSERT INTO roll (`time`, `name`, `type`, `start`, `end`, `result`) VALUES (?, ?, ?, ?, ?, ?)", time(), $sender, 1, $min, $max, $num);
$ver_num = $this->db->lastInsertId();
$msg = "Between $min and $max I rolled a <highlight>$num<end>, to verify do /tell <myname> verify $ver_num";
$options = array();
for ($i = $min; $i <= $max; $i++) {
$options []= $i;
}
list($ver_num, $result) = $this->roll($sender, $options);
$msg = "The roll is <highlight>$result<end> between $min and $max. To verify do /tell <myname> verify $ver_num";
} else {
$msg = "You can only flip or roll once every 30 seconds.";
$msg = "You can only flip or roll once every $timeBetweenRolls seconds.";
}
}

$sendto->reply($msg);
}

/**
* @HandlesCommand("roll")
* @Matches("/^roll (.+)$/i")
*/
public function rollNamesCommand($message, $channel, $sender, $sendto, $args) {
$names = $args[1];
$timeBetweenRolls = $this->settingManager->get('time_between_rolls');
$row = $this->db->queryRow("SELECT * FROM roll WHERE `name` = ? AND `time` >= ? LIMIT 1", $sender, time() - $timeBetweenRolls);
if ($row === null) {
$options = explode(' ', $names);
list($ver_num, $result) = $this->roll($sender, $options);
$msg = "The roll is <highlight>$result<end> out of possible options: $names. To verify do /tell <myname> verify $ver_num";
} else {
$msg = "You can only flip or roll once every $timeBetweenRolls seconds.";
}

$sendto->reply($msg);
}

/**
* @HandlesCommand("flip")
* @Matches("/^flip$/i")
*/
public function flipCommand($message, $channel, $sender, $sendto, $args) {
$row = $this->db->queryRow("SELECT * FROM roll WHERE `name` = ? AND `time` >= ? LIMIT 1", $sender, time() - 30);
$timeBetweenRolls = $this->settingManager->get('time_between_rolls');
$row = $this->db->queryRow("SELECT * FROM roll WHERE `name` = ? AND `time` >= ? LIMIT 1", $sender, time() - $timeBetweenRolls);
if ($row === null) {
$flip = rand(1, 2);
$this->db->exec("INSERT INTO roll (`time`, `name`, `type`, `result`) VALUES (?, ?, ?, ?)", time(), $sender, 0, $flip);
$ver_num = $this->db->lastInsertId();
if ($flip == 1) {
$msg = "The coin landed <highlight>heads<end>, to verify do /tell <myname> verify $ver_num";
} else {
$msg = "The coin landed <highlight>tails<end>, to verify do /tell <myname> verify $ver_num";
}
$options = array('heads', 'tails');
list($ver_num, $result) = $this->roll($sender, $options);
$msg = "The coin landed <highlight>$result<end>. To verify do /tell <myname> verify $ver_num";
} else {
$msg = "You can only flip or roll once every 30 seconds.";
$msg = "You can only flip or roll once every $timeBetweenRolls seconds.";
}

$sendto->reply($msg);
Expand All @@ -158,21 +191,19 @@ public function verifyCommand($message, $channel, $sender, $sendto, $args) {
$id = $args[1];
$row = $this->db->queryRow("SELECT * FROM roll WHERE `id` = ?", $id);
if ($row === null) {
$msg = "That verify number does not exist.";
$msg = "Verify number <highlight>$id<end> does not exist.";
} else {
$time = time() - $row->time;
$msg = "$time seconds ago I told <highlight>$row->name<end>: ";
if ($row->type == 0) {
if ($row->result == 1) {
$msg .= "The coin landed <highlight>heads<end>.";
} else {
$msg .= "The coin landed <highlight>tails<end>.";
}
} else {
$msg .= "Between $row->start and $row->end I rolled a <highlight>$row->result<end>.";
}
$time = $this->util->unixtime_to_readable(time() - $row->time);
$msg = "<highlight>$row->result<end> rolled by <highlight>$row->name<end> $time ago. Possible options: ";
$msg .= str_replace("|", " ", $row->options) . ".";
}

$sendto->reply($msg);
}

public function roll($sender, $options) {
$result = $this->util->rand_array_value($options);
$this->db->exec("INSERT INTO roll (`time`, `name`, `options`, `result`) VALUES (?, ?, ?, ?)", time(), $sender, implode($options, "|"), $result);
return array($this->db->lastInsertId(), $result);
}
}
2 changes: 1 addition & 1 deletion modules/HELPBOT_MODULE/roll.sql
@@ -1 +1 @@
CREATE TABLE IF NOT EXISTS roll (`id` INTEGER PRIMARY KEY AUTO_INCREMENT, `time` INT, `name` VARCHAR(25), `type` INT, `start` INT, `end` INT, `result` INT);
CREATE TABLE IF NOT EXISTS roll (`id` INTEGER PRIMARY KEY AUTO_INCREMENT, `time` INT, `name` VARCHAR(255), `options` VARCHAR(5000), `result` VARCHAR(255));
4 changes: 4 additions & 0 deletions modules/HELPBOT_MODULE/roll.txt
Expand Up @@ -11,6 +11,10 @@ To roll a number between 1 and a number:
To roll a number between a startvalue and a endvalue:
<highlight><tab><symbol>roll 'startvalue' 'endvalue'<end>

To roll a random value:
<highlight><tab><symbol>roll 'value1' 'value2' 'value3' ...<end>
Note that values must not contain spaces.

<header2>Verify Command<end>

To verify a roll:
Expand Down
11 changes: 6 additions & 5 deletions upgrade.php
Expand Up @@ -79,11 +79,6 @@ function checkIfTableExists($db, $table) {
}

function checkIfColumnExists($db, $table, $column) {
// If the table doesn't exist, return true since the table will be created with the correct column.
if (checkIfTableExists($db, $table) == false) {
return true;
}

// Else if the table exists but the column doesn't, return false so the table will be updated with the correct column.
try {
$data = $db->query("SELECT $column FROM $table");
Expand All @@ -109,4 +104,10 @@ function loadSQLFile($db, $filename) {
upgrade($db, $line);
}
}

// if roll table has 'type' column, then drop it so it can be reloaded with new schema changes
// it shouldn't matter if the data in that table is lost -Tyrence
if (checkIfColumnExists($db, 'roll', 'type')) {
$db->exec("DROP TABLE roll");
}
?>

0 comments on commit 4171e77

Please sign in to comment.