L* R*
HOME FORUM DOWNLOADS
Content
  Links
     Browse SVN
     SVN Commit log
     Documentation (Wiki)
  Developers
     Taskmanager
User
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 01, 2008, 11:03:13 PM

Login with username, password and session length
Search



Advanced search
Support GoPHP5.org
BeBot - An Anarchy Online/Age Of Conan chat automaton > Forum > Modules > 0.4.x Custom/Unofficial Modules > Topic: Timer
Pages: [1]   Go Down
« previous next »
Print
Author Topic: Timer  (Read 537 times)
0 Members and 1 Guest are viewing this topic.
lazarus
Rookie
**
Offline Offline

Posts: 25


Timer
« on: July 29, 2007, 06:24:37 AM »

Request Smiley MUHAHA! Smiley

I dont know how to do it Smiley
and the old module did not work. :/
Logged
Etric
Freshman
*
Offline Offline

Posts: 4


Re: Timer
« Reply #1 on: July 30, 2007, 09:04:39 AM »

This is the Custom Timer Modul i use with the new BeBot.
it's just rewritten and the original code is from someoneelse in this forum (sorry forgett where i've got the code from)

only downside, everyone can delete every timer, this should be changed to the new restriction.

code see below post
« Last Edit: July 30, 2007, 12:26:05 PM by Etric » Logged
lazarus
Rookie
**
Offline Offline

Posts: 25


Re: Timer
« Reply #2 on: July 30, 2007, 09:51:49 AM »

So that timer module dont use the settings module?
Logged
Khalem
BeBot Founder
Administrator
Grandmaster
********
Offline Offline

Gender: Male
Posts: 670



WWW
Re: Timer
« Reply #3 on: July 30, 2007, 09:52:01 AM »

Looks like you saved me a bit of work as this was on my personal ToDo list Tongue

I'll have a look at it later Smiley
Logged

BeBot Founder and Fixer Kingpin
Madman coder and destroyer of good code
Etric
Freshman
*
Offline Offline

Posts: 4


Re: Timer
« Reply #4 on: July 30, 2007, 10:08:10 AM »

Quote from: lazarus on July 30, 2007, 09:51:49 AM
So that timer module dont use the settings module?

i've made this more as a personal hack than something that should run for everyone, thats the reason it does'nt use the settings or access restriction parts of the bot (had no need for that)

but it's not that hard to implement this if you wish.

(and just as a side note to Khalem and the rest of the BeBot team: you done an awsome job with the new 0.4.0 version. All the changes since the 0.3 version i've used bevor are really great Smiley
Logged
Alreadythere
BeBot Maintainer
Administrator
Grandmaster
********
Offline Offline

Posts: 1085


Re: Timer
« Reply #5 on: July 30, 2007, 12:18:13 PM »

I'm working on a more complex timer implementation for 0.4/0.5, will make a post when I got a first running version.
Logged
Etric
Freshman
*
Offline Offline

Posts: 4


Re: Timer
« Reply #6 on: July 30, 2007, 12:25:45 PM »

Quote from: Alreadythere on July 30, 2007, 12:18:13 PM
I'm working on a more complex timer implementation for 0.4/0.5, will make a post when I got a first running version.

ah nice Smiley until that comes here is the first version with security, settings and table prefix implementation.

if your timer implements loops i'll make a little backflip here Smiley

Code:
<?php
/*
* CustomTimer.php - enables custom private/open timer.
*
* BeBot - An Anarchy Online Chat Automaton
* Copyright (C) 2004 Jonas Jax
* Copyright (C) 2005-2007 Thomas Juberg Stensås, ShadowRealm Creations and the BeBot development team.
*
* Developed by:
* - Alreadythere (RK2)
* - Blondengy (RK1)
* - Blueeagl3 (RK1)
* - Glarawyn (RK1)
* - Khalem (RK1)
* - Naturalistic (RK1)
*
* See Credits file for all aknowledgements.
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
*  USA
*/

/*
Add a "_" at the beginning of the file (_ClassName.php) if you do not want it to be loaded.
*/

$db -> query("CREATE TABLE IF NOT EXISTS " . $db -> define_tablename("timers", "true") . " (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`type` VARCHAR( 1 ) NOT NULL ,
`setby` VARCHAR( 100 ) NOT NULL ,
`description` VARCHAR( 255 ) NOT NULL ,
`expires` INT( 11 ) NOT NULL ,
PRIMARY KEY ( `id` )
);"
);  

$customTimer = new CustomTimer($bot);

$commands["tell"]["timer"] = &$customTimer;
$commands["pgmsg"]["timer"] = &$customTimer;
$commands["gc"]["timer"] = &$customTimer;

$cron["2sec"][] = &$customTimer;

/*
The Class itself...
*/
class CustomTimer
{
var $bot;
var $help;
var $timers;
var $output;
var $date_format;

/*
Constructor:
Hands over a referance to the "Bot" class.
Defines access control for the commands
Creates settings for the module
Defines help for the commands
*/
function CustomTimer (&$bot)
{
$this -> bot = &$bot;

$this -> bot -> accesscontrol -> create ('all', 'timer', 'GUEST');

$this -> bot -> settings -> create('CustomTimers', 'date_format', 'M j, Y, G:i', 'sets the format for the date/time shown in the blob window');
$this -> bot -> settings -> create('CustomTimers', 'Security', 'superadmin', 'Defines which group is allowed to delete timers beside the owner');

$this -> help['description'] = 'Custom Timer modul. This enables you to set your own custom timers.';
$this -> help['command']['timer']="- Shows the current timer";
$this -> help['command']['timer o h:m:s <text>']="- Adds a new open timer. Counts in guildchat.";
$this -> help['command']['timer p h:m:s <text>']="- Adds a new private timer. Counts in tell.";
$this -> help['command']['timer del <id>']="- Deletes the timer <id> if you are the owner or have the rights to do it.";
$this -> help['command']['timer reload']="- realoads the timer if something strange is shown.";

$this -> load_timers();

}

/*
This is somewhat a kludge until the internal handling of commands gets altered, in the future, these functions will be depreciated and
the command handler will be called directly.
Just pass off to the unified handler
*/
function tell($source, $msg)
{
$this -> handler ($source, $msg, 1);
}

function pgmsg($source, $msg)
{
$this -> handler ($source, $msg, 2);
}

function gc($source, $msg)
{
$this -> handler ($source, $msg, 3);
}

/*
Unified message handler
$source: The originating player
$msg: The actual message, including command prefix and all
$type: The channel the message arrived from. 1 Being tells, 2 being private groupm 3 being guildchat
*/
function handler($source, $msg, $type)
{
$vars = explode(' ', strtolower($msg));
if (!empty($this -> bot -> commpre))
{
$vars[0] = substr($vars[0], 1);
}

$command = $vars[0];

switch($command)
{
case 'timer':
if($vars[1] == "")
$this -> bot -> send_output($source, $this -> show_timers(), $type);
else if($vars[1] == "o" || $vars[1] == "p")
{
preg_match("/^" . $this -> bot -> commpre . "timer (o|p) ([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?) (.*)$/i", $msg, $info);
$this -> bot -> send_output($source, $this -> set_timer($source, $info[1], $info[2], $info[3], $info[4], $info[5]), $type);
}
else if($vars[1] == "del")
$this -> bot -> send_output($source, $this -> delete_timer($source, $vars[2]), $type);
else if($vars[1] == "reload")
$this -> bot -> send_output($source, $this -> reload_timers(), $type);
else
{
$this -> bot -> send_output($source, "##error##Invalid syntax##end##. Please use ##highlight##!timer [o|p] h:m:s text##end##", $type);
}
break;
default:
// Just a safety net to allow you to catch errors where a module has registered  a command, but fails to actually do anything about it
// $this -> bot -> send_output($source, $text, $type) will send $text to $source by tell if $type is 1 (tell) or to the apropriate channel if $type is 2 or 3.
$this -> bot -> send_output($source, "Broken plugin, recieved unhandled command: $command", $type);
}
}

function set_timer($name, $type, $hours, $minutes, $seconds, $description)
{
$stamp = mktime() + ($hours*60*60) + ($minutes*60) + $seconds;
$this -> bot -> db -> query("INSERT INTO #___timers (type, setby, description, expires) VALUES ('$type','$name', '$description', $stamp)");
$this -> load_timers();
return "##highlight##Timer set.##end##";
}

function load_timers()
{
$this -> timers = $this -> bot -> db -> select("SELECT * FROM #___timers WHERE expires > ".mktime()." ORDER BY id ASC");
$this -> update_output();
}

function reload_timers()
{
$this -> timers = $this -> bot -> db -> select("SELECT * FROM #___timers WHERE expires > ".mktime()." ORDER BY id ASC");
$this -> update_output();
return "##highlight##Timer neu geladen.##end##";
}

function delete_timer($name, $id)
{
$result = $this -> bot -> db -> select ("SELECT id, setby FROM #___timers WHERE id = $id");

if (empty($result))
return "##error##No timer with that ID.##end##";

if($this -> bot -> security -> check_access($name, $this -> bot -> settings -> get("CustomTimers", "Security")) || strtolower($name) == strtolower($result[0][1]))
{
$this -> bot -> db -> query("DELETE FROM #___timers WHERE id = $id");
$this -> load_timers();
return "##highlight##Timer deleted.##end##";
}
else
{
return "##error##Sorry, you are not allowed to delete this timer.##end##";
}
}

// keeps a list updated so we dont have to make one all the time
function update_output()
{
$output = "";
if (!empty($this -> timers))
{
foreach($this -> timers AS $timer)
{
$dateFrom = date("d-m-Y H:i:s", mktime());
$dateTo = date("d-m-Y H:i:s", $timer[4]);

$diffa = $this -> getDateDifference($dateFrom, $dateTo);

$time_ago = $diffa['days']." days ".$diffa['hours']."h ".$diffa['minutes']. "min ".$diffa['seconds']. "sec left";

if($timer[1] == "p") { $timertype = "private"; }
if($timer[1] == "o") { $timertype = "open"; }
$output .= "\n\n###error##".$timer[0]."##end## :: ##highlight##".$timertype."##end## :: setby ##highlight##".$timer[2]."##end## ::  ##normal##".$timer[3]."##end## :: ##highlight##".$time_ago."##end##";
}
}
$this -> output = $output;
}

function show_timers()
{
if (empty($this -> output))
{
return "##error##No timers set.##end##";
}
else
{
$output = "##blob_title##::::: TIMER Overview :::::##end##\n\n";
$output .= "Time now :: ##highlight##" . date($this -> bot -> settings -> get("CustomTimers", "date_format"), mktime())."##end##\n";
$output .= "______________________________________________________\n" . $this -> output;
return "Timers :: " . $this -> bot -> make_blob("click to view", $output);
}
}

function getDateDifference($dateFrom, $dateTo)
{
   $difference = null;

   $dateFromElements = split(' ', $dateFrom);
   $dateToElements = split(' ', $dateTo);

   $dateFromDateElements = split('-', $dateFromElements[0]);
   $dateFromTimeElements = split(':', $dateFromElements[1]);
   $dateToDateElements = split('-', $dateToElements[0]);
   $dateToTimeElements = split(':', $dateToElements[1]);

   // Get unix timestamp for both dates

   $date1 = mktime($dateFromTimeElements[0], $dateFromTimeElements[1], $dateFromTimeElements[2], $dateFromDateElements[1], $dateFromDateElements[0], $dateFromDateElements[2]);
   $date2 = mktime($dateToTimeElements[0], $dateToTimeElements[1], $dateToTimeElements[2], $dateToDateElements[1], $dateToDateElements[0], $dateToDateElements[2]);

   if( $date1 > $date2 )
   {
       return null;
   }

   $diff = $date2 - $date1;

   $days = 0;
   $hours = 0;
   $minutes = 0;
   $seconds = 0;

   if ($diff % 86400 <= 0)  // there are 86,400 seconds in a day
   {
       $days = $diff / 86400;
   }

   if($diff % 86400 > 0)
   {
       $rest = ($diff % 86400);
       $days = ($diff - $rest) / 86400;

       if( $rest % 3600 > 0 )
       {
           $rest1 = ($rest % 3600);
           $hours = ($rest - $rest1) / 3600;

           if( $rest1 % 60 > 0 )
           {
               $rest2 = ($rest1 % 60);
               $minutes = ($rest1 - $rest2) / 60;
               $seconds = $rest2;
           }
           else
           {
               $minutes = $rest1 / 60;
           }
       }
       else
       {
           $hours = $rest / 3600;
       }
   }

   $difference = array (
               "days" => $days,
               "hours" => $hours,
               "minutes" => $minutes,
               "seconds" => $seconds
           );

   return $difference;
}

/*
This gets called on cron
*/
function cron()
{
if (empty($this -> timers)) return;

foreach($this -> timers AS $timer)
{

$type   = $timer[1]; // o/p
$soruce = $timer[2]; // name
$show   = false;