/* (non-Javadoc)
* @see com.google.gwt.maeglin89273.game.ashinyballonthecross.shared.PlayerService#getLoginInfo(java.lang.String)
*/
@Override
public CheckLoginResponse checkLogin(CheckLoginRequest request) {
UserService svc=getUserService();
User user=svc.getCurrentUser();
LoginInfo loginInfo;
CheckLoginResponse response;
if(user!=null){
loginInfo=new LoginInfo(LoginInfo.Status.LOGGED_IN,svc.createLogoutURL(request.getRquestUrl()),new GoogleAccount(user.getEmail(),user.getNickname()));
DatastoreService datastore=getDatastoreService();
TransportablePlayer localPlayer=request.getLocalPlayer();
Key key=KeyFactory.createKey(Player.class.getSimpleName(), user.getEmail());
String keyString=KeyFactory.keyToString(key);
try{
Entity player=datastore.get(key);
if(localPlayer.getID().equals(player.getProperty(TransportablePlayer.ID_PROPERTY))){
//the achievement has changed
if(!player.getProperty(TransportablePlayer.ACHIEVEMENT_PROPERTY).equals(localPlayer.getEncryptedAchievements())){
//if the date is greater than client timestamp, do download. Else, do upload
if(localPlayer.getTimestamp()!=null&&((Date)player.getProperty(TransportablePlayer.DATE_PROPERTY)).compareTo(localPlayer.getTimestamp())>0){
//do download
response=doDownload(player,loginInfo,keyString);
}else{
//do upload
response=doUpload(datastore, player, loginInfo, keyString, localPlayer);
}
}else{
//local player patch
if(localPlayer.getTimestamp()==null){
localPlayer.setTimestamp((Date)player.getProperty(TransportablePlayer.DATE_PROPERTY));
}
localPlayer.setKey(keyString);
response=new CheckLoginResponse(Status.UPLOAD,loginInfo,localPlayer);
}
}else{
//do download
response=doDownload(player,loginInfo,keyString);
}
}catch(EntityNotFoundException e){
//request to create a new player
response=requestNewPlayer(keyString, loginInfo, localPlayer);
}
}else{
loginInfo=new LoginInfo(LoginInfo.Status.LOGGED_OUT, svc.createLoginURL(request.getRquestUrl()));
response=new CheckLoginResponse(Status.NOT_LOGGED_IN,loginInfo,null);
}
return response;
}