def close_event(**options): db = DB() user_id, status, error = db.auth(options['username'], options['password']) if status: return response_error(status, error) admin_id, status, error = db.get_event_admin_id(options['event_id']) if status: return response_error(status, error) if admin_id != user_id: return response_error(1, 'you are not event admin') if options['event_status'] == 'Closed': participants, status, error = db.get_event_participants(options['event_id']) if status: return response_error(status, error) if not all(elem in participants for elem in options['results'].keys()): return response_error(1, "list of results doesnt match with participants") # for username, result in options['results'].items(): for username in participants: result = random.choice(['W', 'D', 'L']) points = 2 if result == 'W' else 1 if result == 'D' else 0 status, errot = db.set_result(options['event_id'], username, result, points) if status: return response_error(status, error) status, error = db.update_event_status(options['event_id'], options['event_status']) if status: return response_error(status, error) return response_ok()
def get_local_rating(**options): db = DB() events, status, error = db.get_list_events(options['sport_id']) if status: return response_error(status, error) local_events = [] for event in events: participants, status, error = db.get_event_participants(event) if status: return response_error(status, error) if all(elem in options['usernames'] for elem in participants): local_events.append(event) result = {username: 0 for username in options['usernames']} for event in local_events: for username in options['usernames']: res, status, error = db.get_user_result(username, event) if status: return response_error(status, error) if res == 'W': result[username] += 2 if res == 'D': result[username] += 1 return response_ok(result)
def leave_event(**options): db = DB() user_id, status, error = db.auth(options['username'], options['password']) if status: return response_error(status, error) participants, status, error = db.get_event_participants(options['event_id']) if status: return response_error(status, error) admin_id, status, error = db.get_event_admin_id(options['event_id']) if status: return response_error(status, error) if user_id == admin_id: return response_error(1, 'admin cant leave event, you should close it') if options['username'] not in participants: return response_error(1, 'you are not in participants list') status, error = db.leave_event(user_id, options['event_id']) if status: return response_error(status, error) return response_ok()
def join_event(**options): db = DB() user_id, status, error = db.auth(options['username'], options['password']) if status: return response_error(status, error) participants, status, error = db.get_event_participants(options['event_id']) if status: return response_error(status, error) event_info, status, error = db.get_event_info(options['event_id']) if status: return response_error(status, error) if options['username'] in participants: return response_error(1, 'you are already registered') if len(participants) == event_info['participants_number_max']: return response_error(1, 'event is full') status, error = db.join_event(user_id, options['event_id']) if status: return response_error(status, error) return response_ok()
def get_event(**options): db = DB() event_info, status, error = db.get_event_info(options['event_id']) if status: return response_error(status, error) # dt = datetime.strptime(event_info['timestamp'], '%Y-%m-%d %H:%M:%S') dt = event_info['timestamp'] event_info['timestamp'] = int(dt.timestamp()) participants, status, error = db.get_event_participants(options['event_id']) if status: return response_error(status, error) return response_ok({'event_info': event_info, 'participants': participants})
def add_follow(**options): db = DB() user_id, status, error = db.auth(options['username'], options['password']) if status: return response_error(status, error) status, error = db.create_follow(user_id, options['sport_id'], options['location']) if status: return response_error(status, error) events, status, error = db.get_list_events(options['sport_id'], options['location']) if status: return response_error(status, error) suggested_events = [] for event in events: participants, status, error = db.get_event_participants(event) if status: return response_error(status, error) if not options['username'] in participants: suggested_events.append(event) return response_ok({'event_ids': suggested_events})