def test_delete_instance_and_messages():
    test_iid = test_utils.make_instance()
    instance = test_utils.get_instance_model(test_iid)
    for i in xrange(10):
        message = Message(parent=instance, sender=firstpid, msg_type="blah", recipient=firstpid, content="%d" % i)
        message.put()
    messages = Message.all(keys_only=True).ancestor(instance.key()).fetch(1000)
    assert len(messages) == 10
    test_utils.post_server_command(test_iid, "sys_delete_instance", [])
    messages = Message.all(keys_only=True).ancestor(instance.key()).fetch(1000)
    assert len(messages) == 0
def test_delete_messages():
  iid = 'instance_messages_iid'
  player = '*****@*****.**'
  game = Game.get_or_insert(key_name = gid)
  instance = GameInstance.get_or_insert(parent = game,
                                        key_name = iid,
                                        players = [player],
                                        leader = player)
  for i in xrange(50):
    message = Message(parent = instance,
                      sender = player,
                      msg_type = 'blah',
                      recipient = player,
                      content = '%d' % i)
    message.put()
  messages = Message.all(keys_only = True).ancestor(instance.key()).fetch(1000)
  assert len(messages) == 50

  instance.delete_messages('blah')
  messages = Message.all(keys_only = True).ancestor(instance.key()).fetch(1000)
  assert len(messages) == 0
def new_game_command(instance, player, arguments = None):
  """ Start a new game and reset any game in progress.

  Args:
    instance: The GameInstance database model for this operation.
    player: The player starting a new game. Must be the only player
      in the instance.
    arguments: Not used, can be any value.

  Returns:
    A list containing the number of guesses remaining, the starting
    score of the player, the player's historical high score and the
    number of games completed in the past.

  Raises:
    ValueError if there is more than 1 player in the instance
    or the player is not the current leader.
  """
  old_games = instance.get_messages_query('bac_game', player,
                                          sender = player,
                                          keys_only = True)
  db.delete(old_games)

  score = scoreboard.get_score(instance, player)
  if (score == 0):
    # Score is [high score, total score, games played]
    score = [0, 0, 0]
    scoreboard.set_score(instance, player, score)

  game = Message(parent = instance, sender = player,
                 msg_type = 'bac_game', recipient = player)
  game.bac_solution = sample(colors, solution_size)
  game.bac_guesses_remaining = starting_guesses
  game.bac_score = solution_size * starting_guesses * 2
  game.bac_last_guess = ['']
  game.bac_last_reply = ''
  game.put()

  return [game.bac_guesses_remaining, game.bac_score, score,
          game.key().id()]
def test_delete_messages():
    iid = 'instance_messages_iid'
    player = '*****@*****.**'
    game = Game.get_or_insert(key_name=gid)
    instance = GameInstance.get_or_insert(parent=game,
                                          key_name=iid,
                                          players=[player],
                                          leader=player)
    for i in xrange(50):
        message = Message(parent=instance,
                          sender=player,
                          msg_type='blah',
                          recipient=player,
                          content='%d' % i)
        message.put()
    messages = Message.all(keys_only=True).ancestor(instance.key()).fetch(1000)
    assert len(messages) == 50

    instance.delete_messages('blah')
    messages = Message.all(keys_only=True).ancestor(instance.key()).fetch(1000)
    assert len(messages) == 0
def make_new_poll_command(instance, player, arguments):
  """ Make a new poll.

  Args:
    instance: The game instance to add the poll to.
    player: The email of the player creating the poll.
    arguments: A two item list containing the question and a
      second list of 2-5 options.

  Returns:
    Returns a list with information about the poll just created.
    See get_poll_return_list for its format.

  Raises:
    ValueError if the player is not in the instance.
  """
  instance.check_player(player)
  if not arguments[0]:
    raise ValueError('Question cannot be empty')
  size = len(arguments[1])
  if size < 2 or size > 5:
    raise ValueError('Incorrect number of options for poll. ' +
                     'Must be between two and five.')

  poll = Message(parent = instance, sender = player,
                 msg_type = 'poll', recipient = '')
  poll.put()
  arguments.append(poll.key().id())
  poll.content = simplejson.dumps(arguments)
  poll.votes = [0] * size
  poll.open = True
  poll.voters = ['']
  poll.put()
  return get_poll_return_list(poll)
def get_game(iid, game_id):
    key = Key.from_path('Game', gid, 'GameInstance', iid, 'Message', game_id)
    return Message.get(key)
def get_game(iid, game_id):
    key = Key.from_path("Game", gid, "GameInstance", iid, "Message", game_id)
    return Message.get(key)
def get_poll(iid, poll_id):
    key = Key.from_path('Game', gid, 'GameInstance', iid, 'Message', poll_id)
    return Message.get(key)
def get_poll(iid, poll_id):
  key = Key.from_path('Game', gid, 'GameInstance', iid, 'Message', poll_id)
  return Message.get(key)