Add some comments and the disconnect stuff. Remove old lib.
[irbot.git] / plugins / jet.php
blob41f68f9ecdd3d909d1ee12ebfabc690dd3086ad7
1 <?php
2 /**
3 * This file is part of IrBot, irc robot.
4 * Copyright (C) 2007 Bellière Ludovic
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 class jet implements plugin {
22 private $IRCConn,$formater;
23 private $message;
24 private $commands;
26 public function __construct($main) {
27 $this->IRCConn = bot::GetInstance();
28 $this->formater = text_format::GetInstance();
29 $this->commands = array(
30 'string2dice' => array( // !jet string2dice
31 'accepted_args' => 1,
32 'requier_args' => 1,
33 'help' => 'some help for this method1',
34 'type' => 'private',
36 'caract' => array (
37 'accepted_args' => 3,
38 'requier_args' => 1,
39 'help' => "Jet basé sur une caractéristique.\n".
40 "Argument premier : Valeur de la caractéristique\n".
41 "Argument second : Base du jet, par défaut une base 20 (peut être mis a 100 pour calculer le poid transportable par exemple)\n".
42 "Argument troisième : ".$this->formater->bold('[1|0]')." Spécifie si le jet doit retourner quelque chose d'aléatoire, sinon",
43 'type' => 'private'
48 public function commands_list() {
49 return $this->commands;
52 public function current_message ($message) {
53 /* gived by irc::parse_get
54 $message = array (
55 'type' => PRIVMSG|NOTICE,
56 'from' => Nick (reply to),
57 'to' => BotName / ChannelName,
58 'message' => The message
61 $this->message = $message;
64 public function string2dice ($str) {
65 $str = strtolower($str);
67 $numbers = array(0,1,2,3,4,5,6,7,8,9);
68 $modifiers = array('+','-','*','/');
70 if ($str == '1d1')
71 return 1;
73 $copy = $str;
74 $i = 0;
75 $len_copy = strlen($copy);
77 $number_of_dice = '';
78 $value_of_dice = '';
79 $modifier = '';
80 $modifier_nature = '';
81 $step = 'first';
82 while ($len_copy > $i) {
83 /**
84 * Si le caractère courant n'est pas un nombre, alors quelques vérifications.
85 * Nous vérifions la partie du dé que nous créons : première (avant le 'd'), deuxième (après le 'd')
86 * et si un modifieur existe ('+','-','*','/').
88 if ($copy[$i] == 'd' || in_array($copy[$i],$modifiers)) {
89 // Nombre de dés a lancer manquant
90 if ($i == 0) {
91 trigger_error('Wrong dice format : "'.$copy.'". Must be [Number]d[Value][Modifier][Number]',E_USER_WARNING);
92 return false;
95 if ($copy[$i] == 'd') {
96 $step = 'second';
97 } elseif (in_array($copy[$i],$modifiers)) {
98 // Valeur du dé manquant
99 if (empty($value_of_dice)) {
100 trigger_error('Wrong dice format : "'.$copy.'". Must be [Number]d[Value][Modifier][Number]',E_USER_WARNING);
101 return false;
103 $step = 'modifier';
104 $modifier_nature = $copy[$i];
105 } else {
106 trigger_error('Unknown caracter \'' . $copy[$i] . '\'.',E_USER_WARNING);
108 } else {
109 if ($step == 'first') {
110 $number_of_dice.= $copy[$i];
113 if ($step == 'second') {
114 $value_of_dice.= $copy[$i];
117 if ($step == 'modifier') {
118 $modifier.= $copy[$i];
122 $i++;
125 $de = self::dice($number_of_dice,$value_of_dice);
126 if ($modifier_nature == '') {
127 $r = $de;
128 } else {
129 $r = eval('return abs(ceil($de'.$modifier_nature.'$modifier));');
132 if (bot::$myBotName == $this->message['to']) {
133 $replyto = $this->message['from'];
134 } else {
135 $replyto = $this->message['to'];
138 $this->IRCConn->privmsg($replyto,$r);
142 * Jet basé sur une caractéristique.
144 * @param int Valeur de la caractéristique
145 * @param int Base du jet, par défaut une base 20 (peut être mis a 100 pour calculer le poid transportable par exemple)
146 * @param boolean Spécifie si le jet doit retourner quelque chose d'aléatoire, sinon
148 public function caract($caract,$base=20,$rand=true) {
149 $caract = intval($caract);
150 $base = intval($base);
152 $de = ($rand) ? self::dice(1,20) : 0 ;
153 $result = floor(($de+$caract) * $base / 20);
154 // (n / 20) * base => la base par défaut est 20
157 if ($rand)
158 $result = $result/2;
160 $this->IRCConn->privmsg($this->message['from'],$result);
161 return $result;
165 * Lance un dé
166 * @param int $number nombre de dé lancé.
167 * @param int $value valeur maximale du dé.
169 private function dice($number,$value) {
170 $number = intval($number);
171 $value = intval($value);
173 // Si la valeur du dé est a 0, on retourne tout de suite 0.
174 if ($value === 0)
175 return 0;
177 $toreturn=0;
178 while ($number>0) {
179 $toreturn+= rand(1,$value);
180 $number-=1;
182 return $toreturn;