def score_match_event(user, match, tower, key, data): if not MatchEvent.objects.filter(scorer=user, collision_id=key).exists(): me = MatchEvent(match=match, microseconds=get_microseconds(), \ scorer=user, tower=tower, alliance=data['alliance'], \ level=int(data['level']), collision_id=key, undo_score=data['undo_score']) me.save() update_from_match_event(me)
def init(): play_sound('start') match = ScoringSystem.objects.all()[0].current_match match.reset() ScoringDevice.objects.all().update(on_center=False) match.actual_start = tz_aware_now() match.save() start_match_lighting() us = get_microseconds() return match, us
def get_stats(self, confirmed): stats = {} stats['scorer'] = self.scorer.username stats['scorer_id'] = self.scorer.id stats['confirmed'] = confirmed diff = tz_aware_now() - self.last_contact stats['last_contact'] = elapsed_time(diff.seconds, separator=', ') try: diff = get_microseconds()-self.scorer.matchevent_set.all().latest('id').microseconds stats['last_event'] = elapsed_time(diff/1000000, separator=', ') except: stats['last_event'] = 'N/A' return stats
def get_stats(self, confirmed): stats = {} stats['scorer'] = self.scorer.username stats['scorer_id'] = self.scorer.id stats['confirmed'] = confirmed diff = tz_aware_now() - self.last_contact stats['last_contact'] = elapsed_time(diff.seconds, separator=', ') try: diff = get_microseconds() - self.scorer.matchevent_set.all( ).latest('id').microseconds stats['last_event'] = elapsed_time(diff / 1000000, separator=', ') except: stats['last_event'] = 'N/A' return stats
def scorekeeper(request): group = Group.objects.get(name='Scorekeepers') if group not in request.user.groups.all(): raise Http404 del(group) try: ss = ScoringSystem.objects.all()[0] except: ss = ScoringSystem() ss.save() try: match = ss.current_match alliances = match.get_alliances() except: match = None timer_dict = get_timer_dict() sd_avail = ScoringDevice.objects.filter(tower__isnull=True) towers = {} if timer_dict['match_state'] != 'pre_match': try:towers['low_red'] = ScoringDevice.objects.get(scorer=match.scorer_low_red).get_stats(match.scorer_low_red_confirmed) except: try: diff = get_microseconds() - match.scorer_low_red.match_event_set.all().latest('id').microseconds except: diff = 0 try: towers['low_red'] = {'scorer':match.scorer_low_red.username, \ 'confirmed':match.scorer_low_red_confirmed, 'last_contact': 'N/A', \ 'last_event': elapsed_time(diff/100000, separator=', ')} except: pass try: towers['high_red'] = ScoringDevice.objects.get(scorer=match.scorer_high_red).get_stats(match.scorer_high_red_confirmed) except: try: diff = get_microseconds() - match.scorer_high_red.scorer.match_event_set.all().latest('id').microseconds except: diff = 0 try: towers['high_red'] = {'scorer':match.scorer_high_red.username, \ 'confirmed':match.scorer_high_red_confirmed, 'last_contact': 'N/A', \ 'last_event': elapsed_time(diff/100000, separator=', ')} except: pass try: towers['low_blue'] = ScoringDevice.objects.get(scorer=match.scorer_low_blue).get_stats(match.scorer_low_blue_confirmed) except: try: diff = get_microseconds() - match.scorer_low_blue.match_event_set.all().latest('id').microseconds except: diff = 0 try: towers['low_blue'] = {'scorer':match.scorer_low_blue.username, \ 'confirmed':match.scorer_low_blue_confirmed, 'last_contact': 'N/A', \ 'last_event': elapsed_time(diff/100000, separator=', ')} except: pass try: towers['high_blue'] = ScoringDevice.objects.get(scorer=match.scorer_high_blue).get_stats(match.scorer_high_blue_confirmed) except: try: diff = get_microseconds() - match.scorer_high_blue.match_event_set.all().latest('id').microseconds except: diff = 0 try: towers['high_blue'] = {'scorer':match.scorer_high_blue.username, \ 'confirmed':match.scorer_high_blue_confirmed, 'last_contact': 'N/A', \ 'last_event': elapsed_time(diff/100000, separator=', ')} except: pass else: for sd in ScoringDevice.objects.filter(tower__isnull=False): try: if 'high_blue'==sd.tower.name: confirmed = match.scorer_high_blue_confirmed elif 'high_red'==sd.tower.name: confirmed = match.scorer_high_red_confirmed elif 'low_blue'==sd.tower.name: confirmed = match.scorer_low_blue_confirmed elif 'low_red'==sd.tower.name: confirmed = match.scorer_low_red_confirmed except: confirmed = False towers[sd.tower.name] = sd.get_stats(confirmed) try:del(sd) except: pass try:del(confirmed) except: pass matches = Match.objects.all() del(ss) if request.is_ajax(): sd_avail_list = [] for sd in sd_avail: sd_avail_list.append({'id': sd.scorer.id, 'username': sd.scorer.username }) try: del(sd) except: pass del(sd_avail) del(matches) del(request) locs = locals() locs['match'] = model_to_dict(match) locs['match']['match_events'] = [] for event in match.matchevent_set.all().order_by('id'): event_dict = model_to_dict(event) event_dict['scorer'] = model_to_dict(event.scorer, fields=('id', 'username')) event_dict['tower'] = model_to_dict(event.tower) locs['match']['match_events'].append(event_dict) f=('name', 'number') locs['alliances']['blue']['team_1']['t'] = model_to_dict(alliances['blue']['team_1']['t'],fields=f) locs['alliances']['blue']['team_2']['t'] = model_to_dict(alliances['blue']['team_2']['t'],fields=f) locs['alliances']['red']['team_1']['t'] = model_to_dict(alliances['red']['team_1']['t'], fields=f) locs['alliances']['red']['team_2']['t'] = model_to_dict(alliances['red']['team_2']['t'], fields=f) del(locs['match']['actual_start']) del(locs['match']['time']) del(locs['match']['red_center_active_start']) del(locs['match']['blue_center_active_start']) return HttpResponse(json.dumps(locs), 'application/json') del(request) return render_to_response('scorekeeper/scorekeeper.html', locals())
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)
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)
def scorekeeper(request): group = Group.objects.get(name='Scorekeepers') if group not in request.user.groups.all(): raise Http404 del (group) try: ss = ScoringSystem.objects.all()[0] except: ss = ScoringSystem() ss.save() try: match = ss.current_match alliances = match.get_alliances() except: match = None timer_dict = get_timer_dict() sd_avail = ScoringDevice.objects.filter(tower__isnull=True) towers = {} if timer_dict['match_state'] != 'pre_match': try: towers['low_red'] = ScoringDevice.objects.get( scorer=match.scorer_low_red).get_stats( match.scorer_low_red_confirmed) except: try: diff = get_microseconds( ) - match.scorer_low_red.match_event_set.all().latest( 'id').microseconds except: diff = 0 try: towers['low_red'] = {'scorer':match.scorer_low_red.username, \ 'confirmed':match.scorer_low_red_confirmed, 'last_contact': 'N/A', \ 'last_event': elapsed_time(diff/100000, separator=', ')} except: pass try: towers['high_red'] = ScoringDevice.objects.get( scorer=match.scorer_high_red).get_stats( match.scorer_high_red_confirmed) except: try: diff = get_microseconds( ) - match.scorer_high_red.scorer.match_event_set.all().latest( 'id').microseconds except: diff = 0 try: towers['high_red'] = {'scorer':match.scorer_high_red.username, \ 'confirmed':match.scorer_high_red_confirmed, 'last_contact': 'N/A', \ 'last_event': elapsed_time(diff/100000, separator=', ')} except: pass try: towers['low_blue'] = ScoringDevice.objects.get( scorer=match.scorer_low_blue).get_stats( match.scorer_low_blue_confirmed) except: try: diff = get_microseconds( ) - match.scorer_low_blue.match_event_set.all().latest( 'id').microseconds except: diff = 0 try: towers['low_blue'] = {'scorer':match.scorer_low_blue.username, \ 'confirmed':match.scorer_low_blue_confirmed, 'last_contact': 'N/A', \ 'last_event': elapsed_time(diff/100000, separator=', ')} except: pass try: towers['high_blue'] = ScoringDevice.objects.get( scorer=match.scorer_high_blue).get_stats( match.scorer_high_blue_confirmed) except: try: diff = get_microseconds( ) - match.scorer_high_blue.match_event_set.all().latest( 'id').microseconds except: diff = 0 try: towers['high_blue'] = {'scorer':match.scorer_high_blue.username, \ 'confirmed':match.scorer_high_blue_confirmed, 'last_contact': 'N/A', \ 'last_event': elapsed_time(diff/100000, separator=', ')} except: pass else: for sd in ScoringDevice.objects.filter(tower__isnull=False): try: if 'high_blue' == sd.tower.name: confirmed = match.scorer_high_blue_confirmed elif 'high_red' == sd.tower.name: confirmed = match.scorer_high_red_confirmed elif 'low_blue' == sd.tower.name: confirmed = match.scorer_low_blue_confirmed elif 'low_red' == sd.tower.name: confirmed = match.scorer_low_red_confirmed except: confirmed = False towers[sd.tower.name] = sd.get_stats(confirmed) try: del (sd) except: pass try: del (confirmed) except: pass matches = Match.objects.all() del (ss) if request.is_ajax(): sd_avail_list = [] for sd in sd_avail: sd_avail_list.append({ 'id': sd.scorer.id, 'username': sd.scorer.username }) try: del (sd) except: pass del (sd_avail) del (matches) del (request) locs = locals() locs['match'] = model_to_dict(match) locs['match']['match_events'] = [] for event in match.matchevent_set.all().order_by('id'): event_dict = model_to_dict(event) event_dict['scorer'] = model_to_dict(event.scorer, fields=('id', 'username')) event_dict['tower'] = model_to_dict(event.tower) locs['match']['match_events'].append(event_dict) f = ('name', 'number') locs['alliances']['blue']['team_1']['t'] = model_to_dict( alliances['blue']['team_1']['t'], fields=f) locs['alliances']['blue']['team_2']['t'] = model_to_dict( alliances['blue']['team_2']['t'], fields=f) locs['alliances']['red']['team_1']['t'] = model_to_dict( alliances['red']['team_1']['t'], fields=f) locs['alliances']['red']['team_2']['t'] = model_to_dict( alliances['red']['team_2']['t'], fields=f) del (locs['match']['actual_start']) del (locs['match']['time']) del (locs['match']['red_center_active_start']) del (locs['match']['blue_center_active_start']) return HttpResponse(json.dumps(locs), 'application/json') del (request) return render_to_response('scorekeeper/scorekeeper.html', locals())