def index(season_id=None): if season_id: penalties = orm.to_json(orm.get_season_penalties(season_id)) else: penalties = orm.to_json(orm.get_penalties()) if penalties is None: abort(404, "No penalties") return penalties
def create(season_id, match_id): new_id = orm.add_season_match.first(season_id, match_id) new_match = orm.to_json(orm.get_match_by_id.first(match_id)) season = orm.to_json(orm.get_season_by_id.first(season_id)) orm.update_season_match_count(season_id) if (season['max_time'] is not None and new_match['datetime'] >= season['start_time'] + season['max_time']) or \ (season['max_matches'] is not None and season['played_matches'] + 1 >= season['max_matches']): seasons_controller.delete(season_id) return { 'message': 'Match added to season', 'value': new_id, }
def viz(algo, user_id, viz): # Put your different viz requests here res = orm.to_json( {'musigma_team': { 'history': orm.get_user_musigma_team_history }}[algo][viz](user_id)) return {viz: res}
def show(ids, season_id): logging.debug('musigma_team.show({}, {})'.format(ids, season_id)) env_name = 'global' if not season_id else 'season' set_trueskill_env(env_name) env = get_trueskill_env(env_name) players = { id: orm.to_json(orm.get_user_musigma_team.first(id, season_id)) for id in ids } rates = { id: Rating(float(p['mu']), float(p['sigma'])) if p is not None else Rating() for (id, p) in players.items() } matches = get_all_possible_matches(ids) matches = [[{p: rates[p] for p in t} for t in m] for m in matches] qualities = [env.quality(m) for m in matches] qual, idx = max((_, idx) for (idx, _) in enumerate(qualities)) logger.debug('qualities: {}'.format(qualities)) logger.debug('max quality: {}, {}'.format(qual, idx)) match = matches[idx] match = { 'r_ids': [user_id for user_id in match[0]], 'b_ids': [user_id for user_id in match[1]], 'quality': qual } logger.debug('match: {}'.format(match)) return match
def create(b_score, r_score, duration, datetime, b1_pseudo, b2_pseudo, b3_pseudo, b4_pseudo, b5_pseudo, b6_pseudo, r1_pseudo, r2_pseudo, r3_pseudo, r4_pseudo, r5_pseudo, r6_pseudo): print("FDP") if b1_pseudo is None or r1_pseudo is None: abort(403, 'Each team must have at lease one player.') # Check if match hasn't been submitted already last_match = orm.to_json(orm.get_last_pending_match.first()) if last_match is not None and last_match[ 'b_score'] == b_score and last_match[ 'r_score'] == r_score and last_match[ 'b1_pseudo'] == b1_pseudo and last_match[ 'r1_pseudo'] == r1_pseudo and last_match[ 'b2_pseudo'] == b2_pseudo and last_match[ 'r2_pseudo'] == r2_pseudo and last_match[ 'b3_pseudo'] == b3_pseudo and last_match[ 'r3_pseudo'] == r3_pseudo: abort(403, 'Match already pendgin for validation.') new_match_id = orm.create_pending_match.first( b_score, r_score, duration, datetime, b1_pseudo, b2_pseudo, b3_pseudo, b4_pseudo, b5_pseudo, b6_pseudo, r1_pseudo, r2_pseudo, r3_pseudo, r4_pseudo, r5_pseudo, r6_pseudo) return { 'message': 'Pending match created, waiting validation', 'value': new_match_id, }
def run(algo, ids): logger.debug("ids: {}".format(ids)) if (len(ids) < 2): abort(400, 'Not enough players') cur_season = orm.to_json(orm.get_running_season.first()) cur_season_id = cur_season['id'] if cur_season else None logger.debug("current season id: {}".format(cur_season_id)) return {'musigma_team': musigma_team.show}[algo](ids, cur_season_id)
def login(login, password): if orm.auth_admin.first(login, password): user = orm.to_json(orm.get_user_by_trigram.first(login)) return { 'message': 'Auth successful', 'Bearer': create_access_token(identity=user['id']) } abort(401)
def get_rates(ids): # Get musigma user / create it if none res = [] for user_id in ids: pl = orm.to_json( orm.get_user_musigma_team.first(user_id, season_id)) rating = Rating(float(pl['mu']), float( pl['sigma'])) if pl is not None else Rating() res.append(rating) return res
def update(match_id, **kwargs): # TODO: Some work will be needed here when adding new algos cur_season = orm.to_json(orm.get_running_season.first()) cur_season_id = cur_season['id'] if cur_season else None update_funcs = [musigma_team.update] # Update season and global for each algo available for update_func in update_funcs: update_func(match_id, None, **kwargs) if cur_season_id is not None: update_func(match_id, cur_season_id, **kwargs) return {'message': 'Algos updated'}
def get(self, algo): args = parser_algo_from_pseudos.parse_args() pseudos = args['pseudos'] + [ None for x in range(12 - len(args['pseudos'])) ] players = orm.to_json(orm.get_users_by_pseudo(*pseudos)) ids = [x['id'] for x in players] algo = algos.run(algo, ids) res = {'r_pseudos': [], 'b_pseudos': [], 'quality': algo['quality']} for id in algo['r_ids']: res['r_pseudos'].append( next(pl['pseudo'] for pl in players if pl['id'] == id)) for id in algo['b_ids']: res['b_pseudos'].append( next(pl['pseudo'] for pl in players if pl['id'] == id)) return res
def index(algo, season_id): res = orm.to_json({'musigma_team': orm.get_ranked_users_musigma_team}[algo](season_id)) ranking = { 'algo': algo, 'users': [{ 'id': o['user_id'], 'pseudo': o['pseudo'], 'usual_pseudos': o['usual_pseudos'], 'is_active': o['is_active'], 'exposition': o['exposition'], 'mu': o['mu'], 'sigma': o['sigma'], 'rank': o['rank'] } for o in res] } return ranking
def delete(season_id): with orm.transaction(): orm.apply_musigma_team_penalties(season_id) orm.terminate_season(season_id, datetime.datetime.now()) # Award stars ranked_users = orm.to_json( orm.get_ranked_users_musigma_team(season_id)) if len(ranked_users) > 1: orm.award_gold_star(ranked_users[0]['user_id']) orm.award_loser_star(ranked_users[-1]['user_id']) if len(ranked_users) > 2: orm.award_silver_star(ranked_users[1]['user_id']) if len(ranked_users) > 3: orm.award_copper_star(ranked_users[2]['user_id']) return { 'message': 'Season finished', }
def update(match_id, season_id, r_ids, b_ids, r_score, b_score): def get_rates(ids): # Get musigma user / create it if none res = [] for user_id in ids: pl = orm.to_json( orm.get_user_musigma_team.first(user_id, season_id)) rating = Rating(float(pl['mu']), float( pl['sigma'])) if pl is not None else Rating() res.append(rating) return res env_name = 'global' if not season_id else 'season' set_trueskill_env(env_name) env = get_trueskill_env(env_name) r_ids = [user_id for user_id in r_ids if user_id is not None] b_ids = [user_id for user_id in b_ids if user_id is not None] r_rates = get_rates(r_ids) b_rates = get_rates(b_ids) new_r_rates, new_b_rates = rate([r_rates, b_rates], ranks=[b_score, r_score]) # Lower is better new_rates = new_r_rates + new_b_rates new_expositions = [expose(r) for r in new_rates] with orm.transaction(): for idx, user_id in enumerate(r_ids + b_ids): # Init user to default stats if not already in base user_musigma_team = orm.to_json( orm.get_user_musigma_team(user_id, season_id)) if len(user_musigma_team) == 0: init_user(user_id, season_id) orm.create_user_musigma_team(user_id, match_id, season_id, new_expositions[idx], new_rates[idx].mu, new_rates[idx].sigma)
def show(season_id): season = orm.to_json(orm.get_season_by_id.first(season_id)) if season is None: abort(404, "Season not found") return season
def show(match_id): match = orm.to_json(orm.get_pending_match_by_id.first(match_id)) if match is None: abort(404, "Pending match not found") return match
def index(): matches = orm.to_json(orm.get_pending_matches()) if matches is None: abort(404, "No pending matches") return matches
def get_user_custom_stats(user_id): stats = orm.to_json(orm.get_user_custom_stats.first(user_id)) return stats
def get_user_match_stats(user_id): stats = orm.to_json(orm.get_user_match_stats.first(user_id)) return stats
def show(user_id): user = orm.to_json(orm.get_user_by_id.first(user_id)) if user is None: abort(404, "User not found") return user
def index(season_id): matches = orm.to_json(orm.get_season_matches(season_id)) if matches is None: abort(404, "Season not found") return matches
def show_current(): return orm.to_json(orm.get_running_season.first())
def index(user_id): matches = orm.to_json(orm.get_user_matches(user_id)) if matches is None: abort(404, "No matches found") return matches
def rank(stat, method, season_id): res = orm.to_json(orm.get_ranking(stat, method, season_id)) return {'stat': stat, 'method': method, 'ranking': res}
def index(): seasons = orm.to_json(orm.get_seasons()) if seasons is None: abort(404, "No seasons found") return seasons
def show(algo, user_id): show_func = {'musigma_team': orm.get_all_user_musigma_rankings}[algo] return orm.to_json(show_func(user_id))
def index(): users = orm.to_json(orm.get_users()) if users is None: abort(404, "No users") return users
def convert(match_id, validator): pending_match = orm.to_json(orm.get_pending_match_by_id.first(match_id)) # pending_stats = orm.to_json(orm.get_pending_match_stats(match_id)) if pending_match is None: abort(404, "Pending match not found") ids = {'r': [], 'b': []} for team in ['r', 'b']: for idx in range(1, 7): pseudo = pending_match[team + str(idx)]['user_pseudo'] if pseudo is None: pending_match[team + str(idx)] = None continue player = orm.to_json(orm.get_user_by_pseudo.first(pseudo)) if player is None: abort( 400, "Pseudo {} can't be matched against existing players". format(pseudo)) pending_match[team + str(idx)]['user_id'] = player['id'] ids[team].append(player['id']) try: with orm.transaction(): new_match_id = matches_controller.create( pending_match['b_score'], pending_match['r_score'], pending_match['duration'], pending_match['datetime'], pending_match['b1']['user_id'] if pending_match['b1'] else None, pending_match['b2']['user_id'] if pending_match['b2'] else None, pending_match['b3']['user_id'] if pending_match['b3'] else None, pending_match['b4']['user_id'] if pending_match['b4'] else None, pending_match['b5']['user_id'] if pending_match['b5'] else None, pending_match['b6']['user_id'] if pending_match['b6'] else None, pending_match['r1']['user_id'] if pending_match['r1'] else None, pending_match['r2']['user_id'] if pending_match['r2'] else None, pending_match['r3']['user_id'] if pending_match['r3'] else None, pending_match['r4']['user_id'] if pending_match['r4'] else None, pending_match['r5']['user_id'] if pending_match['r5'] else None, pending_match['r6']['user_id'] if pending_match['r6'] else None, validator)['value'] for team in ['r', 'b']: for idx in range(1, 7): player = pending_match[team + str(idx)] if player is None: continue matches_stats_controller.create( new_match_id, player['user_id'], player['score'], player['tags'], player['popped'], player['grabs'], player['drops'], player['hold'], player['captures'], player['prevent'], player['returns'], player['support'], player['pups']) matchespending_stats_controller.delete_all(new_match_id) delete(match_id) # Link match to season season = seasons_controller.show_current() if season is not None: seasons_matches_controller.create(season['id'], new_match_id) # Update algo # TODO: All player stats will probably need to be passed here to update algo algos_controller.update(new_match_id, r_ids=ids['r'], b_ids=ids['b'], r_score=pending_match['r_score'], b_score=pending_match['b_score']) except postgresql.exceptions.ForeignKeyError: abort(400, "Something failed. Confident in all user ids ?") return { 'message': 'Match validated', 'value': new_match_id, }
def wrapper(*args, **kwargs): user = to_json(get_user_by_id.first(get_jwt_identity())) if not user['is_admin']: return abort(401) return function(*args, **kwargs)
import orm history = orm.to_json(orm.get_musigma_team()) seen_user_ids = [] seen_season_ids = {} nb = 0 orm.drop_musigma_team() for m in history: if m['season_id']: print(m['season_id']) if m['season_id'] not in seen_season_ids: seen_season_ids[m['season_id']] = [] if m['user_id'] not in seen_season_ids[m['season_id']]: orm.create_user_musigma_team(m['user_id'], None, m['season_id'], 0, 25, 8.333333333333334) nb += 1 orm.create_user_musigma_team(m['user_id'], m['match_id'], m['season_id'], m['exposition'], m['mu'], m['sigma']) nb += 1 else: if m['user_id'] not in seen_user_ids: seen_user_ids.append(m['user_id']) orm.create_user_musigma_team(m['user_id'], None, None, 0, 25, 8.333333333333334) nb += 1 orm.create_user_musigma_team(m['user_id'], m['match_id'], m['season_id'], m['exposition'], m['mu'], m['sigma']) nb += 1