]> jfr.im git - irc/evilnet/x3.git/blob - src/plugins/hangman/plugin.py
mod-python: remove unused function
[irc/evilnet/x3.git] / src / plugins / hangman / plugin.py
1 # anoy module
2
3 import svc
4 import re
5 import fileinput
6 import random
7
8 # HANGMAN !!!!
9 # /---
10 # | o
11 # | /|\
12 # | / \
13 # =======
14
15 class Game:
16 target = '' #channel or user's nick who we are playing with
17 word = ''
18 maskchar = '*'
19 man = 0
20 dictionary = "/usr/share/dict/words"
21
22 def __init__(self, irc, target, length=0):
23 self.irc = irc
24 self.target = target
25 length = int(length)
26 if(length > 3 and length < 100):
27 self.length = length
28 else:
29 self.length = random.randrange(5, 9)
30 # What constitutes a valid word?
31 self.valid = re.compile(r"^[a-zA-Z]+$")
32 self.guesses = {}
33
34 if(self.length < 3):
35 self.reply("You can only play with 3 or more letters")
36 self.man = 9999
37 return
38
39 if(self.newword(self.length)):
40 self.reply("HANGMAN is starting!")
41 self.printstatus()
42 else:
43 self.reply("Aborting game")
44 self.man = 9999
45 return
46
47 def validword(self):
48 if(len(self.word) == self.length and self.valid.search(self.word)):
49 return True
50 return False
51
52 def newword(self, length):
53 numlines = 0
54 for line in open(self.dictionary, "r"):
55 numlines += 1
56 tries = 0
57
58 if(numlines < 100):
59 raise Exception("Dictionary has too few words")
60
61 while((not self.validword())): #failsafe dont loop forever...
62 tries += 1
63 if(tries > 10):
64 self.reply("Error finding a %s letter word"%length)
65 return False
66 #raise(Exception("DictError", "Unable to find %s letter word"%length))
67 i = 0
68 randline = random.randrange(1, numlines-1)
69 for line in open(self.dictionary, 'r'):
70 if(i >= randline):
71 self.word = line.rstrip()
72 if(not self.validword() and i < randline + 50):
73 continue
74 else:
75 break # give up on this block and try again
76 i += 1
77 if(len(self.word) < 3):
78 self.reply("Unable to find a word in the dictionary!")
79 return False
80
81 return True
82
83
84 def maskedword(self):
85 mask = []
86 for i in self.word:
87 if(i in self.guesses or not i.isalpha()):
88 mask.append(i)
89 else:
90 mask.append(self.maskchar)
91 return(''.join(mask))
92
93 def manpart(self, part, num):
94 if(self.man >= num):
95 return part
96 else:
97 return " "
98
99 def printstatus(self):
100 print("DEBUG: the word is '%s'"%self.word)
101 self.reply(" /---%s "%( self.manpart(",", 1 )) )
102 self.reply(" | %s Make "%( self.manpart("o",2)) )
103 self.reply(" | %s%s%s your "%( self.manpart("/",4), self.manpart("|",3), self.manpart("\\", 5) ) )
104 self.reply(" | %s %s guess! "%( self.manpart("/",6), self.manpart("\\",7) ))
105 self.reply(" ====")
106 self.reply(self.maskedword())
107
108 if(self.won() == True):
109 self.reply("YOU WON! FOR NOW!!")
110 elif(self.won() == False):
111 self.reply("Your DEAD! DEAAAAAAAD!")
112
113
114 def won(self):
115 if(self.man >= 7):
116 return False
117
118 for i in self.word:
119 if(not i in self.guesses.keys()):
120 return None
121 return True
122
123 def guess(self, irc, letter):
124 self.irc = irc
125
126 if(self.won() != None):
127 self.reply("This game is over. Start another!")
128 return
129 if(len(letter) > 1):
130 self.reply("Guess a single letter only, please.")
131 return
132 if(not letter.isalpha()):
133 self.reply("Letters only. Punctuation will be filled in for you.")
134 return
135 if(letter in self.guesses):
136 self.reply("Pay attention! %s has already been guessed! I'm hanging you anyway!"%letter)
137 self.man += 1
138 self.printstatus()
139 return
140
141 self.guesses[letter] = True
142
143 if(self.won() != None):
144 pass
145 elif(self.word.find(letter) >= 0):
146 self.reply("YOU GOT ONE! But I'll hang you yet!!")
147 else:
148 self.reply("NO! MuaHaHaHaHa!")
149 self.man += 1
150
151 self.printstatus()
152
153 def reply(self, msg):
154 self.irc.send_target_privmsg(self.irc.service, self.target, msg)
155
156 class Hangman:
157 config = {}
158
159 def __init__(self, handler, irc):
160 self.handler = handler
161 self.name = "hangman"
162
163 handler.addcommand(self.name, "start", self.start)
164 handler.addcommand(self.name, "end", self.end)
165 handler.addcommand(self.name, "guess", self.guess)
166
167 self.games = {} # list of game objects
168
169 def target(self, irc):
170 if(len(irc.target)):
171 return irc.target
172 else:
173 return irc.caller
174
175 def start(self, irc, arg):
176 playwith = self.target(irc)
177 if(playwith in self.games.keys() and self.games[playwith].won() == None):
178 irc.reply("There is a game is in progress here, End it before you start another.")
179 return
180
181 if(arg.isdigit()):
182 self.games[playwith] = Game(irc, playwith, arg)
183 else:
184 self.games[playwith] = Game(irc, playwith)
185
186 def end(self, irc, unused):
187 playwith = self.target(irc)
188 if(self.target(irc) in self.games.keys()):
189 self.games[playwith].reply("Game ended by %s"%irc.caller)
190 del(self.games[playwith])
191 else:
192 irc.reply("No game here to end")
193
194 def guess(self, irc, arg):
195 playwith = self.target(irc)
196 if(self.target(irc) in self.games.keys()):
197 self.games[playwith].guess(irc, arg)
198 else:
199 irc.reply("No game here in progress. Start one!")
200
201 def dance(self, irc, args):
202 nick = irc.caller
203 user = svc.get_user(nick)
204
205 reply = "Ok,"
206 if(user and "account" in user):
207 reply += " Mr. %s"%user["account"]
208
209 reply += " we can dance"
210 if(len(args)):
211 reply += " "
212 reply += args
213 reply += "."
214
215 irc.reply(reply)
216
217 def nickof(self, irc, bot):
218 info = svc.get_info()
219
220 if(bot and bot in info.keys()):
221 irc.reply("%s has nick %s"%(bot, info[bot]))
222 else:
223 irc.reply("I dunno. Try %s"%str(info.keys()))
224
225 Class = Hangman