Bry8Star

xchat timer counter repeat script

Mar 11th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.73 KB | None | 0 0
  1. #
  2. # COUNTER
  3. # Author: Bry8Star. <bry 8 s tar a.t ya hoo d.o.t c om>
  4. #
  5. # Copyright (C) 2013 Bright Star, Bry8Star.
  6. #
  7. # You Must obtain explicit permission from Author, before using this code.
  8. #
  9. # This plugin can Repeat a (or set of) task either with fixed-length
  10. # time, or with variable amount of time. This Plugin can be turned
  11. # off or on, max repeatation number can be set, fixed or variable amount
  12. # of time duration can also be set, etc.
  13. #
  14. #
  15. #
  16. # Many "prnt" command lines added for debug purpose, that is, to show
  17. # what is going on, once working fine or expected, then disable those
  18. # lines by placing a "#" symbol in front of "prnt" command.
  19.  
  20.  
  21. use strict;
  22. use warnings;
  23. use Xchat qw(:all);
  24. Xchat::register('COUNTER','1.0.3','COUNTER allows to repeat tasks. Such as showing counting numbers. More info, type /COUNTER');
  25.  
  26.  
  27. # Controlling Global Variables:
  28. my($callAgain)=0; # indicator for Counter: Enable(1) or Disable(0) ?
  29. my($counter)=0; # it stores counting/loop number.
  30. my($upto)=5; # number of repeatation/loop amount, is 5.
  31. my($timerExist)=0; # Whan a hook_timer is created then this is set to 1.
  32. my($timerType)=2; # 1 = fixed-length timer, 2 = variable-length timer.
  33. my($hookPoint)=0; # used for storing timer's ref-/mem-location-point.
  34. my($timeInterval)=10000; # user given main time interval. 10000ms=10sec.
  35. my($variableTime)=5000; # variable time amount. Max Half of $timeInterval.
  36. my($randomTime)=10000; # random time = (+/-~$variableTime + $timeInterval).
  37.  
  38. prnt("\x{2}COUNTER\x{2} plugin for XChat, started. \(A plugin for repeating tasks\). To see more info, you may type: /COUNTER");
  39.  
  40.  
  41. # This portion developed by Bry8Star:
  42. Xchat::hook_command( "COUNTER", sub{
  43. if (not defined $_[0][1]) {
  44. # User did not specify any sub-command or option after main command:
  45. my($mS)="";
  46. $mS .= "\x{2}COUNTER\x{2} --> Currently this COUNTER Plugin is \x{2}". (($callAgain )? "ON" : "OFF") ."\x{2}.\n";
  47. $mS .= "Command Examples:\n";
  48. $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";
  49. $mS .= " \x{2}/COUNTER OFF\x{2} <-- disables/stops counting/repeatations and stops showing counting numbers.\n";
  50. $mS .= " \x{2}/COUNTER upto 20\x{2} <-- will count/repeat upto 20 then stop. Current value is ". $upto .".\n";
  51. $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";
  52. $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.";
  53. prnt($mS);
  54. $mS = "";
  55. } else {
  56. # User specified a sub-command or option after main command:
  57. my($userDat1) = lc((not defined $_[0][1] )? "" : $_[0][1]);
  58. my($userDat2) = lc((not defined $_[0][2] )? "" : $_[0][2]);
  59. # my($userDat3) = lc((defined $_[0][3] )? $_[0][3] : "");
  60. if ($userDat1 eq 'off' || '0') {
  61. $callAgain = 0; # disable COUNTER.
  62. if ($hookPoint or $timerExist) {
  63. $timerExist = 0; # 0 = No. 1 = yes.
  64. prnt("COUNTER --> Stopped a previously running Timer. \(\$timerExist=". $timerExist .", \$hookPoint=". $hookPoint . "\) \(h_c\)");
  65. Xchat::unhook( $hookPoint );
  66. # $hookPoint = 0;
  67. };
  68. prnt("COUNTER --> is now OFF/disabled. Repeatation/Counting is stopped.");
  69. $counter = 0;
  70. } elsif ($userDat1 eq 'on' || '1') {
  71. if ($hookPoint or $timerExist) {
  72. $timerExist = 0;
  73. prnt("COUNTER --> Stopped a previously running Timer. \(\$timerExist=". $timerExist .", \$hookPoint=". $hookPoint . "\) \(h_c\)");
  74. Xchat::unhook( $hookPoint );
  75. # $hookPoint = 0;
  76. };
  77. $callAgain = 1; # enable COUNTER.
  78. $counter = 0;
  79. prnt("COUNTER --> is ON/enabled . Starting to count upto ". $upto .".");
  80. \&start_timer();
  81. return EAT_ALL;
  82. } elsif ($userDat1 eq "upto") {
  83. if (int(abs($userDat2))) {
  84. # grabbing only positive integer portion:
  85. $upto = int(abs($userDat2));
  86. prnt("COUNTER --> will count/repeat upto ". $upto ." if/when enabled.");
  87. } else {
  88. prnt("COUNTER --> This value \"". $_[0][2] ."\" is not a valid value for \"upto\" sub-command, please specify a different number. Current value is: ". $upto);
  89. };
  90. } elsif ($userDat1 eq "variable") {
  91. if (int(abs($userDat2))) {
  92. if (abs($userDat2) <= ms2sec($timeInterval/2)) {
  93. $variableTime = sec2ms(abs($userDat2));
  94. 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);
  95. } else {
  96. prnt("COUNTER --> acceptable value can be maximum ". ms2sec($timeInterval/2) ." seconds (which is, Half of current main Time Interval). Current value is: ". ms2sec($variableTime));
  97. };
  98. } else {
  99. 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));
  100. };
  101. } elsif ($userDat1 eq "interval") {
  102. if (int(abs($userDat2)) >= 2) {
  103. $timeInterval = sec2ms(abs($userDat2));
  104. prnt("COUNTER --> plugin will start to show counting numbers on screen after each main Time Interval of ". ms2sec($timeInterval) ." seconds, until counting reaches ". $upto);
  105. if ($variableTime > ($timeInterval/2)) {
  106. $variableTime = int($timeInterval/2);
  107. 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.");
  108. };
  109. $randomTime = $timeInterval;
  110. } else {
  111. 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));
  112. };
  113. } else {
  114. 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\".");
  115. };
  116. };
  117. return EAT_ALL;
  118. }, {data => "", help_text => "COUNTER: Plugin for repeating tasks. To see more info, type /COUNTER"});
  119. # END of hook_command( "COUNTER", ...)
  120.  
  121.  
  122.  
  123. # {data => "", priority => Xchat::PRI_LOW, help_text => "COUNTER: Plugin for repeating tasks. To see more info, type /COUNTER"}
  124.  
  125.  
  126.  
  127. # This portion developed by Bry8Star:
  128. # Simple small (and frequently used) functions:
  129. sub ms2sec {
  130. # convert milli-seconds to seconds
  131. # the "int" will remove fractional portion
  132. return int(( 0 + $_[0] )? ((0 + $_[0]) / 1000) : 0);
  133. }
  134.  
  135.  
  136. sub sec2ms {
  137. # convert seconds to milli-seconds
  138. # the "int" will remove fractional portion
  139. return int(( 0 + $_[0] )? ((0 + $_[0]) * 1000) : 0);
  140. }
  141.  
  142.  
  143.  
  144.  
  145. # This portion developed by Bry8Star:
  146. sub call_and_do_something {
  147. $counter=$counter+1;
  148. # BEGIN or place your other codes here, which you want to
  149. # execute repeatedly based on this COUNTER plugin.
  150. # ...
  151. prnt("". $counter ." \(\$hookPoint=". $hookPoint .", \$randomTime=". $randomTime .", \$timeInterval=". $timeInterval ."\) \(c_a_d_s\)");
  152. #if (defined $_[0]) {
  153. # prnt("\$\_[0]=". $_[0] ." \(c_a_d_s\)");
  154. #$_[0] = 1 + $_[0];
  155. #} else {
  156. # prnt("\$\_[0]= \(empty\)\(c_a_d_s\)");
  157. #};
  158. # END of your codes.
  159. #
  160. # Adding below codes as safe-guard for not allowing a timer
  161. # to keep on running. Even if there is a coding mistake,
  162. # a timer should stop by itself and to force it to stop.
  163. if ($callAgain == 0 or $counter >= $upto) {
  164. # If COUNTER is set to OFF, or when loop exceeds, then:
  165. prnt("COUNTER --> Counting finished. \(c_a_d_s\)");
  166. $callAgain = 0; # disable COUNTER.
  167. if ($hookPoint or $timerExist) {
  168. prnt("COUNTER --> Stopped a running Timer. \(\$timerExist=". $timerExist .", \$hookPoint=". $hookPoint . "\) \(c_a_d_s\)");
  169. Xchat::unhook( $hookPoint );
  170. # $hookPoint = 0;
  171. $timerExist = 0; # 0 = no. 1 = yes.
  172. };
  173. #if ($timerType == 2) {
  174. # return Xchat::REMOVE;
  175. #};
  176. # return Xchat::REMOVE;
  177. } else {
  178. \&start_timer();
  179. # # \&last_timer_executed_once();
  180. # return Xchat::KEEP;
  181. # return Xchat::REMOVE;
  182. };
  183. # return 0;
  184. # return Xchat::REMOVE;
  185. } # END of sub call_and_do_something
  186.  
  187.  
  188.  
  189.  
  190. # This portion developed by Bry8Star:
  191. sub last_timer_executed_once {
  192. if ($hookPoint or $timerExist) {
  193. prnt("COUNTER --> Stopped a previously running ". (($timerType == 2 )? "\(variable-time-length\)" : "\(fixed-timed\)") ." Timer. \(\$timerExist=". $timerExist .", \$hookPoint=". $hookPoint . "\) \(l_t_e_o\)");
  194. Xchat::unhook( $hookPoint );
  195. # $hookPoint = 0;
  196. $timerExist = 0;
  197. };
  198. &start_timer();
  199. return Xchat::EAT_NONE;
  200. } # END of sub last_timer_executed_once
  201.  
  202.  
  203.  
  204.  
  205. # This portion developed by Bry8Star:
  206. sub start_timer {
  207. # if $timerType or $_[0] is 1, then keep on loop with
  208. # fixed time length based timer.
  209. # if $timerType or $_[0] is 2, then loop with variable timer
  210. # once only, and then close previous timer and start another
  211. # timer with different time length again.
  212. if ($callAgain == 1 && $counter < $upto) {
  213. ###while ($callAgain == 1 && $counter < $upto) {
  214. if ($hookPoint or $timerExist) {
  215. prnt("COUNTER --> Stopped a previously running ". (($timerType == 2 )? "\(variable-time-length\)" : "\(fixed-timed\)") ." Timer. \(\$timerExist=". $timerExist .", \$hookPoint=". $hookPoint . "\) \(s_t\)");
  216. Xchat::unhook( $hookPoint );
  217. # $hookPoint = 0;
  218. $timerExist = 0;
  219. };
  220. if ($timerType == 2) {
  221. # Creating a random amount of time in between from 0
  222. # to $variableTime amount of time, then randomly either
  223. # adding or subtracting that from/with $timeInterval,
  224. # and then placing result inside $randomTime :
  225. variate_repeat_time();
  226. };
  227. 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\)");
  228. # my($testTimer)=1;
  229. # Make sure to use "\" and "&" immediately before the sub-function
  230. # name, used inside the hook_timer command, to create one timer only,
  231. # or else, it will create $upto number of multiple Timers at the
  232. # same time and XChat may crash/non-responsive.
  233. $timerExist += 1;
  234. $hookPoint = Xchat::hook_timer ( int(($timerType == 2 )? $randomTime : $timeInterval) , \&call_and_do_something ,{data => ""});
  235. ##}; # End of each looping
  236. };
  237.  
  238. # Completely stop even the last timer, when followings are true:
  239. if ($callAgain == 0 or $counter >= $upto) {
  240. prnt("COUNTER --> Counting finished. \(s_t\)");
  241. $callAgain = 0;
  242. if ($hookPoint or $timerExist) {
  243. prnt("COUNTER --> Stopped a previously running Timer. \(\$timerExist=". $timerExist .", \$hookPoint=". $hookPoint . "\) \(s_t\)");
  244. Xchat::unhook( $hookPoint );
  245. # $hookPoint = 0;
  246. $timerExist = 0;
  247. };
  248. };
  249. # return 0;
  250. return Xchat::EAT_NONE;
  251. } # END of sub start_timer
  252.  
  253.  
  254.  
  255. # This portion developed by Bry8Star:
  256. sub variate_repeat_time {
  257. # Creating a random time-duration between 0 to $variableTime
  258. # seconds, and then randomly either subtracting it or adding
  259. # it with user's given main time-duration $timeInterval, to
  260. # variate/fluctuate total timer duration.
  261. # XChat internally uses milli-seconds.
  262. my($maxRange) = int(1 + ((abs($variableTime) )? (abs($variableTime)) : 0 )); # using milli-seconds
  263. # Generate a random (1st-)number, from 0 to 10000 (milli-seconds):
  264. my($randomNumB2inZroAndVarTime) = int( rand( int($maxRange) ));
  265. # Create another (2nd-)random/any number starting from 0 to 7:
  266. my($randomPlusMinus) = int( rand( 8));
  267. # If 2nd random Num is from 0 to 3 only then (try to) multiply with -1:
  268. if ($randomPlusMinus <= 3) {
  269. # If 1st random Num is not 0, then multiply with -1:
  270. if ($randomNumB2inZroAndVarTime >= 1) {
  271. # Converting to a Negative number:
  272. $randomTime = -1 * $randomNumB2inZroAndVarTime;
  273. };
  274. } else {
  275. $randomTime = $randomNumB2inZroAndVarTime;
  276. };
  277. # Add +/- 0 to $variableTime amount of variation, with main $timeInterval:
  278. $randomTime = int( abs( $randomTime + abs($timeInterval) ));
  279. return Xchat::EAT_NONE;
  280. } # END of sub variate_repeat_time
Advertisement
Add Comment
Please, Sign In to add comment