def post(self): try: jsondata = json.loads(self.request.body) except UnicodeDecodeError: jsondata = json.loads(self.request.body,encoding='latin-1') formkey = ndb.Key(urlsafe=jsondata['form_key']) form = formkey.get() #if authenticated if bool(jsondata['auth']): session = Session.last_from_username(jsondata['user']) avatar = session[0].avatar else: avatar = 'http://betweetdotnet.appspot.com/img/default.jpg' bet = Bet(parent = formkey, description = form.description, user = jsondata['user'], fields = jsondata['fields'], authenticated = bool(jsondata['auth']), avatar = avatar) bet.put()
def parse_row(self, a, row): safea = a.string.encode('ascii', 'replace') score = re.search("[0-9]\-[0-9]", safea) source = self.url if score is not None: current_score = score.group() teams = safea.partition(' %s ' % current_score) else: teams = safea.partition(' vs ') team_a = teams[0] team_b = teams[2] match_date = row.td.a if match_date is None: match_date = row.td.div match_date = match_date['title'] # empty rows: team_a_raw_odds = row.find('td', {'class' : 'odds'}) if team_a_raw_odds == None: raise UnparsableException("Odds not found found for first team") return 0 team_b_raw_odds = row.find('td', {'class' : 'odds noBorder '}) if team_b_raw_odds == None: return 0 draw_raw_odds = row.find('td', {'class' : 'odds noBorder'}) if draw_raw_odds == None: return 0 team_a_odds = self.calc_odds(team_a_raw_odds.a.string.strip().encode('ascii', 'replace')) team_b_odds = self.calc_odds(team_b_raw_odds.a.string.strip().encode('ascii', 'replace')) draw_odds = self.calc_odds(draw_raw_odds.a.string.strip().encode('ascii', 'replace')) matchup = Matchup.all().filter('team_a_name = ', team_a).filter('team_b_name', team_b) result = matchup.fetch(1); date_match_formatted = parse_date(match_date.split()) if len(result) == 0: matchup = Matchup(team_a_name = team_a, team_b_name = team_b, source = source, date_match = match_date, date_match_formatted = date_match_formatted ) matchup.put() else: matchup = result[0] bet = Bet(team_a_odds = team_a_odds, team_b_odds = team_b_odds, draw_odds = draw_odds, match = matchup ) bet.put()
def post(self): request_data = json.loads(self.request.body) logging.info(request_data) player = current_user_player() match_key = ndb.Key(Match, int(request_data['match_id'])) match = match_key.get() match_player = MatchPlayer.query(MatchPlayer.match == match_key, MatchPlayer.player == player.key).get() if match.state() == "finished": error_400(self.response, "ERROR_FINISHED_GAME", "You can not bet on a finished game.") return if not validate_request_data(self.response, request_data, ['match_id', 'bet']): return if not match_player: error_400(self.response, "ERROR_NOT_OWN_GAME", "You have to bet on your own game.") return if request_data['bet']['id']: bet = Bet.get_by_id(int(request_data['bet']['id'])) else: bet = Bet(match=match_key, player=player.key, winning_faction=match_player.faction) new_bet_value = int(request_data['bet']['value']) bet_value_dif = new_bet_value - bet.value if bet_value_dif < 0: error_400(self.response, "ERROR_BET_LOWER", "Your new bet %s is lower than your previous bet %s" % (new_bet_value, bet.value)) return if bet_value_dif > player.cash: error_400(self.response, "ERROR_NOT_ENOUGH_CASH", "Not enough cash. Your player has %s cash and your new bet requires %s" % (player.cash, bet_value_dif)) return bet.value = new_bet_value bet.put() player.cash -= bet_value_dif player.put() client_object_path = "player.matches.[%s].bets.[%s]" % (match_key.id(), bet.key.id()) match.websocket_notify_players("Match_UpdatedOrNewBet", client_object_path, bet.get_data(), player.key) set_json_response(self.response, {'bet': bet.get_data(), 'cash': player.cash})