class LeaderboardView(LoginRequiredMixin, generic.TemplateView): template_name = 'Leaderboard.html' login_url = '/start' # Post Request Values: # code: game code for Leaderboard to be displayed def get(self, request, code, *args, **kwargs): # temp game self.game_creator = GameCreatorMiddleware(request.user.username) self.game_player = GamePlayerMiddleware(request.user.username) if not self.game_creator.is_authorized_to_access_game( code) and not self.game_player.is_authorized_to_access_game( code): return handler(request, 404) self.game = _GameMiddleware(code) return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'leaderboards': self.game_creator.get_leaderboard(code) })
def test_valid_location_code_of_not_joined_game(self): user_middleware = GamePlayerMiddleware(self.david.username) # Canva location, Canva game game_code = '13T2-JFRN' # we know that if this fails, the post request won't work and hence this must be tested self.assertFalse(user_middleware.is_authorized_to_access_game(game_code), "ERROR! User can register a game " "location of a non-joined game")
class GamePlayingListView(LoginRequiredMixin, generic.TemplateView): template_name = 'play-games.html' login_url = '/start' player = None def get(self, request, code, *args, **kwargs): self.game = _GameMiddleware(code) if not self.game: return handler(request, 404) self.player = GamePlayerMiddleware(request.user.username) if not self.player.is_authorized_to_access_game(code): return handler(request, 404) self.maps = MapsMiddleware() lat_long = [] visited = self.player.locations_visited(code) for location in visited: if location[1] != "???": temp = [] latitude, longitude = self.maps.get_coordinate(location[1]) temp.append(float(latitude)) temp.append(float(longitude)) temp.append(location[1]) lat_long.append(temp) return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'visited': self.player.locations_visited(code), 'lat_long': lat_long }) # Handling of when a game player inputs a location code within a game # Post Request Values: # game_code: game code retrieved from a hidden input in the form # location_code: location code inputed by a user def post(self, request, *args, **kwargs): self.game = _GameMiddleware(request.POST['game_code']) if not self.game: return handler(request, 404) self.player = GamePlayerMiddleware(request.user.username) if not self.player.is_authorized_to_access_game( request.POST['game_code']): return handler(request, 404) error = "" if len(request.POST['location_code']) == 9: result = self.player.visit_location(request.POST['location_code'], request.POST['game_code']) if not result: error = "Invalid Location Code" else: error = "Invalid Location Code" self.maps = MapsMiddleware() lat_long = [] visited = self.player.locations_visited(request.POST['game_code']) for location in visited: if location[1] != "???": temp = [] latitude, longitude = self.maps.get_coordinate(location[1]) temp.append(float(latitude)) temp.append(float(longitude)) temp.append(location[1]) lat_long.append(temp) return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'visited': self.player.locations_visited( request.POST['game_code']), 'lat_long': lat_long, 'error': error })