#!/usr/bin/env python3 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. """Keep track of players in a multi-round tourney Can handle both Non-elimination and Elimination styles """ __all__ = ['main', 'add_players', 'assign_rooms', 'print_help'] __author__ = "Cody A. Taylor ( codemister99@yahoo.com )" __version__ = "0.1.2 alpha" import CodeLIB from strict_type import type_check from rand_pop import choice_pop def main(): players = add_players({}) print_help() while True: cmd, *args = input("> ").strip().lower().split() if cmd in {"a", "add"}: if len(args) != 0 and args[0] in {"yes", "y", "true", "ask"}: players = add_players(players, True) else: players = add_players(players, False) elif cmd in {"r", "room", "rooms", "assign"}: try: num_per_room = int(args[0]) except (ValueError, IndexError) as err: print("Error:", err) else: print(assign_rooms(list(players.keys()), num_per_room)) elif cmd in {"s", "score"}: try: name = str(args[0]) score = int(args[1]) players[name] += score except (ValueError, IndexError, KeyError) as err: print("Error:", err) elif cmd in {"exit", "quit", "q"}: break else: print_help() @type_check def add_players(dict_of_players : dict, ask_value=False) -> dict: """A dict add key, value function: {"name": score} returns the dict """ print("Use 'break' to stop adding names") while True: name = input("Name of player: ").strip().lower() if name == "break": break if ask_value == True: score = CodeLIB.get_int("Current score of {}".format(name), min=0, default=0) dict_of_players[name] = score else: dict_of_players[name] = 0 return dict_of_players @type_check def assign_rooms(names : list, number_per_room : int) -> str: """Randomly assign rooms returns a multi-line string """ room_str = "" num_rooms = len(names) // number_per_room for room_number in range(1, (num_rooms + 1)): room_str += "Room {}\n".format(room_number) for _ in range(number_per_room): room_str += " {}\n".format(choice_pop(names)) if len(names) != 0: room_str += "Room {}\n".format(room_number + 1) while len(names) != 0: #tack on odd number to last room room_str += " {}\n".format(choice_pop(names)) return room_str def print_help(): print("Commands:", " add - add more playes with optional score", " a [Yes Y True Ask]", " add [Yes Y True Ask]", " assign - assign rooms to players", " r [num_per_room]", " room [num_per_room]", " rooms [num_per_room]", " assign [num_per_room]", " score - add value to given player's score", " s [name] [value]", " score [name] [value]", " exit - quit the program", " exit", " quit", " q", sep="\n") if __name__ == "__main__": main()