def reload(prev): if hasattr(prev, 'challenges') and isinstance(prev.challenges, dict): for chan, challenge in prev.challenges.iteritems(): challenges[chan] = challenge modal.set_mode(chan, 'upoopia') if hasattr(prev, 'games') and isinstance(games, dict): new_games = dict() for chan, game in prev.games.iteritems(): if game not in new_games: new_games[game] = UpoopiaText(prev=game) games[chan] = new_games[game] modal.set_mode(chan, 'upoopia')
def h_upoopia(bot, id, chan, args, full_msg): if not chan: return # Parse arguments. args = args.split() if len(args) >= 2: opp_chan, colour = args[:2] if 'blue'.startswith(colour.lower()): colour = BLACK elif 'red'.startswith(colour.lower()): colour = WHITE else: reply(bot, id, chan, 'Error: "%s" is not a valid colour.' % colour) return elif len(args) >= 1: opp_chan, colour = args[0], None elif len(chan_link.links.get(chan.lower(), set())) == 1: # Implicitly select the single channel to which we are linked. [opp_chan], colour = chan_link.links[chan.lower()], None else: if chan.lower() in challenges: opp_chan, colour = challenges[chan.lower()] bot.send_msg(chan, 'A challenge issued to %s is currently pending.' % opp_chan) elif chan.lower() in games: game = games[chan.lower()] [opp] = [chan for chan in game.names.values() if chan.lower() != chan.lower()] bot.send_msg(chan, 'A game of Upoopia against %s is currently in session.' % opp) else: bot.send_msg(chan, 'No game of Upoopia is active.' ' See \2!help upoopia\2 for starting a game.') return if not opp_chan.startswith('#'): reply(bot, id, chan, 'Error: "%s" is not a valid channel name.' % opp_chan) return elif opp_chan.lower() == chan.lower(): reply(bot, id, chan, 'Error: your opponent must be in a different channel!') return # If not already linked, require operator status. if not chan_link.is_linked(chan, opp_chan): pre_ms, pre_cs = bot.isupport['PREFIX'] umodes = channel.umode_channels[chan.lower()] umodes = umodes.get(id.nick.lower(), '') if not any(m in umodes for m in pre_ms[:pre_ms.find('o')+1]): reply(bot, id, chan, 'Error: only channel operators may initiate a channel link.') return # Acquire the 'upoopia' mode in this channel. if modal.get_mode(chan) == 'upoopia' and chan.lower() in challenges: # If another challenge exists, cancel it. old_opp_chan, colour = challenges.pop(chan.lower()) bot.send_msg(chan, 'Challenge to %s cancelled.' % old_opp_chan) elif modal.get_mode(chan): yield sign('CONTEND_MODE', bot, id, chan) return else: modal.set_mode(chan, 'upoopia') if opp_chan.lower() in challenges: # If a reciprocating challenge exists, immediately start the game. opp_opp_chan, opp_colour = challenges[opp_chan.lower()] if opp_opp_chan.lower() == chan.lower(): del challenges[opp_chan.lower()] yield util.sub(start_game(bot, chan, opp_chan, colour, opp_colour)) return elif chan_link.is_linked(chan, opp_chan) \ and modal.get_mode(opp_chan) in (None, 'upoopia'): # Otherwise, if the opponent channel is still linked and does not # have any pending challenges, also immediately start the game. modal.set_mode(opp_chan, 'upoopia') yield util.sub(start_game(bot, chan, opp_chan, colour, None)) return # Otherwise, just record the challenge. challenges[chan.lower()] = (opp_chan, colour) bot.send_msg(chan, 'Challenge issued: waiting for %s to reciprocate.' % opp_chan)