def show_players(tournament): form = [{ 'name': "sort_method", 'title': "sort method", 'type': "select", 'options': ["alpha", "rank", "id"] }] win = view.InputWin(INFO_H, INFO_W, 2, MENU_W + 8, *form) win.draw() if win.get_results()['sort_method'] == "rank": players = sorted(tournament.players, key=attrgetter('rank')) else: players = sorted(tournament.players, key=attrgetter('last_name')) players = { f"{p.first_name} {p.last_name}": (f"{p.first_name} {p.last_name}\n" f"rank: {p.rank}\n" f"score: {p.score}\n" f"birth date: {p.birth_date}\n" f"gender: {p.gender}") for p in players } win = view.MenuWin(MENU_H, MENU_W, 2, 2, **players) win.draw() selected = win.navigate() win.clear() win.refresh() return selected
def new_player(): form = [{ 'name': "first_name", 'title': "first name", 'type': "string", }, { 'name': "last_name", 'title': "last name", 'type': "string", }, { 'name': "gender", 'title': "gender", 'type': "select", 'options': ["Male", "Female"], }, { 'name': "birth_date", 'title': "birth date", 'type': "date", }, { 'name': "rank", 'title': "rank", 'type': "int", }] win = view.InputWin(INFO_H, INFO_W, 2, MENU_W + 8, *form) win.draw() results = win.get_results() if win.validate(results): model.new_player(**results).save() view.Popup("info", "player saved").draw() else: view.Popup("info", "player discarded").draw() win.clear() win.refresh()
def update_description(tournament): form = [{'name': "description", 'title': "description", 'type': "long"}] win = view.InputWin(INFO_W, INFO_W, 2, MENU_W + 8, *form) win.draw() results = win.get_results() if win.validate(results): tournament.update_description(results['description']) view.Popup("info", "description updated").draw() else: view.Popup("info", "new description discarded").draw()
def update_rank(tournament): selected = show_players(tournament) form = [{'name': "rank", 'title': "new rank", 'type': "int"}] win = view.InputWin(INFO_W, INFO_W, 2, MENU_W + 8, *form) win.draw() results = win.get_results() if win.validate(results): tournament.players[selected].update_rank(results['rank']) view.Popup("info", "player rank updated").draw() else: view.Popup("info", "new player rank discarded").draw()
def save_tournament(tournament): form = [{ 'name': 'save', 'title': "save tournament ?", 'type': "select", 'options': ["Yes", "No"] }] win = view.InputWin(INFO_H, INFO_W, 2, MENU_W + 8, *form) win.draw() if win.get_results()['save'] == "Yes": tournament.save() view.Popup("info", "tournament saved").draw() else: view.Popup("info", "tournament not saved").draw()
def new_tournament(): if model.number_of_players() < 8: view.Popup("info", "not enough players in the database !").draw() return form = [{ 'name': "name", 'title': "tournament name", 'type': "string" }, { 'name': "location", 'title': "location", 'type': "string" }, { 'name': "date", 'title': "date", 'type': "date" }, { 'name': "players", 'title': "players", 'type': "menu", 'nb_choices': 8, 'options': model.list_players() }, { 'name': "time_format", 'title': "time format", 'type': "select", 'options': ["bullet", "blitz", "fast move"] }, { 'name': "description", 'title': "description", 'type': "long" }] win = view.InputWin(INFO_H, INFO_W, 2, MENU_W + 8, *form) win.draw() results = win.get_results() if win.validate(results): tournament = model.Tournament(**results) tournament.save() view.Popup("info", "new tournament created").draw() return tournament else: view.Popup("info", "tournament discarded").draw() win.clear() win.refresh()
def finish_round(tournament): form = [{ 'name': i, 'title': (f"{m.white.first_name} (white) " f"vs {m.black.first_name} (black)"), 'type': "select", 'options': ["black", "white", "draw"] } for i, m in enumerate(tournament.current_round.matches)] win = view.InputWin(INFO_H, INFO_W, 2, MENU_W + 8, *form) win.draw() results = win.get_results() if win.validate(results): results = list(results.values()) tournament.end_round(results) view.Popup("info", "round finished, scores registered").draw() else: view.Popup("info", "scores discarded").draw()
def list_players(): if model.number_of_players() <= 0: view.Popup("info", "No players in the database").draw() else: form = [{ 'name': "sort_method", 'title': "sort method", 'type': "select", 'options': ["alpha", "rank", "id"] }] win = view.InputWin(INFO_H, INFO_W, 2, MENU_W + 8, *form) win.draw() sort_method = win.get_results()['sort_method'] players = model.list_players(sort_method) win = view.MenuWin(MENU_H, MENU_W, 2, 2, **players) win.draw() selected = win.navigate() win.clear() win.refresh() return selected + 1 # adjust for 1-indexed database id