Esempio n. 1
0
 def get(self,game_id):
   check_admin(self)
   game=Game.get_by_id(int(game_id))
   players=Player.all().filter('active =',True).order('surname').fetch(1000)
   fielders=PlayerGame.all().filter('game =',game)
   batting_order=sorted(fielders,key=lambda p: p.batting_position)
   bowling_order=sorted(fielders,key=lambda p: p.bowling_position)
   template_values={'user_meta':get_meta(),'game':game,'players':players,'dismissal_types':dismissal_types,'batting_order':batting_order,'bowling_order':bowling_order,
     'fielders':fielders}
   template=jinja_environment.get_template('templates/edit_game.html')
   self.response.out.write(template.render(template_values))
Esempio n. 2
0
 def get(self, game_id=None):
     user_meta = get_meta()
     if not user_meta:
         self.redirect("/")
     game = Game.get_by_id(int(game_id))
     if not game_id:
         game = next_game()
     players = game.playergame_set
     template_values = {
         "game": game,
         "user_meta": user_meta,
         "batsmen": sorted([b for b in players if b.batted], key=lambda player: player.batting_position),
         "bowlers": sorted([b for b in players if b.bowled], key=lambda player: player.bowling_position),
         "fielders": players,
     }
     if check_mobile():
         template = jinja_environment.get_template("templates/mobile/game.html")
     else:
         template = jinja_environment.get_template("templates/game.html")
     self.response.out.write(template.render(template_values))
Esempio n. 3
0
 def post(self):
   players={}
   check_admin(self)
   game=Game.get_by_id(int(self.request.get('game')))
   batsmen=[]
   bowlers=[]
   fielders=[]
   
   # TODO: convert to a background task
       
   # Check Duplicates
   for i in range(1,13):
     if self.request.get('batsman-'+str(i)+'-batted'):
       batsmen.append(self.request.get('batsman-'+str(i)))
     if self.request.get('bowler-'+str(i)+'-bowled'):
       bowlers.append(self.request.get('bowler-'+str(i)))
     if self.request.get('fielder-'+str(i)) != '0':
       fielders.append(self.request.get('fielder-'+str(i)))
   
   if len(set(batsmen)) != len(batsmen):
     self.response.out.write("Error. Duplicate batsmen")
     return
   if len(set(bowlers)) != len(bowlers):
     self.response.out.write("Error. Duplicate bowlers")
     return
   if len(set(fielders)) != len(fielders):
     self.response.out.write("Error. Duplicate fielders")
     return
   
   memcache.delete('horse')
   pg=None
   # batting
   for i in range(1,12):
     si='batsman-'+str(i)
     if not self.request.get(si+'-batted') or self.request.get(si) == '0':
       continue
     p_id=self.request.get(si)
     try:
       pg=players[p_id]
     except:
       player=Player.get_by_id(int(p_id))
       pg=PlayerGame(player=player,game=game)
       players[p_id]=pg
     pg.batted=True
     if self.request.get(si+'-not_out'):
       pg.not_out=True
     else:
       pg.not_out=False
     pg.runs=int(self.request.get(si+'-runs',default_value="0"))
     pg.balls_faced=int(self.request.get(si+'-balls',default_value="0"))
     pg.fours_hit=int(self.request.get(si+'-fours',default_value="0"))
     pg.sixes_hit=int(self.request.get(si+'-sixes',default_value="0"))
     if not pg.not_out:
       pg.how_out=db.Category(self.request.get(si+'-how_out').replace('-',' '))
     else:
       pg.how_out=None
     pg.batting_position=i
   
   # bowling
   total_conceded=0
   for i in range(1,12):
     si='bowler-'+str(i)
     if not self.request.get(si+'-bowled') or self.request.get(si) == '0':
       continue
     p_id=self.request.get(si)
     try:
       pg=players[p_id]
     except:
       player=Player.get_by_id(int(p_id))
       pg=PlayerGame(player=player,game=game)
       players[p_id]=pg
     pg.bowled=True
     overs_tokens=self.request.get(si+'-overs',default_value="0.0").split('.')
     pg.overs=int(overs_tokens[0])
     if len(overs_tokens) > 1:
       pg.balls=int(overs_tokens[1])
     else:
       pg.balls=0
     pg.maidens=int(self.request.get(si+'-maidens',default_value="0"))
     pg.runs_conceded=int(self.request.get(si+'-runs',default_value="0"))
     total_conceded+=pg.runs_conceded
     pg.wickets=int(self.request.get(si+'-wickets',default_value="0"))
     pg.wides=int(self.request.get(si+'-wides',default_value="0"))
     pg.no_balls=int(self.request.get(si+'-no_balls',default_value="0"))
     pg.fours=int(self.request.get(si+'-fours',default_value="0"))
     pg.sixes=int(self.request.get(si+'-sixes',default_value="0"))
     pg.bowling_position=i
   
   # fielding
   for i in range(1,13):
     si='fielder-'+str(i)
     if self.request.get(si)=='0':
       continue
     p_id=self.request.get(si)
     try:
       pg=players[p_id]
     except:
       player=Player.get_by_id(int(p_id))
       pg=PlayerGame(player=player,game=game)
       players[p_id]=pg
     pg.catches=int(self.request.get(si+'-catches',default_value="0"))
     pg.drops=int(self.request.get(si+'-drops',default_value="0"))
     pg.diving_drops=int(self.request.get(si+'-diving',default_value="0"))
     pg.non_attempts=int(self.request.get(si+'-non_attempts',default_value="0"))
     pg.run_outs=int(self.request.get(si+'-run_outs',default_value="0"))
     pg.misfields=int(self.request.get(si+'-misfields',default_value="0"))
     pg.other=int(self.request.get(si+'-other',default_value="0"))
     
   # TODO: Validation
   for pg in game.playergame_set:
     pg.delete()
   
   bat_total=0
   bowl_total=0
   bat_scores={}
   bowl_scores={}
   for key,pg in players.iteritems():
     bat_scores[key]=batting_score(pg)
     bat_total+=bat_scores[key]
     bowl_scores[key]=bowling_score(pg,total_conceded)
     bowl_total+=bowl_scores[key]
     pg.fielding_points=fielding_score(pg)
 
   bat_factor = 1
   bowl_factor = 1
   if bat_total > 0:    
     bat_factor = 100.0/bat_total
   if bowl_total > 0:
     if game.overs == 20:
       bowl_factor = 80.0/bowl_total
     else:
       bowl_factor = 100.0/bowl_total
   for key,pg in players.iteritems():
     pg.batting_points=int(round(bat_scores[key]*bat_factor))
     pg.bowling_points=int(round(bowl_scores[key]*bowl_factor))
     pg.put()
     
   completed=self.request.get('completed')
   if completed:
     game_completed(game)
     for key,pg in players.iteritems():
       p=pg.player
       update_prices(p)
       p.put()
   else:
     game.played=False
   game.opposition=self.request.get('opposition')
   game.score=self.request.get('dedication-score')
   game.opposition_score=self.request.get('opposition-score')
   game.result=self.request.get('result')
   game.put()
   
   self.redirect('/admin')