diff --git a/sqlite.db b/sqlite.db index e758ab3..c461d6f 100644 Binary files a/sqlite.db and b/sqlite.db differ diff --git a/src/main/java/com/yutou/Data/Ship.java b/src/main/java/com/yutou/Data/Ship.java new file mode 100644 index 0000000..9b3779e --- /dev/null +++ b/src/main/java/com/yutou/Data/Ship.java @@ -0,0 +1,81 @@ +package com.yutou.Data; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +public class Ship { + private int level; + private String title; + private int shipCid; + private int type; + private List equipment; + + public int getLevel() { + + return level; + } + + public void setLevel(int level) { + this.level = level; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getShipCid() { + return shipCid; + } + + public void setShipCid(int shipCid) { + this.shipCid = shipCid; + } + + public List getEquipment() { + return equipment; + } + + public void setEquipment(List equipment) { + this.equipment = equipment; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + public static Ship getInstance(JSONObject json){ + try { + Ship ship=new Ship(); + ship.setLevel(json.getInt("level")); + ship.setShipCid(json.getInt("shipCid")); + ship.setTitle(json.getString("title")); + ship.setType(json.getInt("type")); + List equipment=new ArrayList<>(); + JSONArray equip=json.getJSONArray("equipment"); + for (int i = 0; i < equip.length(); i++) { + try{ + equipment.add(equip.getInt(i)+""); + }catch (Exception e){ + equipment.add(equip.getString(i)); + } + } + ship.setEquipment(equipment); + return ship; + }catch (Exception e){ + e.printStackTrace(); + System.out.println(json.toString()); + System.exit(0); + } + return null; + } +} diff --git a/src/main/java/com/yutou/controller/MapTop/MapTopController.java b/src/main/java/com/yutou/controller/MapTop/MapTopController.java new file mode 100644 index 0000000..05ed720 --- /dev/null +++ b/src/main/java/com/yutou/controller/MapTop/MapTopController.java @@ -0,0 +1,70 @@ +package com.yutou.controller.MapTop; + +import com.alibaba.fastjson.JSONArray; +import com.yutou.Data.Ship; +import com.yutou.jianrdb.Bean.GameInfoLog; +import com.yutou.jianrdb.Mapper.GameInfoLogService; +import com.yutou.maptop.Bean.MapTop; +import com.yutou.maptop.Bean.UserTeam; +import com.yutou.maptop.services.MapTopService; +import com.yutou.maptop.services.UserTeamService; +import com.yutou.utlis.JianrDataTools; +import org.json.JSONObject; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.annotation.Resource; +import java.util.List; + +@Controller +public class MapTopController { + @Resource + GameInfoLogService gameInfoLogService; + @Resource + UserTeamService userTeamService; + @Resource + MapTopService mapTopService; + + @ResponseBody + @RequestMapping("/map/update.do") + public String updateMapTop() { + updateMap(1); + return "ok"; + } + + private void updateMap(int limit) { + List list = gameInfoLogService.getGameInfoLogByType("DealNode", limit, 500); + tcapprocessData(list); + if (list.size() == 500) { + updateMap(limit + 500); + } + } + + private void tcapprocessData(List list) { + for (GameInfoLog gameInfoLog : list) { + try { + if (!userTeamService.checkData(gameInfoLog.getUsername(), JianrDataTools.getMapNode(gameInfoLog.getExt()), JianrDataTools.getShip(new JSONObject(gameInfoLog.getMessage()),JianrDataTools.SHIP_TITLE))) { + MapTop map = new MapTop(); + map.setMapid(JianrDataTools.getMapId(gameInfoLog.getExt())); + map.setMapNode(JianrDataTools.getMapNode(gameInfoLog.getExt())); + map.setShipTeam(JianrDataTools.getShip(new JSONObject(gameInfoLog.getMessage()),JianrDataTools.SHIP_TYPE)); + int id=mapTopService.insertMapTop(map); + System.out.println(">>>>>>ID="+id); + UserTeam team=new UserTeam(); + team.setMid(id); + team.setMapNode(map.getMapNode()); + team.setUname(gameInfoLog.getUsername()); + team.setEquipment(JSONArray.toJSONString(Ship.getInstance(new JSONObject(gameInfoLog.getMessage())).getEquipment())); + team.setShip(JianrDataTools.getShip(new JSONObject(gameInfoLog.getMessage()),JianrDataTools.SHIP_TITLE)); + team.setCreateTime(gameInfoLog.getClient_time()); + userTeamService.insertTeam(team); + } + }catch (Exception e){ + e.printStackTrace(); + System.exit(0); + } + + } + } +} diff --git a/src/main/java/com/yutou/controller/test.java b/src/main/java/com/yutou/controller/test.java index c5d20b9..838a900 100644 --- a/src/main/java/com/yutou/controller/test.java +++ b/src/main/java/com/yutou/controller/test.java @@ -38,25 +38,7 @@ public class test { return JSON.toJSONString(mod); } private static int index=0; - @Resource - UserTeamService daoServier; - @ResponseBody - @RequestMapping("/team/insert.do") - public String searchMod(String id) { - UserTeam team=new UserTeam(); - team.setUname("名字"+(index++)); - team.setCreateTime(new Date()); - team.setShip("aaabbbccc"+index); - daoServier.insertTeam(team); - return "ok"; - } - @ResponseBody - @RequestMapping("/team/select.do") - public String selectTeam(){ - List list= daoServier.selectById(-1); - return JSON.toJSONString(list); - } - + @RequestMapping("/redis/set.do") @ResponseBody diff --git a/src/main/java/com/yutou/jianrdb/Dao/GameInfoLogDao.java b/src/main/java/com/yutou/jianrdb/Dao/GameInfoLogDao.java index 22cbf29..2011ede 100644 --- a/src/main/java/com/yutou/jianrdb/Dao/GameInfoLogDao.java +++ b/src/main/java/com/yutou/jianrdb/Dao/GameInfoLogDao.java @@ -9,10 +9,7 @@ import java.util.List; @Qualifier("db2SqlSessionTemplate") public interface GameInfoLogDao { - @Select("select * from game_info_log where id=#{id}") - GameInfoLog getLog(@Param("id")int id); - - @Select("select * from game_info_log where type=#{type}") - List getAllByType(@Param("type")String type); + @Select("select * from game_info_log where type=#{type} limit #{limit},#{count}") + List getAllByType(@Param("type")String type,@Param("limit")int limit,@Param("count")int count); } diff --git a/src/main/java/com/yutou/jianrdb/Mapper/GameInfoLogService.java b/src/main/java/com/yutou/jianrdb/Mapper/GameInfoLogService.java index 8f2eabc..9cb23bc 100644 --- a/src/main/java/com/yutou/jianrdb/Mapper/GameInfoLogService.java +++ b/src/main/java/com/yutou/jianrdb/Mapper/GameInfoLogService.java @@ -5,13 +5,14 @@ import com.yutou.jianrdb.Dao.GameInfoLogDao; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.util.List; @Service public class GameInfoLogService { @Resource GameInfoLogDao gameInfoLogDao; - public GameInfoLog selectGameInfoLogById(int id){ - return gameInfoLogDao.getLog(id); + public List getGameInfoLogByType(String type,int limit,int count){ + return gameInfoLogDao.getAllByType(type, limit, count); } } diff --git a/src/main/java/com/yutou/maptop/Bean/MapTop.java b/src/main/java/com/yutou/maptop/Bean/MapTop.java index c09f43a..e389e67 100644 --- a/src/main/java/com/yutou/maptop/Bean/MapTop.java +++ b/src/main/java/com/yutou/maptop/Bean/MapTop.java @@ -1,11 +1,15 @@ package com.yutou.maptop.Bean; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + public class MapTop { private int id; - private int mapid; - private String mapNode; + private int mapId; + private int mapNode; private String shipTeam; - private int teamId; public int getId() { return id; @@ -16,34 +20,41 @@ public class MapTop { } public int getMapid() { - return mapid; + return mapId; } public void setMapid(int mapid) { - this.mapid = mapid; + this.mapId = mapid; } - public String getMapNode() { - return mapNode; - } - public void setMapNode(String mapNode) { - this.mapNode = mapNode; - } public String getShipTeam() { return shipTeam; } public void setShipTeam(String shipTeam) { + List list=new ArrayList<>(); + shipTeam=shipTeam.replace("[",""); + shipTeam=shipTeam.replace("]",""); + for (String ship : shipTeam.split(",")) { + list.add(Integer.parseInt(ship)); + } + Collections.sort(list); + shipTeam="["; + for (Integer integer : list) { + shipTeam+=integer+","; + } + shipTeam = shipTeam.substring(0, shipTeam.length() - 1) + "]"; + this.shipTeam = shipTeam; } - public int getTeamId() { - return teamId; + public int getMapNode() { + return mapNode; } - public void setTeamId(int teamId) { - this.teamId = teamId; + public void setMapNode(int mapNode) { + this.mapNode = mapNode; } } diff --git a/src/main/java/com/yutou/maptop/Bean/UserTeam.java b/src/main/java/com/yutou/maptop/Bean/UserTeam.java index 96b1714..5f9f8a5 100644 --- a/src/main/java/com/yutou/maptop/Bean/UserTeam.java +++ b/src/main/java/com/yutou/maptop/Bean/UserTeam.java @@ -4,7 +4,10 @@ import java.util.Date; public class UserTeam { private int id; + private int mid; + private int mapNode; private String uname; + private String equipment; private String ship; private Date createTime; @@ -39,4 +42,28 @@ public class UserTeam { public void setCreateTime(Date createTime) { this.createTime = createTime; } + + public int getMid() { + return mid; + } + + public void setMid(int mid) { + this.mid = mid; + } + + public int getMapNode() { + return mapNode; + } + + public void setMapNode(int mapNode) { + this.mapNode = mapNode; + } + + public String getEquipment() { + return equipment; + } + + public void setEquipment(String equipment) { + this.equipment = equipment; + } } diff --git a/src/main/java/com/yutou/maptop/Dao/MapDao.java b/src/main/java/com/yutou/maptop/Dao/MapDao.java index 20b01ae..1a3e34e 100644 --- a/src/main/java/com/yutou/maptop/Dao/MapDao.java +++ b/src/main/java/com/yutou/maptop/Dao/MapDao.java @@ -1,16 +1,24 @@ package com.yutou.maptop.Dao; import com.yutou.maptop.Bean.MapTop; -import org.apache.ibatis.annotations.Insert; -import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.*; import java.util.List; public interface MapDao { - @Select("select * from maptop where id=#{id}") - List selectMapByMapId(@Param("id") int id); + @Select("select * from maptop where mapId=#{mapId} limit #{limit},#{count}") + List selectMapByMapId(@Param("mapId")int mapId,@Param("limit") int limit,@Param("count")int count); - @Insert("insert into maptop(mapId,mapNode,shipTeam,teamId) values(#{mapId},#{mapNode},#{shipTeam},#{teamId})") - void insertMapTop(MapTop top); + @Insert("insert into maptop(mapId,mapNode,shipTeam) values(#{mapId},#{mapNode},#{shipTeam})") + @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id") + int insertMapTop(MapTop top); + + @Select("select count(id) from maptop where mapId=#{mapId} and mapNode=#{mapNode}") + int checkMap(MapTop top); + + @Select("select id from maptop order by id desc limit 1,1") + int getDescId(); + + @Select("select * from maptop where mapId=#{mapId} and mapNode=#{mapNode} and shipTeam=#{shipTeam}") + MapTop selectMapByData(@Param("mapId")int mapId,@Param("mapNode")int mapNode,@Param("shipTeam")String shipTeam); } diff --git a/src/main/java/com/yutou/maptop/Dao/UserTeamDao.java b/src/main/java/com/yutou/maptop/Dao/UserTeamDao.java index da935c3..33abdf0 100644 --- a/src/main/java/com/yutou/maptop/Dao/UserTeamDao.java +++ b/src/main/java/com/yutou/maptop/Dao/UserTeamDao.java @@ -1,9 +1,7 @@ package com.yutou.maptop.Dao; import com.yutou.maptop.Bean.UserTeam; -import org.apache.ibatis.annotations.Insert; -import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.*; import java.util.List; @@ -11,9 +9,15 @@ public interface UserTeamDao { @Select("select * from userTeam where id=#{id}") List selectTeamById(@Param("id") int id); - @Insert("insert into userTeam(uname,ship,createTime) values(#{uname},#{ship},#{createTime})") + @Insert("insert into userTeam(uname,ship,createTime,mid,mapNode,equipment) values(#{uname},#{ship},#{createTime},#{mid},#{mapNode},#{equipment})") void insertTeam( UserTeam param); - @Select("select * from userTeam") - List selectAll(); + @Select("select count(id) from userTeam where uname=#{uname} and ship=#{ship} and mapNode=#{mapNode}") + int checkData(UserTeam userTeam); + + + @Delete("delete from userTeam") + void deleteData(); + @Delete("DELETE FROM sqlite_sequence WHERE name = 'userTeam'") + void clear(); } diff --git a/src/main/java/com/yutou/maptop/services/MapTopService.java b/src/main/java/com/yutou/maptop/services/MapTopService.java new file mode 100644 index 0000000..2a79012 --- /dev/null +++ b/src/main/java/com/yutou/maptop/services/MapTopService.java @@ -0,0 +1,39 @@ +package com.yutou.maptop.services; + +import com.yutou.maptop.Bean.MapTop; +import com.yutou.maptop.Dao.MapDao; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +@Service +public class MapTopService { + @Resource + MapDao mapDao; + + public List selectMapByMapId(int mapId, int limit, int count) { + return mapDao.selectMapByMapId(mapId, limit, count); + } + + public int insertMapTop(MapTop top) { + int id=0; + MapTop old=mapDao.selectMapByData(top.getMapid(),top.getMapNode(),top.getShipTeam()); + if(old!=null){//检测到有对应的map数据,返回id + return old.getId(); + }else{ + try { + id=mapDao.getDescId();//获取最后的id + }catch (Exception ignored){ + + } + mapDao.insertMapTop(top); + id++; + } + return id; + } + + public int checkMap(MapTop top) { + return mapDao.checkMap(top); + } +} diff --git a/src/main/java/com/yutou/maptop/services/UserTeamService.java b/src/main/java/com/yutou/maptop/services/UserTeamService.java index 6271b88..4e8940a 100644 --- a/src/main/java/com/yutou/maptop/services/UserTeamService.java +++ b/src/main/java/com/yutou/maptop/services/UserTeamService.java @@ -12,14 +12,22 @@ public class UserTeamService { @Resource UserTeamDao dao; - public void insertTeam(UserTeam team){ + public void insertTeam(UserTeam team) { dao.insertTeam(team); } - public List selectById(int id){ - if(id==-1){ - return dao.selectAll(); - } - return dao.selectTeamById(id); + public boolean checkData(String uname, int mapId, String ship) { + if (ship == null) + return true; + UserTeam team = new UserTeam(); + team.setUname(uname); + team.setMapNode(mapId); + team.setShip(ship); + return dao.checkData(team) > 0; + } + + public void clear() { + dao.deleteData(); + dao.clear(); } } diff --git a/src/main/java/com/yutou/utlis/JianrDataTools.java b/src/main/java/com/yutou/utlis/JianrDataTools.java new file mode 100644 index 0000000..bfa827f --- /dev/null +++ b/src/main/java/com/yutou/utlis/JianrDataTools.java @@ -0,0 +1,51 @@ +package com.yutou.utlis; + +import com.yutou.Data.Ship; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +public class JianrDataTools { + public final static int SHIP_TYPE = 0; + public final static int SHIP_TITLE = 1; + public final static int SHIP_CID = 2; + + public static String getShip(JSONObject message, int type) { + String ship = null; + if(message.isNull("warReport")){ + return null; + } + JSONArray array = message.getJSONObject("warReport").getJSONArray("selfShips"); + ship = "["; + for (int i = 0; i < array.length(); i++) { + Ship sp = Ship.getInstance(array.getJSONObject(i)); + switch (type) { + case SHIP_CID: + ship += sp.getShipCid() + ","; + break; + case SHIP_TITLE: + ship += sp.getTitle() + ","; + break; + case SHIP_TYPE: + ship += sp.getType() + ","; + break; + } + + } + ship = ship.substring(0, ship.length() - 1) + "]"; + return ship; + } + + + public static int getMapId(String ext) { + JSONObject json = new JSONObject(ext); + return json.getInt("n") / 100; + } + + public static int getMapNode(String ext) { + JSONObject json = new JSONObject(ext); + return json.getInt("n"); + } +}