77de7d9ad408063df4e1938d73816227480cb129
[zzz-dywypi.git] / plugins / Pokedex / plugin.py
1 # encoding: utf8
2 ###
3 # Copyright (c) 2010, Alex Munroe
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # * Redistributions of source code must retain the above copyright notice,
10 # this list of conditions, and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright notice,
12 # this list of conditions, and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of the author of this software nor the name of
15 # contributors to this software may be used to endorse or promote products
16 # derived from this software without specific prior written consent.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29
30 ###
31
32 import supybot.utils as utils
33 from supybot.commands import *
34 import supybot.plugins as plugins
35 import supybot.ircutils as ircutils
36 import supybot.callbacks as callbacks
37
38 import pokedex.db
39 import pokedex.db.tables as tables
40 import pokedex.lookup
41
42 class Pokedex(callbacks.Plugin):
43 """Add the help for "@plugin help Pokedex" here
44 This should describe *how* to use this plugin."""
45 def __init__(self, irc):
46 self.__parent = super(Pokedex, self)
47 self.__parent.__init__(irc)
48 self.db = pokedex.db.connect(self.registryValue('databaseURL'))
49
50 def pokedex(self, irc, msg, args, thing):
51 """<thing...>
52
53 Looks up <thing> in the veekun Pokédex."""
54
55 # Similar logic to the site, here.
56 results = pokedex.lookup.lookup(thing, session=self.db)
57
58 # Nothing found
59 if len(results) == 0:
60 irc.reply("I don't know what that is.")
61 return
62
63 # Multiple matches; propose them all
64 if len(results) > 1:
65 if results[0].exact:
66 reply = "Are you looking for"
67 else:
68 reply = "Did you mean"
69
70 # For exact name matches with multiple results, use type prefixes
71 # (item:Metronome). For anything else, omit them
72 use_prefixes = (results[0].exact
73 and '*' not in thing
74 and '?' not in thing)
75
76 result_strings = []
77 for result in results:
78 result_string = result.name
79 if use_prefixes:
80 # Table classes know their singular names
81 prefix = result.object.__singlename__
82 result_string = prefix + ':' + result_string
83 result_strings.append(result_string)
84
85 irc.reply("{0}: {1}?".format(reply, '; '.join(result_strings)))
86 return
87
88 # If we got here, there's an exact match; hurrah!
89 result = results[0]
90 if isinstance(result.object, tables.Pokemon):
91 irc.reply("""{name}, {type}-type Pokémon.""".format(
92 name=result.object.name,
93 type='/'.join(_.name for _ in result.object.types),
94 )
95 )
96
97 elif isinstance(result.object, tables.Move):
98 irc.reply("""{name}, {type}-type move.""".format(
99 name=result.object.name,
100 type=result.object.type.name,
101 )
102 )
103
104 elif isinstance(result.object, tables.Type):
105 irc.reply("""{name}, a type.""".format(
106 name=result.object.name,
107 )
108 )
109
110 elif isinstance(result.object, tables.Item):
111 irc.reply("""{name}, an item.""".format(
112 name=result.object.name,
113 )
114 )
115
116 elif isinstance(result.object, tables.Ability):
117 irc.reply("""{name}, an ability.""".format(
118 name=result.object.name,
119 )
120 )
121
122 else:
123 # This can only happen if lookup.py is upgraded and we are not
124 irc.reply("Uhh.. I found that, but I don't know what it is. :(")
125
126 pokedex = wrap(pokedex, [rest('something')])
127
128
129 Class = Pokedex
130
131
132 # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: