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)
class LocationAdd(LoginRequiredMixin, generic.TemplateView): template_name = 'addlocation.html' login_url = '/start' creator = None def get(self, request, code, *args, **kwargs): self.game = _GameMiddleware(code) self.creator = GameCreatorMiddleware(request.user) if not self.creator.is_authorized_to_access_game(code): return handler(request, 404) return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'lat_long': [-33.865143, 151.209900], 'location_name': "" }) # Handling of the various post requests to the add location page # Paramaters: # code: the game code of game to be modified # location_order: the location to be added to the game # location_search: the location to be searched for def post(self, request, code, *args, **kwargs): self.game = _GameMiddleware(code) self.creator = GameCreatorMiddleware(request.user) self.maps = MapsMiddleware() if not self.creator.is_authorized_to_access_game(code): return handler(request, 404) if 'location_order' in request.POST.keys(): location = request.POST['location_order'].title().strip() return self._add_location(request, code, location) # Search for the location and return latitude and longitude to display to the user for confirmation elif 'locationSearch' in request.POST.keys(): location = request.POST['locationSearch'].title() try: latitude, longitude = self.maps.get_coordinate( request.POST['locationSearch']) latitude = float(latitude) longitude = float(longitude) except: latitude = -33.865143 longitude = 151.209900 location = location + " * Not Found - Please Try Again *" return render(request, self.template_name, context={ 'game_details': self.game.get_code_and_name(), 'lat_long': [latitude, longitude], 'location_name': location, 'code': code }) # Add location to a game # Paramaters: # code: the game code of game to be modified # location: the new name of the location to be added to the game def _add_location(self, request, code, location): created = self.maps.create_game_location(code, location) return HttpResponseRedirect('/game/create/' + code + "/" + created.code)
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 })