Bot IRC - Documentation plugins
Plugin plug_motus
Jeu motus
Description
Le célèbre jeu motus.
Ce plugin nécessite une table mySql contenant les mots à proposer.
Cette table se compose d'un seul champ 'mot'.
Le mot proposé doit être connu dans la liste des mots en mémoire.
Version noir et blanc : les lettres bien placées sont en gras, les lettres mal placées en normal
les lettres ne faisant pas partie du mot sont en *.
Version colorisée : les lettres bien placées sont en rouge, les lettres mal placées sont en bleu,
les lettres ne faisant pas partie du mot sont en noir.
Utilisation.
Commandes utilisateurs :
- !motus :: Démarre le jeu avec un nouveau mot si pas de jeu en cours
- mot :: Tente un mot.

Source du plugin.
<?php
/**
* fichier : plug_motus.php :
* plugin du jeu motus
*
* @package Cspbot
* @since 2007/04/14
* @version 0.1
* @author Alain Nicolas (mcAllan)
* @copyright See licence.txt
*
*/
class plug_motus extends plugin {
/**
* IRCMain object
* @var object IRCMain
*/
private $main;
/**
* Longueur du mot
* @var int lm
*/
private $lm=5;
/**
* $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->motus();
}
/**
*
* main Function
*
*/
private function motus() {
if ( $this->main->type == 'PUBLIC' ) {
$replyto = 'public';
} else {
$replyto = $this->main->from;
}
if(preg_match('`!motus`i', $this->main->mess)) {
if(!isset($this->game[$replyto])){
$this->newword($replyto);
}else{
$msg = 'Partie en cours proposez un mot ('.$this->lm.' lettres)';
$this->answer($replyto, $msg, $from);
}
return TRUE;
}
if(preg_match('`!msol`i', $this->main->mess)) {
if($this->main->isBotOp($this->main->from) && isset($this->game[$replyto])){
$msg = '->'.$this->game[$replyto]['word'];
$this->main->sendPrivNotice($this->main->from, $msg);
return TRUE;
}elseif($this->main->isBotOp($this->main->from) && isset($this->game['public'])){
$msg = '->'.$this->game['public']['word'];
$this->main->sendPrivNotice($this->main->from, $msg);
return TRUE;
}else{
return FALSE;
}
}
if(preg_match('`([a-z]+)`i', $this->main->mess, $T)) {
$mot = trim($T[1]);
if(strlen($mot)==$this->lm && isset($this->game[$replyto])){
$this->tryword($this->main->from, $replyto, $mot);
return TRUE;
}else{
return FALSE;
}
}
}
/**
*
* Nouveau mot
*
*/
private function newword($rt) {
$query ="SELECT mot FROM ods WHERE length(mot)=$this->lm 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,
'try' => 0 );
$msg = 'Nouveau jeu : un mot de '.$this->lm.' lettres';
$this->answer($rt, $msg, $from);
}
/**
*
* Un mot proposée
*
*/
private function tryword($from, $rt, $mot) {
if (!isset($this->game[$rt])) return FALSE;
$query ="SELECT mot FROM ods WHERE mot = '$mot'";
$result = mysql_query( $query);
if (!$result) printf("ERROR: %s\n",mysql_error());
if(mysql_num_rows($result)==0){
$msg = '??? '.$mot;
$this->answer($rt, $msg, $from);
return false;
}
if($mot==$this->game[$rt]['word']){
$msg = $from . ' GAGNE : '.$mot;
$this->answer($rt, $msg, $from);
unset($this->game[$rt]);
return;
}
$target = $this->game[$rt]['word'];
$respons='';
$r=array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
$w=array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
for($i=0; $i<$this->lm; $i++){
if($target{$i}==$mot{$i}){
$r[$i]= 2;
$w[$i]= 2;
}else{
for($t=0; $t<$this->lm; $t++){
if($target{$i}==$mot{$t} && $r[$t]==0 && $w[$i]==0){
$r[$t]=1;
$w[$i]=1;
}
}
}
}
if($rt == 'public'){
# Réponse en noir et blanc
for($i=0; $i<$this->lm; $i++){
if($r[$i]==2){ $respons .= "\002".$mot{$i}."\002"; }
elseif($r[$i]==1){ $respons .= "".$mot{$i}.""; }
else{ $respons .= '*';}
}
}else{
# Réponse en couleur
for($i=0; $i<$this->lm; $i++){
if($r[$i]==2){ $respons .= "\002\0034".$mot{$i}."\003\002"; }
elseif($r[$i]==1){ $respons .= "\002\0032".$mot{$i}."\003\002"; }
else{ $respons .= $mot{$i};}
}
}
$this->answer($rt, $respons, $from);
}
/**
*
* 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_motus.php :
* plugin du jeu motus
*
* @package Cspbot
* @since 2007/04/14
* @version 0.1
* @author Alain Nicolas (mcAllan)
* @copyright See licence.txt
*
*/
class plug_motus extends plugin {
/**
* IRCMain object
* @var object IRCMain
*/
private $main;
/**
* Longueur du mot
* @var int lm
*/
private $lm=5;
/**
* $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->motus();
}
/**
*
* main Function
*
*/
private function motus() {
if ( $this->main->type == 'PUBLIC' ) {
$replyto = 'public';
} else {
$replyto = $this->main->from;
}
if(preg_match('`!motus`i', $this->main->mess)) {
if(!isset($this->game[$replyto])){
$this->newword($replyto);
}else{
$msg = 'Partie en cours proposez un mot ('.$this->lm.' lettres)';
$this->answer($replyto, $msg, $from);
}
return TRUE;
}
if(preg_match('`!msol`i', $this->main->mess)) {
if($this->main->isBotOp($this->main->from) && isset($this->game[$replyto])){
$msg = '->'.$this->game[$replyto]['word'];
$this->main->sendPrivNotice($this->main->from, $msg);
return TRUE;
}elseif($this->main->isBotOp($this->main->from) && isset($this->game['public'])){
$msg = '->'.$this->game['public']['word'];
$this->main->sendPrivNotice($this->main->from, $msg);
return TRUE;
}else{
return FALSE;
}
}
if(preg_match('`([a-z]+)`i', $this->main->mess, $T)) {
$mot = trim($T[1]);
if(strlen($mot)==$this->lm && isset($this->game[$replyto])){
$this->tryword($this->main->from, $replyto, $mot);
return TRUE;
}else{
return FALSE;
}
}
}
/**
*
* Nouveau mot
*
*/
private function newword($rt) {
$query ="SELECT mot FROM ods WHERE length(mot)=$this->lm 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,
'try' => 0 );
$msg = 'Nouveau jeu : un mot de '.$this->lm.' lettres';
$this->answer($rt, $msg, $from);
}
/**
*
* Un mot proposée
*
*/
private function tryword($from, $rt, $mot) {
if (!isset($this->game[$rt])) return FALSE;
$query ="SELECT mot FROM ods WHERE mot = '$mot'";
$result = mysql_query( $query);
if (!$result) printf("ERROR: %s\n",mysql_error());
if(mysql_num_rows($result)==0){
$msg = '??? '.$mot;
$this->answer($rt, $msg, $from);
return false;
}
if($mot==$this->game[$rt]['word']){
$msg = $from . ' GAGNE : '.$mot;
$this->answer($rt, $msg, $from);
unset($this->game[$rt]);
return;
}
$target = $this->game[$rt]['word'];
$respons='';
$r=array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
$w=array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
for($i=0; $i<$this->lm; $i++){
if($target{$i}==$mot{$i}){
$r[$i]= 2;
$w[$i]= 2;
}else{
for($t=0; $t<$this->lm; $t++){
if($target{$i}==$mot{$t} && $r[$t]==0 && $w[$i]==0){
$r[$t]=1;
$w[$i]=1;
}
}
}
}
if($rt == 'public'){
# Réponse en noir et blanc
for($i=0; $i<$this->lm; $i++){
if($r[$i]==2){ $respons .= "\002".$mot{$i}."\002"; }
elseif($r[$i]==1){ $respons .= "".$mot{$i}.""; }
else{ $respons .= '*';}
}
}else{
# Réponse en couleur
for($i=0; $i<$this->lm; $i++){
if($r[$i]==2){ $respons .= "\002\0034".$mot{$i}."\003\002"; }
elseif($r[$i]==1){ $respons .= "\002\0032".$mot{$i}."\003\002"; }
else{ $respons .= $mot{$i};}
}
}
$this->answer($rt, $respons, $from);
}
/**
*
* 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;
*/
?>
