def print_game_results(): while True: if not game_running(): break time.sleep(1) results = get_winners() for place in sorted(results): print "Place", place, "is team", results[place]
def match_path(self): """Tries to match a path with every url in urlpatterns. Searches through the urlpatterns to find a URL matching the given path. If it finds one, it tries to call it. Then it breaks out, so it will only call the first pattern it matches. This method calls the corresponding function in urlpatterns from urls.py, so expect side effects from the game controller. It also handles serializtion of JSON and will send a 400 error if given invalid JSON. Wil send a 404 if no matching URL is found. """ # Special case connection. Shut up I know it's ugly. connect_match = re.match(r'/connect', self.path) if connect_match: self._connect_client() return # Get the data from the method try: data = self._process_POST_data() except ValueError: # Invalid JSON output = {'error': 'Invalid or non-JSON POST data recieved'} self.respond(400, output) return # Every call but connect requires authorization if not self._validate_client(data): return # Check that the game is running if not game_running(): print "Game done" output = {'error': 'Game has ended'} self.respond(404, output) self._spin_down() return for url in urlpatterns: match = re.match(url[0], self.path) # check if match is found if match: # url[2] is the function referenced in the url to call # It is called with the group dictionary from the regex # and the unrolled JSON data as keyworded arguments # A two-tuple is returned, which is unrolled and passed as # arguments to respond self.respond(*url[2](match.groupdict(), **data)) return # no url match found, send 404 output = {'error': 'API call not found'} self.respond(404, output) return