def make_game(game): err = "" away = game.away home = game.home if( not away.hasRecord() and not home.hasRecord() ): err = "Both record not exist" return game, err ## handle 1 team case if( not away.hasRecord() and home.hasRecord() ): away.pitchers.append( rdPitcher('00') ) if( away.hasRecord() and not home.hasRecord() ): home.pitchers.append( rdPitcher('00') ) if( away.hasRecord() ): if( len(away.pitchers) == 0 ): err = away.name + u"沒有先發投手" return game, err home.E, err = parse_order_table(away) if( err != "" ): err += " in Away Record" return game, err err = parse_pitcher_info(away, home.pitchers) if( err != "" ): err += " in Away Record" return game, err if( home.hasRecord() ): if( len(home.pitchers) == 0 ): err = home.name + u"沒有先發投手" return game, err away.E, err = parse_order_table(home) if( err != "" ): err += " in Home Record" return game, err err = parse_pitcher_info(home, away.pitchers) if( err != "" ): err += " in Home Record" return game, err # calculate total scores away.compute_statistic() home.compute_statistic() game.away = away game.home = home return game, err
def make_game(game): err = "" away = game.away home = game.home if (not away.hasRecord() and not home.hasRecord()): err = "Both record not exist" return game, err ## handle 1 team case if (not away.hasRecord() and home.hasRecord()): away.pitchers.append(rdPitcher('00')) if (away.hasRecord() and not home.hasRecord()): home.pitchers.append(rdPitcher('00')) if (away.hasRecord()): if (len(away.pitchers) == 0): err = away.name + u"沒有先發投手" return game, err home.E, err = parse_order_table(away) if (err != ""): err += " in Away Record" return game, err err = parse_pitcher_info(away, home.pitchers) if (err != ""): err += " in Away Record" return game, err if (home.hasRecord()): if (len(home.pitchers) == 0): err = home.name + u"沒有先發投手" return game, err away.E, err = parse_order_table(home) if (err != ""): err += " in Home Record" return game, err err = parse_pitcher_info(home, away.pitchers) if (err != ""): err += " in Home Record" return game, err # calculate total scores away.compute_statistic() home.compute_statistic() game.away = away game.home = home return game, err
def make_team(team_name, scores, str_table): err = "" team = Team() team.name = team_name if (scores == None): scores = [0] * 7 if (len(scores) < 7): scores = scores + [0] * (7 - len(scores)) team.scores = scores for r in range(len(str_table)): row = str_table[r] if (len(row) < 3): err = "format error(row %d): name position PA PA..." % (r + 1) break order = str(r + 1) no = row[0] pos = row[1].upper() PAs = row[2:] team.batters.append(rdBatter(order, no, pos)) team.order_table.append(PAs) if (pos == 'P'): team.pitchers.append(rdPitcher(no)) return team, err
def make_team(team_name, scores, str_table): err = "" team = Team() team.name = team_name if( scores == None ): scores = [0] * 7 if( len(scores) < 7 ): scores = scores + [0] * (7 - len(scores)) team.scores = scores for r in range(len(str_table)): row = str_table[r] if( len(row) < 3 ): err = "format error(row %d): name position PA PA..." %(r+1) break order = str(r+1) no = row[0] pos = row[1].upper() PAs = row[2:] team.batters.append( rdBatter(order, no, pos) ) team.order_table.append( PAs ) if( pos == 'P' ): team.pitchers.append( rdPitcher(no) ) return team, err
def parse_pitcher_info(team, pitchers): err = "" # parse inning information turn = 0 order = 0 isER = True nOrder = len(team.order_table) nBatter = team.num_of_batters() supp_out = 0 # supposed out pitcher = pitchers[0] while (True): pa = team.order_table[order][turn][1] # change pitcher if (pa.change_pitcher != None): no = pa.change_pitcher # find whether pitcher had been on field before is_new_pitcher = True for p in pitchers: if p.number == no: pitcher = p is_new_pitcher = False break if (is_new_pitcher): pitcher = rdPitcher(no) pitchers.append(pitcher) err = pitcher.AddPa(pa, isER) if (err != ""): break if (pa.endInning == '!'): # end of game break supp_out += pa.out if (pa.result == "E"): supp_out += 1 if (supp_out >= 3): isER = False if (pa.endInning == '#'): # change inning supp_out = 0 isER = True order += 1 if (order == nOrder): order = 0 turn += 1 return err
def parse_pitcher_info(team, pitchers): err = "" # parse inning information turn = 0 order = 0 isER = True nOrder = len(team.order_table) nBatter = team.num_of_batters() supp_out = 0 # supposed out pitcher = pitchers[0] while(True): pa = team.order_table[order][turn][1] # change pitcher if( pa.change_pitcher != None ): no = pa.change_pitcher # find whether pitcher had been on field before is_new_pitcher = True for p in pitchers: if p.number == no: pitcher = p is_new_pitcher = False break if( is_new_pitcher ): pitcher = rdPitcher(no) pitchers.append( pitcher ) err = pitcher.AddPa(pa, isER) if( err != "" ): break if( pa.endInning == '!'): # end of game break supp_out += pa.out if( pa.result == "E" ): supp_out += 1 if( supp_out >= 3 ): isER = False if( pa.endInning == '#' ): # change inning supp_out = 0 isER = True order += 1 if( order == nOrder ): order = 0 turn += 1 return err
def newgame(request): if request.method != 'POST': teams = Team.objects.all() context = {'teams':teams} return render (request , 'sbleague/newgame.html',context) else : teams = Team.objects.all() home_teamID = request.POST.get("hometeamID", "") away_teamID = request.POST.get("awayteamID", "") hometeam = Team.objects.get(pk=home_teamID) awayteam = Team.objects.get(pk=away_teamID) homeplayer = Member.objects.filter(team = home_teamID).order_by("number") awayplayer = Member.objects.filter(team = away_teamID).order_by("number") date = request.POST.get("date", "") location = request.POST.get("location", "") game_id = request.POST.get("game_id", "") ############################################################################# away_record = request.POST.get("away_rd", "") home_record = request.POST.get("home_rd", "") record = None record_err = "" # ===== record parser if( len(away_record) and len(home_record) ): awayteam_name = awayteam.name.encode('utf8')[0:6] hometeam_name = hometeam.name.encode('utf8')[0:6] away_table = text_to_table(away_record.encode('utf8')) home_table = text_to_table(home_record.encode('utf8')) record, record_err = parse_game_record(awayteam_name, None, away_table, hometeam_name, None, home_table) record.game_type = "台大慢壘聯盟" record.date = date record.location = location record.game_id = game_id record.away.raw_record = away_record.encode('utf8') record.home.raw_record = home_record.encode('utf8') else: if( len(away_record) == 0 ): record_err = "Away 沒有記錄" else: record_err = "Home 沒有記錄" ############################################################################# if( date == u'' ): err_message = "請輸入日期" context = {'teams': teams, 'awayteam': awayteam, 'hometeam': hometeam, 'date': date, 'location': location, 'game_id': game_id, 'away_record': away_record, 'home_record': home_record, 'warning': err_message} return render(request, 'sbleague/newgame.html', context) if( game_id == u'' ): err_message = "請輸入場次編號" context = {'teams': teams, 'awayteam': awayteam, 'hometeam': hometeam, 'date': date, 'location': location, 'game_id': game_id, 'away_record': away_record, 'home_record': home_record, 'warning': err_message} return render(request, 'sbleague/newgame.html', context) game_exist = True try: new = Game.objects.get(gameID=game_id) except Game.DoesNotExist: # --- add new game game_exist = False max_batter_nums = 25 max_pitcher_nums = 5 if( record != None and record_err == ""): # --- append batter_num to 25 and pitcher_num to 5 if( record.away.nBatters < max_batter_nums ): for i in range(max_batter_nums-record.away.nBatters): record.away.batters.append(rdBatter()) if( record.home.nBatters < max_batter_nums ): for i in range(max_batter_nums-record.home.nBatters): record.home.batters.append(rdBatter()) if( record.away.nPitchers < max_pitcher_nums ): for i in range(max_pitcher_nums-record.away.nPitchers): record.away.pitchers.append(rdPitcher()) if( record.home.nPitchers < max_pitcher_nums ): for i in range(max_pitcher_nums-record.home.nPitchers): record.home.pitchers.append(rdPitcher()) if( game_exist ): err_message = "重複的場次編號" context = {'teams': teams, 'awayteam': awayteam, 'hometeam': hometeam, 'date': date, 'location': location, 'game_id': game_id, 'away_record': away_record, 'home_record': home_record, 'warning': err_message} return render(request, 'sbleague/newgame.html', context) # === record error if( record_err != "" ): err_message = record_err context = {'teams': teams, 'awayteam': awayteam, 'hometeam': hometeam, 'date': date, 'location': location, 'game_id': game_id, 'away_record': away_record, 'home_record': home_record, 'warning': err_message} return render(request, 'sbleague/newgame.html', context) # === success add new game if 'send' in request.POST: # --- send data context = {'hometeam': hometeam, 'awayteam': awayteam, 'homeplayer': homeplayer ,'awayplayer': awayplayer, 'date': date, 'location': location , 'game_id': game_id, 'home_away': range(2), 'max_batter_nums': max_batter_nums, 'max_pitcher_nums': max_pitcher_nums, 'record': record} return render(request, 'sbleague/newgame_detail.html', context) elif 'download' in request.POST: filename = '%d.txt' %int(game_id) filepath = 'sbleague/static/txt/%s' %filename with open(filepath, 'w') as f: f.write(record.post_ptt) print "save %s" %filepath response = HttpResponse(FileWrapper( file(filepath) ), content_type=mimetypes.guess_type(filepath)[0] ) response['Content-Disposition'] = 'attachment; filename=%s' %filename response['Content-Length'] = os.path.getsize(filepath) return response else: # --- preview err_message = "preview" context = {'teams': teams, 'awayteam': awayteam, 'hometeam': hometeam, 'date': date, 'location': location, 'game_id': game_id, 'away_record': away_record, 'home_record': home_record, 'warning': err_message, 'record': record, 'preview': True} return render(request, 'sbleague/newgame.html', context)
def newgame(request): if request.method != 'POST': teams = Team.objects.all() context = {'teams': teams} return render(request, 'sbleague/newgame.html', context) else: teams = Team.objects.all() home_teamID = request.POST.get("hometeamID", "") away_teamID = request.POST.get("awayteamID", "") hometeam = Team.objects.get(pk=home_teamID) awayteam = Team.objects.get(pk=away_teamID) homeplayer = Member.objects.filter(team=home_teamID).order_by("number") awayplayer = Member.objects.filter(team=away_teamID).order_by("number") date = request.POST.get("date", "") location = request.POST.get("location", "") game_id = request.POST.get("game_id", "") ############################################################################# away_record = request.POST.get("away_rd", "") home_record = request.POST.get("home_rd", "") record = None record_err = "" # ===== record parser if (len(away_record) and len(home_record)): awayteam_name = awayteam.name.encode('utf8')[0:6] hometeam_name = hometeam.name.encode('utf8')[0:6] away_table = text_to_table(away_record.encode('utf8')) home_table = text_to_table(home_record.encode('utf8')) record, record_err = parse_game_record(awayteam_name, None, away_table, hometeam_name, None, home_table) record.game_type = "台大慢壘聯盟" record.date = date record.location = location record.game_id = game_id record.away.raw_record = away_record.encode('utf8') record.home.raw_record = home_record.encode('utf8') else: if (len(away_record) == 0): record_err = "Away 沒有記錄" else: record_err = "Home 沒有記錄" ############################################################################# if (date == u''): err_message = "請輸入日期" context = { 'teams': teams, 'awayteam': awayteam, 'hometeam': hometeam, 'date': date, 'location': location, 'game_id': game_id, 'away_record': away_record, 'home_record': home_record, 'warning': err_message } return render(request, 'sbleague/newgame.html', context) if (game_id == u''): err_message = "請輸入場次編號" context = { 'teams': teams, 'awayteam': awayteam, 'hometeam': hometeam, 'date': date, 'location': location, 'game_id': game_id, 'away_record': away_record, 'home_record': home_record, 'warning': err_message } return render(request, 'sbleague/newgame.html', context) game_exist = True try: new = Game.objects.get(gameID=game_id) except Game.DoesNotExist: # --- add new game game_exist = False max_batter_nums = 25 max_pitcher_nums = 5 if (record != None and record_err == ""): # --- append batter_num to 25 and pitcher_num to 5 if (record.away.nBatters < max_batter_nums): for i in range(max_batter_nums - record.away.nBatters): record.away.batters.append(rdBatter()) if (record.home.nBatters < max_batter_nums): for i in range(max_batter_nums - record.home.nBatters): record.home.batters.append(rdBatter()) if (record.away.nPitchers < max_pitcher_nums): for i in range(max_pitcher_nums - record.away.nPitchers): record.away.pitchers.append(rdPitcher()) if (record.home.nPitchers < max_pitcher_nums): for i in range(max_pitcher_nums - record.home.nPitchers): record.home.pitchers.append(rdPitcher()) if (game_exist): err_message = "重複的場次編號" context = { 'teams': teams, 'awayteam': awayteam, 'hometeam': hometeam, 'date': date, 'location': location, 'game_id': game_id, 'away_record': away_record, 'home_record': home_record, 'warning': err_message } return render(request, 'sbleague/newgame.html', context) # === record error if (record_err != ""): err_message = record_err context = { 'teams': teams, 'awayteam': awayteam, 'hometeam': hometeam, 'date': date, 'location': location, 'game_id': game_id, 'away_record': away_record, 'home_record': home_record, 'warning': err_message } return render(request, 'sbleague/newgame.html', context) # === success add new game if 'send' in request.POST: # --- send data context = { 'hometeam': hometeam, 'awayteam': awayteam, 'homeplayer': homeplayer, 'awayplayer': awayplayer, 'date': date, 'location': location, 'game_id': game_id, 'home_away': range(2), 'max_batter_nums': max_batter_nums, 'max_pitcher_nums': max_pitcher_nums, 'record': record } return render(request, 'sbleague/newgame_detail.html', context) elif 'download' in request.POST: filename = '%d.txt' % int(game_id) filepath = 'sbleague/static/txt/%s' % filename with open(filepath, 'w') as f: f.write(record.post_ptt) print "save %s" % filepath response = HttpResponse( FileWrapper(file(filepath)), content_type=mimetypes.guess_type(filepath)[0]) response[ 'Content-Disposition'] = 'attachment; filename=%s' % filename response['Content-Length'] = os.path.getsize(filepath) return response else: # --- preview err_message = "preview" context = { 'teams': teams, 'awayteam': awayteam, 'hometeam': hometeam, 'date': date, 'location': location, 'game_id': game_id, 'away_record': away_record, 'home_record': home_record, 'warning': err_message, 'record': record, 'preview': True } return render(request, 'sbleague/newgame.html', context)