def server_command(gid, iid, pid, command, arguments):
  """ Performs the desired server command.

  Args:
    gid: The game id of the Game model for this operation.
    iid: The instance id of the GameInstance model for
      this operation.
    pid: The player id of the requesting player.
    command: The key identifying the command to execute.
    arguments: JSON representation of arguments to the command.

  If the gid and iid specify a valid game instance model it will be
  passed to the server command. In the case that the iid is empty or
  refers to a game instance that doesn't exist, a game model will be
  used. Most commands will fail if passed a game model instead of a
  game instance, but some are indifferent to the model passed to
  them.

  Unless the dynamic property do_not_put has been set to False, this
  will put the database model after the command has been
  performed. This means that server commands do not need to make
  intermediate puts of the instance model passed to them.

  Returns:
    A tuple of the model used in the server command's execution and a
    two item dictionary:
      'type': The requested command key.
      'contents': A Python value of the response value of the
        command. This varies among server commands but must always be
        able to be encoded to JSON.

  Raises:
    ValueError if the game id or player id is invalid.
    ValueError if the arguments json cannot be parsed.
    ValueError if command is not a known server command.
  """
  utils.check_gameid(gid)
  player = utils.check_playerid(pid)
  model = None
  if iid:
    model = utils.get_instance_model(gid, iid)
  if model is None:
    model = utils.get_game_model(gid)
    if model is None:
      model = Game(key_name = gid, instance_count = 0)

  arguments = simplejson.loads(arguments)
  reply = ''

  if command in command_dict:
    reply = command_dict[command](model, player, arguments)
    if 'do_not_put' not in model.dynamic_properties() or not model.do_not_put:
      model.put()
  else:
    raise ValueError("Invalid server command: %s." % command)

  if not isinstance(reply, list):
    reply = [reply]
  return model, {TYPE_KEY : command, CONTENTS_KEY: reply}
def get_instance_lists(gid, iid, pid):
  """ Return the instances that a player has been invited to and joined.

  Args:
    gid: The game id of the Game object that this method targets.
    iid: The instance id of the Game Instance object that this
      method targets.
    pid: A string containing the requesting player's email address.

  The gid and pid must be valid, but the iid can be blank. This is
  because a player must be able to query for lists of instances
  without being in one.

  Returns:
    A tuple containing a database model and a dictionary of instance
    lists.  The database model will be a Game Instance if the gid and
    iid parameters specify a valid GameInstance, otherwise the model
    will be a Game.  Instance lists are returned in the same format
    as get_instance_lists_dictionary.

  Raises:
    ValueError if the game id or player id are invalid.
  """
  utils.check_gameid(gid)
  player = utils.check_playerid(pid)
  model = game = utils.get_game_model(gid)
  if game is None:
    game = Game(key_name = gid, instance_count = 0)
    game.put()
    model = game
  elif iid:
    instance = utils.get_instance_model(gid,iid)
    if instance:
      model = instance
  instance_lists = get_instances_lists_as_dictionary(game, player)
  return model, instance_lists