#! /usr/bin/env python # # Chi-fou-mi bot. # # Bourdon Pierre from ircbot import SingleServerIRCBot from irclib import nm_to_n, irc_lower from threading import Thread import time class TimeoutThread(Thread): def __init__(self, time, callback, params): Thread.__init__(self) self.setDaemon(False) self.time = time self.callback = callback self.params = params def run(self): time.sleep(self.time) self.callback(*self.params) class ChiFouBot(SingleServerIRCBot): def __init__(self, channel, nickname, owner, server, port=6667): SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) self.channel = channel self.owner = owner self.ingame = False self.scores = {} self.stop = False def on_nicknameinuse(self, c, e): c.nick(c.get_nickname() + "_") def on_welcome(self, c, e): c.mode(c.get_nickname(), "+B") c.join(self.channel) def on_privmsg(self, c, e): if self.ingame == False: self.startgame(nm_to_n(e.source())) self.addplayer(nm_to_n(e.source()), e.arguments()[0]) def on_pubmsg(self, c, e): a = e.arguments()[0].split(":", 1) if len(a) > 1 and irc_lower(a[0]) == irc_lower(self.connection.get_nickname()): self.do_command(e, a[1].strip()) return def do_command(self, e, cmd): nick = nm_to_n(e.source()) c = self.connection isop = nick in self.channels.items()[0][1].opers() isop = isop or nick == self.owner if cmd == "die" and isop: self.die() elif cmd == "stop" and isop: self.stop = True def startgame(self, nick): self.players = {} c = self.connection TimeoutThread(30, ChiFouBot.endgame, [self]).start() c.privmsg(self.channel, "%s a joué ! À vous , vous avez 30 secondes !" % nick) self.ingame = True def score2str(self, score): strs = ["Pierre", "Papier", "Ciseaux"] return strs[score] def str2score(self, score): score = score.lower() strs = ["pierre", "papier", "ciseaux"] for i, s in enumerate(strs): if score == s: return i return -1 def gagnant(self, s1, s2): if s1 == s2: return 0 if (s1 + 2) % 3 == s2: return 1 else: return 2 def addplayer(self, nick, scorestr): c = self.connection sc = self.str2score(scorestr) if sc == -1: c.privmsg(nick, "Vous devez jouer soit pierre, soit papier, soit ciseaux !") return if not nick in self.players.keys() and len(self.players.keys()) != 0: c.privmsg(self.channel, "%s vient de jouer !" % nick) self.players[nick] = sc def endgame(self): self.ingame = False c = self.connection #c.privmsg(self.channel, "STOP ! C'est fini !") s = " | ".join(["%s: %s" % (pl, self.score2str(self.players[pl])) for pl in self.players.keys()]) c.privmsg(self.channel, s) l = [] if len(self.players) == 1: return for p in self.players.keys(): s1 = self.players[p] li = [] if not p in self.scores.keys(): self.scores[p] = 0 for p2 in self.players.keys(): if p == p2: continue s2 = self.players[p2] sc = self.gagnant(s1, s2) st = "" if sc == 0: st = "fait match nul" self.scores[p] += 0 elif sc == 1: st = "gagné" self.scores[p] += 1 else: st = "perdu" self.scores[p] += -1 li.append("a %s contre %s" % (st, p2)) s = p + " " + ', '.join(li) l.append(s) s = '. '.join(l) # c.privmsg(self.channel, s) s = "Scores: " l = [] for p in self.scores.keys(): l.append("%s: %d" % (p, self.scores[p])) s += ' | '.join(l) c.privmsg(self.channel, s) if self.stop: self.die() def main(): import sys if len(sys.argv) != 5: print "Usage: testbot " sys.exit(1) s = sys.argv[1].split(":", 1) server = s[0] if len(s) == 2: try: port = int(s[1]) except ValueError: print "Error: Erroneous port." sys.exit(1) else: port = 6667 channel = sys.argv[2] nickname = sys.argv[3] owner = sys.argv[4] bot = ChiFouBot(channel, nickname, owner, server, port) bot.start() if __name__ == "__main__": main()