package com.appspot.mscheckers;
import java.io.IOException;
import java.util.Hashtable;
import java.util.UUID;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.appspot.mscheckers.core.CheckerModel;
import com.appspot.mscheckers.core.MoveTable;
import com.appspot.mscheckers.core.Position;
import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.gson.Gson;
public class Checkers extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public static Hashtable<String, User> userMap = new Hashtable<String, User>(20); //Liste des users connect�s au Channel
public static Hashtable<String, User> inSignIn = new Hashtable<String, User>(5); //Liste des users qui ont demand� un nouveau token et qui n'ont pas encore �tablit la connexion Channel
public static Hashtable<String, String> gameTokenList = new Hashtable<String, String>(20);
public static Hashtable<String, User> playerMap = new Hashtable<String, User>(40);
public static Logger log = Logger.getLogger("---------------------------Checkers---------------------------");
static{
User u = new User("123321", "JA(Joueur Automatique)");
userMap.put(u.getUserId(), u);
log.setLevel(java.util.logging.Level.INFO);
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//resp.setContentType("application/json");
resp.setContentType("text/plain");
Gson gson = new Gson();
String r="";
//AJOUTER UN FILTRE SUR L'URL POUR QUE N'IMPORTE QUI NE PUISSE PAS CONTACTER LA SERVLET
//JE CROIS QUE CE N EST PAS UNE BONNE IDEE
if(req.getParameter(Constant.ACTION) == null ){
resp.getWriter().print(r);
return;
}
String action = req.getParameter(Constant.ACTION).toString();
char cBrd[][];
char turn;
String userId;
String userName;
ChannelService channelService ;
log.info("++++++++++OnlineCHeckers++++++++++++ : "+action);
switch(action){
case Constant.NEW_GAME_TOKEN:
userId = req.getParameter(Constant.USER_ID).toString();
if(userMap.get(userId) == null)
break;
String ngToken = String.valueOf(UUID.randomUUID().getMostSignificantBits());
channelService = ChannelServiceFactory.getChannelService();
channelService.sendMessage(new ChannelMessage(userId, "{\""+Constant.MESSAGE_TYPE+"\":\""+Constant.NEW_GAME_TOKEN+"\"}"));
channelService.sendMessage(new ChannelMessage(userId, gson.toJson(ngToken)));
gameTokenList.put(ngToken, userId);
break;
case Constant.NEW_GAME:
cBrd = CheckerModel.checkerBoard();
r = gson.toJson(cBrd);
break;
case Constant.PLAY:
cBrd = gson.fromJson(req.getParameter("cBoard[][]").toString(), char[][].class);
turn = gson.fromJson(req.getParameter("player").toString(), char.class);
Position[][] mt = MoveTable.moveTable(cBrd, turn);
r = gson.toJson(mt);
break;
case Constant.NEW_TOKEN:
userId = req.getParameter(Constant.USER_ID).toString();
userName = req.getParameter(Constant.USER_NAME).toString();
User user = new User(userId, userName);
inSignIn.put(userId, user);
//XXXXXXXXXXXTest if user exist xxxxxxxxxxxxxxxxxxxxxxx
channelService = ChannelServiceFactory.getChannelService();
String token = channelService.createChannel(userId);
r = token;
break;
case Constant.CONNECTED_USER_LIST:
userId = req.getParameter(Constant.USER_ID).toString();
if( userMap.get(userId) != null){
channelService = ChannelServiceFactory.getChannelService();
channelService.sendMessage(new ChannelMessage(userId, "{\""+Constant.MESSAGE_TYPE+"\":\""+Constant.CONNECTED_USER_LIST+"\"}"));
channelService.sendMessage(new ChannelMessage(userId, gson.toJson(userMap)));
}else
r = "You are not connected!!!";
break;
}
resp.getWriter().print(r);
}
}