Bot IRC - Documentation plugins
Plugin plug_pendu
Jeu du pendu
Description
Le célèbre jeu du pendu.
Ce plugin nécessite une table mySql contenant les mots à proposer.
Cette table se compose d'un seul champ 'mot'.
Utilisation.
Commandes utilisateurs :
- !pendu newword :: Démarre le jeu avec un nouveau mot
- !pendu try <lettre> :: Tente une lettre

Source du plugin.
<?php
/**
* fichier : plug_pendu.php :
* plugin du jeu du pendu
*
* @package Cspbot
* @since 2007/04/13
* @version 0.1
* @author Fabrice Lezoray - Tetsuo - Alain Nicolas (mcAllan)
* @copyright See licence.txt
*
*/
class plug_Pendu extends plugin {
/**
* IRCMain object
* @var object IRCMain
*/
private $main;
/**
* $game array
* @var array game
*/
private $game = array();
/**
* Constructor
*
* @param IRCMain main
*
*/
public function __construct($main) {
$this->main = $main;
$link = @mysql_connect( MYSQL_HOST,
MYSQL_USER,
MYSQL_PWD );
if (!$link) {
printf("poconnecté: %s\n",mysql_error());
exit;
}
mysql_select_db(MYSQL_BASE);
}
/**
*
* Start run plugin
*
*/
public function start() {
$this->pendu();
}
/**
*
* main Function
*
*/
private function pendu() {
if ( $this->main->type == 'PUBLIC' ) {
$replyto = 'public';
} else {
$replyto = $this->main->from;
}
if(preg_match('`!pendu newword`i', $this->main->mess)) {
$this->newword($replyto);
return TRUE;
}
if(preg_match('`!pendu try ([a-z]+)`i', $this->main->mess, $T)) {
$letter = $T[1];
$this->tryletter($this->main->from, $replyto, $letter);
return TRUE;
}
if(preg_match('`!pendu display`i', $this->main->mess)) {
$this->display($replyto);
return TRUE;
}
}
/**
*
* Nouveau mot
*
*/
private function newword($rt) {
$query ="SELECT mot FROM ods WHERE length(mot)<10 AND length(mot)>6 ORDER BY RAND() LIMIT 1";
$result = mysql_query( $query);
if (!$result) printf("ERROR: %s\n",mysql_error());
$row = mysql_fetch_assoc($result);
$newword = $row['mot'];
$fl = $newword{0};
$ll = $newword{strlen($newword)-1};
$mask = preg_replace('`[^'.$fl.$ll.']`i', '_', $newword);
$this->game[$rt] = array ( 'word' => $newword,
'mask' => $mask,
'letters' =>$fl.$ll,
'try' => 0 );
$msg = sprintf('nouveau jeu: %s',$this->game[$rt]['mask'] );
$this->answer($rt, $msg, $from);
}
/**
*
* Une lettre proposée
*
*/
private function tryletter($from, $rt, $letter) {
if (!isset($this->game[$rt])) return FALSE;
if (strlen($letter)>1) {
if (0==strcasecmp( $this->game[$rt]['word'],$letter)) {
$msg = sprintf('GAGNE %s : erreur(s) %s',$this->game[$rt]['word'],$this->game[$rt]['try']);
unset($this->game[$rt]);
} else {
$this->game[$rt]['try']++;
$msg = sprintf('ERREUR %s : erreur(s) %s',$this->game[$rt]['mask'],$this->game[$rt]['try']);
}
} else {
if (strpos($this->game[$rt]['letters'],$letter) !== FALSE ) return FALSE;
$this->game[$rt]['letters'] .= $letter;
$oldmask = $this->game[$rt]['mask'];
$this->game[$rt]['mask'] = preg_replace('`[^'.$this->game[$rt]['letters'].']`i','_', $this->game[$rt]['word']);
if ( $this->game[$rt]['mask'] == $oldmask ) $this->game[$rt]['try']++;
if ($this->game[$rt]['mask']===$this->game[$rt]['word']) {
$msg = sprintf('GAGNE %s : erreur(s) %s',$this->game[$rt]['mask'],$this->game[$rt]['try']);
unset($this->game[$rt]);
} else {
$msg = sprintf(' %s : erreur(s) %s',$this->game[$rt]['mask'],$this->game[$rt]['try']);
}
}
$this->answer($rt, $msg, $from);
}
/**
*
* Un mot proposé
*
*/
private function display($rt) {
if(!isset($this->game[$rt])) {
return FALSE;
}
if ($this->game[$rt]['mask']===$this->game[$rt]['word']) {
$msg = sprintf('FINI %s : essai %s',$this->game[$rt]['mask'],$this->game[$rt]['try']);
} else {
$msg = sprintf(' %s : erreur(1) %s',$this->game[$rt]['mask'],$this->game[$rt]['try']);
}
$this->answer($rt, $msg);
}
/**
*
* Affichage réponse
*
*/
private function answer($rt, $msg, $to=''){
if($rt == 'public'){
$this->main->sendMsg($msg);
}else{
$this->main->sendPrivMsg($rt, $msg);
}
}
}
/*
--
-- Structure de la table `ods`
--
CREATE TABLE `ods` (
`mot` varchar(15) NOT NULL default ''
) TYPE=MyISAM;
*/
?>
/**
* fichier : plug_pendu.php :
* plugin du jeu du pendu
*
* @package Cspbot
* @since 2007/04/13
* @version 0.1
* @author Fabrice Lezoray - Tetsuo - Alain Nicolas (mcAllan)
* @copyright See licence.txt
*
*/
class plug_Pendu extends plugin {
/**
* IRCMain object
* @var object IRCMain
*/
private $main;
/**
* $game array
* @var array game
*/
private $game = array();
/**
* Constructor
*
* @param IRCMain main
*
*/
public function __construct($main) {
$this->main = $main;
$link = @mysql_connect( MYSQL_HOST,
MYSQL_USER,
MYSQL_PWD );
if (!$link) {
printf("poconnecté: %s\n",mysql_error());
exit;
}
mysql_select_db(MYSQL_BASE);
}
/**
*
* Start run plugin
*
*/
public function start() {
$this->pendu();
}
/**
*
* main Function
*
*/
private function pendu() {
if ( $this->main->type == 'PUBLIC' ) {
$replyto = 'public';
} else {
$replyto = $this->main->from;
}
if(preg_match('`!pendu newword`i', $this->main->mess)) {
$this->newword($replyto);
return TRUE;
}
if(preg_match('`!pendu try ([a-z]+)`i', $this->main->mess, $T)) {
$letter = $T[1];
$this->tryletter($this->main->from, $replyto, $letter);
return TRUE;
}
if(preg_match('`!pendu display`i', $this->main->mess)) {
$this->display($replyto);
return TRUE;
}
}
/**
*
* Nouveau mot
*
*/
private function newword($rt) {
$query ="SELECT mot FROM ods WHERE length(mot)<10 AND length(mot)>6 ORDER BY RAND() LIMIT 1";
$result = mysql_query( $query);
if (!$result) printf("ERROR: %s\n",mysql_error());
$row = mysql_fetch_assoc($result);
$newword = $row['mot'];
$fl = $newword{0};
$ll = $newword{strlen($newword)-1};
$mask = preg_replace('`[^'.$fl.$ll.']`i', '_', $newword);
$this->game[$rt] = array ( 'word' => $newword,
'mask' => $mask,
'letters' =>$fl.$ll,
'try' => 0 );
$msg = sprintf('nouveau jeu: %s',$this->game[$rt]['mask'] );
$this->answer($rt, $msg, $from);
}
/**
*
* Une lettre proposée
*
*/
private function tryletter($from, $rt, $letter) {
if (!isset($this->game[$rt])) return FALSE;
if (strlen($letter)>1) {
if (0==strcasecmp( $this->game[$rt]['word'],$letter)) {
$msg = sprintf('GAGNE %s : erreur(s) %s',$this->game[$rt]['word'],$this->game[$rt]['try']);
unset($this->game[$rt]);
} else {
$this->game[$rt]['try']++;
$msg = sprintf('ERREUR %s : erreur(s) %s',$this->game[$rt]['mask'],$this->game[$rt]['try']);
}
} else {
if (strpos($this->game[$rt]['letters'],$letter) !== FALSE ) return FALSE;
$this->game[$rt]['letters'] .= $letter;
$oldmask = $this->game[$rt]['mask'];
$this->game[$rt]['mask'] = preg_replace('`[^'.$this->game[$rt]['letters'].']`i','_', $this->game[$rt]['word']);
if ( $this->game[$rt]['mask'] == $oldmask ) $this->game[$rt]['try']++;
if ($this->game[$rt]['mask']===$this->game[$rt]['word']) {
$msg = sprintf('GAGNE %s : erreur(s) %s',$this->game[$rt]['mask'],$this->game[$rt]['try']);
unset($this->game[$rt]);
} else {
$msg = sprintf(' %s : erreur(s) %s',$this->game[$rt]['mask'],$this->game[$rt]['try']);
}
}
$this->answer($rt, $msg, $from);
}
/**
*
* Un mot proposé
*
*/
private function display($rt) {
if(!isset($this->game[$rt])) {
return FALSE;
}
if ($this->game[$rt]['mask']===$this->game[$rt]['word']) {
$msg = sprintf('FINI %s : essai %s',$this->game[$rt]['mask'],$this->game[$rt]['try']);
} else {
$msg = sprintf(' %s : erreur(1) %s',$this->game[$rt]['mask'],$this->game[$rt]['try']);
}
$this->answer($rt, $msg);
}
/**
*
* Affichage réponse
*
*/
private function answer($rt, $msg, $to=''){
if($rt == 'public'){
$this->main->sendMsg($msg);
}else{
$this->main->sendPrivMsg($rt, $msg);
}
}
}
/*
--
-- Structure de la table `ods`
--
CREATE TABLE `ods` (
`mot` varchar(15) NOT NULL default ''
) TYPE=MyISAM;
*/
?>
