Beispiel #1
0
def act_init_match(session, name, team_a, team_b, sched_begin, sched_end):
    match = Match()
    match.sched_begin = sched_begin
    match.sched_end = sched_end
    match.begin = None
    match.end = None
    match.name = name
    match.team_a = team_a
    match.team_b = team_b
    session.add(match)
    session.commit()
    return match.id
Beispiel #2
0
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)
Beispiel #3
0
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()