package com.game.cloudymon;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.http.*;
import com.game.cloudymon.dal.EntityDAO;
import com.game.cloudymon.entity.MonsterDB;
import com.game.cloudymon.entity.RouteMap;
import com.game.cloudymon.entity.StaticEntity.eMonsterType;
import com.game.util.Utils;
import com.game.util.ResourceReader;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@SuppressWarnings("serial")
public class AdminServlet extends HttpServlet
{
public static Logger logger = Logger.getLogger("AdminServlet");
public void doGet(HttpServletRequest req, HttpServletResponse resp)
{
try
{
doProcess(req, resp);
} catch (Exception e)
{
logger.severe(e.getMessage());
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
{
try
{
doProcess(req, resp);
} catch (Exception e)
{
logger.severe(e.getMessage());
}
}
public void doProcess(HttpServletRequest req, HttpServletResponse resp)
{
try
{
String action = req.getParameter("action").toLowerCase();
if (action == null || action.isEmpty())
{
logger.severe("Invalid action.");
return;
}
switch (action)
{
case "importmonsters":
// http://localhost:8888/admin?action=importmonsters
doImportMonsters(req, resp);
break;
case "createroutemap":
// http://localhost:8888/admin/route.jsp
doCreateRouteMap(req, resp);
break;
default:
break;
}
} catch (Exception e)
{
logger.severe(e.getMessage());
}
}
private void doImportMonsters(HttpServletRequest req, HttpServletResponse resp)
{
InputStream is = getServletContext().getResourceAsStream("/db/Pokemon.csv");
List<MonsterDB> monsterDBs = ResourceReader.readMonsterDB(is);
EntityDAO.deleteAllEntityKind(MonsterDB.class.getSimpleName());
EntityDAO.addAbstractEntities(monsterDBs);
MonsterDB monsterDB = (MonsterDB) EntityDAO.getObjByName(MonsterDB.class, "Bulbasaur", null, null);
eMonsterType type1 = eMonsterType.valueOf((monsterDB.types.get(0)));
}
private void doCreateRouteMap(HttpServletRequest req, HttpServletResponse resp)
{
String name = req.getParameter("name");
if (name == null || name.isEmpty())
return;
RouteMap routeMap = new RouteMap();
routeMap.name = name;
routeMap.lat = Double.parseDouble(req.getParameter("lat"));
routeMap.lng = Double.parseDouble(req.getParameter("lng"));
routeMap.radius = Double.parseDouble(req.getParameter("radius"));
routeMap.mapBlockWidth = Long.parseLong(req.getParameter("mapBlockWidth"));
routeMap.mapBlockHeight = Long.parseLong(req.getParameter("mapBlockHeight"));
routeMap.mapBlockSize = Long.parseLong(req.getParameter("mapBlockSize"));
routeMap.monsterIDs = Arrays.asList(Utils.parseStr2LongArr(req.getParameter("monsterIDs")));
routeMap.monsterCatchRates = Arrays.asList(Utils.parseStr2DoubleArr(req.getParameter("monsterCatchRates")));
InputStream is = getServletContext().getResourceAsStream(RouteMap.linkImgServletPrefix + name + ".csv");
//routeMap.mapMovement_B = ResourceReader.readMapMovement(is);
routeMap.mapMovementJSON = new Text(Utils.toJSONString(ResourceReader.readMapMovement(is)));
EntityDAO.addAbstractEntity(routeMap);
}
}