示例#1
0
 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 test_cannot_add_location_to_an_archived_game(self):
        game_code = "9XMQ-FXYJ"
        game_creator_middleware = GameCreatorMiddleware(self.david.username)
        game_creator_middleware.stop_game(game_code)

        # if can_change_game(code) is false, no changes are allowed to the game, even adding location is not allowed
        self.assertFalse(
            game_creator_middleware.can_change_game(game_code),
            "ERROR! Can add location to a game even "
            "after it is archived")
    def test_cannot_delete_location_of_archived_game_of_yours(self):
        creator_middleware = GameCreatorMiddleware(self.david.username)
        self.assertTrue(
            creator_middleware.is_authorized_to_access_game("9XMQ-FXYJ"))

        # archive the game
        creator_middleware.stop_game("9XMQ-FXYJ")

        # delete location we know that if can_change_game returns False, the post request wont succeed and the
        # location cannot be deleted
        self.assertFalse(creator_middleware.can_change_game("9XMQ-FXYJ"),
                         "ERROR! User can change game even if it's archived")
示例#4
0
class GameCreationListView(LoginRequiredMixin, generic.TemplateView):
    template_name = 'game-create.html'
    login_url = '/start'
    form = GameRenameForm

    # Depending on the game status various versions of this page will be displayed
    #    If the game is NOT PUBLISHED then game settings can be still modifed and locations can be added
    #    If the game is LIVE then the game can only be stopped
    #    If the game is ARCHIVED then no changes can be made to the game including deletion
    def get(self, request, code, *args, **kwargs):
        # temp game
        self.game_creator = GameCreatorMiddleware(request.user.username)
        game = self.game_creator.get_game(code)

        if not game:
            return handler(request, 404)

        if not self.game_creator.is_authorized_to_access_game(code):
            return handler(request, 404)

        if game.game.live:
            self.template_name = 'game-create-live.html'
        elif game.game.archived:
            self.template_name = 'game-create-archived.html'

        self.maps = MapsMiddleware()

        return render(
            request,
            self.template_name,
            context={
                'locations_code':
                self.game_creator.get_ordered_locations_of_game(code),
                'game_details':
                self.game_creator.get_code_and_name(code),
                'code':
                code,
                'lat_long':
                self.maps.get_list_of_long_lat(code)
            })

    # Handling of the various POST requests that can be made on the game create page
    def post(self, request, *args, **kwargs):

        self.game_creator = GameCreatorMiddleware(request.user.username)

        if not self.game_creator.is_authorized_to_access_game(kwargs['code']):
            return handler(request, 404)

        if not self.game_creator.can_change_game(
                kwargs['code']) and 'game_stop' not in request.POST.keys():
            return handler(request, 404)

        if 'title' in request.POST.keys() and 'code' in kwargs.keys():
            return self._update_title_post_request(request, **kwargs)
        elif 'location_order' in request.POST.keys():
            return self._update_location_order_post_request(request, **kwargs)
        elif 'game_delete' in request.POST.keys():
            return self._delete_game(request, *args, **kwargs)
        elif 'game_start' in request.POST.keys():
            return self._start_game(request, *args, **kwargs)
        elif 'game_stop' in request.POST.keys(
        ) and request.POST['game_stop'] != '':
            return self._stop_game(request, *args, **kwargs)

    # Modify the title/name of the game
    # Paramaters:
    #    code: the game code of game to be modified
    #    title: the new name of the game
    def _update_title_post_request(self, request, *args, **kwargs):
        self.maps = MapsMiddleware()
        self.game_creator = GameCreatorMiddleware(request.user.username)
        self.game = _GameMiddleware(kwargs['code'])
        self.game.change_name(request.POST['title'])
        return render(request,
                      self.template_name,
                      context={
                          'locations_code':
                          self.game_creator.get_ordered_locations_of_game(
                              kwargs['code']),
                          'game_details':
                          self.game_creator.get_code_and_name(kwargs['code']),
                          'code':
                          kwargs['code'],
                          'lat_long':
                          self.maps.get_list_of_long_lat(kwargs['code'])
                      })

    # Update the order of locations in a game
    # Paramaters:
    #    location_order: list of location codes in the new order
    #    code: the game code of game to be modified
    def _update_location_order_post_request(self, request, *args, **kwargs):
        self.maps = MapsMiddleware()
        self.game_creator = GameCreatorMiddleware(request.user.username)
        codes_order_list = request.POST['location_order'].split(',')
        self.game_creator.update_location_order(codes_order_list,
                                                kwargs['code'])
        return render(request,
                      self.template_name,
                      context={
                          'locations_code':
                          self.game_creator.get_ordered_locations_of_game(
                              kwargs['code']),
                          'game_details':
                          self.game_creator.get_code_and_name(kwargs['code']),
                          'code':
                          kwargs['code'],
                          'lat_long':
                          self.maps.get_list_of_long_lat(kwargs['code'])
                      })

    # Delete a game that has been created
    # Paramaters:
    #    game_delete: the game code of game to be deleted
    def _delete_game(self, request, *args, **kwargs):
        self.game_creator.delete_game(request.POST['game_delete'])
        return HttpResponseRedirect('/')

    # Start the game so that game players can play it
    # Paramaters:
    #    game_start: the game code of game to be started
    def _start_game(self, request, *args, **kwargs):
        self.game_creator.start_game(request.POST['game_start'])
        return HttpResponseRedirect('/game/create/' +
                                    request.POST['game_start'])

    # Stop a game that is currently being played and as such archiving it
    # Paramaters:
    #    game_stop: the game code of game to be stopped
    def _stop_game(self, request, *args, **kwargs):
        self.game_creator.stop_game(request.POST['game_stop'])
        return HttpResponseRedirect('/game/create/' +
                                    request.POST['game_stop'])
示例#5
0
 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!")