def get_game_history(self, request): """Return the game's history of moves.""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if game: return MessageForm(message=game.history) else: raise endpoints.NotFoundException('Game not found!')
def resume_game(self, request): """ This function resumes a game that has been saved. Input: Game safe url key""" game = utils.get_by_urlsafe(request.urlsafekey, Game) if game.status == "save": game.status = "start" game.put() return MessageForm(message=" The game has been resume") else: raise endpoints.ConflictException('Cannot resume a Game over')
def save_game(self, request): """ This function saves a game; The game can be played later. Input: Game safe url key""" game = utils.get_by_urlsafe(request.urlsafekey, Game) if game.status != "over": game.status = "save" game.put() return MessageForm(message=" The game has been save") else: raise endpoints.ConflictException('Cannot save a Game over')
def cancel_game(self, request): """This function delete a game. Input: Game safe url key""" game = utils.get_by_urlsafe(request.urlsafekey, Game) if game.status == "over": raise endpoints.ConflictException('Cannot delete a Game over') for hist in GameHistory.query(GameHistory.game == game.key): hist.key.delete() game.key.delete() return MessageForm(message=" Game cancelled")
def cancel_game(self, request): """Cancel the game.""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if game: if game.game_over: raise endpoints.BadRequestException('Game is already over.') else: game.key.delete() return MessageForm(message='Game deleted.') else: raise endpoints.NotFoundException('Game not found!')
def set_firstPlayer(self, request): """ This function sets the first player of a given game. Input: game safe url key, player username""" game = utils.get_by_urlsafe(request.game_urlsafekey, Game) player = utils.get_by_username(request.player) position = Position.query(Position.game == game.key, Position.player == player.key).get() position.isPlayingNow = True position.put() message = "Player %s token is set %s" % (player.username, "True") return MessageForm(message=message)
def send(request): ''' Checks if it is a POST request, and if valid creates a new private message. ''' menu_items=MenuItem.objects.all() if request.method == 'POST': # If the form has been submitted... form = MessageForm(request.POST) # A form bound to the POST data if form.is_valid(): #Get the needed content userto=form.cleaned_data['player_to'] theuser=User.objects.get(username=userto) sendcontent=form.cleaned_data['mcontent'] now=datetime.datetime.now() #Created a new message newmsg=PrivateMessage(sender=request.user, receiver=theuser, send_date=now, content=sendcontent) newmsg.save() #Redirect to the dashboad return HttpResponseRedirect('/dashboard') else: #If its not a POST request, reply generate and reply a custom error message return render_to_response('cannot.html', {'menu_items':menu_items})
def signIn(self, request): """ This function register a new user as player. Input: A unique username and email address""" username = request.username email = request.email if not utils.valid_username(username) or not utils.valid_email(email): raise endpoints.ConflictException('Invalid Input') if Player.query(Player.username == request.username).get(): raise endpoints.ConflictException( 'A User with that name already exists!') Player(username=username, email=email).put() # create default player if not exist if not Player.query(Player.username == "computer").get(): self._defaultPlayer() return MessageForm(message="Welcome Player %s. Enjoy racing!!!" % username)
def roll_dice(self, request): """ The function renders a random integer between 1 and 6. """ return MessageForm(message=str(random.randint(1, 6)))