def put(self): parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('id', type=str, required=True, help="Id of the status cannot be missing!", location='json') parser.add_argument('name', type=str, required=True, help="Name of the status cannot be missing!", location='json') parser.add_argument('lock', type=bool, required=True, help="Lock cannot be missing!", location='json') args = parser.parse_args() try: update_status = """ UPDATE STATUS SET NAME = %s, LOCK = %s WHERE ID = %s """ params = (args['name'], args['lock'], args['id']) row_status = writeSQL(update_status, params) if row_status: return {'success': True}, 200 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def put(self): try: parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('id', type=str, required=True, help="match id cannot be missing!", location='json') parser.add_argument('team_a', type=int, required=True, help="team id a cannot be missing!", location='json') parser.add_argument('team_b', type=int, required=True, help="team id b cannot be missing!", location='json') parser.add_argument('toss', type=int, required=True, help="toss cannot be missing!", location='json') args = parser.parse_args() update_match= """ UPDATE MATCH SET TEAM_A = %s, TEAM_B = %s, TOSS = %s WHERE ID = %s """ params = (args['team_a'], args['team_b'], args['toss'], args['id']) update = writeSQL(update_match, params) if update: query_match_toss= """ select match.team_a, match.team_b, match.toss, teama.name as team_a_name, teamb.name as team_b_name from "match" join team as teama on match.team_a = teama.id join team as teamb on match.team_b = teamb.id where match.id = %s """ params = (args['id'],) row_match = readSQL(query_match_toss, params) if row_match: teamA_name = row_match['team_a_name'] teamB_name = row_match['team_b_name'] match_status = '' if row_match['toss'] == args['team_a']: if row_match['team_a'] == args['team_a']: match_status = row_match['team_a_name']+" won the toss and elected to bat first" else: match_status = row_match['team_a_name']+" won the toss and elected to field first" elif row_match['toss'] == args['team_b']: if row_match['team_b'] == args['team_a']: match_status = row_match['team_b_name']+" won the toss and elected to bat first" else: match_status = row_match['team_b_name']+" won the toss and elected to field first" inn_key = "match:"+str(args['id'])+":innings" live_key_1 = "livescorecard:"+str(args['id'])+":current:1" live_val_1 = json.dumps({'over': "0.0", 'ball': 0, 'match_status': match_status}) live_key_2 = "livescorecard:"+str(args['id'])+":current:2" live_val_2 = json.dumps({'over': "0.0", 'ball': 0, 'match_status': 'Innings 2 is in progress'}) quick_key_1 = "quickscorecard:"+str(args['id'])+":current:1" quick_val_1 = json.dumps({'score': "0/0 (0.0)", 'team': teamA_name}) quick_key_2 = "quickscorecard:"+str(args['id'])+":current:2" quick_val_2 = json.dumps({'score': "0/0 (0.0)", 'team': teamB_name}) app.redis.set(inn_key, 1) app.redis.set(live_key_1, live_val_1) app.redis.set(live_key_2, live_val_2) app.redis.set(quick_key_1, quick_val_1) app.redis.set(quick_key_2, quick_val_2) return {'success': True }, 200 else: return {'success': False, 'message': 'no match found' }, 404 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error" }, 500
def post(self): parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('name', type=str, required=True, help="Name of the match cannot be missing!", location='json') parser.add_argument('type', type=str, required=True, help="Type cannot be missing!", location='json') parser.add_argument('startdate', type=str, required=True, help="Start date cannot be missing!", location='json') parser.add_argument('enddate', type=str, required=True, help="End date cannot be missing!", location='json') parser.add_argument('venue', type=str, required=True, help="Venue cannot be missing!", location='json') parser.add_argument('created', type=str, required=True, help="Created date cannot be missing!", location='json') parser.add_argument('modified', type=str, required=True, help="Modified date cannot be missing!", location='json') parser.add_argument('teams', type=list, required=True, help="Modified date cannot be missing!", location='json') args = parser.parse_args() try: query_matchtype= """ SELECT G_ID FROM MATCHTYPE ORDER BY ID DESC LIMIT 1 """ row_grp_id = readSQL(query_matchtype, None) if row_grp_id: if(row_grp_id['g_id']): grp_id = row_grp_id['g_id'] + 1 else: grp_id = 1 insert_matchtype= """ INSERT INTO MATCHTYPE (NAME, TYPE, START_DATE, END_DATE, G_ID, VENUE, CREATED, MODIFIED) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) """ params = (args['name'], args['type'], args['startdate'], args['enddate'], grp_id, args['venue'], args['created'], args['modified']) write_matchtype = writeSQL(insert_matchtype, params) if write_matchtype: for team in args['teams']: insert_points= """ INSERT INTO POINTS (TEAM_ID, G_ID, CREATED, MODIFIED) VALUES (%s, %s, %s, %s) """ params = (team, grp_id, getDatetime(), getDatetime()) writeSQL(insert_points, params) return {'success': 'true', 'message': 'match type created'}, 200 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error" }, 500
def put(self): try: parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('name', type=str, required=True, help="Name of the team cannot be missing!", location='json') parser.add_argument('logo', type=str, required=True, help="Logo cannot be missing!", location='json') parser.add_argument('shortname', type=str, required=True, help="Short name cannot be missing!", location='json') parser.add_argument('country', type=str, required=True, help="Country cannot be missing!", location='json') parser.add_argument('status', type=str, required=True, help="Status cannot be missing!", location='json') parser.add_argument('id', type=str, required=True, help="id cannot be missing!", location='json') args = parser.parse_args() query_insert = """ UPDATE TEAM SET NAME = %s, LOGO = %s, SHORTNAME = %s, C_ID = %s, STATUS = %s WHERE ID = %s """ params = (args['name'], args['logo'], args['shortname'], args['country'], args['status'], args['id']) teams = writeSQL(query_insert, params) if teams: return { 'success': True, 'message': 'Updated successfully' }, 200 else: return {'sucess': False}, 400 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def post(self): try: parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('name', type=str, required=True, help="Name of the team cannot be missing!", location='json') parser.add_argument('logo', type=str, required=True, help="Logo cannot be missing!", location='json') parser.add_argument('shortname', type=str, required=True, help="Short name cannot be missing!", location='json') parser.add_argument('country', type=str, required=True, help="Country cannot be missing!", location='json') parser.add_argument('status', type=str, required=True, help="Status cannot be missing!", location='json') args = parser.parse_args() query_insert = """ INSERT INTO TEAM (NAME, LOGO, SHORTNAME, C_ID, STATUS) VALUES (%s, %s, %s, %s, %s) """ params = (args['name'], args['logo'], args['shortname'], args['country'], args['status']) teams = writeSQL(query_insert, params) if teams: return { 'success': True, 'message': 'Successfully created' }, 200 else: return {'sucess': False}, 400 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def post(self): parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('name', type=str, required=True, help="Name cannot be blank!", location='json') parser.add_argument('username', type=str, required=True, help="Username cannot be blank!", location='json') parser.add_argument('password', type=str, required=True, help="password cannot be blank!", location='json') args = parser.parse_args() try: if args and ('name' in args and 'username' in args and 'password' in args): name = args['name'] username = args['username'] password = args['password'] if not (username is None and password is None): query = """ INSERT INTO USERS (NAME, USERNAME, PASSWORD) VALUES (%s, %s, %s) """ hashed_password = app.bcrypt.generate_password_hash( password.encode('utf-8')) params = (name, username, hashed_password.decode('utf8')) count = writeSQL(query, params) if count: return {'success': True}, 200 else: return {'sucess': False}, 400 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def delete(self): try: args = request.args if 'id' in args: delete_status = """ DELETE FROM STATUS WHERE ID = %s """ params = (args['id'], ) row_status = writeSQL(delete_status, params) if row_status: return {'success': True}, 200 else: return {'success': False, 'message': 'no data found'}, 404 else: return { 'success': False, 'message': 'missing required parameter' }, 400 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def post(self): parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('name', type=str, required=True, help="Name of the status cannot be missing!", location='json') parser.add_argument('lock', type=bool, required=True, help="lock cannot be missing!", location='json') parser.add_argument('match_id', type=int, required=True, help="Match ID cannot be missing!", location='json') args = parser.parse_args() try: insert_status = """ INSERT INTO STATUS (NAME, LOCK) VALUES (%s, %s) """ params = (args['name'], args['lock']) row_status = writeSQL(insert_status, params) if row_status: get_curr_inng = app.redis.get("match:" + str(args['match_id']) + ":innings") if get_curr_inng: get_inng = json.loads(get_curr_inng) curr_key = "livescorecard:" + str( args['match_id']) + ":current:" + str(get_inng) curr_livecard = json.loads(app.redis.get(curr_key)) curr_livecard['match_status'] = args['name'] update_status = app.redis.set(curr_key, json.dumps(curr_livecard)) if update_status: #manual commentry list_com = "list:" + str( args['match_id']) + ":" + str(get_inng) man_com_key = "commentry:" + str( args['match_id']) + ":" + str(get_inng) + ":manual" obj = {} obj['comment'] = args['name'] obj['datetime'] = datetime.utcnow() if (app.redis.hkeys(man_com_key)): index_val = [ val for loc, val in enumerate( app.redis.hkeys(man_com_key)) if math.floor(float(val)) == curr_livecard['ball'] ] if (index_val): max_val = float(max(index_val)) field_key = format(max_val + 0.1, ".1f") else: field_key = curr_livecard['ball'] + 0.1 else: field_key = curr_livecard['ball'] + 0.1 list_man_com_key = man_com_key + ":" + str(field_key) app.redis.lpush(list_com, list_man_com_key) app.redis.hset(man_com_key, field_key, json.dumps(obj, default=default)) return {'success': True}, 200 else: return { 'success': False, 'message': 'no match id found' }, 404 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def put(self): parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('match_id', type=int, required=True, help="Match id cannot be blank!", location='json') parser.add_argument('won', type=int, required=True, help="Winning team cannot be blank!", location='json') parser.add_argument('team_a', type=int, required=True, help="Team A cannot be blank!", location='json') parser.add_argument('team_b', type=int, required=True, help="Team B cannot be blank!", location='json') parser.add_argument('team_a_score', type=int, required=True, help="Team A Score cannot be blank!", location='json') parser.add_argument('team_b_score', type=int, required=True, help="Team B Score cannot be blank!", location='json') parser.add_argument('team_a_name', type=str, required=True, help="Team A name cannot be blank!", location='json') parser.add_argument('team_b_name', type=str, required=True, help="Team B name cannot be blank!", location='json') parser.add_argument('team_b_wicket', type=int, required=True, help="Team B wicket cannot be blank!", location='json') parser.add_argument('nr', type=bool, location='json') parser.add_argument('superover', type=bool, location='json') args = parser.parse_args() try: match_id = args['match_id'] won = args['won'] nr = args['nr'] superover = args['superover'] team_a = args['team_a'] team_a_score = args['team_a_score'] team_b = args['team_b'] team_b_score = args['team_b_score'] team_b_wicket = args['team_b_wicket'] team_a_name = args['team_a_name'] team_b_name = args['team_b_name'] if (superover or nr): if (nr): margin_by_runs = None description = "Match tied" margin_by_wickets = None elif (superover): margin_by_runs = None description = "Match won in super over" margin_by_wickets = None elif (won == team_a): margin_by_runs = team_a_score - team_b_score description = team_a_name + " won by " + str( margin_by_runs) + " runs" margin_by_wickets = None elif (won == team_b): margin_by_wickets = 10 - team_b_wicket description = team_b_name + " won by " + str( margin_by_wickets) + " wickets" margin_by_runs = None query = """ INSERT INTO RESULT (WON, NR, SUPEROVER, MARGIN_BY_RUNS, MARGIN_BY_WICKETS, TEAM_A, TEAM_A_SCORE, TEAM_B, TEAM_B_SCORE, TEAM_B_WICKET, DESCRIPTION) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING ID; """ params = (won, nr, superover, margin_by_runs, margin_by_wickets, team_a, team_a_score, team_b, team_b_score, team_b_wicket, description) last_id = writeFetchSQL(query, params) if last_id: query_match = """ UPDATE MATCH SET RESULT_ID = %s, STATUS = 'PAST' WHERE ID = %s """ params = (last_id, match_id) count = writeSQL(query_match, params) if count: get_curr_inng = app.redis.get("match:" + str(match_id) + ":innings") get_inng = json.loads(get_curr_inng) curr_key = "livescorecard:" + str( match_id) + ":current:" + str(get_inng) curr_livecard = json.loads(app.redis.get(curr_key)) curr_livecard['match_status'] = description app.redis.set(curr_key, json.dumps(curr_livecard)) return {'success': True}, 200 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def get(self): try: args = request.args get_curr_inng = app.redis.get("match:" + str(args['matchid']) + ":innings") get_inng = json.loads(get_curr_inng) # postgres db query_playerscorecard = """ SELECT * FROM PLAYERSCORECARD WHERE MATCH_ID = %s AND INNINGS = %s ORDER BY ID desc limit 1 """ params = (args['matchid'], get_inng) row_playerscorecard = readSQL(query_playerscorecard, params) if row_playerscorecard: del_playerscorecard = """ DELETE FROM PLAYERSCORECARD WHERE ID = %s """ params = (row_playerscorecard['id'], ) del_record = writeSQL(del_playerscorecard, params) prv_playerscorecard = """ SELECT * FROM PLAYERSCORECARD WHERE ID = %s AND MATCH_ID = %s """ prv_id = row_playerscorecard['id'] - 1 params = (prv_id, args['matchid']) prv_record = readSQL(prv_playerscorecard, params) # ball_id = prv_playerscorecard['ball_id'] if prv_record and 'team_a_over' in prv_record and 'team_b_over' in prv_record: team_a_over = prv_record['team_a_over'] team_b_over = prv_record['team_b_over'] else: team_a_over = "0.0" team_b_over = "0.0" if del_record: #redis if get_inng == 1: over = team_a_over else: over = team_b_over curr_key = "livescorecard:" + str( args['matchid']) + ":current:" + str(get_inng) com_key = "list:" + str( args['matchid']) + ":" + str(get_inng) curr_livecard = app.redis.get(curr_key) key_info = json.loads(curr_livecard) get_list = app.redis.lrange(com_key, 0, -1) count_len = app.redis.llen(com_key) - 3 for key in get_list: actual_key = key splited_key = key.split(':') if (splited_key[3] == 'manual' and math.floor( float(splited_key[4])) == key_info['ball']): app.redis.lrem(com_key, count_len, actual_key) elif (splited_key[3].isdigit() and math.floor( float(splited_key[3])) == key_info['ball']): app.redis.lrem(com_key, count_len, actual_key) count_len = count_len - 1 key_info['over'] = str(over) key_info['ball'] = key_info['ball'] - 1 app.redis.set(curr_key, json.dumps(key_info)) return { 'success': True, 'message': 'sucessfully deleted the last record' }, 200 else: return { 'success': False, 'message': 'no record to delete' }, 200 except Exception as e: print(e) app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def post(self): parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('name', type=str, required=True, help="Name of the match cannot be missing!", location='json') parser.add_argument('date', type=str, required=True, help="Date cannot be missing!", location='json') parser.add_argument('venue', type=str, required=True, help="Venue cannot be missing!", location='json') parser.add_argument('format', type=int, required=True, help="Format cannot be missing!", location='json') parser.add_argument('team_squad_1', type=list, required=True, help="Team squad 1 cannot be missing!", location='json') parser.add_argument('team_squad_2', type=list, required=True, help="Team squad 2 cannot be missing!", location='json') parser.add_argument('team_a', type=int, required=True, help="Team A cannot be missing!", location='json') parser.add_argument('team_b', type=int, required=True, help="Team B cannot be missing!", location='json') parser.add_argument('match_type', type=int, required=True, help="Match Type cannot be missing!", location='json') parser.add_argument('result', type=int, location='json') parser.add_argument('country', required=True, type=int, help="Country cannot be missing!", location='json') parser.add_argument('mom', type=int, location='json') parser.add_argument('match_status', type=str, required=True, location='json') parser.add_argument('description', type=str, required=True, help="Description cannot be missing!", location='json') args = parser.parse_args() try: #get format 'id' for the match row_format = getMatchFormat(args) if row_format is None: return {'success': False, 'message': 'format not found'}, 404 #get match type 'id' for the match row_match_type = getMatchType(args) if row_match_type is None: return { 'success': False, 'message': 'match type not found' }, 404 #get country 'id' for the match row_country = getCountry(args) if row_country is None: return {'success': False, 'message': 'country not found'}, 404 #get team squad ids for the match try: if args and ('team_squad_1' in args and 'team_squad_2' in args): query_squad = """ SELECT TEAM_SQUAD_1, TEAM_SQUAD_2 FROM squad ORDER BY ID desc limit 1 """ row_squad = readSQL(query_squad, None) if row_squad is None: t_id_1 = 1 t_id_2 = 2 else: t_id_1 = row_squad['team_squad_1'] + 2 t_id_2 = row_squad['team_squad_2'] + 2 insert_squad = """ INSERT INTO SQUAD (TEAM_SQUAD_1, TEAM_SQUAD_2) VALUES (%s, %s) """ params = (t_id_1, t_id_2) writeSQL(insert_squad, params) # insert team 1 players in team squad for player in args['team_squad_1']: query_team_squad_1 = """ INSERT INTO TEAMSQUAD (PLAYER_ID, TEAM_ID, T_ID_1, T_ID_2, SELECTED, POSITION, PLAYER_IN) VALUES (%s, %s, %s, %s, %s, %s, %s) """ params = (player['player'], player['team'], t_id_1, None, player['status'], player['position'], player['status']) writeSQL(query_team_squad_1, params) # insert team 2 players in team squad for player in args['team_squad_2']: query_team_squad_2 = """ INSERT INTO TEAMSQUAD (PLAYER_ID, TEAM_ID, T_ID_1, T_ID_2, SELECTED, POSITION, PLAYER_IN) VALUES (%s, %s, %s, %s, %s, %s, %s) """ params = (player['player'], player['team'], None, t_id_2, player['status'], player['position'], player['status']) writeSQL(query_team_squad_2, params) except Exception as e: query_delete_teamsquad = """ DELETE FROM TEAMSQUAD WHERE T_ID_1 = %s OR T_ID_2 = %s """ params = (t_id_1, t_id_2) writeSQL(query_delete_teamsquad, params) if t_id_1 and t_id_2: query_delete_squad = """ DELETE FROM SQUAD WHERE TEAM_SQUAD_1 = %s OR TEAM_SQUAD_2 = %s """ params = (t_id_1, t_id_2) writeSQL(query_delete_squad, params) app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500 #get score squad ids for the match try: query_score = """ SELECT SCORECARD_ID FROM SCORE ORDER BY ID desc limit 1 """ row_score = readSQL(query_score, None) if row_score is None: s_id = 1 else: s_id = row_score['scorecard_id'] + 1 query = """ INSERT INTO SCORE (SCORECARD_ID) VALUES (%s)""" params = (s_id, ) writeSQL(query, params) except Exception as e: query_delete_teamsquad = """ DELETE FROM TEAMSQUAD WHERE T_ID_1 = %s OR T_ID_2 = %s """ params = (t_id_1, t_id_2) writeSQL(query_delete_teamsquad, params) if t_id_1 and t_id_2: query_delete_squad = """ DELETE FROM SQUAD WHERE TEAM_SQUAD_1 = %s OR TEAM_SQUAD_2 = %s """ params = (t_id_1, t_id_2) writeSQL(query_delete_squad, params) if s_id: query_delete_score = """ DELETE FROM SCORE WHERE SCORECARD_ID = %s """ params = (s_id, ) writeSQL(query_delete_score, params) app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500 #match table insertion try: query_squad = """ SELECT ID FROM SQUAD ORDER BY ID desc limit 1 """ row_squad = readSQL(query_squad, None) query_score = """ SELECT ID FROM SCORE ORDER BY ID desc limit 1 """ row_score = readSQL(query_score, None) query = """ INSERT INTO MATCH (NAME, DATE, VENUE, TEAM_A, TEAM_B, STATUS, SQUAD_ID, FORMAT_ID, MATCHTYPE_ID, SCORE_ID, COUNTRY_ID, DESCRIPTION ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """ params = (args['name'], args['date'], args['venue'], args['team_a'], args['team_b'], args['match_status'], row_squad['id'], row_format['id'], row_match_type['id'], row_score['id'], row_country['id'], args['description']) count = writeSQL(query, params) if count: return { 'success': True, 'message': 'match is created' }, 200 except Exception as e: query_delete_teamsquad = """ DELETE FROM TEAMSQUAD WHERE T_ID_1 = %s OR T_ID_2 = %s """ params = (t_id_1, t_id_2) writeSQL(query_delete_teamsquad, params) if t_id_1 and t_id_2: query_delete_squad = """ DELETE FROM SQUAD WHERE TEAM_SQUAD_1 = %s OR TEAM_SQUAD_2 = %s """ params = (t_id_1, t_id_2) writeSQL(query_delete_squad, params) if s_id: query_delete_score = """ DELETE FROM SCORE WHERE SCORECARD_ID = %s """ params = (s_id, ) writeSQL(query_delete_score, params) app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def put(self): parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('id', type=str, required=True, help="match id cannot be missing!", location='json') parser.add_argument('name', type=str, required=True, help="Name of the match cannot be missing!", location='json') parser.add_argument('date', type=str, required=True, help="Date cannot be missing!", location='json') parser.add_argument('venue', type=str, required=True, help="Venue cannot be missing!", location='json') parser.add_argument('format', type=int, required=True, help="Format cannot be missing!", location='json') parser.add_argument('team_squad_1', type=list, location='json') parser.add_argument('team_squad_2', type=list, location='json') parser.add_argument('team_a', type=int, required=True, help="Team A cannot be missing!", location='json') parser.add_argument('team_b', type=int, required=True, help="Team B cannot be missing!", location='json') parser.add_argument('match_type', type=int, required=True, help="Match Type cannot be missing!", location='json') parser.add_argument('country', required=True, type=int, help="Country cannot be missing!", location='json') parser.add_argument('match_status', type=str, required=True, location='json') parser.add_argument('description', type=str, required=True, help="Description cannot be missing!", location='json') args = parser.parse_args() try: #get format 'id' for the match row_format = getMatchFormat(args) if row_format is None: return {'success': False, 'message': 'format not found'}, 404 #get match type 'id' for the match row_match_type = getMatchType(args) if row_match_type is None: return { 'success': False, 'message': 'match type not found' }, 404 #get country 'id' for the match row_country = getCountry(args) if row_country is None: return {'success': False, 'message': 'country not found'}, 404 #get team squad ids for the match try: if args and ('team_squad_1' in args and args['team_squad_1']): query_squad = """ SELECT * FROM SQUAD WHERE ID = (SELECT SQUAD_ID FROM MATCH WHERE ID = %s) """ params = (args['id'], ) row_squad = readSQL(query_squad, params) # delete team squad query_delete_teamsquad = """ DELETE FROM TEAMSQUAD WHERE T_ID_1 = %s """ params = (row_squad['team_squad_1'], ) writeSQL(query_delete_teamsquad, params) # insert team 1 players in team squad for player in args['team_squad_1']: query_team_squad_1 = """ INSERT INTO TEAMSQUAD (PLAYER_ID, TEAM_ID, T_ID_1, T_ID_2, SELECTED, POSITION, PLAYER_IN) VALUES (%s, %s, %s, %s, %s, %s, %s) """ params = (player['player'], player['team'], row_squad['team_squad_1'], None, player['status'], player['position'], player['status']) writeSQL(query_team_squad_1, params) if args and ('team_squad_2' in args and args['team_squad_2']): query_squad_2 = """ SELECT * FROM SQUAD WHERE ID = (SELECT SQUAD_ID FROM MATCH WHERE ID = %s) """ params = (args['id'], ) row_squad_2 = readSQL(query_squad_2, params) # delete team squad query_delete_teamsquad_2 = """ DELETE FROM TEAMSQUAD WHERE T_ID_2 = %s """ params = (row_squad_2['team_squad_2'], ) writeSQL(query_delete_teamsquad_2, params) # insert team 2 players in team squad for player in args['team_squad_2']: query_team_squad_2 = """ INSERT INTO TEAMSQUAD (PLAYER_ID, TEAM_ID, T_ID_1, T_ID_2, SELECTED, POSITION, PLAYER_IN) VALUES (%s, %s, %s, %s, %s, %s, %s) """ params = (player['player'], player['team'], None, row_squad['team_squad_2'], player['status'], player['position'], player['status']) writeSQL(query_team_squad_2, params) except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500 #match table insertion try: query = """ UPDATE MATCH SET NAME = %s, DATE = %s, VENUE = %s, TEAM_A = %s, TEAM_B = %s, FORMAT_ID = %s, MATCHTYPE_ID = %s, COUNTRY_ID = %s, DESCRIPTION = %s WHERE ID = %s """ params = (args['name'], args['date'], args['venue'], args['team_a'], args['team_b'], row_format['id'], row_match_type['id'], row_country['id'], args['description'], args['id']) count = writeSQL(query, params) if count: return { 'success': True, 'message': 'match is updated' }, 200 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def put(self): parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('innings', type=int, required=True, help="innings cannot be missing!", location='json') parser.add_argument('match_id', type=str, required=True, help="match id of the match cannot be missing!", location='json') parser.add_argument('ballid', type=int, required=True, help="ball id cannot be missing!", location='json') parser.add_argument('rvs_a_target', type=str, required=True, help="penalty cannot be missing!", location='json') parser.add_argument('rvs_a_over', type=str, required=True, help="Format over cannot be missing!", location='json') parser.add_argument('rvs_b_over', type=str, required=True, help="Format over cannot be missing!", location='json') parser.add_argument('message', type=str, required=True, help="Message cannot be missing!", location='json') args = parser.parse_args() try: update_playerscorecard = """ UPDATE PLAYERSCORECARD SET RVS_A_OVER = %s, RVS_B_OVER = %s, RVS_A_TARGET = %s WHERE MATCH_ID = %s AND INNINGS = %s AND BALL_ID = %s """ params = (args['rvs_a_over'], args['rvs_b_over'], args['rvs_a_target'], args['match_id'], args['innings'], args['ballid']) updated_row = writeSQL(update_playerscorecard, params) if updated_row: get_fullcard = "fullscorecard:" + str( args['match_id']) + ":" + str(args['innings']) + ":" + str( args['ballid']) print(get_fullcard) fullcard = json.loads(app.redis.get(get_fullcard)) fullcard['rvs_a_target'] = args['rvs_a_target'] fullcard['rvs_a_over'] = args['rvs_a_over'] fullcard['rvs_b_over'] = args['rvs_b_over'] app.redis.set(get_fullcard, json.dumps(fullcard)) #manual commentry list_com = "list:" + str(args['match_id']) + ":" + str( args['innings']) man_com_key = "commentry:" + str(args['match_id']) + ":" + str( args['innings']) + ":manual" obj = {} obj['comment'] = args['message'] obj['datetime'] = datetime.utcnow() if (app.redis.hkeys(man_com_key)): index_val = [ val for loc, val in enumerate(app.redis.hkeys(man_com_key)) if math.floor(float(val)) == args['ballid'] ] if (index_val): max_val = float(max(index_val)) field_key = format(max_val + 0.1, ".1f") else: field_key = args['ballid'] + 0.1 else: field_key = args['ballid'] + 0.1 list_man_com_key = man_com_key + ":" + str(field_key) app.redis.lpush(list_com, list_man_com_key) app.redis.hset(man_com_key, field_key, json.dumps(obj, default=default)) return {'success': True}, 200 else: return {'success': False, 'message': 'no match found'}, 404 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def post(self): try: parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('name', type=str, required=True, help="Name of the Player cannot be missing!", location='json') parser.add_argument( 'first_name', type=str, required=True, help="First name of the player cannot be missing!", location='json') parser.add_argument( 'last_name', type=str, required=True, help="Last name of the player cannot be missing!", location='json') parser.add_argument('batting_style', type=str, required=True, help="Batting style cannot be missing!", location='json') parser.add_argument('bowling_style', type=str, required=True, help="Bowling style cannot be missing!", location='json') parser.add_argument('bowling_cat', type=str, required=True, help="Bowling category cannot be missing!", location='json') parser.add_argument('type', type=str, required=True, help="Type cannot be missing!", location='json') # parser.add_argument('status', type=str, required=True, help="Status cannot be missing!", location='json') parser.add_argument('team', type=str, required=True, help="Team cannot be missing!", location='json') args = parser.parse_args() query_id = """ INSERT INTO PLAYER (NAME,FIRST_NAME,LAST_NAME, BATTING_STYLE, BOWLING_STYLE, BOWLING_CAT, TYPE, TEAM_ID) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) RETURNING ID """ params = (args['name'], args['first_name'], args['last_name'], args['batting_style'], args['bowling_style'], args['bowling_cat'], args['type'], args['team']) player_id = writeFetchSQL(query_id, params) if player_id: insert_query = """ INSERT INTO TEAMPLAYERS (P_ID, T_ID, STATUS) SELECT %s, %s, %s WHERE NOT EXISTS ( SELECT id FROM TEAMPLAYERS WHERE P_ID = %s AND T_ID = %s ) """ params = (player_id, args['team'], True, player_id, args['team']) teamplayers = writeSQL(insert_query, params) if teamplayers: return { 'success': True, 'message': 'Successfully created' }, 200 else: return {'sucess': False}, 400 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def put(self): try: parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('name', type=str, required=True, help="Name of the Player cannot be missing!", location='json') parser.add_argument( 'first_name', type=str, required=True, help="First Name of the Player cannot be missing!", location='json') parser.add_argument( 'last_name', type=str, required=True, help="Last Name of the Player cannot be missing!", location='json') parser.add_argument('batting_style', type=str, required=True, help="Batting style cannot be missing!", location='json') parser.add_argument('bowling_style', type=str, required=True, help="Bowling style cannot be missing!", location='json') parser.add_argument('bowling_cat', type=str, required=True, help="Bowling category cannot be missing!", location='json') parser.add_argument('type', type=str, required=True, help="Type cannot be missing!", location='json') # parser.add_argument('status', type=str, required=True, help="Status cannot be missing!", location='json') parser.add_argument('team', type=int, required=True, help="Team cannot be missing!", location='json') parser.add_argument('id', type=int, required=True, help="Team cannot be missing!", location='json') args = parser.parse_args() query_insert = """ UPDATE PLAYER SET NAME = %s, FIRST_NAME= %s, LAST_NAME= %s, BATTING_STYLE = %s, BOWLING_STYLE = %s,BOWLING_CAT = %s, TYPE = %s, TEAM_ID = %s WHERE ID = %s """ params = (args['name'], args['first_name'], args['last_name'], args['batting_style'], args['bowling_style'], args['bowling_cat'], args['type'], args['team'], args['id']) players = writeSQL(query_insert, params) if players: return { 'success': True, 'message': 'Updated successfully' }, 200 else: return {'sucess': False}, 400 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def put(self): parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('innings', type=int, required=True, help="innings cannot be missing!", location='json') parser.add_argument('match_id', type=int, required=True, help="match id of the match cannot be missing!", location='json') parser.add_argument('ballid', type=int, required=True, help="ball id cannot be missing!", location='json') parser.add_argument('penalty', type=str, required=True, help="penalty cannot be missing!", location='json') parser.add_argument('formatOver', type=str, required=True, help="Format over cannot be missing!", location='json') parser.add_argument('message', type=str, required=True, help="Message cannot be missing!", location='json') args = parser.parse_args() try: innings = int(args['innings']) matchid = args['match_id'] ballid = args['ballid'] penalty = args['penalty'] formatOver = int(args['formatOver']) - 1 + 0.6 query_playerscorecard = """ SELECT * FROM PLAYERSCORECARD WHERE MATCH_ID = %s AND INNINGS = %s ORDER BY ID desc limit 1 """ params = (matchid, innings) row_last_record = readSQL(query_playerscorecard, params) if (row_last_record): if (innings == 1): # teamA is doing mistake if (penalty == 'teamA'): if (row_last_record['penalty_a']): pnlt_a = row_last_record['penalty_a'] + 5 else: pnlt_a = 5 update_record = """ UPDATE PLAYERSCORECARD SET PENALTY_A = %s WHERE MATCH_ID = %s AND BALL_ID = %s """ params = (pnlt_a, matchid, ballid) updated_record = writeSQL(update_record, params) if (updated_record): query_playerscorecard = """ SELECT * FROM PLAYERSCORECARD WHERE MATCH_ID = %s AND INNINGS = %s ORDER BY ID desc limit 1 """ params = (matchid, innings) row_lastrecord = readSQL(query_playerscorecard, params) if (row_lastrecord): pnlt = 0 if (row_last_record['ball_id'] == args['ballid']): pnlt = row_lastrecord['penalty_a'] else: if (row_last_record['penalty_a']): pnlt = row_last_record['penalty_a'] + 5 else: pnlt = 5 get_curr_inng = app.redis.get("match:" + str(matchid) + ":innings") get_inng = json.loads(get_curr_inng) curr_key = "livescorecard:" + str( matchid) + ":current:" + str(get_inng) curr_livecard = app.redis.get(curr_key) key_info = json.loads(curr_livecard) get_fullcard = "fullscorecard:" + str( matchid) + ":" + str(innings) + ":" + str( key_info['ball']) fullcard = json.loads( app.redis.get(get_fullcard)) fullcard['teamBRuns'] = row_lastrecord[ 'team_b_score'] + 5 # print(fullcard['teamBRuns']) for key, value in fullcard['innings_1'].items( ): if key == 'extras': fullcard['innings_1']['extras'][ 'pnlt'] = pnlt updated_key = app.redis.set( get_fullcard, json.dumps(fullcard, default=default)) if (updated_key): update_playerscorecard = """ UPDATE PLAYERSCORECARD SET PENALTY_A = %s, TEAM_B_SCORE = %s WHERE ID = %s """ params = (pnlt, row_lastrecord['team_b_score'] + 5, row_lastrecord['id']) updated_row = writeSQL( update_playerscorecard, params) if (updated_row): #manual commentry list_com = "list:" + str( matchid) + ":" + str(get_inng) man_com_key = "commentry:" + str( matchid) + ":" + str( get_inng) + ":manual" obj = {} obj['comment'] = args['message'] obj['datetime'] = datetime.utcnow() if (app.redis.hkeys(man_com_key)): index_val = [ val for loc, val in enumerate( app.redis.hkeys( man_com_key)) if math.floor(float(val)) == key_info['ball'] ] if (index_val): max_val = float(max(index_val)) field_key = format( max_val + 0.1, ".1f") else: field_key = key_info[ 'ball'] + 0.1 else: field_key = key_info['ball'] + 0.1 list_man_com_key = man_com_key + ":" + str( field_key) app.redis.lpush( list_com, list_man_com_key) app.redis.hset( man_com_key, field_key, json.dumps(obj, default=default)) return {'success': True}, 200 # teamB is doing mistake elif (penalty == 'teamB'): if (row_last_record['penalty_b']): pnlt_b = row_last_record['penalty_b'] + 5 else: pnlt_b = 5 update_record = """ UPDATE PLAYERSCORECARD SET PENALTY_B = %s WHERE MATCH_ID = %s AND BALL_ID = %s """ params = (pnlt_b, matchid, ballid) updated_record = writeSQL(update_record, params) if (updated_record): query_playerscorecard = """ SELECT * FROM PLAYERSCORECARD WHERE MATCH_ID = %s AND INNINGS = %s ORDER BY ID desc limit 1 """ params = (matchid, innings) row_lastrecord = readSQL(query_playerscorecard, params) if (row_lastrecord): pnlt = 0 if (row_last_record['ball_id'] == args['ballid']): pnlt = row_lastrecord['penalty_b'] else: if (row_last_record['penalty_b']): pnlt = row_last_record['penalty_b'] + 5 else: pnlt = 5 score = row_lastrecord['team_a_score'] + 5 over = row_lastrecord['team_a_over'] balls = int(over) + (round(over % 1, 2) * 10 / 6) cal_cur_rate = score / balls get_curr_inng = app.redis.get("match:" + str(matchid) + ":innings") get_inng = json.loads(get_curr_inng) curr_key = "livescorecard:" + str( matchid) + ":current:" + str(get_inng) curr_livecard = app.redis.get(curr_key) key_info = json.loads(curr_livecard) get_fullcard = "fullscorecard:" + str( matchid) + ":" + str(innings) + ":" + str( key_info['ball']) fullcard = json.loads( app.redis.get(get_fullcard)) fullcard['teamARuns'] = score fullcard['curr_rate'] = round(cal_cur_rate, 2) for key, value in fullcard['innings_2'].items( ): if key == 'extras': fullcard['innings_2']['extras'][ 'pnlt'] = pnlt updated_key = app.redis.set( get_fullcard, json.dumps(fullcard, default=default)) if updated_key: update_playerscorecard = """ UPDATE PLAYERSCORECARD SET TEAM_A_SCORE = %s, CURR_RATE = %s, PENALTY_B = %s WHERE ID = %s """ params = (score, float(round(cal_cur_rate, 2)), pnlt, row_lastrecord['id']) updated_row = writeSQL( update_playerscorecard, params) if (updated_row): #manual commentry list_com = "list:" + str( matchid) + ":" + str(get_inng) man_com_key = "commentry:" + str( matchid) + ":" + str( get_inng) + ":manual" obj = {} obj['comment'] = args['message'] obj['datetime'] = datetime.utcnow() if (app.redis.hkeys(man_com_key)): index_val = [ val for loc, val in enumerate( app.redis.hkeys( man_com_key)) if math.floor(float(val)) == key_info['ball'] ] if (index_val): max_val = float(max(index_val)) field_key = format( max_val + 0.1, ".1f") else: field_key = key_info[ 'ball'] + 0.1 else: field_key = key_info['ball'] + 0.1 list_man_com_key = man_com_key + ":" + str( field_key) app.redis.lpush( list_com, list_man_com_key) app.redis.hset( man_com_key, field_key, json.dumps(obj, default=default)) return {'success': True}, 200 elif (innings == 2): # teamA is doing mistake if (penalty == 'teamA'): if (row_last_record['penalty_a']): pnlt_a = row_last_record['penalty_a'] + 5 else: pnlt_a = 5 update_record = """ UPDATE PLAYERSCORECARD SET PENALTY_A = %s WHERE MATCH_ID = %s AND BALL_ID = %s """ params = (pnlt_a, matchid, ballid) updated_record = writeSQL(update_record, params) if (updated_record): query_playerscorecard = """ SELECT * FROM PLAYERSCORECARD WHERE MATCH_ID = %s AND INNINGS = %s ORDER BY ID desc limit 1 """ params = (matchid, innings) row_lastrecord = readSQL(query_playerscorecard, params) if (row_lastrecord): pnlt = 0 if (row_last_record['ball_id'] == args['ballid']): pnlt = row_lastrecord['penalty_a'] else: if (row_last_record['penalty_a']): pnlt = row_last_record['penalty_a'] + 5 else: pnlt = 5 teama_score = row_lastrecord['team_a_score'] teamb_score = row_lastrecord['team_b_score'] + 5 teamb_over = row_lastrecord['team_b_over'] balls = int(teamb_over) + ( round(teamb_over % 1, 2) * 10 / 6) cal_cur_rate = teamb_score / balls remainingOvrs = float(formatOver) - float( teamb_over) remainingBalls = int(remainingOvrs) + ( round(remainingOvrs % 1, 2) * 10 / 6) cal_req_rate = round( (teama_score - teamb_score) / remainingBalls, 2) get_curr_inng = app.redis.get("match:" + str(matchid) + ":innings") get_inng = json.loads(get_curr_inng) curr_key = "livescorecard:" + str( matchid) + ":current:" + str(get_inng) curr_livecard = app.redis.get(curr_key) key_info = json.loads(curr_livecard) get_fullcard = "fullscorecard:" + str( matchid) + ":" + str(innings) + ":" + str( key_info['ball']) fullcard = json.loads( app.redis.get(get_fullcard)) fullcard['teamBRuns'] = teamb_score fullcard['curr_rate'] = round(cal_cur_rate, 2) fullcard['req_rate'] = cal_req_rate # print(fullcard) for key, value in fullcard['innings_1'].items( ): if key == 'extras': fullcard['innings_1']['extras'][ 'pnlt'] = pnlt updated_key = app.redis.set( get_fullcard, json.dumps(fullcard, default=default)) if updated_key: query_playerscorecard = """ UPDATE PLAYERSCORECARD SET TEAM_B_SCORE = %s, CURR_RATE = %s, REQ_RATE = %s, PENALTY_A = %s WHERE ID = %s """ params = (teamb_score, float(round(cal_cur_rate, 2)), cal_req_rate, pnlt, row_lastrecord['id']) updated_playerscorecard = writeSQL( query_playerscorecard, params) if updated_playerscorecard: #manual commentry list_com = "list:" + str( matchid) + ":" + str(get_inng) man_com_key = "commentry:" + str( matchid) + ":" + str( get_inng) + ":manual" obj = {} obj['comment'] = args['message'] obj['datetime'] = datetime.utcnow() if (app.redis.hkeys(man_com_key)): index_val = [ val for loc, val in enumerate( app.redis.hkeys( man_com_key)) if math.floor(float(val)) == key_info['ball'] ] if (index_val): max_val = float(max(index_val)) field_key = format( max_val + 0.1, ".1f") else: field_key = key_info[ 'ball'] + 0.1 else: field_key = key_info['ball'] + 0.1 list_man_com_key = man_com_key + ":" + str( field_key) app.redis.lpush( list_com, list_man_com_key) app.redis.hset( man_com_key, field_key, json.dumps(obj, default=default)) return {'success': True}, 200 # teamB is doing mistake elif (penalty == 'teamB'): if (row_last_record['penalty_b']): pnlt_b = row_last_record['penalty_b'] + 5 else: pnlt_b = 5 update_record = """ UPDATE PLAYERSCORECARD SET PENALTY_B = %s WHERE MATCH_ID = %s AND BALL_ID = %s """ params = (pnlt_b, matchid, ballid) updated_record = writeSQL(update_record, params) if (updated_record): query_playerscorecard = """ SELECT * FROM PLAYERSCORECARD WHERE MATCH_ID = %s AND INNINGS = %s ORDER BY ID desc limit 1 """ params = (matchid, innings) row_lastrecord = readSQL(query_playerscorecard, params) if (row_lastrecord): pnlt = 0 if (row_last_record['ball_id'] == args['ballid']): pnlt = row_lastrecord['penalty_b'] else: if (row_last_record['penalty_b']): pnlt = row_last_record['penalty_b'] + 5 else: pnlt = 5 teama_score = row_lastrecord['team_a_score'] + 5 teamb_score = row_lastrecord['team_b_score'] teamb_over = row_lastrecord['team_b_over'] balls = int(teamb_over) + ( round(teamb_over % 1, 2) * 10 / 6) cal_cur_rate = teamb_score / balls remainingOvrs = float(formatOver) - float( teamb_over) remainingBalls = int(remainingOvrs) + ( round(remainingOvrs % 1, 2) * 10 / 6) cal_req_rate = round( (teama_score - teamb_score) / remainingBalls, 2) get_curr_inng = app.redis.get("match:" + str(matchid) + ":innings") get_inng = json.loads(get_curr_inng) curr_key = "livescorecard:" + str( matchid) + ":current:" + str(get_inng) curr_livecard = app.redis.get(curr_key) key_info = json.loads(curr_livecard) get_fullcard = "fullscorecard:" + str( matchid) + ":" + str(innings) + ":" + str( key_info['ball']) fullcard = json.loads( app.redis.get(get_fullcard)) fullcard['teamARuns'] = teama_score fullcard['teamBRuns'] = teamb_score fullcard['curr_rate'] = round(cal_cur_rate, 2) fullcard['req_rate'] = cal_req_rate for key, value in fullcard['innings_2'].items( ): if key == 'extras': fullcard['innings_2']['extras'][ 'pnlt'] = pnlt updated_key = app.redis.set( get_fullcard, json.dumps(fullcard, default=default)) if updated_key: query_playerscorecard = """ UPDATE PLAYERSCORECARD SET TEAM_A_SCORE = %s, CURR_RATE = %s, REQ_RATE = %s, PENALTY_B = %s WHERE ID = %s """ params = (teama_score, float(round(cal_cur_rate, 2)), cal_req_rate, pnlt, row_lastrecord['id']) updated_playerscorecard = writeSQL( query_playerscorecard, params) if updated_playerscorecard: #manual commentry list_com = "list:" + str( matchid) + ":" + str(get_inng) man_com_key = "commentry:" + str( matchid) + ":" + str( get_inng) + ":manual" obj = {} obj['comment'] = args['message'] obj['datetime'] = datetime.utcnow() if (app.redis.hkeys(man_com_key)): index_val = [ val for loc, val in enumerate( app.redis.hkeys( man_com_key)) if math.floor(float(val)) == key_info['ball'] ] if (index_val): max_val = float(max(index_val)) field_key = format( max_val + 0.1, ".1f") else: field_key = key_info[ 'ball'] + 0.1 else: field_key = key_info['ball'] + 0.1 list_man_com_key = man_com_key + ":" + str( field_key) app.redis.lpush( list_com, list_man_com_key) app.redis.hset( man_com_key, field_key, json.dumps(obj, default=default)) return {'success': True}, 200 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def put(self): parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('match_id', type=int, required=True, help="match id cannot be missing!", location='json') parser.add_argument('player_id', type=int, required=True, help="player id cannot be missing!", location='json') parser.add_argument('message', type=str, required=True, help="message cannot be missing!", location='json') parser.add_argument('type', type=str, required=True, help="type cannot be missing!", location='json') args = parser.parse_args() try: match_id = args['match_id'] get_curr_inng = app.redis.get("match:" + str(args['match_id']) + ":innings") get_inng = json.loads(get_curr_inng) curr_key = "livescorecard:" + str(match_id) + ":current:" + str( get_inng) curr_livecard = app.redis.get(curr_key) key_info = json.loads(curr_livecard) if (args['type'] == 'batsman'): get_fullcard = "fullscorecard:" + str( args['match_id']) + ":" + str(get_inng) + ":" + str( key_info['ball']) fullcard = json.loads(app.redis.get(get_fullcard)) if get_inng == 1: key = "innings_1" player = str(args['player_id']) + "_pname" elif get_inng == 2: key = "innings_2" player = str(args['player_id']) + "_pname" if player in fullcard[key]['batting']: player_record = fullcard[key]['batting'][player] fullcard[key]['batting'][player]['status'] = 'retrd' fullcard[key]['yetToBat'].append(player_record['name']) fullcard_key = "fullscorecard:" + str( args['match_id']) + ":" + str(get_inng) + ":" + str( key_info['ball']) app.redis.set(fullcard_key, json.dumps(fullcard)) query_playerscorecard = """ SELECT * FROM PLAYERSCORECARD WHERE MATCH_ID = %s AND INNINGS = %s ORDER BY ID desc limit 1 """ params = (args['match_id'], get_inng) row_playerscorecard = readSQL(query_playerscorecard, params) if row_playerscorecard: update_playerscorecard = """ UPDATE PLAYERSCORECARD SET RETIRED = %s WHERE ID = %s """ params = (int(args['player_id']), row_playerscorecard['id']) updated_row = writeSQL(update_playerscorecard, params) if updated_row: #manual commentry list_com = "list:" + str(match_id) + ":" + str( get_inng) man_com_key = "commentry:" + str( match_id) + ":" + str(get_inng) + ":manual" obj = {} obj['comment'] = args['message'] obj['datetime'] = datetime.utcnow() if (app.redis.hkeys(man_com_key)): index_val = [ val for loc, val in enumerate( app.redis.hkeys(man_com_key)) if math.floor(float(val)) == key_info['ball'] ] if (index_val): max_val = float(max(index_val)) field_key = format(max_val + 0.1, ".1f") else: field_key = key_info['ball'] + 0.1 else: field_key = key_info['ball'] + 0.1 list_man_com_key = man_com_key + ":" + str( field_key) app.redis.lpush(list_com, list_man_com_key) app.redis.hset(man_com_key, field_key, json.dumps(obj, default=default)) return {'success': True}, 200 elif (args['type'] == 'bowler'): #manual commentry list_com = "list:" + str(match_id) + ":" + str(get_inng) man_com_key = "commentry:" + str(match_id) + ":" + str( get_inng) + ":manual" obj = {} obj['comment'] = args['message'] obj['datetime'] = datetime.utcnow() if (app.redis.hkeys(man_com_key)): index_val = [ val for loc, val in enumerate(app.redis.hkeys(man_com_key)) if math.floor(float(val)) == key_info['ball'] ] if (index_val): max_val = float(max(index_val)) field_key = format(max_val + 0.1, ".1f") else: field_key = key_info['ball'] + 0.1 else: field_key = key_info['ball'] + 0.1 list_man_com_key = man_com_key + ":" + str(field_key) app.redis.lpush(list_com, list_man_com_key) app.redis.hset(man_com_key, field_key, json.dumps(obj, default=default)) return {'success': True}, 200 else: return {'success': False, 'message': 'no player found'}, 404 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500
def post(self): parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('id', type=str, required=True, help="match id cannot be missing!", location='json') args = parser.parse_args() try: update_match = """ UPDATE MATCH SET STATUS = 'LIVE' WHERE ID = %s """ params = (args['id'], ) update = writeSQL(update_match, params) if update: squad_key = """ select match.team_a, match.team_b, player.id, player.name, player.first_name, player.last_name, player.batting_style, teamsquad.team_id, teamsquad.selected, teamsquad."position" from match join squad on squad.id = match.squad_id join teamsquad on teamsquad.t_id_1 = squad.team_squad_1 or teamsquad.t_id_2 = squad.team_squad_2 join player on player.id = teamsquad.player_id where match.id= %s and (teamsquad.selected = true or teamsquad.selected = false) GROUP BY teamsquad.team_id, player.id, player.name, teamsquad.team_id, teamsquad."position", match.team_a, match.team_b, teamsquad.selected order by teamsquad."position" asc """ params_key = (args['id'], ) squad_players = readManySQL(squad_key, params_key) if (squad_players): obj = {} # innings 1 formation obj["innings_1"] = {} obj["innings_1"]["batting"] = {} obj["innings_1"]["yetToBat"] = [] for player in squad_players: if (player['team_id'] == player['team_a'] and player['selected']): unq_name = str(player['id']) + "_pname" obj["innings_1"]["batting"][unq_name] = { "id": player['id'], "name": player['name'], "first_name": player['first_name'], "last_name": player['last_name'], "batting_style": player['batting_style'], "status": "", "run": 0, "balls": 0, "four": 0, "six": 0, "strikeRate": 0 } obj["innings_1"]["totalRuns"] = 0 obj["innings_1"]["totalWickets"] = 0 obj["innings_1"]["totalOvers"] = 0 obj["innings_1"]["extras"] = { "byes": 0, "legByes": 0, "wide": 0, "noBall": 0, "pnlt": 0 } obj["innings_1"]["yetToBat"].append(player['name']) obj["innings_1"]["fallOfWickets"] = [] obj["innings_1"]["Bowling"] = {} # innings 2 formation obj["innings_2"] = {} obj["innings_2"]["batting"] = {} obj["innings_2"]["yetToBat"] = [] for player in squad_players: if (player['team_id'] == player['team_b'] and player['selected']): unq_name = str(player['id']) + "_pname" obj["innings_2"]["batting"][unq_name] = { "id": player['id'], "name": player['name'], "first_name": player['first_name'], "last_name": player['last_name'], "batting_style": player['batting_style'], "status": "", "run": 0, "balls": 0, "four": 0, "six": 0, "strikeRate": 0 } obj["innings_2"]["totalRuns"] = 0 obj["innings_2"]["totalWickets"] = 0 obj["innings_2"]["totalOvers"] = 0 obj["innings_2"]["extras"] = { "byes": 0, "legByes": 0, "wide": 0, "noBall": 0, "pnlt": 0 } obj["innings_2"]["yetToBat"].append(player['name']) obj["innings_2"]["fallOfWickets"] = [] obj["innings_2"]["Bowling"] = {} val = json.dumps(obj) key = "fullscorecard:" + str(args['id']) return_key = app.redis.set(key, val) if return_key: return {'success': True}, 200 else: return { 'success': False, 'message': "SERVER/DB error" }, 500 else: return {'success': False, 'message': 'no match found'}, 404 except Exception as e: app.log.exception(e) return {'success': False, 'message': "SERVER/DB error"}, 500