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 })
def test_user_cannot_join_a_game_with_invalid_code_with_invalid_format( self): user_middleware = GamePlayerMiddleware(self.david.username) self.assertFalse( user_middleware.can_join_game("QWERTYUIOP-1234567"), "ERROR! User joined a game using a valid code with invalid format!" )
def test_user_cannot_join_an_archived_game_they_created(self): user_middleware = GamePlayerMiddleware(self.david.username) game_creator_middleware = GameCreatorMiddleware(self.david.username) game_creator_middleware.stop_game("9XMQ-FXYJ") self.assertFalse( user_middleware.can_join_game("9XMQ-FXYJ"), "ERROR! User joined an archived game that they have created!")
def post(self, request, *args, **kwargs): self.player = GamePlayerMiddleware(request.user.username) self.creator = GameCreatorMiddleware(request.user.username) if {'old_password', 'new_password1', 'new_password2'}.issubset(request.POST.keys()): form = PasswordChangeForm(request.user, request.POST) if form.is_valid(): form.save() update_session_auth_hash(request, form.user) return HttpResponseRedirect('/') return render(request, self.template_name, context={ 'games_played': self.player.get_games_played(), 'games_created': self.creator.get_number_created_games(), 'name': self.player.get_name(), 'username': self.player.get_username(), 'password_form': form }) return render(request, '404.html')
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")
def get(self, request, *args, **kwargs): self.player = GamePlayerMiddleware(request.user.username) return render(request, self.template_name, context={ 'recent_game_ranks': self.player.rank_in_most_recent_games(10) })
def test_invalid_location_code_of_invalid_game(self): user_middleware = GamePlayerMiddleware(self.david.username) try: result = user_middleware.visit_location('V444-GFEN', 'W2Z0-8888') # Invalid game and location codes except Exception as e: self.assertTrue(e.__class__ == AmazingRaceApp.models.Game.DoesNotExist, "ERROR! User cannot add an invalid " "game location of a an invalud game") return self.assertFalse(True, "ERROR! Invalid game is somehow validated...")
def get(self, request, *args, **kwargs): self.player = GamePlayerMiddleware(request.user.username) games = self.player.list_played_games() game_status = [] for x in self.player.list_played_games(): game_status.append(self.player.get_status_of_game(x[0].code)) return render(request, self.template_name, context={ 'page_name': 'Played', 'game_and_status': zip(games, game_status) })
def get(self, request, *args, **kwargs): self.player = GamePlayerMiddleware(request.user.username) self.creator = GameCreatorMiddleware(request.user.username) self.password_form = PasswordChangeForm(request.user) return render(request, self.template_name, context={ 'games_played': self.player.get_games_played(), 'games_created': self.creator.get_number_created_games(), 'name': self.player.get_name(), 'username': self.player.get_username(), 'password_form': self.password_form })
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 })
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) })
class HomepageView(LoginRequiredMixin, generic.TemplateView): template_name = 'home.html' login_url = '/start' form = GameTitleForm player = None def get(self, request, *args, **kwargs): self.player = GamePlayerMiddleware(request.user.username) return render(request, self.template_name, context={ 'recent_game_ranks': self.player.rank_in_most_recent_games(10) }) # Handling of when a game player inputs a game code on the home page or creation of a new game # Post Request Values: # code: game code inputed def post(self, request, *args, **kwargs): player = GamePlayerMiddleware(request.user.username) form = self.form(request.POST) # Inputting new game code to start playing a game if 'code' in request.POST.keys() and request.POST['code'] != '': game = _GameMiddleware(request.POST['code']) if not game.game: return render(request, self.template_name, context={ 'recent_game_ranks': player.rank_in_most_recent_games(10), 'game_error': "Oops, incorrect code!" }) else: if player.can_join_game(request.POST['code']): player.join_game(request.POST['code']) return HttpResponseRedirect("/game/leaderboard/" + request.POST['code']) else: return render(request, self.template_name, context={ 'recent_game_ranks': player.rank_in_most_recent_games(10), 'game_error': "Oops, incorrect code!" }) # Inputing a game name to start creating a new game if form.is_valid(): game = form.save() game_creator = GameCreator.objects.create(game=game, creator=request.user) return redirect('create_game', game.code) return render(request, self.template_name, {'form': form})
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) })
class GamePlayedListView(LoginRequiredMixin, generic.TemplateView): template_name = 'game-list.html' login_url = '/start' player = None def get(self, request, *args, **kwargs): self.player = GamePlayerMiddleware(request.user.username) games = self.player.list_played_games() game_status = [] for x in self.player.list_played_games(): game_status.append(self.player.get_status_of_game(x[0].code)) return render(request, self.template_name, context={ 'page_name': 'Played', 'game_and_status': zip(games, game_status) })
class ProfilepageView(LoginRequiredMixin, generic.TemplateView): template_name = 'profilepage.html' login_url = '/start' player = None creator = None password_form = None def get(self, request, *args, **kwargs): self.player = GamePlayerMiddleware(request.user.username) self.creator = GameCreatorMiddleware(request.user.username) self.password_form = PasswordChangeForm(request.user) return render(request, self.template_name, context={ 'games_played': self.player.get_games_played(), 'games_created': self.creator.get_number_created_games(), 'name': self.player.get_name(), 'username': self.player.get_username(), 'password_form': self.password_form }) def post(self, request, *args, **kwargs): self.player = GamePlayerMiddleware(request.user.username) self.creator = GameCreatorMiddleware(request.user.username) if {'old_password', 'new_password1', 'new_password2'}.issubset(request.POST.keys()): form = PasswordChangeForm(request.user, request.POST) if form.is_valid(): form.save() update_session_auth_hash(request, form.user) return HttpResponseRedirect('/') return render(request, self.template_name, context={ 'games_played': self.player.get_games_played(), 'games_created': self.creator.get_number_created_games(), 'name': self.player.get_name(), 'username': self.player.get_username(), 'password_form': form }) return render(request, '404.html')
def post(self, request, *args, **kwargs): player = GamePlayerMiddleware(request.user.username) form = self.form(request.POST) # Inputting new game code to start playing a game if 'code' in request.POST.keys() and request.POST['code'] != '': game = _GameMiddleware(request.POST['code']) if not game.game: return render(request, self.template_name, context={ 'recent_game_ranks': player.rank_in_most_recent_games(10), 'game_error': "Oops, incorrect code!" }) else: if player.can_join_game(request.POST['code']): player.join_game(request.POST['code']) return HttpResponseRedirect("/game/leaderboard/" + request.POST['code']) else: return render(request, self.template_name, context={ 'recent_game_ranks': player.rank_in_most_recent_games(10), 'game_error': "Oops, incorrect code!" }) # Inputing a game name to start creating a new game if form.is_valid(): game = form.save() game_creator = GameCreator.objects.create(game=game, creator=request.user) return redirect('create_game', game.code) return render(request, self.template_name, {'form': form})
def test_valid_but_wrong_order_location_code_of_joined_game(self): user_middleware = GamePlayerMiddleware(self.david.username) result = user_middleware.visit_location('UFQ7-XMJO', 'WS30-8FA3') self.assertFalse(result, "ERROR! User can add a valid game location in the wrong order of a joined game")
def test_valid_but_wrong_order_location_code_of_created_game(self): user_middleware = GamePlayerMiddleware(self.david.username) # Deloitte location, Canva game result = user_middleware.visit_location('4GL4-BHJ3', '13T2-JFRN') self.assertFalse(result, "ERROR! User can add an out of order game location of a created game")
def test_user_cannot_join_archived_game_of_another_creator(self): user_middleware = GamePlayerMiddleware(self.david.username) game_creator_middleware = GameCreatorMiddleware(self.mustafa.username) game_creator_middleware.stop_game("RIPM-VKBR") self.assertFalse(user_middleware.can_join_game("RIPM-VKBR"), "ERROR! User joined an archived game!")
def test_invalid_location_code_of_joined_game(self): user_middleware = GamePlayerMiddleware(self.david.username) result = user_middleware.visit_location('V4H7-EEEN', 'WS30-8FA3') self.assertFalse(result, "ERROR! User can add an invalid game location of a joined game")
def test_user_cannot_join_a_live_game_they_created(self): user_middleware = GamePlayerMiddleware(self.david.username) self.assertFalse( user_middleware.can_join_game("9XMQ-FXYJ"), "ERROR! User joined a live game that they have created!")
def test_user_cannot_join_an_unpublished_they_created(self): user_middleware = GamePlayerMiddleware(self.david.username) self.assertFalse( user_middleware.can_join_game("13T2-JFRN"), "ERROR! User joined an archived game that they have created!")
def test_user_cannot_join_an_already_joined_game(self): user_middleware = GamePlayerMiddleware(self.david.username) self.assertFalse(user_middleware.can_join_game("WS30-8FA3"), "ERROR! User joined a game they are already in!")
def test_invalid_location_code_of_non_joined_game(self): user_middleware = GamePlayerMiddleware(self.david.username) result = user_middleware.visit_location('9999-GFEN', 'RIPM-VKBR') self.assertFalse(result, "ERROR! User can add a invalid game location of a mon-joined game")
def test_valid_location_code_of_wrong_non_joined_game(self): user_middleware = GamePlayerMiddleware(self.david.username) result = user_middleware.visit_location('V4H7-GFEN', 'RIPM-VKBR') self.assertFalse(result, "ERROR! User cannot add a valid game location of a joined game")
def test_user_cannot_join_a_not_published_game_of_another_creator(self): user_middleware = GamePlayerMiddleware(self.david.username) self.assertFalse( user_middleware.can_join_game("4Y1H-M4NX"), "ERROR! User can join a game that has not been published yet!")
def test_non_conventional_location_code_of_joined_game(self): user_middleware = GamePlayerMiddleware(self.david.username) result = user_middleware.visit_location('Non conventional code format....', 'WS30-8FA3') self.assertFalse(result, "ERROR! User can add a invalid game location code format of a 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 })
def test_valid_but_wrong_order_location_code_of_non_joined_game(self): user_middleware = GamePlayerMiddleware(self.david.username) # Juhu Beach location of Jewel of India game result = user_middleware.visit_location('6VK7-IG74', 'RIPM-VKBR') self.assertFalse(result, "ERROR! User can add a valid game location of wrong order to a non-joined game")
def test_user_can_join_live_game_they_are_not_in_of_another_creator(self): user_middleware = GamePlayerMiddleware(self.david.username) self.assertTrue( user_middleware.can_join_game("RIPM-VKBR"), "ERROR! User cannot join a live game they are not in!")