地图加优化
parent
4a2b19044a
commit
5b65323371
|
|
@ -46,7 +46,7 @@ public class LoginRequestHandler extends BaseHandler {
|
|||
.setPlayer(player)
|
||||
.build();
|
||||
try {
|
||||
MessageUtil.sendMessage(iSession, userId,msgIndex, 1, msgId+1, getPlayerInfoResponse, true);
|
||||
MessageUtil.sendMessage(iSession, 1, MessageTypeProto.MessageType.LOGIN_RESPONSE_VALUE, getPlayerInfoResponse, true);
|
||||
LOGGER.info("back to client!");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
package com.ljsd.jieling.handler.map;
|
||||
|
||||
import com.ljsd.jieling.handler.BaseHandler;
|
||||
import com.ljsd.jieling.netty.cocdex.PacketNetData;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MapEnterRequestHandler extends BaseHandler{
|
||||
@Override
|
||||
public MessageTypeProto.MessageType getMessageCode() {
|
||||
return MessageTypeProto.MessageType.MAP_ENTER_REQUEST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(ISession iSession, PacketNetData netData) throws Exception {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package com.ljsd.jieling.handler.map;
|
||||
|
||||
import com.ljsd.jieling.handler.BaseHandler;
|
||||
import com.ljsd.jieling.logic.dao.User;
|
||||
import com.ljsd.jieling.logic.dao.UserManager;
|
||||
import com.ljsd.jieling.netty.cocdex.PacketNetData;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.MapInfoProto;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.util.MessageUtil;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MapGetInfoRequestHandler extends BaseHandler {
|
||||
@Override
|
||||
public MessageTypeProto.MessageType getMessageCode() {
|
||||
return MessageTypeProto.MessageType.MAP_GET_INFO_REQUEST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(ISession iSession, PacketNetData netData) throws Exception {
|
||||
int uid = iSession.getUid();
|
||||
User user = UserManager.getUser(uid);
|
||||
MapInfoProto.MapInfo.MapGetInfoResponse mapGetInfoResponse = MapInfoProto.MapInfo.MapGetInfoResponse
|
||||
.newBuilder()
|
||||
.setMapId(user.getMapManager().getCurMapId())
|
||||
.build();
|
||||
MessageUtil.sendMessage(iSession, 1, MessageTypeProto.MessageType.MAP_GET_INFO_RESPONSE_VALUE, mapGetInfoResponse, true);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +1,59 @@
|
|||
package com.ljsd.jieling.logic.dao;
|
||||
|
||||
import com.ljsd.common.mogodb.MongoBase;
|
||||
import com.ljsd.jieling.logic.map.CMap;
|
||||
|
||||
|
||||
import com.ljsd.common.mogodb.MongoBase;
|
||||
import com.ljsd.jieling.logic.map.Cell;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
|
||||
public class MapManager extends MongoBase {
|
||||
private Map<Integer, CMap> cMapMap;
|
||||
|
||||
public MapManager(){
|
||||
cMapMap = new ConcurrentHashMap<>();
|
||||
}
|
||||
public void removeCard(int id) {
|
||||
removeString("cMapMap." + id);
|
||||
cMapMap.remove(id);
|
||||
private int curMapId;
|
||||
|
||||
private int curXY;
|
||||
|
||||
private Map<Integer, Cell> mapInfo = new ConcurrentHashMap<>();
|
||||
|
||||
private Map<Integer, List<Integer>> crossMapInfos = new ConcurrentHashMap<>();
|
||||
|
||||
public Map<Integer, Cell> getMapInfo() {
|
||||
return mapInfo;
|
||||
}
|
||||
|
||||
public void addCard(CMap cMap) throws Exception {
|
||||
cMap.init(getRoot(), getMongoKey() + ".cMapMap." + cMap.getId());
|
||||
updateString("cMapMap." + cMap.getId(), cMap);
|
||||
cMapMap.put(cMap.getId(), cMap);
|
||||
public Map<Integer, List<Integer>> getCrossMapInfos() {
|
||||
return crossMapInfos;
|
||||
}
|
||||
|
||||
|
||||
public Map<Integer, CMap> getcMapMap() {
|
||||
return cMapMap;
|
||||
public int getCurMapId() {
|
||||
return curMapId;
|
||||
}
|
||||
|
||||
public void setcMapMap(Map<Integer, CMap> cMapMap) {
|
||||
this.cMapMap = cMapMap;
|
||||
public void setCurMapId(int curMapId) throws Exception {
|
||||
updateString("curMapId", curXY);
|
||||
this.curMapId = curMapId;
|
||||
}
|
||||
|
||||
public int getCurXY() {
|
||||
return curXY;
|
||||
}
|
||||
|
||||
public void setCurXY(int curXY) throws Exception {
|
||||
updateString("curXY", curXY);
|
||||
this.curXY = curXY;
|
||||
}
|
||||
|
||||
public void addOrUpdateCell(int key, Cell cell) throws Exception {
|
||||
updateString(getMongoKey() + ".mapInfo." + key, mapInfo);
|
||||
this.mapInfo.put(key, cell);
|
||||
}
|
||||
|
||||
public void removeCell(int key) {
|
||||
removeString(getMongoKey() + ".mapInfo." + key);
|
||||
this.mapInfo.remove(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,11 +15,14 @@ public class User extends MongoRoot {
|
|||
|
||||
private MapManager mapManager;
|
||||
|
||||
public User(int uid,MapManager mapManager){
|
||||
setId(Integer.toString(uid));
|
||||
this.mapManager = mapManager;
|
||||
public User(int uid) {
|
||||
setId(String.valueOf(uid));
|
||||
playerInfoManager = new PlayerInfoManager();
|
||||
itemManager = new ItemManager();
|
||||
heroManager = new HeroManager();
|
||||
}
|
||||
|
||||
|
||||
public static void init(LjsdMongoTemplate ljsdMongoTemplate) {
|
||||
_myMongoDBPool = ljsdMongoTemplate;
|
||||
}
|
||||
|
|
@ -32,4 +35,36 @@ public class User extends MongoRoot {
|
|||
public static String getCollectionName() {
|
||||
return _COLLECTION_NAME;
|
||||
}
|
||||
|
||||
public PlayerInfoManager getPlayerInfoManager() {
|
||||
return playerInfoManager;
|
||||
}
|
||||
|
||||
public void setPlayerInfoManager(PlayerInfoManager playerInfoManager) {
|
||||
this.playerInfoManager = playerInfoManager;
|
||||
}
|
||||
|
||||
public ItemManager getItemManager() {
|
||||
return itemManager;
|
||||
}
|
||||
|
||||
public void setItemManager(ItemManager itemManager) {
|
||||
this.itemManager = itemManager;
|
||||
}
|
||||
|
||||
public HeroManager getHeroManager() {
|
||||
return heroManager;
|
||||
}
|
||||
|
||||
public void setHeroManager(HeroManager heroManager) {
|
||||
this.heroManager = heroManager;
|
||||
}
|
||||
|
||||
public MapManager getMapManager() {
|
||||
return mapManager;
|
||||
}
|
||||
|
||||
public void setMapManager(MapManager mapManager) {
|
||||
this.mapManager = mapManager;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ public class UserManager {
|
|||
user = ljsdMongoTemplate.findById(User.getCollectionName(), Integer.toString(uid), User.class);
|
||||
}
|
||||
if (user == null) {
|
||||
user = new User(uid,new MapManager());
|
||||
user = new User(uid);
|
||||
UserManager.addUser(user);
|
||||
ljsdMongoTemplate.save(user);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,60 +0,0 @@
|
|||
package com.ljsd.jieling.logic.map;
|
||||
|
||||
|
||||
|
||||
import com.ljsd.common.mogodb.MongoBase;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
|
||||
public class CMap extends MongoBase {
|
||||
|
||||
|
||||
private int id;
|
||||
|
||||
private int curMapId;
|
||||
|
||||
private int curXY;
|
||||
|
||||
private Map<Integer, Cell> mapInfo = new ConcurrentHashMap<>();
|
||||
|
||||
private Map<Integer, List<Integer>> crossMapInfos = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
public void setId(int id) throws Exception {
|
||||
updateString("id", id);
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Map<Integer, Cell> getMapInfo() {
|
||||
return mapInfo;
|
||||
}
|
||||
|
||||
public Map<Integer, List<Integer>> getCrossMapInfos() {
|
||||
return crossMapInfos;
|
||||
}
|
||||
|
||||
public int getCurMapId() {
|
||||
return curMapId;
|
||||
}
|
||||
|
||||
public void setCurMapId(int curMapId) throws Exception {
|
||||
updateString("curMapId", curXY);
|
||||
this.curMapId = curMapId;
|
||||
}
|
||||
|
||||
public int getCurXY() {
|
||||
return curXY;
|
||||
}
|
||||
|
||||
public void setCurXY(int curXY) throws Exception {
|
||||
updateString("curXY", curXY);
|
||||
this.curXY = curXY;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,14 @@
|
|||
package com.ljsd.jieling.logic.map;
|
||||
|
||||
import com.ljsd.jieling.logic.dao.MapManager;
|
||||
import com.ljsd.jieling.logic.dao.User;
|
||||
import com.ljsd.jieling.logic.dao.UserManager;
|
||||
import com.ljsd.jieling.models.SCMap;
|
||||
import com.ljsd.jieling.models.SCMapEvent;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.MapInfoProto;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.util.MessageUtil;
|
||||
import com.ljsd.jieling.util.ToolsUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
|
@ -30,32 +37,35 @@ public class MapLogic {
|
|||
}
|
||||
|
||||
/**
|
||||
* 获取地图信息
|
||||
* @param uid
|
||||
* 进地图
|
||||
* @param mapId
|
||||
*/
|
||||
public void getMapInfo(int uid) throws Exception {
|
||||
// get db
|
||||
CMap cMap = null;
|
||||
if (cMap == null) {
|
||||
initMap(uid, 1);
|
||||
public void enterMap(ISession iSession, int mapId, MessageTypeProto.MessageType messageType) throws Exception {
|
||||
int uid = iSession.getUid();
|
||||
User user = UserManager.getUser(uid);
|
||||
MapManager mapManager = user.getMapManager();
|
||||
if (mapManager == null) {
|
||||
mapManager = initMap(uid, 1);
|
||||
user.setMapManager(mapManager);
|
||||
}
|
||||
|
||||
// save db
|
||||
MapInfoProto.MapInfo.MapEnterResponse mapEnterResponse = MapInfoProto.MapInfo.MapEnterResponse
|
||||
.newBuilder()
|
||||
.build();
|
||||
MessageUtil.sendMessage(iSession, 1, messageType.getNumber(), mapEnterResponse, true);
|
||||
}
|
||||
|
||||
|
||||
private void initMap(int uid, int mapId) throws Exception {
|
||||
private MapManager initMap(int uid, int mapId) throws Exception {
|
||||
if (mapId == 0) {
|
||||
mapId = 1;
|
||||
}
|
||||
Map<Integer, SCMap> scMap = SCMap.sCMap.get(mapId);
|
||||
if (scMap == null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
CMap cMap = new CMap();
|
||||
cMap.setId(uid);
|
||||
cMap.setCurMapId(mapId);
|
||||
Map<Integer, Cell> mapInfo = cMap.getMapInfo();
|
||||
MapManager mapManager = new MapManager();
|
||||
mapManager.setCurMapId(mapId);
|
||||
Map<Integer, Cell> mapInfo = mapManager.getMapInfo();
|
||||
mapInfo.clear();
|
||||
Random random = new Random();
|
||||
for (Map.Entry<Integer, SCMap> entry : scMap.entrySet()) {
|
||||
|
|
@ -68,10 +78,11 @@ public class MapLogic {
|
|||
int xy = ToolsUtil.xy2Pos(x, y);
|
||||
SCMapEvent scMapEvent = SCMapEvent.scMapEventMap.get(scMap1.getEvent());
|
||||
if (scMapEvent.getStyle() == EventType.enter) {
|
||||
cMap.setCurXY(xy);
|
||||
mapManager.setCurXY(xy);
|
||||
}
|
||||
mapInfo.put(xy, cellValue);
|
||||
}
|
||||
return mapManager;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -82,7 +93,7 @@ public class MapLogic {
|
|||
*/
|
||||
public void finishEvent(int uid, int xy) throws Exception {
|
||||
// get db
|
||||
CMap cMap = null;
|
||||
MapManager cMap = null;
|
||||
Cell cell = cMap.getMapInfo().get(xy);
|
||||
if (cell == null) {
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -44,8 +44,8 @@ public class MessageUtil {
|
|||
}
|
||||
|
||||
|
||||
public static void sendMessage(ISession session, int uid,int index, int result, int msgId, GeneratedMessage generatedMessage, boolean flush) throws Exception {
|
||||
byte[] byteBuf = wrappedBuffer(uid, session.getToken(), index, result, msgId, generatedMessage);
|
||||
public static void sendMessage(ISession session, int result, int msgId, GeneratedMessage generatedMessage, boolean flush) throws Exception {
|
||||
byte[] byteBuf = wrappedBuffer(session.getUid(), session.getToken(), session.getIndex(), result, msgId, generatedMessage);
|
||||
if (flush) {
|
||||
session.writeAndFlush(byteBuf);
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in New Issue