def test_cannot_delete_location_of_live_game_of_yours(self):
        creator_middleware = GameCreatorMiddleware(self.david.username)
        self.assertTrue(
            creator_middleware.is_authorized_to_access_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 live")
    def test_can_add_location_to_published_game_of_yours(self):
        unpublished_game_code = "13T2-JFRN"

        game_creator_middleware = GameCreatorMiddleware(self.david.username)

        # if can_change_game(code) is True, changes are allowed to the game, including adding locations
        self.assertTrue(
            game_creator_middleware.can_change_game(unpublished_game_code),
            "ERROR! Cannot add location to a game even "
            "if its not published yet")
    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")
Exemplo n.º 4
0
class LocationListView(LoginRequiredMixin, generic.TemplateView):
    template_name = 'locations.html'
    login_url = '/start'

    locations = None
    player = None
    creator = None
    form = ChangeClueForm

    def get(self, request, game_code, location_code, *args, **kwargs):
        self.locations = GameCreatorMiddleware(request.user.username)
        self.game = _GameMiddleware(game_code)
        self.maps = MapsMiddleware()

        this_location = self.locations.get_location_by_code(location_code)

        this_location_copy = self.locations.get_location_by_code(location_code)
        for x in this_location:
            location_name = str(x)

        if not self.locations.is_authorized_to_access_game(game_code):
            return handler(request, 404)

        latitude, longitude = self.maps.get_coordinate(location_name)
        latitude = float(latitude)
        longitude = float(longitude)

        return render(request,
                      self.template_name,
                      context={
                          'locations_code': this_location,
                          'game_details': self.game.get_code_and_name(),
                          'game_player_name': self.locations.get_name(),
                          'game_player_username':
                          self.locations.get_username(),
                          'lat_long': [latitude, longitude],
                          'game_code': self.game.game.code,
                          'location_code': this_location_copy.first()
                      })

    # Handling of the various post request that can be made on the locations page
    def post(self, request, game_code, location_code, *args, **kwargs):
        self.locations = GameCreatorMiddleware(request.user.username)
        self.game = _GameMiddleware(game_code)
        self.maps = MapsMiddleware()

        if not self.locations.is_authorized_to_access_game(
                request.POST['game_code']):
            return handler(request, 404)

        if 'delete_location_code' in request.POST.keys(
        ) and self.locations.can_change_game(request.POST['game_code']):
            return self._delete_location(
                request, game_code, request.POST['delete_location_code'],
                self.locations.get_location_by_code(location_code))
        if 'code' in request.POST.keys():
            return self._change_clue(request, request.POST['game_code'],
                                     request.POST['code'], *args, **kwargs)

        return handler(request, 404)

    # Delete a location from a game
    # Paramaters:
    #    game_code: the game code of game to be modified
    #    location_code: the location code of the location to be deleted
    def _delete_location(self, request, game_code, location_code, *args,
                         **kwargs):
        self.maps = MapsMiddleware()
        self.maps.delete_location(game_code, location_code)
        return HttpResponseRedirect('/game/create/' + game_code)

    # Modify the clue of a location
    # Paramaters:
    #    game_code: the game code of game to be modified
    #    location_code: the location code of the location to be modified
    #    clues: the new clue for the location
    def _change_clue(self, request, game_code, location_code, *args, **kwargs):
        self.creator = GameCreatorMiddleware(None)
        self.creator.user = request.user
        form = self.form(instance=self.creator.get_location_of_game(
            game_code=game_code, location_code=location_code),
                         data=request.POST)

        if form.is_valid():
            location = form.save(commit=False)
            location.clues = request.POST['clues']
            location.save()
            return HttpResponseRedirect('/game/create/' + game_code)

        return handler(request, 404)
Exemplo n.º 5
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'])