#!/usr/bin/env python3 # # game7.py # # 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 Hockey stats """ __version__ = "0.4" __author__ = "Cody A. Taylor ( codemister99@yahoo.com )" __all__ = ["main", "menu_help", "start_teams", "TeamStats"] from optparse import OptionParser import CodeLIB def main(): """Create the home and away teams, keeping track of both stats """ home, away, home_args, away_args = start_teams() exit_game, promt = False, str(home.team + " Vs " + away.team + "> ") while exit_game == False: command, *args = input(promt).lower().split() try: no_team_error = "Error: {0}: unknown team argument".format(args[0]) except IndexError: pass if command in {"exit", "quit", "q"}: if not command == "exit": print(TeamStats.leader(home, away, True)) exit_game = True elif command == "leader": print(TeamStats.leader(home, away)) elif command == "stats": if len(args) >= 1: if args[0] in home_args: print(home) elif args[0] in away_args: print(away) else: print(no_team_error) else: print(home, away, sep="\n\n", end="\n\n") elif command in {"help", "menu"}: menu_help() elif command in {"shot", "goal", "check", "penalty"}: try: value = int(args[1]) except IndexError: value = 1 except ValueError as err: print("Error:", err, "\nAdding '0' to {0} count, try again".format(command)) value = 0 try: if command == "shot": if args[0] in home_args: home.shot(value) elif args[0] in away_args: away.shot(value) else: print(no_team_error) elif command == "goal": if args[0] in home_args: home.goal(value) elif args[0] in away_args: away.goal(value) else: print(no_team_error) elif command == "check": if args[0] in home_args: home.check(value) elif args[0] in away_args: away.check(value) else: print(no_team_error) elif command == "penalty": if args[0] in home_args: home.penalty(value) elif args[0] in away_args: away.penalty(value) else: print(no_team_error) except IndexError: print("Error: {0} requires team and value arguements".format(command)) continue else: print("Error: {0}: unknown command".format(command)) def menu_help(): """Print a pretty menu that is helpful """ print("""game7.py - keep track of Hockey game stats Program options: menu - print this menu help - print this menu exit - leave the program quit, q - print the winner and leave the program leader - print who is winning the game Stat options: = can be 'home', 'away', or 'team name' if name is one word [value] = number to add (1 is assumed if not input) shot [value] - add value to shot count goal [value] - add value to goal and shot counts check [value] - add value to check count penalty [value] - add value to the penalties count stats [team] - print the total for both teams or given team """ ); return def start_teams(): """Parse input args and ask questions if needed""" parser = OptionParser(version="{0:>7}{1}\n{2:>45}".format("", __version__, __author__), description="Keep track of hockey stats during a game. " "A shell style program, options are to skip setup questions and are not needed.", usage="Usage: %prog [-H -A -m]") parser.add_option("-H", "--home", type="string", default=None, dest="home", help="name of the home team") parser.add_option("-A", "--away", type="string", default=None, dest="away", help="name of the away team") parser.add_option("-m", "--menu", action="store_true", default=False, dest="menu", help="show the shell menu") opts, args = parser.parse_args() #if no options are passed, then ask if user if they want to see the menu: if opts.menu == True: menu_help() elif opts.home or opts.away: pass else: if CodeLIB.get_ans("Show menu", "? ", default=False, name="show menu"): menu_help() if not opts.home: opts.home = input("Home team name: ") if not opts.away: opts.away = input("Away team name: ") return TeamStats(opts.home), TeamStats(opts.away), {"home", opts.home.lower()}, {"away", opts.away.lower()} class TeamStats(object): """Keep track of the stats (int values) associated to a team """ def __init__(self, team, *, shots=0, goals=0, checks=0, penalties=0): self.team = str(team) self.shots = int(shots) self.goals = int(goals) self.checks = int(checks) self.penalties = int(penalties) def __repr__(self): return "{0.team!r}: {0.shots!r} shots, {0.goals!r} goals, {0.checks!r} checks, {0.penalties!r} penalties".format(self) def __str__(self): return "{1}: {0.shots!r} shots, {0.goals!r} goals, {0.checks!r} checks, {0.penalties!r} penalties".format(self, self.team) def __lt__(self, other): return self.goals < other.goals def __le__(self, other): return self.goals <= other.goals def __eq__(self, other): return self.goals == other.goals def shot(self, value=1): """Incrament shot count by value """ self.shots += int(value) def goal(self, value=1): """Incrament goal and shot count by value """ self.goals += int(value) self.shots += int(value) def check(self, value=1): """Incrament check count by value """ self.checks += int(value) def penalty(self, value=1): """Incrament penalty count by value """ self.penalties += int(value) def leader(self, other, end_of_game=False): """returns a pretty string saying who is the leader (or who has won) """ end = " are in the lead" if end_of_game == False else " have Won the game!" if self > other: return "The " + self.team + end elif self < other: return "The " + other.team + end else: return "The " + self.team + " and the " + other.team + " are Tied Up!" if __name__ == "__main__": main()