}
@Override
public PlayerCreatedResponse createNewPlayer(Player player) {
User user=getUser();
PlayerCreatedResponse response;
try{
checkLoggedIn(user);
}catch(IllegalStateException e){
response=new PlayerCreatedResponse(CreateStatus.NOT_LOGGED_IN);
}
DatastoreService datastore=getDatastoreService();
Query query=new Query(Player.class.getSimpleName()).addFilter(TransportablePlayer.ID_PROPERTY, Query.FilterOperator.EQUAL,player.getID());
if(datastore.prepare(query).asSingleEntity()==null){
// TransportablePlayer id is not duplicated. Create a new player
Entity newPlayer =new Entity(KeyFactory.stringToKey(player.getKey()));
Date createTime =new Date();
newPlayer.setProperty(TransportablePlayer.USER_PROPERTY, user);
newPlayer.setProperty(TransportablePlayer.ID_PROPERTY, player.getID());
newPlayer.setProperty(TransportablePlayer.DATE_PROPERTY, createTime);
newPlayer.setProperty(TransportablePlayer.TOTAL_PROPERTY, player.getTotal());
newPlayer.setProperty(TransportablePlayer.ACHIEVEMENT_PROPERTY, player.getEncryptedAchievements());
datastore.put(newPlayer);
LOG.info("new player \""+player.getID()+"\" is created.");
response=new PlayerCreatedResponse(createTime);
}else{
// TransportablePlayer id is duplicated. Send the status back
response=new PlayerCreatedResponse(CreateStatus.DUPLICATED);
}
return response;
}