def act_team_change(self, team, player_a, player_b, source=None): e = Event() e.type = Event.EV_TYPE_CHANGE e.team = team e.player_a = player_a e.player_b = player_b self.act_event(e, source)
def act_switch_teams(self, source=None): e = Event() e.type = Event.EV_TYPE_SWAP if self.order == [None, None]: e.red_team = self.teams[0] e.blue_team = self.teams[1] else: e.red_team = self.order[1] e.blue_team = self.order[0] self.act_event(e, source)
def act_goal_undo(self, team, source=None): e = Event() e.type = Event.EV_TYPE_GOAL_UNDO e.team = team self.act_event(e, source)
def replay_match(orig_match_id, mult=1.0, initial_wait=0.0): session = Session() # Retrieve the original match and prepare the new event list # (already sorted by SQLAlchemy) orig_match = session.query(Match).filter(Match.id == orig_match_id).one() events = [((min(max(x.timestamp, orig_match.begin), orig_match.end) - orig_match.begin).total_seconds(), x) for x in orig_match.events] ref_time = 0.0 for i in xrange(len(events)): delta = events[i][0] - ref_time ref_time = events[i][0] events[i] = (delta / mult, events[i][1]) # Replicate the original match match = Match() match.sched_begin = datetime.datetime.now() + datetime.timedelta(seconds=0.5 * initial_wait) match.sched_end = match.sched_begin + multiply_timedelta(1.0 / mult, orig_match.sched_end - orig_match.sched_begin) match.name = "Replay of \"%s\"" % (orig_match.name) match.team_a = orig_match.team_a match.team_b = orig_match.team_b session.add(match) # Add the advantage phases phase = AdvantagePhase() phase.match = match phase.start_sec = 0 phase.advantage = 10 session.add(phase) phase = AdvantagePhase() phase.match = match phase.start_sec = 30 * 60 phase.advantage = 5 session.add(phase) phase = AdvantagePhase() phase.match = match phase.start_sec = 60 * 60 phase.advantage = 3 session.add(phase) # Replicate the player_matches for orig_pm in session.query(PlayerMatch).filter(PlayerMatch.match == orig_match): pm = PlayerMatch() pm.match = match pm.player = orig_pm.player pm.team = orig_pm.team session.add(pm) # Flush and commit session.flush() session.commit() # Print match ID and start wait print "> Feeding match with ID %d" % (match.id) print "> Scheduled times: %s -- %s" % (match.sched_begin, match.sched_end) print "> Waiting initial %f seconds..." % (initial_wait) time.sleep(initial_wait) # Set begin match.begin = datetime.datetime.now() print "> Match begins at %s" % (match.begin) session.commit() # Replay events for wait_secs, orig_ev in events: print "> Waiting %f seconds..." % (wait_secs) time.sleep(wait_secs) ev = Event() ev.timestamp = datetime.datetime.now() ev.match = match ev.type = orig_ev.type ev.source = orig_ev.source ev.team = orig_ev.team ev.player_a = orig_ev.player_a ev.player_b = orig_ev.player_b ev.red_team = orig_ev.red_team ev.blue_team = orig_ev.blue_team ev.phase = orig_ev.phase print "> Pushing event of type %s and timestamp %s" % (ev.type, ev.timestamp) session.add(ev) ev.check_type() session.commit() # Set end match.end = datetime.datetime.now() print "> Match ends at %s" % (match.end) session.commit() print "> Finished feeding match with ID %d" % (match.id) print "> Scheduled times were: %s -- %s" % (match.sched_begin, match.sched_end)
def import_from_2012(): old_session = OldSession() session = Session() # Match and teams match = Match() match.name = "24 ore 2012" first_team = session.query(Team).filter(Team.name == "Matematici").one() second_team = session.query(Team).filter(Team.name == "Fisici").one() match.team_a = first_team match.team_b = second_team # Team mapping _team_map = {} old_first_team = old_session.query(OldTeam).filter(OldTeam.name == "Matematici").one() old_second_team = old_session.query(OldTeam).filter(OldTeam.name == "Fisici").one() _team_map[old_first_team.id] = first_team _team_map[old_second_team.id] = second_team team_map = lambda x: _team_map[x.id] # Player mapping _player_map = {} player_matches = [] found_player_id = set() for old_player in old_session.query(OldPlayer).all(): player = Player.get_or_create(session, old_player.fname, old_player.lname, None) _player_map[old_player.id] = player player_match = PlayerMatch() player_match.player = player player_match.match = match player_match.team = team_map(old_player.team) player_matches.append(player_match) player_map = lambda x: _player_map[x.id] # Events events = [] for old_event in old_session.query(OldEvent).order_by(OldEvent.timestamp).all(): if old_event.type == 'sched_begin': match.sched_begin = old_event.timestamp elif old_event.type == 'begin': match.begin = old_event.timestamp elif old_event.type == 'sched_end': match.sched_end = old_event.timestamp elif old_event.type == 'end': match.end = old_event.timestamp else: event = Event() event.match = match event.timestamp = old_event.timestamp event.source = Event.EV_SOURCE_MANUAL event.type = old_event.type if old_event.type in [Event.EV_TYPE_GOAL, Event.EV_TYPE_GOAL_UNDO]: event.team = _team_map[int(old_event.param)] elif old_event.type == Event.EV_TYPE_SWAP: old_red_team_id, old_blue_team_id = map(int, old_event.param.split(',')) event.red_team = _team_map[old_red_team_id] event.blue_team = _team_map[old_blue_team_id] elif old_event.type == Event.EV_TYPE_CHANGE: old_team_id, old_player_a_id, old_player_b_id = map(int, old_event.param.split(',')) event.team = _team_map[old_team_id] event.player_a = _player_map[old_player_a_id] event.player_b = _player_map[old_player_b_id] found_player_id.add(event.player_a.id) found_player_id.add(event.player_b.id) else: assert(False, "Command not supported") events.append(event) session.add(match) for pm in player_matches: if pm.player.id in found_player_id: session.add(pm) for ev in events: session.add(ev) assert(ev.check_type()) session.flush() old_session.rollback() session.commit()
def import_log(match, session, logfile): first_team = session.query(Team).filter(Team.name == "Matematici").one() second_team = session.query(Team).filter(Team.name == "Fisici").one() events = [] first_players = set() second_players = set() def add_player(p): if not p in goalPerPlayer: goalPerPlayer[p] = 0 timePerPlayer[p] = 0 try: fname, lname = p.split(' ', 1) except ValueError: fname = p lname = '' player = Player.get_or_create(session, fname, lname, None) return player def add_pair(p): if not p in goalPerPair: goalPerPair[p] = 0 timePerPair[p] = 0 def split_and_add_players(data): playersList = data.split(',') playersList.sort() players = tuple(playersList) p1 = add_player(players[0]) p2 = add_player(players[1]) add_pair(players) return players, (p1, p2) log = dict() for line in codecs.open(logfile, encoding='utf-8'): if line.startswith("#"): continue match_ = line_re.match(line) if match_ == None: print "Bad line: %s" % (line) sys.exit(1) else: log[int(match_.group(1))] = (match_.group(2), match_.group(3)) firstGoal = 0 secondGoal = 0 firstBest = 0 secondBest = 0 swapped = False goalPerPlayer = dict() goalPerPair = dict() timePerPlayer = dict() timePerPair = dict() freqPerPlayer = dict() freqPerPair = dict() firstLastTime = None secondLastTime = None firstPlayers = (None, None) secondPlayers = (None, None) startTime = None stopTime = None lastTime = None frames = log.keys() frames.sort() for time in frames: if lastTime != None: assert(time > lastTime) lastTime = time cmd = log[time][0] data = log[time][1] goal = 0 first = None if cmd == 'TEAM_SWAP': swapped = not swapped # Event ev = Event() ev.match = match ev.timestamp = datetime.datetime.fromtimestamp(float(time) / 1000) ev.type = Event.EV_TYPE_SWAP ev.source = Event.EV_SOURCE_MANUAL ev.red_team = first_team if not swapped else second_team ev.blue_team = second_team if not swapped else first_team events.append(ev) elif cmd == 'RED_GOAL': goal = 1 first = True elif cmd == 'BLUE_GOAL': goal = 1 first = False elif cmd == 'RED_GOAL_UNDO': goal = -1 first = True elif cmd == 'BLUE_GOAL_UNDO': goal = -1 first = False elif cmd == 'FIRST_TEAM_CHANGE': if firstLastTime != None: timeDelta = time - firstLastTime timePerPair[firstPlayers] += timeDelta timePerPlayer[firstPlayers[0]] += 0.5 * timeDelta timePerPlayer[firstPlayers[1]] += 0.5 * timeDelta firstLastTime = time firstPlayers, (p1, p2) = split_and_add_players(data) first_players.add(p1) first_players.add(p2) # Event ev = Event() ev.match = match ev.timestamp = datetime.datetime.fromtimestamp(float(time) / 1000) ev.type = Event.EV_TYPE_CHANGE ev.source = Event.EV_SOURCE_MANUAL ev.team = first_team ev.player_a = p1 ev.player_b = p2 events.append(ev) elif cmd == 'SECOND_TEAM_CHANGE': if secondLastTime != None: timeDelta = time - secondLastTime timePerPair[secondPlayers] += timeDelta timePerPlayer[secondPlayers[0]] += 0.5 * timeDelta timePerPlayer[secondPlayers[1]] += 0.5 * timeDelta secondLastTime = time secondPlayers, (p1, p2) = split_and_add_players(data) second_players.add(p1) second_players.add(p2) # Event ev = Event() ev.match = match ev.timestamp = datetime.datetime.fromtimestamp(float(time) / 1000) ev.type = Event.EV_TYPE_CHANGE ev.source = Event.EV_SOURCE_MANUAL ev.team = second_team ev.player_a = p1 ev.player_b = p2 events.append(ev) elif cmd == 'START_MATCH': firstLastTime = time secondLastTime = time startTime = time # Match match.sched_begin = datetime.datetime.fromtimestamp(float(startTime) / 1000) match.begin = datetime.datetime.fromtimestamp(float(startTime) / 1000) _, first_team_name, second_team_name = data.split(',') assert(first_team_name == "Matematici") assert(second_team_name == "Fisici") match.team_a = first_team match.team_b = second_team # Initial swap ev = Event() ev.match = match ev.timestamp = match.begin ev.type = Event.EV_TYPE_SWAP ev.source = Event.EV_SOURCE_MANUAL ev.red_team = first_team ev.blue_team = second_team events.append(ev) elif cmd == 'STOP_MATCH': if firstLastTime != None: timeDelta = time - firstLastTime timePerPair[firstPlayers] += timeDelta timePerPlayer[firstPlayers[0]] += 0.5 * timeDelta timePerPlayer[firstPlayers[1]] += 0.5 * timeDelta if secondLastTime != None: timeDelta = time - secondLastTime timePerPair[secondPlayers] += timeDelta timePerPlayer[secondPlayers[0]] += 0.5 * timeDelta timePerPlayer[secondPlayers[1]] += 0.5 * timeDelta stopTime = time match.sched_end = datetime.datetime.fromtimestamp(float(stopTime) / 1000) match.end = datetime.datetime.fromtimestamp(float(stopTime) / 1000) elif cmd == 'START_LOG': pass else: print "Unknown command: %s" % (cmd) #print (first, goal) if first == None: assert(goal == 0) if first != None: ev = Event() ev.match = match ev.timestamp = datetime.datetime.fromtimestamp(float(time) / 1000) assert(goal == 1 or goal == -1) if goal == 1: ev.type = Event.EV_TYPE_GOAL else: ev.type = Event.EV_TYPE_GOAL_UNDO ev.source = Event.EV_SOURCE_MANUAL if swapped: first = not first if first: ev.team = first_team assert(goal != 0) firstGoal += goal goalPerPlayer[firstPlayers[0]] += 0.5 * goal goalPerPlayer[firstPlayers[1]] += 0.5 * goal goalPerPair[firstPlayers] += goal if firstGoal > secondGoal and firstBest < firstGoal - secondGoal: firstBest = firstGoal - secondGoal else: ev.team = second_team assert(goal != 0) secondGoal += goal goalPerPlayer[secondPlayers[0]] += 0.5 * goal goalPerPlayer[secondPlayers[1]] += 0.5 * goal goalPerPair[secondPlayers] += goal if secondGoal > firstGoal and secondBest < secondGoal - firstGoal: secondBest = secondGoal - firstGoal events.append(ev) session.add(match) # Player association for player in first_players: playermatch = PlayerMatch() playermatch.match = match playermatch.player = player playermatch.team = first_team session.add(playermatch) session.flush() for player in second_players: playermatch = PlayerMatch() playermatch.match = match playermatch.player = player playermatch.team = second_team session.add(playermatch) session.flush() for ev in events: session.add(ev) assert(ev.check_type()) session.flush() session.commit()