Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # COUNTER
- # Author: Bry8Star. <bry 8 s tar a.t ya hoo d.o.t c om>
- #
- # Copyright (C) 2013 Bright Star, Bry8Star.
- #
- # You Must obtain explicit permission from Author, before using this code.
- #
- # This plugin can Repeat a (or set of) task either with fixed-length
- # time, or with variable amount of time. This Plugin can be turned
- # off or on, max repeatation number can be set, fixed or variable amount
- # of time duration can also be set, etc.
- #
- #
- #
- # Many "prnt" command lines added for debug purpose, that is, to show
- # what is going on, once working fine or expected, then disable those
- # lines by placing a "#" symbol in front of "prnt" command.
- use strict;
- use warnings;
- use Xchat qw(:all);
- Xchat::register('COUNTER','1.0.3','COUNTER allows to repeat tasks. Such as showing counting numbers. More info, type /COUNTER');
- # Controlling Global Variables:
- my($callAgain)=0; # indicator for Counter: Enable(1) or Disable(0) ?
- my($counter)=0; # it stores counting/loop number.
- my($upto)=5; # number of repeatation/loop amount, is 5.
- my($timerExist)=0; # Whan a hook_timer is created then this is set to 1.
- my($timerType)=2; # 1 = fixed-length timer, 2 = variable-length timer.
- my($hookPoint)=0; # used for storing timer's ref-/mem-location-point.
- my($timeInterval)=10000; # user given main time interval. 10000ms=10sec.
- my($variableTime)=5000; # variable time amount. Max Half of $timeInterval.
- my($randomTime)=10000; # random time = (+/-~$variableTime + $timeInterval).
- prnt("\x{2}COUNTER\x{2} plugin for XChat, started. \(A plugin for repeating tasks\). To see more info, you may type: /COUNTER");
- # This portion developed by Bry8Star:
- Xchat::hook_command( "COUNTER", sub{
- if (not defined $_[0][1]) {
- # User did not specify any sub-command or option after main command:
- my($mS)="";
- $mS .= "\x{2}COUNTER\x{2} --> Currently this COUNTER Plugin is \x{2}". (($callAgain )? "ON" : "OFF") ."\x{2}.\n";
- $mS .= "Command Examples:\n";
- $mS .= " \x{2}/COUNTER ON\x{2} <-- Enable this Counter plugin. It will start counting/repeat upto ". $upto . " and will show counting numbers after every ". ms2sec($timeInterval) ." seconds of main Interval time.\n";
- $mS .= " \x{2}/COUNTER OFF\x{2} <-- disables/stops counting/repeatations and stops showing counting numbers.\n";
- $mS .= " \x{2}/COUNTER upto 20\x{2} <-- will count/repeat upto 20 then stop. Current value is ". $upto .".\n";
- $mS .= " \x{2}/COUNTER variable 5\x{2} <-- Start randomly add or subtract variable time-legth in between +/- 5 seconds with main Interval time of ". ms2sec($timeInterval) ." seconds. Current value is ". ms2sec($variableTime) ." seconds. It can be upto +/- half of main Time Inteval ". ms2sec($timeInterval) ." seconds.\n";
- $mS .= " \x{2}/COUNTER interval 30\x{2} <-- start to show numbers on screen after each 30 seconds of main Time Interval. Current value is ". ms2sec($timeInterval) ." seconds.";
- prnt($mS);
- $mS = "";
- } else {
- # User specified a sub-command or option after main command:
- my($userDat1) = lc((not defined $_[0][1] )? "" : $_[0][1]);
- my($userDat2) = lc((not defined $_[0][2] )? "" : $_[0][2]);
- # my($userDat3) = lc((defined $_[0][3] )? $_[0][3] : "");
- if ($userDat1 eq 'off' || '0') {
- $callAgain = 0; # disable COUNTER.
- if ($hookPoint or $timerExist) {
- $timerExist = 0; # 0 = No. 1 = yes.
- prnt("COUNTER --> Stopped a previously running Timer. \(\$timerExist=". $timerExist .", \$hookPoint=". $hookPoint . "\) \(h_c\)");
- Xchat::unhook( $hookPoint );
- # $hookPoint = 0;
- };
- prnt("COUNTER --> is now OFF/disabled. Repeatation/Counting is stopped.");
- $counter = 0;
- } elsif ($userDat1 eq 'on' || '1') {
- if ($hookPoint or $timerExist) {
- $timerExist = 0;
- prnt("COUNTER --> Stopped a previously running Timer. \(\$timerExist=". $timerExist .", \$hookPoint=". $hookPoint . "\) \(h_c\)");
- Xchat::unhook( $hookPoint );
- # $hookPoint = 0;
- };
- $callAgain = 1; # enable COUNTER.
- $counter = 0;
- prnt("COUNTER --> is ON/enabled . Starting to count upto ". $upto .".");
- \&start_timer();
- return EAT_ALL;
- } elsif ($userDat1 eq "upto") {
- if (int(abs($userDat2))) {
- # grabbing only positive integer portion:
- $upto = int(abs($userDat2));
- prnt("COUNTER --> will count/repeat upto ". $upto ." if/when enabled.");
- } else {
- prnt("COUNTER --> This value \"". $_[0][2] ."\" is not a valid value for \"upto\" sub-command, please specify a different number. Current value is: ". $upto);
- };
- } elsif ($userDat1 eq "variable") {
- if (int(abs($userDat2))) {
- if (abs($userDat2) <= ms2sec($timeInterval/2)) {
- $variableTime = sec2ms(abs($userDat2));
- prnt("COUNTER --> plugin will start to randomly add or subtract variable time-length in betwenn +/- ". ms2sec($variableTime) ." seconds with main time interval of ". ms2sec($timeInterval) ." seconds, and then show numbers on screen from 1 to ". $upto);
- } else {
- prnt("COUNTER --> acceptable value can be maximum ". ms2sec($timeInterval/2) ." seconds (which is, Half of current main Time Interval). Current value is: ". ms2sec($variableTime));
- };
- } else {
- prnt("COUNTER --> This value \"". $_[0][2] ."\" is not a valid value for \"variable\" sub-command, please specify a different number. Current value is: ". ms2sec($variableTime));
- };
- } elsif ($userDat1 eq "interval") {
- if (int(abs($userDat2)) >= 2) {
- $timeInterval = sec2ms(abs($userDat2));
- prnt("COUNTER --> plugin will start to show counting numbers on screen after each main Time Interval of ". ms2sec($timeInterval) ." seconds, until counting reaches ". $upto);
- if ($variableTime > ($timeInterval/2)) {
- $variableTime = int($timeInterval/2);
- prnt("COUNTER --> previous Variable Time amount, was higher than half of current main Time Interval amount, so lowered Variable Time and set to ". ms2sec($variableTime) ." seconds.");
- };
- $randomTime = $timeInterval;
- } else {
- prnt("COUNTER --> This value \"". $_[0][2] ."\" is not a valid value for \"interval\" sub-command, please specify a different number. Number must be at-least: 2 (seconds), or more. Current value is: ". ms2sec($timeInterval));
- };
- } else {
- prnt("COUNTER --> This value \"". $_[0][2] ."\" is not a valid (sub-)command or a valid value for this Plugin. Sub-commands can be \"upto\", \"variable\", \"interval\".");
- };
- };
- return EAT_ALL;
- }, {data => "", help_text => "COUNTER: Plugin for repeating tasks. To see more info, type /COUNTER"});
- # END of hook_command( "COUNTER", ...)
- # {data => "", priority => Xchat::PRI_LOW, help_text => "COUNTER: Plugin for repeating tasks. To see more info, type /COUNTER"}
- # This portion developed by Bry8Star:
- # Simple small (and frequently used) functions:
- sub ms2sec {
- # convert milli-seconds to seconds
- # the "int" will remove fractional portion
- return int(( 0 + $_[0] )? ((0 + $_[0]) / 1000) : 0);
- }
- sub sec2ms {
- # convert seconds to milli-seconds
- # the "int" will remove fractional portion
- return int(( 0 + $_[0] )? ((0 + $_[0]) * 1000) : 0);
- }
- # This portion developed by Bry8Star:
- sub call_and_do_something {
- $counter=$counter+1;
- # BEGIN or place your other codes here, which you want to
- # execute repeatedly based on this COUNTER plugin.
- # ...
- prnt("". $counter ." \(\$hookPoint=". $hookPoint .", \$randomTime=". $randomTime .", \$timeInterval=". $timeInterval ."\) \(c_a_d_s\)");
- #if (defined $_[0]) {
- # prnt("\$\_[0]=". $_[0] ." \(c_a_d_s\)");
- #$_[0] = 1 + $_[0];
- #} else {
- # prnt("\$\_[0]= \(empty\)\(c_a_d_s\)");
- #};
- # END of your codes.
- #
- # Adding below codes as safe-guard for not allowing a timer
- # to keep on running. Even if there is a coding mistake,
- # a timer should stop by itself and to force it to stop.
- if ($callAgain == 0 or $counter >= $upto) {
- # If COUNTER is set to OFF, or when loop exceeds, then:
- prnt("COUNTER --> Counting finished. \(c_a_d_s\)");
- $callAgain = 0; # disable COUNTER.
- if ($hookPoint or $timerExist) {
- prnt("COUNTER --> Stopped a running Timer. \(\$timerExist=". $timerExist .", \$hookPoint=". $hookPoint . "\) \(c_a_d_s\)");
- Xchat::unhook( $hookPoint );
- # $hookPoint = 0;
- $timerExist = 0; # 0 = no. 1 = yes.
- };
- #if ($timerType == 2) {
- # return Xchat::REMOVE;
- #};
- # return Xchat::REMOVE;
- } else {
- \&start_timer();
- # # \&last_timer_executed_once();
- # return Xchat::KEEP;
- # return Xchat::REMOVE;
- };
- # return 0;
- # return Xchat::REMOVE;
- } # END of sub call_and_do_something
- # This portion developed by Bry8Star:
- sub last_timer_executed_once {
- if ($hookPoint or $timerExist) {
- prnt("COUNTER --> Stopped a previously running ". (($timerType == 2 )? "\(variable-time-length\)" : "\(fixed-timed\)") ." Timer. \(\$timerExist=". $timerExist .", \$hookPoint=". $hookPoint . "\) \(l_t_e_o\)");
- Xchat::unhook( $hookPoint );
- # $hookPoint = 0;
- $timerExist = 0;
- };
- &start_timer();
- return Xchat::EAT_NONE;
- } # END of sub last_timer_executed_once
- # This portion developed by Bry8Star:
- sub start_timer {
- # if $timerType or $_[0] is 1, then keep on loop with
- # fixed time length based timer.
- # if $timerType or $_[0] is 2, then loop with variable timer
- # once only, and then close previous timer and start another
- # timer with different time length again.
- if ($callAgain == 1 && $counter < $upto) {
- ###while ($callAgain == 1 && $counter < $upto) {
- if ($hookPoint or $timerExist) {
- prnt("COUNTER --> Stopped a previously running ". (($timerType == 2 )? "\(variable-time-length\)" : "\(fixed-timed\)") ." Timer. \(\$timerExist=". $timerExist .", \$hookPoint=". $hookPoint . "\) \(s_t\)");
- Xchat::unhook( $hookPoint );
- # $hookPoint = 0;
- $timerExist = 0;
- };
- if ($timerType == 2) {
- # Creating a random amount of time in between from 0
- # to $variableTime amount of time, then randomly either
- # adding or subtracting that from/with $timeInterval,
- # and then placing result inside $randomTime :
- variate_repeat_time();
- };
- prnt("COUNTER --> Creating a new ". (($timerType == 2 )? "\(variable-time-length\)" : "\(fixed-timed\)") ." Timer for ". int(ms2sec(($timerType == 2 )? $randomTime : $timeInterval)) ." seconds \(~". (($timerType == 2 )? $randomTime : $timeInterval) ." milli-seconds\). \(s_t\)");
- # my($testTimer)=1;
- # Make sure to use "\" and "&" immediately before the sub-function
- # name, used inside the hook_timer command, to create one timer only,
- # or else, it will create $upto number of multiple Timers at the
- # same time and XChat may crash/non-responsive.
- $timerExist += 1;
- $hookPoint = Xchat::hook_timer ( int(($timerType == 2 )? $randomTime : $timeInterval) , \&call_and_do_something ,{data => ""});
- ##}; # End of each looping
- };
- # Completely stop even the last timer, when followings are true:
- if ($callAgain == 0 or $counter >= $upto) {
- prnt("COUNTER --> Counting finished. \(s_t\)");
- $callAgain = 0;
- if ($hookPoint or $timerExist) {
- prnt("COUNTER --> Stopped a previously running Timer. \(\$timerExist=". $timerExist .", \$hookPoint=". $hookPoint . "\) \(s_t\)");
- Xchat::unhook( $hookPoint );
- # $hookPoint = 0;
- $timerExist = 0;
- };
- };
- # return 0;
- return Xchat::EAT_NONE;
- } # END of sub start_timer
- # This portion developed by Bry8Star:
- sub variate_repeat_time {
- # Creating a random time-duration between 0 to $variableTime
- # seconds, and then randomly either subtracting it or adding
- # it with user's given main time-duration $timeInterval, to
- # variate/fluctuate total timer duration.
- # XChat internally uses milli-seconds.
- my($maxRange) = int(1 + ((abs($variableTime) )? (abs($variableTime)) : 0 )); # using milli-seconds
- # Generate a random (1st-)number, from 0 to 10000 (milli-seconds):
- my($randomNumB2inZroAndVarTime) = int( rand( int($maxRange) ));
- # Create another (2nd-)random/any number starting from 0 to 7:
- my($randomPlusMinus) = int( rand( 8));
- # If 2nd random Num is from 0 to 3 only then (try to) multiply with -1:
- if ($randomPlusMinus <= 3) {
- # If 1st random Num is not 0, then multiply with -1:
- if ($randomNumB2inZroAndVarTime >= 1) {
- # Converting to a Negative number:
- $randomTime = -1 * $randomNumB2inZroAndVarTime;
- };
- } else {
- $randomTime = $randomNumB2inZroAndVarTime;
- };
- # Add +/- 0 to $variableTime amount of variation, with main $timeInterval:
- $randomTime = int( abs( $randomTime + abs($timeInterval) ));
- return Xchat::EAT_NONE;
- } # END of sub variate_repeat_time
Advertisement
Add Comment
Please, Sign In to add comment