def pick(self, number): with self.printer.buffer(self.channel) as buff: if self.ranked: points = len(number) for i, choice in enumerate(number): player = self.order[choice - 1] response = player.popResponses() if self.order.index(player) == choice - 1 else player.popBets() buff += CAHPREFIX + "%s. %s: 00,01 %s " % (ordinal(i+1), player.nick, self.substitute(response)) player.addPoints(points) points -= 1 else: points = 1 for i in {i for i in self.order if i.bets}: i.points -= 1 points += 1 winner = self.order[number - 1] winning = winner.popResponses() if self.order.index(winner) == number - 1 else winner.popBets() winner.addPoints(points) buff += CAHPREFIX + "%s picked %s's response: 00,01 %s " % (self.czar.nick, winner.nick, self.substitute(winning)) reveal = [] for i in self.order: response = i.popResponses() or i.popBets() if response: reveal.append("%s %s" % (i.nick, " ".join("01,00 %s " % ((i[0].upper() + i[1:]).rstrip(".")) for i in response))) output = [[]] for i in reveal: if sum(len(i) + 2 for i in output) + len(i) < 375: output[-1].append(i) else: output.append([i]) for i in output: buff += CAHPREFIX + " · ".join(i) self.next()
def pick(self, number): # Logging try: self.logwinner([number-1] if type(number) == int else [i-1 for i in number]) except sqlite3.ProgrammingError: sys.__stdout__.write("sqlite3 error\n") with printer.buffer(self.channel) as buff: if self.ranked: points = len(number) for i, choice in enumerate(number): player = self.order[choice - 1] response = player.popResponses() if self.order.index(player) == choice - 1 else player.popBets() buff += "00,01 15,14 01,15 %s. %s: 00,01 %s " % (ordinal(i+1), player.nick, self.substitute(response)) player.addPoints(points) points -= 1 else: points = 1 for i in {i for i in self.order if i.bets}: i.points -= 1 points += 1 winner = self.order[number - 1] winning = winner.popResponses() if self.order.index(winner) == number - 1 else winner.popBets() winner.addPoints(points) buff += "00,01 15,14 01,15 %s picked %s's response: 00,01 %s " % (self.czar.nick, winner.nick, self.substitute(winning)) for i in self.order: response = i.popResponses() or i.popBets() if response: buff += "00,01 15,14 01,15 %s: %s" % (i.nick, self.substitute(response)) self.next()
def endgame(self): self.state = "failed" with printer.buffer(self.channel) as buffer: buffer += "00,01 Cards Against Humanity is over!" players = sorted(self.allplayers, key=CAHPlayer.score)[::-1] for i, player in enumerate(players): if i and players[i-1].score() == player.score(): rank = " " else: rank = ordinal(i+1) buffer += "00,01 15,14 01,15 %s: %s - %d points" % (rank, player.nick, player.score())
def endgame(self): if self.state == "failed": return self.state = "failed" with self.printer.buffer(self.channel) as buffer: buffer += "01│00,01 Cards Against Humanity is over!" players = sorted(self.allplayers, key=CAHPlayer.score)[::-1] for i, player in enumerate(players): if i and players[i-1].score() == player.score(): rank = " " else: rank = ordinal(i+1) + (" " if i < 9 else "") buffer += CAHPREFIX + "%s 01│ %s - %d point%s" % (rank, player.nick, player.score(), ["", "s"][cmp(1, player.score())])
def endgame(self): if self.state == "failed": return self.state = "failed" with self.printer.buffer(self.channel) as buffer: buffer += "01│00,01 Cards Against Humanity is over!" players = sorted(self.allplayers, key=CAHPlayer.score)[::-1] for i, player in enumerate(players): if i and players[i - 1].score() == player.score(): rank = " " else: rank = ordinal(i + 1) + (" " if i < 9 else "") buffer += CAHPREFIX + "%s 01│ %s - %d point%s" % ( rank, player.nick, player.score(), ["", "s"][cmp( 1, player.score())])
def pick(self, number): with self.printer.buffer(self.channel) as buff: if self.ranked: points = len(number) for i, choice in enumerate(number): player = self.order[choice - 1] response = player.popResponses() if self.order.index( player) == choice - 1 else player.popBets() buff += CAHPREFIX + "%s. %s: 00,01 %s " % ( ordinal(i + 1), player.nick, self.substitute(response)) player.addPoints(points) points -= 1 else: points = 1 for i in {i for i in self.order if i.bets}: i.points -= 1 points += 1 winner = self.order[number - 1] winning = winner.popResponses() if self.order.index( winner) == number - 1 else winner.popBets() winner.addPoints(points) buff += CAHPREFIX + "%s picked %s's response: 00,01 %s " % ( self.czar.nick, winner.nick, self.substitute(winning)) reveal = [] for i in self.order: response = i.popResponses() or i.popBets() if response: reveal.append( "%s %s" % (i.nick, " ".join("01,00 %s " % ((i[0].upper() + i[1:]).rstrip(".")) for i in response))) output = [[]] for i in reveal: if sum(len(i) + 2 for i in output) + len(i) < 375: output[-1].append(i) else: output.append([i]) for i in output: buff += CAHPREFIX + " · ".join(i) self.next()
def trigger(self, server, line): printer = server.printer x = line.split() channel = x[2].lower() nick = Address(x[0]).nick if not channel.startswith("#"): return elif channel in self.games and not self.games[channel].failed(): game = self.games[channel] with game.lock: if x[3].lower() == ":!join": if game.addPlayer(nick): printer.message(CAHPREFIX + "%s is our %s player." % (nick, ordinal(len(game.players) - bool(game.rando))), channel) if game.state == "collect": printer.message("01│00,01 %s " % re.sub("[*^]_+", "_______", game.question), nick, "NOTICE") player = game.getPlayer(nick) game.repopulate(player) player.printHand(printer) else: printer.message(CAHPREFIX + "%s is already in the game." % nick, channel) elif x[3].lower() == ":!score": game.printplayers() elif x[3].lower() == ":!rando": if game.rando: printer.message(CAHPREFIX + "Rando Cardrissian is now out of the game.", channel) game.removeRando() else: printer.message(CAHPREFIX + "Rando Cardrissian is now playing.", channel) game.addRando() elif game.state == "signups" and x[3][1:].lower() in ["!start", "!go", "!begin"]: if len(game.players) > 2 and nick in [i.nick for i in game.players]: game.start() elif len(game.players) > 2: printer.message(CAHPREFIX + "I don't care what you think.", channel) else: printer.message(CAHPREFIX + "The game can't begin without at least 3 players.", channel) elif nick in [i.nick for i in game.players]: player = game.getPlayer(nick) if x[3].lower() == ":!discard" and player.points: args = " ".join(x[4:]) args = args.replace(",", " ") cards = sorted({int(i) for i in args.split() if i.isdigit() and 1 <= int(i) <= len(player.hand)})[::-1] if len(cards) > game.maxcards / 2: printer.message(CAHPREFIX + "You can't discard more than half your hand at once.", nick, "NOTICE") else: for i in cards: game.answers.append(player.hand.pop(i-1)) random.shuffle(game.answers) game.repopulate(player) player.points -= 1 player.printHand(printer) elif x[3].lower() == ":!hand": game.repopulate(player) player.printHand(printer) elif x[3].lower() in [":!choose", ":!pick"]: if player != game.czar and game.state == "collect": args = line.split(" ", 4)[-1] if "," in args and game.bets: if player.points: args, bet = args.split(",") bet = [int(i) for i in bet.strip().split()] else: printer.message(CAHPREFIX + "Not enough points to bet, sorry.", nick, "NOTICE") else: bet = [] args = [int(i) for i in args.split()] if all((bet + args).count(i) == 1 and 1 <= i <= len(player.hand) for i in bet + args) and (len(args) == game.numcards() and len(bet) in [game.numcards(), 0]): player.setResponses(args) player.setBet(bet) printer.message(CAHPREFIX + "00,01 Response 01,00 %s " % game.substitute(player.responses), nick, "NOTICE") if bet: printer.message(CAHPREFIX + "01,00 Backup 01,00 %s " % game.substitute(player.bets), nick, "NOTICE") else: printer.message(CAHPREFIX + "Invalid arguments.", nick, "NOTICE") if not [i for i in game.players if i.responses is None and i != game.czar]: game.judge() elif player == game.czar and game.state == "judge": if x[4].isdigit() and 1 <= int(x[4]) <= len(game.order) and game.ranked == False: # Logging try: game.logwinner([int(x[4])-1], self.expansiondir) except sqlite3.ProgrammingError: sys.stderr.write("sqlite3 error\n") game.pick(int(x[4])) elif len(x[4:]) == min(len(game.players) - 2, 3) and all([str.isdigit and 1 <= int(n) <= len(game.order) and x[4:].count(n) == 1 for n in x[4:]]) and game.ranked == True: # Logging try: game.logwinner([int(i)-1 for i in x[4:]], self.expansiondir) except sqlite3.ProgrammingError: sys.stderr.write("sqlite3 error\n") game.pick([int(i) for i in x[4:]]) else: printer.message(CAHPREFIX + "Invalid arguments.", nick, "NOTICE") elif x[3].lower() in [":!leave", ":!quit"]: printer.message(CAHPREFIX + "%s is quitting the game, quitter." % player.nick, channel) game.remove(player) if game.state != 'failed': if player == game.czar: game.czar = game.players.pop() game.players.insert(0, game.czar) if game.czar == game.rando: game.czar = game.players.pop() game.players.insert(0, game.czar) game.czar.responses = None game.czar.bets = None if game.state == "judge": game.state = "collect" printer.message(CAHPREFIX + "%s is the new czar." % game.czar.nick, channel) game.judge() elif x[3].lower() == ":!score": game.printplayers() elif x[3].lower() in [":!cah", ":!cards"]: args = self.parseSettings(line.split(" ", 4)[-1]) self.games[channel] = CardsAgainstHumanity(printer, channel, **args) deck = ("(%s) " % self.games[channel].deck) if self.games[channel].deck is not None else "" printer.message("01│00,01 Cards Against Humanity %s will begin in a minute. Want to !join us?" % deck, channel) threading.Timer(150, self.games[channel].start).start()
def trigger(self, sdata): x = sdata.split() channel = x[2].lower() nick = Address(x[0]).nick if not channel.startswith("#"): return elif x[3] in [":!Q.", ":!A."]: if x[3][2] == "Q": data = " ".join(x[4:]) data = re.sub("_+", "_______", data) with self.lock: CardsAgainstHumanity.expansionqs.append(data) with open("old_data/cah/questions.txt", "w") as f: f.write("\n".join(CardsAgainstHumanity.expansionqs)) printer.message("00,01 15,14 01,15 Added: 00,01 %s " % (data), channel) else: data = " ".join(x[4:]) data = data.strip() if re.search("[^.?!]$", data): data += "." with self.lock: CardsAgainstHumanity.expansionas.append(data) with open("old_data/cah/answers.txt", "w") as f: f.write("\n".join(CardsAgainstHumanity.expansionas)) printer.message("00,01 15,14 01,15 Added: 01,00 %s " % (data), channel) elif channel in self.games and not self.games[channel].failed(): game = self.games[channel] with game.lock: if x[3].lower() == ":!join": if game.addPlayer(nick): printer.message("00,01 15,14 01,15 %s is our %s player." % (nick, ordinal(len(game.players) - bool(game.rando))), channel) else: printer.message("00,01 15,14 01,15 %s is already in the game." % nick, channel) elif x[3].lower() == ":!score": game.printplayers() elif x[3].lower() == ":!rando": if game.rando: printer.message("00,01 15,14 01,15 Rando Cardrissian is now out of the game.", channel) game.removeRando() else: printer.message("00,01 15,14 01,15 Rando Cardrissian is now playing.", channel) game.addRando() elif game.state == "signups" and x[3][1:].lower() in ["!start", "!go", "!begin"]: if len(game.players) > 2 and nick in [i.nick for i in game.players]: game.start() elif len(game.players) > 2: printer.message("00,01 15,14 01,15 I don't care what you think.", channel) else: printer.message("00,01 15,14 01,15 The game can't begin without at least 3 players.", channel) elif nick in [i.nick for i in game.players]: player = game.getPlayer(nick) if x[3].lower() == ":!discard" and player.points: args = " ".join(x[4:]) args = args.replace(",", " ") cards = sorted(list(set(map(int, list(filter(str.isdigit and 1 <= int(x) <= len(player.hand), args.split()))))))[::-1] if len(cards) > game.maxcards / 2: printer.message("00,01 15,14 01,15 You can't discard more than half your hand at once.", nick, "NOTICE") else: for i in cards: game.answers.append(player.hand.pop(i-1)) random.shuffle(game.answers) game.repopulate(player) player.points -= 1 player.printHand() elif x[3].lower() == ":!hand": game.repopulate(player) player.printHand() elif x[3].lower() in [":!choose", ":!pick"]: if player != game.czar and game.state == "collect": args = sdata.split(" ", 4)[-1] if "," in args and game.bets: if player.points: args, bet = args.split(",") bet = [int(i) for i in bet.strip().split()] else: printer.message("00,01 15,14 01,15 Not enough points to bet, sorry.", nick, "NOTICE") else: bet = [] args = [int(i) for i in args.split()] if all((bet + args).count(i) == 1 and 1 <= i <= len(player.hand) for i in bet + args) and (len(args) == game.numcards() and len(bet) in [game.numcards(), 0]): player.setResponses(args) player.setBet(bet) printer.message("Your response: 00,01 %s " % game.substitute(player.responses), nick, "NOTICE") if bet: printer.message(" Backup: 00,01 %s " % game.substitute(player.bets), nick, "NOTICE") else: printer.message("00,01 15,14 01,15 Invalid arguments.", nick, "NOTICE") if not [i for i in game.players if i.responses is None and i != game.czar]: game.judge() elif player == game.czar and game.state == "judge": if x[4].isdigit() and 1 <= int(x[4]) <= len(game.order) and game.ranked == False: game.pick(int(x[4])) elif len(x[4:]) == min(len(game.players) - 2, 3) and all([str.isdigit and 1 <= int(n) <= len(game.order) and x[4:].count(n) == 1 for n in x[4:]]) and game.ranked == True: game.pick([int(i) for i in x[4:]]) else: printer.message("00,01 15,14 01,15 Invalid arguments.", nick, "NOTICE") elif x[3].lower() == ":!leave": if player == game.czar and game.state == "judge": printer.message("00,01 15,14 01,15 HEY GODDAMNIT COME BACK %s" % player.nick.upper(), channel) else: printer.message("00,01 15,14 01,15 %s is quitting the game, quitter." % player.nick, channel) game.remove(player) if game.state != 'failed': if player == game.czar: for i in game.players: i.responses = None i.bets = None game.next() game.judge() elif x[3].lower() == ":!score": game.printplayers() elif x[3].lower() in [":!cah", ":!cards"]: args = self.parseSettings(sdata.split(" ", 4)[-1]) self.games[channel] = CardsAgainstHumanity(channel, **args) printer.message("00,01 Cards Against Humanity will begin in a minute. Want to !join us?", channel) threading.Timer(150, self.games[channel].start).start()
def trigger(self, server, line): printer = server.printer x = line.split() channel = x[2].lower() nick = Address(x[0]).nick if not channel.startswith("#"): return elif channel in self.games and not self.games[channel].failed(): game = self.games[channel] with game.lock: if x[3].lower() == ":!join": if game.addPlayer(nick): printer.message( CAHPREFIX + "%s is our %s player." % (nick, ordinal(len(game.players) - bool(game.rando))), channel) if game.state == "collect": printer.message( "01│00,01 %s " % re.sub("[*^]_+", "_______", game.question), nick, "NOTICE") player = game.getPlayer(nick) game.repopulate(player) player.printHand(printer) else: printer.message( CAHPREFIX + "%s is already in the game." % nick, channel) elif x[3].lower() == ":!score": game.printplayers() elif x[3].lower() == ":!rando": if game.rando: printer.message( CAHPREFIX + "Rando Cardrissian is now out of the game.", channel) game.removeRando() else: printer.message( CAHPREFIX + "Rando Cardrissian is now playing.", channel) game.addRando() elif game.state == "signups" and x[3][1:].lower() in [ "!start", "!go", "!begin" ]: if len(game.players) > 2 and nick in [ i.nick for i in game.players ]: game.start() elif len(game.players) > 2: printer.message( CAHPREFIX + "I don't care what you think.", channel) else: printer.message( CAHPREFIX + "The game can't begin without at least 3 players.", channel) elif nick in [i.nick for i in game.players]: player = game.getPlayer(nick) if x[3].lower() == ":!discard" and player.points: args = " ".join(x[4:]) args = args.replace(",", " ") cards = sorted({ int(i) for i in args.split() if i.isdigit() and 1 <= int(i) <= len(player.hand) })[::-1] if len(cards) > game.maxcards / 2: printer.message( CAHPREFIX + "You can't discard more than half your hand at once.", nick, "NOTICE") else: for i in cards: game.answers.append(player.hand.pop(i - 1)) random.shuffle(game.answers) game.repopulate(player) player.points -= 1 player.printHand(printer) elif x[3].lower() == ":!hand": game.repopulate(player) player.printHand(printer) elif x[3].lower() in [":!choose", ":!pick"]: if player != game.czar and game.state == "collect": args = line.split(" ", 4)[-1] if "," in args and game.bets: if player.points: args, bet = args.split(",") bet = [int(i) for i in bet.strip().split()] else: printer.message( CAHPREFIX + "Not enough points to bet, sorry.", nick, "NOTICE") else: bet = [] args = [int(i) for i in args.split()] if all((bet + args).count(i) == 1 and 1 <= i <= len(player.hand) for i in bet + args) and ( len(args) == game.numcards() and len(bet) in [game.numcards(), 0]): player.setResponses(args) player.setBet(bet) printer.message( CAHPREFIX + "00,01 Response 01,00 %s " % game.substitute(player.responses), nick, "NOTICE") if bet: printer.message( CAHPREFIX + "01,00 Backup 01,00 %s " % game.substitute(player.bets), nick, "NOTICE") else: printer.message( CAHPREFIX + "Invalid arguments.", nick, "NOTICE") if not [ i for i in game.players if i.responses is None and i != game.czar ]: game.judge() elif player == game.czar and game.state == "judge": if x[4].isdigit() and 1 <= int(x[4]) <= len( game.order) and game.ranked == False: # Logging try: game.logwinner([int(x[4]) - 1], self.expansiondir) except sqlite3.ProgrammingError: sys.stderr.write("sqlite3 error\n") game.pick(int(x[4])) elif len(x[4:]) == min( len(game.players) - 2, 3) and all([ str.isdigit and 1 <= int(n) <= len(game.order) and x[4:].count(n) == 1 for n in x[4:] ]) and game.ranked == True: # Logging try: game.logwinner([int(i) - 1 for i in x[4:]], self.expansiondir) except sqlite3.ProgrammingError: sys.stderr.write("sqlite3 error\n") game.pick([int(i) for i in x[4:]]) else: printer.message( CAHPREFIX + "Invalid arguments.", nick, "NOTICE") elif x[3].lower() in [":!leave", ":!quit"]: printer.message( CAHPREFIX + "%s is quitting the game, quitter." % player.nick, channel) game.remove(player) if game.state != 'failed': if player == game.czar: game.czar = game.players.pop() game.players.insert(0, game.czar) if game.czar == game.rando: game.czar = game.players.pop() game.players.insert(0, game.czar) game.czar.responses = None game.czar.bets = None if game.state == "judge": game.state = "collect" printer.message( CAHPREFIX + "%s is the new czar." % game.czar.nick, channel) game.judge() elif x[3].lower() == ":!score": game.printplayers() elif x[3].lower() in [":!cah", ":!cards"]: args = self.parseSettings(line.split(" ", 4)[-1]) self.games[channel] = CardsAgainstHumanity(printer, channel, **args) deck = ("(%s) " % self.games[channel].deck ) if self.games[channel].deck is not None else "" printer.message( "01│00,01 Cards Against Humanity %s will begin in a minute. Want to !join us?" % deck, channel) threading.Timer(150, self.games[channel].start).start()