Ejemplo n.º 1
0
def end_match_lighting():
    #turn off all lights
    change_led_set(TowerLevel.objects.all(), 'off')
    for tl in TowerLevel.objects.all():
        tl.state = 'off'
        update_test_led(tl)
        update_real_leds(tl)
Ejemplo n.º 2
0
def center_done(color, other_color, match):
    center = Tower.objects.get(name='center')
    low_center = center.towerlevel_set.get(level=1)
    if not getattr(match, other_color+'_center_active'):
        low_center.state = 'off'
    else:
        low_center.state = other_color
    low_center.save()
    update_test_led(low_center)
    update_real_leds(low_center)
    setattr(match, color+'_center_active', False)
    for tl in TowerLevel.objects.filter(tower__name__icontains=color):
        tl.state = 'green'
        tl.save()
        update_test_led(tl)
        update_real_leds(tl)

    match.save()
Ejemplo n.º 3
0
def center_done(color, other_color, match):
    center = Tower.objects.get(name='center')
    low_center = center.towerlevel_set.get(level=1)
    if not getattr(match, other_color + '_center_active'):
        low_center.state = 'off'
    else:
        low_center.state = other_color
    low_center.save()
    update_test_led(low_center)
    update_real_leds(low_center)
    setattr(match, color + '_center_active', False)
    for tl in TowerLevel.objects.filter(tower__name__icontains=color):
        tl.state = 'green'
        tl.save()
        update_test_led(tl)
        update_real_leds(tl)

    match.save()
Ejemplo n.º 4
0
def run_match():
    locs = {}
    match_state, locs['center_red_state'], locs['center_blue_state'] = 'start', 'none', 'none'
    locs['center_red_counter_us'], locs['center_blue_counter_us'] = 0, 0
    match, us = init()
    while True:
        usdiff = get_microseconds() - us
        msdiff = round(usdiff / 1000)
        sdiff = round(msdiff / 1000)
        match = Match.objects.get(id=match.id)
        bypass = False
        if locs['center_red_state'] != 'none' and locs['center_blue_state'] != 'none':
            # if both of the timers are less than 5 seconds from the end, blink together
            if msdiff > 145000 \
                    and get_microseconds()-locs['center_red_counter_us'] > 25000000 \
                    and get_microseconds()-locs['center_blue_counter_us'] > 25000000:
                bypass = True
                color_ms_diff = round((get_microseconds() - locs['center_red_counter_us']) / 1000)
                if locs['center_red_state'] == 'on' and (color_ms_diff < 25500 \
                        or 26000 < color_ms_diff < 26500 or 27000 < color_ms_diff < 27500 \
                        or 28000 < color_ms_diff < 28500):
                    locs['center_red_state'] = 'off'
                    center = Tower.objects.get(name='center')
                    low_center = center.towerlevel_set.get(level=1)
                    low_center.state = 'off'
                    update_test_led(low_center)
                    update_real_leds(low_center)
                elif locs['center_red_state'] == 'off' and (25500 < color_ms_diff < 26000 \
                        or 26500 < color_ms_diff < 27000 or 27500 < color_ms_diff < 28000 \
                        or 28500 < color_ms_diff):
                    locs['center_red_state'] = 'on'
                    center = Tower.objects.get(name='center')
                    low_center = center.towerlevel_set.get(level=1)
                    low_center.state = 'purple'
                    update_test_led(low_center)
                    update_real_leds(low_center)
        if not bypass:
            for color, other_color in [('red', 'blue'), ('blue', 'red')]:
                if getattr(match, color+'_center_active') \
                        and locs['center_'+color+'_state'] == 'none':
                    locs['center_'+color+'_state'] = 'on'
                    this_start = get_microseconds()
                    if (us + 120000000) < this_start: #120 seconds
                        this_start = us + 120000000
                    elif locs['center_'+other_color+'_state'] != 'none':
                        other_start = locs['center_'+other_color+'_counter_us']
                        if (this_start - other_start) < 5000000:
                            this_start = other_start + 5000000
                    locs['center_'+color+'_counter_us'] = this_start
                elif locs['center_'+color+'_state'] != 'none':
                    color_ms_diff = round((get_microseconds() - locs['center_'+color \
                            +'_counter_us']) / 1000)
                    if color_ms_diff > 30000: #Done
                        center_done(color, other_color, match)
                        locs['center_'+color+'_state'] = 'none'
                    elif color_ms_diff > 25000:
                        if locs['center_'+color+'_state'] == 'on' \
                            and (color_ms_diff < 25500 \
                                or 26000 < color_ms_diff < 26500 \
                                or 27000 < color_ms_diff < 27500 \
                                or 28000 < color_ms_diff < 28500):
                            locs['center_'+color+'_state'] = 'off'
                            center = Tower.objects.get(name='center')
                            low_center = center.towerlevel_set.get(level=1)
                            if not getattr(match, other_color+'_center_active'):
                                low_center.state = 'off'
                            else:
                                low_center.state = other_color
                            update_test_led(low_center)
                            update_real_leds(low_center)
                        elif locs['center_'+color+'_state'] == 'off' \
                            and (25500 < color_ms_diff < 26000 \
                                or 26500 < color_ms_diff < 27000 \
                                or 27500 < color_ms_diff < 28000 or 28500 < color_ms_diff):
                            locs['center_'+color+'_state'] = 'on'
                            center = Tower.objects.get(name='center')
                            low_center = center.towerlevel_set.get(level=1)
                            if not getattr(match, other_color+'_center_active'):
                                low_center.state = color
                            else:
                                low_center.state = 'purple'
                            update_test_led(low_center)
                            update_real_leds(low_center)

        if match_state != 'end_game' and msdiff > 120000:# last 30 seconds
            match_state = 'end_game'
            end_game()
        if msdiff > 150000:
            end_match()
            return
        time.sleep(0.05)
Ejemplo n.º 5
0
def update_from_match_event(me):
    alliance = me.alliance
    tower = me.tower
    match = me.match
    level = me.level
    if '_' in tower.name: # low or high alliance tower
        if alliance in tower.name: # alliance scoring in own goal
            tl_1 = tower.towerlevel_set.get(level=1)
            if str(level) == str(1) or tl_1.state != alliance or 'low_' in tower.name:
                tl = tl_1
            else:
                tl = tower.towerlevel_set.get(level=2)
            if tl.state == alliance or tl.state == 'off':
                me.dud = True
                me.save()
            else: # It is scorable
                if me.dud == True:
                    me.dud = False
                    me.save()
                tl.state = alliance
                tl.save()
                alliance_towers = TowerLevel.objects.filter(tower__name__contains='_'+alliance)
                alliance_towers_uncharged = alliance_towers.exclude(state=alliance)
                if not alliance_towers_uncharged.exists() \
                        and not getattr(match, alliance+'_center_active'):
                    center = Tower.objects.get(name='center')
                    low_center = center.towerlevel_set.get(level=1)
                    off_color = 'blue' if alliance=='red' else 'red'
                    setattr(match, alliance + '_center_active', True)
                    this_start = tz_aware_now()
                    if match.actual_start+datetime.timedelta(seconds=120) < this_start:
                        this_start = match.actual_start + datetime.timedelta(seconds=120)
                        if not getattr(match, off_color + '_center_active'):
                            low_center.state = alliance
                        else: low_center.state = 'purple'
                    elif not getattr(match, off_color + '_center_active'):
                        low_center.state = alliance
                    else:
                        low_center.state = 'purple'
                        other_start = getattr(match, off_color + '_center_active_start')
                        if (this_start - other_start).total_seconds() < 5.0:
                            td = datetime.timedelta(seconds=5)
                            this_start = other_start+td
                    setattr(match, alliance + '_center_active_start', this_start)
                    update_test_led(low_center)
                    update_real_leds(low_center)

                    alliance_towers.update(state='off')
                    for tl_alliance in alliance_towers:
                        update_test_led(tl_alliance)
                        update_real_leds(tl_alliance)
                    alliance_scorers = ScoringDevice.objects.filter(tower__name__contains='_'+alliance)
                    alliance_scorers.update(on_center=True)
                else:
                    update_test_led(tl)
                    update_real_leds(tl)
                if alliance == 'red':
                    match.red_score += SCORE_SETTINGS[level]
                    match.red_score_pre_penalty += SCORE_SETTINGS[level]
                else:
                    match.blue_score += SCORE_SETTINGS[level]
                    match.blue_score_pre_penalty += SCORE_SETTINGS[level]
        else: # alliance attempting to descore
            descoring_alliance = alliance
            if descoring_alliance == 'red': descored_alliance = 'blue'
            else: descored_alliance = 'red'

            try:
                tl_2 = tower.towerlevel_set.get(level=2)
            except:
                tl = tower.towerlevel_set.get(level=1)
                tl_2 = False
            else:
                if int(level) == 1 or tl_2.state != descored_alliance or 'low_' in tower.name:
                    tl = tower.towerlevel_set.get(level=1)
                else:
                    tl = tl_2

            if tl.state == descored_alliance and not \
                    (tl.level == 1 and tl_2 and tl_2.state == descored_alliance):
                tl.state = 'green'
                tl.save()
                update_test_led(tl)
                update_real_leds(tl)
                if me.dud == True:
                    me.dud = False
                    me.save()
            else:
                me.dud = True
                me.save()


    elif 'center' in tower.name: # scoring on center tower
        s = SCORE_SETTINGS[level]
        if me.undo_score: s = 0 - s
        if alliance == 'red':
            match.red_score += s
            match.red_score_pre_penalty += s
        else:
            match.blue_score += s
            match.blue_score_pre_penalty += s
    match.save()
Ejemplo n.º 6
0
def change_led_set(tls, color=None):
    for tl in tls:
        if color:
            tl.state = color
        update_test_led(tl)
        update_real_leds(tl)
Ejemplo n.º 7
0
def run_match():
    locs = {}
    match_state, locs['center_red_state'], locs[
        'center_blue_state'] = 'start', 'none', 'none'
    locs['center_red_counter_us'], locs['center_blue_counter_us'] = 0, 0
    match, us = init()
    while True:
        usdiff = get_microseconds() - us
        msdiff = round(usdiff / 1000)
        sdiff = round(msdiff / 1000)
        match = Match.objects.get(id=match.id)
        bypass = False
        if locs['center_red_state'] != 'none' and locs[
                'center_blue_state'] != 'none':
            # if both of the timers are less than 5 seconds from the end, blink together
            if msdiff > 145000 \
                    and get_microseconds()-locs['center_red_counter_us'] > 25000000 \
                    and get_microseconds()-locs['center_blue_counter_us'] > 25000000:
                bypass = True
                color_ms_diff = round(
                    (get_microseconds() - locs['center_red_counter_us']) /
                    1000)
                if locs['center_red_state'] == 'on' and (color_ms_diff < 25500 \
                        or 26000 < color_ms_diff < 26500 or 27000 < color_ms_diff < 27500 \
                        or 28000 < color_ms_diff < 28500):
                    locs['center_red_state'] = 'off'
                    center = Tower.objects.get(name='center')
                    low_center = center.towerlevel_set.get(level=1)
                    low_center.state = 'off'
                    update_test_led(low_center)
                    update_real_leds(low_center)
                elif locs['center_red_state'] == 'off' and (25500 < color_ms_diff < 26000 \
                        or 26500 < color_ms_diff < 27000 or 27500 < color_ms_diff < 28000 \
                        or 28500 < color_ms_diff):
                    locs['center_red_state'] = 'on'
                    center = Tower.objects.get(name='center')
                    low_center = center.towerlevel_set.get(level=1)
                    low_center.state = 'purple'
                    update_test_led(low_center)
                    update_real_leds(low_center)
        if not bypass:
            for color, other_color in [('red', 'blue'), ('blue', 'red')]:
                if getattr(match, color+'_center_active') \
                        and locs['center_'+color+'_state'] == 'none':
                    locs['center_' + color + '_state'] = 'on'
                    this_start = get_microseconds()
                    if (us + 120000000) < this_start:  #120 seconds
                        this_start = us + 120000000
                    elif locs['center_' + other_color + '_state'] != 'none':
                        other_start = locs['center_' + other_color +
                                           '_counter_us']
                        if (this_start - other_start) < 5000000:
                            this_start = other_start + 5000000
                    locs['center_' + color + '_counter_us'] = this_start
                elif locs['center_' + color + '_state'] != 'none':
                    color_ms_diff = round((get_microseconds() - locs['center_'+color \
                            +'_counter_us']) / 1000)
                    if color_ms_diff > 30000:  #Done
                        center_done(color, other_color, match)
                        locs['center_' + color + '_state'] = 'none'
                    elif color_ms_diff > 25000:
                        if locs['center_'+color+'_state'] == 'on' \
                            and (color_ms_diff < 25500 \
                                or 26000 < color_ms_diff < 26500 \
                                or 27000 < color_ms_diff < 27500 \
                                or 28000 < color_ms_diff < 28500):
                            locs['center_' + color + '_state'] = 'off'
                            center = Tower.objects.get(name='center')
                            low_center = center.towerlevel_set.get(level=1)
                            if not getattr(match,
                                           other_color + '_center_active'):
                                low_center.state = 'off'
                            else:
                                low_center.state = other_color
                            update_test_led(low_center)
                            update_real_leds(low_center)
                        elif locs['center_'+color+'_state'] == 'off' \
                            and (25500 < color_ms_diff < 26000 \
                                or 26500 < color_ms_diff < 27000 \
                                or 27500 < color_ms_diff < 28000 or 28500 < color_ms_diff):
                            locs['center_' + color + '_state'] = 'on'
                            center = Tower.objects.get(name='center')
                            low_center = center.towerlevel_set.get(level=1)
                            if not getattr(match,
                                           other_color + '_center_active'):
                                low_center.state = color
                            else:
                                low_center.state = 'purple'
                            update_test_led(low_center)
                            update_real_leds(low_center)

        if match_state != 'end_game' and msdiff > 120000:  # last 30 seconds
            match_state = 'end_game'
            end_game()
        if msdiff > 150000:
            end_match()
            return
        time.sleep(0.05)
Ejemplo n.º 8
0
def update_from_match_event(me):
    alliance = me.alliance
    tower = me.tower
    match = me.match
    level = me.level
    if '_' in tower.name:  # low or high alliance tower
        if alliance in tower.name:  # alliance scoring in own goal
            tl_1 = tower.towerlevel_set.get(level=1)
            if str(level) == str(
                    1) or tl_1.state != alliance or 'low_' in tower.name:
                tl = tl_1
            else:
                tl = tower.towerlevel_set.get(level=2)
            if tl.state == alliance or tl.state == 'off':
                me.dud = True
                me.save()
            else:  # It is scorable
                if me.dud == True:
                    me.dud = False
                    me.save()
                tl.state = alliance
                tl.save()
                alliance_towers = TowerLevel.objects.filter(
                    tower__name__contains='_' + alliance)
                alliance_towers_uncharged = alliance_towers.exclude(
                    state=alliance)
                if not alliance_towers_uncharged.exists() \
                        and not getattr(match, alliance+'_center_active'):
                    center = Tower.objects.get(name='center')
                    low_center = center.towerlevel_set.get(level=1)
                    off_color = 'blue' if alliance == 'red' else 'red'
                    setattr(match, alliance + '_center_active', True)
                    this_start = tz_aware_now()
                    if match.actual_start + datetime.timedelta(
                            seconds=120) < this_start:
                        this_start = match.actual_start + datetime.timedelta(
                            seconds=120)
                        if not getattr(match, off_color + '_center_active'):
                            low_center.state = alliance
                        else:
                            low_center.state = 'purple'
                    elif not getattr(match, off_color + '_center_active'):
                        low_center.state = alliance
                    else:
                        low_center.state = 'purple'
                        other_start = getattr(
                            match, off_color + '_center_active_start')
                        if (this_start - other_start).total_seconds() < 5.0:
                            td = datetime.timedelta(seconds=5)
                            this_start = other_start + td
                    setattr(match, alliance + '_center_active_start',
                            this_start)
                    update_test_led(low_center)
                    update_real_leds(low_center)

                    alliance_towers.update(state='off')
                    for tl_alliance in alliance_towers:
                        update_test_led(tl_alliance)
                        update_real_leds(tl_alliance)
                    alliance_scorers = ScoringDevice.objects.filter(
                        tower__name__contains='_' + alliance)
                    alliance_scorers.update(on_center=True)
                else:
                    update_test_led(tl)
                    update_real_leds(tl)
                if alliance == 'red':
                    match.red_score += SCORE_SETTINGS[level]
                    match.red_score_pre_penalty += SCORE_SETTINGS[level]
                else:
                    match.blue_score += SCORE_SETTINGS[level]
                    match.blue_score_pre_penalty += SCORE_SETTINGS[level]
        else:  # alliance attempting to descore
            descoring_alliance = alliance
            if descoring_alliance == 'red': descored_alliance = 'blue'
            else: descored_alliance = 'red'

            try:
                tl_2 = tower.towerlevel_set.get(level=2)
            except:
                tl = tower.towerlevel_set.get(level=1)
                tl_2 = False
            else:
                if int(
                        level
                ) == 1 or tl_2.state != descored_alliance or 'low_' in tower.name:
                    tl = tower.towerlevel_set.get(level=1)
                else:
                    tl = tl_2

            if tl.state == descored_alliance and not \
                    (tl.level == 1 and tl_2 and tl_2.state == descored_alliance):
                tl.state = 'green'
                tl.save()
                update_test_led(tl)
                update_real_leds(tl)
                if me.dud == True:
                    me.dud = False
                    me.save()
            else:
                me.dud = True
                me.save()

    elif 'center' in tower.name:  # scoring on center tower
        s = SCORE_SETTINGS[level]
        if me.undo_score: s = 0 - s
        if alliance == 'red':
            match.red_score += s
            match.red_score_pre_penalty += s
        else:
            match.blue_score += s
            match.blue_score_pre_penalty += s
    match.save()