实现血战到底的基本逻辑,有待调试
parent
e6b409644e
commit
c62efe0565
|
@ -0,0 +1,24 @@
|
|||
package com.ljsd.jieling.handler.room;
|
||||
|
||||
import com.ljsd.jieling.handler.BaseHandler;
|
||||
import com.ljsd.jieling.logic.room.RoomManager;
|
||||
import com.ljsd.jieling.netty.cocdex.PacketNetData;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.protocols.RoomProto;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class RoomCancelMatchRequestHandler extends BaseHandler{
|
||||
@Override
|
||||
public MessageTypeProto.MessageType getMessageCode() {
|
||||
return MessageTypeProto.MessageType.ROOM_CANCEL_MATCH_REQUEST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(ISession iSession, PacketNetData netData) throws Exception {
|
||||
byte[] message = netData.parseClientProtoNetData();
|
||||
RoomProto.RoomCancelMatchRequest request = RoomProto.RoomCancelMatchRequest.parseFrom(message);
|
||||
RoomManager.removePlayer(iSession, request, MessageTypeProto.MessageType.ROOM_CANCEL_MATCH_RESPONSE_VALUE);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.ljsd.jieling.handler.room;
|
||||
|
||||
import com.ljsd.jieling.handler.BaseHandler;
|
||||
import com.ljsd.jieling.logic.room.RoomManager;
|
||||
import com.ljsd.jieling.netty.cocdex.PacketNetData;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.protocols.RoomFightProto;
|
||||
import com.ljsd.jieling.protocols.RoomProto;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class RoomGameStartRequestHandler extends BaseHandler{
|
||||
@Override
|
||||
public MessageTypeProto.MessageType getMessageCode() {
|
||||
return MessageTypeProto.MessageType.ROOM_GAME_START_REQUEST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(ISession iSession, PacketNetData netData) throws Exception {
|
||||
byte[] message = netData.parseClientProtoNetData();
|
||||
RoomFightProto.RoomGameStartRequest request = RoomFightProto.RoomGameStartRequest.parseFrom(message);
|
||||
RoomManager.startGameRequset(iSession, request, MessageTypeProto.MessageType.ROOM_GAME_START_RESPONSE_VALUE);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.ljsd.jieling.handler.room;
|
||||
|
||||
import com.ljsd.jieling.handler.BaseHandler;
|
||||
import com.ljsd.jieling.handler.map.MapLogic;
|
||||
import com.ljsd.jieling.logic.room.RoomManager;
|
||||
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.protocols.RoomProto;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class RoomMatchRequestHandler extends BaseHandler{
|
||||
@Override
|
||||
public MessageTypeProto.MessageType getMessageCode() {
|
||||
return MessageTypeProto.MessageType.ROOM_MATCH_REQUEST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(ISession iSession, PacketNetData netData) throws Exception {
|
||||
byte[] message = netData.parseClientProtoNetData();
|
||||
RoomProto.RoomMatchRequest request = RoomProto.RoomMatchRequest.parseFrom(message);
|
||||
RoomManager.addPlayer(iSession, request, MessageTypeProto.MessageType.ROOM_MATCH_RESPONSE_VALUE);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.ljsd.jieling.handler.room;
|
||||
|
||||
import com.ljsd.jieling.handler.BaseHandler;
|
||||
import com.ljsd.jieling.logic.room.RoomManager;
|
||||
import com.ljsd.jieling.netty.cocdex.PacketNetData;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.protocols.RoomProto;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class RoomStartGameReadyRequestHandler extends BaseHandler{
|
||||
@Override
|
||||
public MessageTypeProto.MessageType getMessageCode() {
|
||||
return MessageTypeProto.MessageType.ROOM_START_GAME_READY_REQUEST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(ISession iSession, PacketNetData netData) throws Exception {
|
||||
byte[] message = netData.parseClientProtoNetData();
|
||||
RoomProto.RoomStartGameReadyRequest request = RoomProto.RoomStartGameReadyRequest.parseFrom(message);
|
||||
RoomManager.ready(iSession, request, MessageTypeProto.MessageType.ROOM_START_GAME_READY_RESPONSE_VALUE);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.ljsd.jieling.handler.room;
|
||||
|
||||
import com.ljsd.jieling.handler.BaseHandler;
|
||||
import com.ljsd.jieling.logic.room.RoomManager;
|
||||
import com.ljsd.jieling.netty.cocdex.PacketNetData;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.protocols.RoomFightProto;
|
||||
import com.ljsd.jieling.protocols.RoomProto;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class RoomSyncMyselfMoveRequestHandler extends BaseHandler{
|
||||
@Override
|
||||
public MessageTypeProto.MessageType getMessageCode() {
|
||||
return MessageTypeProto.MessageType.ROOM_SYNC_MYSELF_MOVE_REQUEST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(ISession iSession, PacketNetData netData) throws Exception {
|
||||
byte[] message = netData.parseClientProtoNetData();
|
||||
RoomFightProto.RoomSyncMyselfMoveRequest request = RoomFightProto.RoomSyncMyselfMoveRequest.parseFrom(message);
|
||||
RoomManager.roomSyncMyselfMoveRequest(iSession, request, MessageTypeProto.MessageType.ROOM_SYNC_MYSELF_MOVE_RESPONSE_VALUE);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.ljsd.jieling.logic.dao;
|
||||
|
||||
public class RoomInfo {
|
||||
private boolean matching;
|
||||
private int roomId;
|
||||
|
||||
public boolean isMatching() {
|
||||
return matching;
|
||||
}
|
||||
|
||||
public void setMatching(boolean matching) {
|
||||
this.matching = matching;
|
||||
}
|
||||
|
||||
public int getRoomId() {
|
||||
return roomId;
|
||||
}
|
||||
|
||||
public void setRoomId(int roomId) {
|
||||
this.roomId = roomId;
|
||||
}
|
||||
}
|
|
@ -114,6 +114,10 @@ public class UserManager {
|
|||
return user;
|
||||
}
|
||||
|
||||
public static User getUserInMem(int uid) {
|
||||
User user = userMap.get(uid);
|
||||
return user;
|
||||
}
|
||||
public static void removeUser(int uid) {
|
||||
userMap.remove(uid);
|
||||
}
|
||||
|
|
|
@ -45,6 +45,8 @@ public class User {
|
|||
private UserMissionManager userMissionManager;
|
||||
private FriendManager friendManager;
|
||||
|
||||
private RoomInfo roomInfo;
|
||||
|
||||
//构造函数必须要声明,否则从mongodb读出来反编译成类不通过
|
||||
public User(){
|
||||
}
|
||||
|
@ -197,4 +199,11 @@ public class User {
|
|||
public FriendManager getFriendManager() {
|
||||
return friendManager;
|
||||
}
|
||||
|
||||
public RoomInfo getRoomInfo() {
|
||||
if(roomInfo == null){
|
||||
roomInfo = new RoomInfo();
|
||||
}
|
||||
return roomInfo;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,195 @@
|
|||
package com.ljsd.jieling.logic.room;
|
||||
|
||||
import com.ljsd.jieling.handler.map.TemporaryItems;
|
||||
import com.ljsd.jieling.logic.dao.UserManager;
|
||||
import com.ljsd.jieling.logic.dao.root.User;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.CommonProto;
|
||||
import com.ljsd.jieling.protocols.FightInfoProto;
|
||||
import com.ljsd.jieling.protocols.RoomFightProto;
|
||||
import com.ljsd.jieling.protocols.RoomProto;
|
||||
import com.ljsd.jieling.util.MessageUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Room {
|
||||
private int roomId;
|
||||
private RoomPlayerInfo[] roomPlayerInfos;
|
||||
private long gameStartTimestamp; //游戏开始时间戳
|
||||
//private static long MAX_GAME_LENGTH = 2*60*1000; //2分钟
|
||||
private static int MAX_GAME_LENGTH = 15*1000; //15秒测试使用
|
||||
|
||||
private int type; //房间类型
|
||||
private int mapId;
|
||||
|
||||
public Room(int roomId, int type, int[] playerArray)
|
||||
{
|
||||
this.roomId = roomId;
|
||||
this.type = type;
|
||||
roomPlayerInfos = new RoomPlayerInfo[playerArray.length];
|
||||
for(int i=0; i<playerArray.length; i++){
|
||||
roomPlayerInfos[i] = new RoomPlayerInfo(playerArray[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private RoomPlayerInfo getPlayer(int uid){
|
||||
for(int i=0; i<roomPlayerInfos.length; i++){
|
||||
if(roomPlayerInfos[i] != null && roomPlayerInfos[i].getUid() == uid){
|
||||
return roomPlayerInfos[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public boolean removeUserIsEndGame(RoomProto.RoomCancelMatchRequest request, User user)
|
||||
{
|
||||
int curUserNumber = 0;
|
||||
for(int i=0; i<roomPlayerInfos.length; i++){
|
||||
if(user.getId() == roomPlayerInfos[i].getUid()){
|
||||
//将该用户从房间中移除
|
||||
roomPlayerInfos[i] = null;
|
||||
}
|
||||
if(roomPlayerInfos[i] != null){
|
||||
curUserNumber++;
|
||||
}
|
||||
}
|
||||
|
||||
if(curUserNumber <= 1){
|
||||
//整局游戏结束
|
||||
endGame(user);
|
||||
return true;
|
||||
}
|
||||
//发送玩家离开房间广播消息
|
||||
return false;
|
||||
}
|
||||
//user 最后离开的玩家,如果为null,则游戏是到时或其他原因结束
|
||||
public void endGame(User user){
|
||||
if(user != null){
|
||||
MessageUtil.sendRoomGameEndIndication(user.getId(), roomId);
|
||||
}
|
||||
for(int i=0; i<roomPlayerInfos.length; i++){
|
||||
if(roomPlayerInfos[i] == null) {
|
||||
continue;
|
||||
}
|
||||
//发送游戏结束消息
|
||||
user = UserManager.getUserInMem(roomPlayerInfos[i].getUid());
|
||||
user.getRoomInfo().setRoomId(-1);
|
||||
|
||||
MessageUtil.sendRoomGameEndIndication(user.getId(), roomId);
|
||||
}
|
||||
}
|
||||
|
||||
public int getRoomId() {
|
||||
return roomId;
|
||||
}
|
||||
|
||||
public boolean timerRunIsEndGame(long timestamp){
|
||||
if(gameStartTimestamp <= 0){
|
||||
//当前游戏还未开始
|
||||
return false;
|
||||
}
|
||||
if(timestamp - gameStartTimestamp >= MAX_GAME_LENGTH){
|
||||
//游戏到时结束
|
||||
endGame(null);
|
||||
return true;
|
||||
}
|
||||
//其他逻辑处理,例如资源点回复
|
||||
return false;
|
||||
}
|
||||
|
||||
public void startGame(){
|
||||
gameStartTimestamp = System.currentTimeMillis();
|
||||
for(int i=0; i<roomPlayerInfos.length; i++){
|
||||
if(roomPlayerInfos[i] == null) {
|
||||
continue;
|
||||
}
|
||||
roomPlayerInfos[i].setState(RoomPlayerInfo.GAMING_STATE);
|
||||
//发送游戏开始消息
|
||||
MessageUtil.sendRoomStartGameIndication(roomPlayerInfos[i].getUid(), roomId, type);
|
||||
}
|
||||
}
|
||||
|
||||
public void ready(int uid){
|
||||
int activeNumber = 0;
|
||||
int readyNumber = 0;
|
||||
for(int i=0; i<roomPlayerInfos.length; i++){
|
||||
if(roomPlayerInfos[i] != null){
|
||||
continue;
|
||||
}
|
||||
activeNumber++;
|
||||
if(roomPlayerInfos[i].getUid() == uid){
|
||||
roomPlayerInfos[i].setState(RoomPlayerInfo.READYED_STATE);
|
||||
}
|
||||
if(roomPlayerInfos[i].getState() != RoomPlayerInfo.UNREADY_STATE){
|
||||
readyNumber++;
|
||||
}
|
||||
}
|
||||
if(activeNumber != readyNumber){
|
||||
return;
|
||||
}
|
||||
|
||||
//全部准备好了,准备开始游戏
|
||||
startGame();
|
||||
}
|
||||
|
||||
public void startGameRequset(ISession iSession, int responseMsgId, RoomFightProto.RoomGameStartRequest request){
|
||||
RoomFightProto.RoomGameStartResponse.Builder builder = RoomFightProto.RoomGameStartResponse.newBuilder();
|
||||
builder.setClientTimestamp(request.getClientTimestamp());
|
||||
builder.setServerTimestamp(System.currentTimeMillis());
|
||||
builder.setMapId(mapId);
|
||||
|
||||
CommonProto.Cell.Builder cellBuilder = CommonProto.Cell.newBuilder();
|
||||
builder.addMapList(cellBuilder.build());
|
||||
|
||||
RoomFightProto.AgentInfo.Builder selfBuilder = RoomFightProto.AgentInfo.newBuilder();
|
||||
builder.setAgentInfo(selfBuilder.build());
|
||||
|
||||
for(int i=0; i<roomPlayerInfos.length; i++){
|
||||
if(roomPlayerInfos[i] == null){
|
||||
continue;
|
||||
}
|
||||
RoomFightProto.AgentInfo.Builder otherBuilder = RoomFightProto.AgentInfo.newBuilder();
|
||||
builder.addOtherAgentInfos(otherBuilder.build());
|
||||
}
|
||||
builder.setLeftTime(Room.MAX_GAME_LENGTH);
|
||||
|
||||
CommonProto.Drop.Builder dropBuilder = CommonProto.Drop.newBuilder();
|
||||
builder.setTemporaryItems(dropBuilder.build());
|
||||
|
||||
MessageUtil.sendMessage(iSession, 0, responseMsgId, builder.build(), true);
|
||||
}
|
||||
|
||||
public void roomSyncMyselfMove(ISession iSession, RoomFightProto.RoomSyncMyselfMoveRequest request, int responseMsgId){
|
||||
RoomPlayerInfo player = getPlayer(iSession.getUid());
|
||||
boolean result = player.move(request.getTartXYsList());
|
||||
MessageUtil.sendRoomSyncMyselfMoveResponse(iSession, responseMsgId, System.currentTimeMillis());
|
||||
if(!result){
|
||||
return;
|
||||
}
|
||||
|
||||
for(int i=0; i<roomPlayerInfos.length; i++){
|
||||
if(roomPlayerInfos[i] == null){
|
||||
continue;
|
||||
}
|
||||
MessageUtil.sendRoomSyncOtherMoveIndiction(roomPlayerInfos[i].getUid(), request.getTartXYsList());
|
||||
}
|
||||
}
|
||||
|
||||
//测试使用
|
||||
public RoomPlayerInfo[] getPlayerArray()
|
||||
{
|
||||
return roomPlayerInfos;
|
||||
}
|
||||
|
||||
public int getType(){
|
||||
return type;
|
||||
}
|
||||
|
||||
public int getMapId() {
|
||||
return mapId;
|
||||
}
|
||||
|
||||
public void setMapId(int mapId) {
|
||||
this.mapId = mapId;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,204 @@
|
|||
package com.ljsd.jieling.logic.room;
|
||||
|
||||
import com.ljsd.jieling.logic.dao.UserManager;
|
||||
import com.ljsd.jieling.logic.dao.root.User;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.protocols.RoomFightProto;
|
||||
import com.ljsd.jieling.protocols.RoomProto;
|
||||
import com.ljsd.jieling.util.MessageUtil;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class RoomManager {
|
||||
//先加入的玩家,在符合规则的情况下,要先匹配上
|
||||
//value uid
|
||||
private static final List<Integer> matchingPlayerList = new ArrayList();
|
||||
private static Object lock; //线程安全锁
|
||||
|
||||
private static int PLAYER_NUMBER_IN_ROOM = 3; //单个房间的最大数量
|
||||
|
||||
public static int SIMPLEST_MATCH_RULE = 0;
|
||||
|
||||
//房间自增ID
|
||||
//private static AtomicInteger incrRoomId = new AtomicInteger(0);
|
||||
private static int incrRoomId = 1; //函数都加了synchronized,因此不需要原子类
|
||||
|
||||
//Key房间ID
|
||||
private static Map<Integer, Room> roomMap = new HashMap<>();
|
||||
private static int MAX_ROOM_NUMBER = 100;
|
||||
|
||||
//以下的所有public方法必须加synchronized
|
||||
|
||||
//有用户加入时,同时做匹配
|
||||
//matchRule 匹配规则
|
||||
public static synchronized void addPlayer(ISession iSession, RoomProto.RoomMatchRequest request, int responseMsgId) throws Exception {
|
||||
User user = UserManager.getUserInMem(iSession.getUid());
|
||||
//if(roomNumber >= MAX_ROOM_NUMBER){
|
||||
if(roomMap.size() >= MAX_ROOM_NUMBER){
|
||||
//没有剩余房间
|
||||
MessageUtil.sendErrorResponse(iSession, 0, responseMsgId, "没有剩余房间");
|
||||
return;
|
||||
}
|
||||
if(user.getRoomInfo().isMatching()){
|
||||
//已经在匹配队列中了
|
||||
MessageUtil.sendErrorResponse(iSession, 0, responseMsgId, "已经在匹配队列中了");
|
||||
return;
|
||||
}
|
||||
if(user.getRoomInfo().getRoomId() != -1){
|
||||
//已经在房间中了
|
||||
MessageUtil.sendErrorResponse(iSession, 0, responseMsgId, "已经在房间中了");
|
||||
return;
|
||||
}
|
||||
user.getRoomInfo().setMatching(true);
|
||||
matchingPlayerList.add(user.getId());
|
||||
|
||||
//默认执行最简匹配规则
|
||||
int[] playerArray = simplestMatch();
|
||||
MessageUtil.sendRoomMatchResponse(iSession, responseMsgId, request.getType(), true);
|
||||
if(playerArray == null){
|
||||
return;
|
||||
}
|
||||
|
||||
createRoom(iSession, request, playerArray);
|
||||
}
|
||||
public static synchronized void removePlayer(ISession iSession, RoomProto.RoomCancelMatchRequest request, int responseMsgId) throws Exception {
|
||||
User user = UserManager.getUserInMem(iSession.getUid());
|
||||
if(user.getRoomInfo().isMatching()){
|
||||
//使用isMatching,防止频繁遍历matchingPlayerList
|
||||
user.getRoomInfo().setMatching(false);
|
||||
matchingPlayerList.remove((Object)user.getId());
|
||||
MessageUtil.sendRoomCancelMatchResponse(iSession, responseMsgId, request.getType(), true);
|
||||
return;
|
||||
}
|
||||
if(user.getRoomInfo().getRoomId() != -1){
|
||||
MessageUtil.sendRoomCancelMatchResponse(iSession, responseMsgId, request.getType(), true);
|
||||
Room room = getRoomById(user.getRoomInfo().getRoomId());
|
||||
if(room.removeUserIsEndGame(request, user)){
|
||||
//游戏结束,移除房间
|
||||
roomMap.remove(room.getRoomId());
|
||||
}
|
||||
user.getRoomInfo().setRoomId(-1);
|
||||
return;
|
||||
}
|
||||
MessageUtil.sendErrorResponse(iSession, 0, responseMsgId, "未再匹配或房间中");
|
||||
}
|
||||
|
||||
private static int[] simplestMatch() {
|
||||
if(matchingPlayerList.size() >= PLAYER_NUMBER_IN_ROOM){
|
||||
int[] playerArray = new int[PLAYER_NUMBER_IN_ROOM];
|
||||
for(int i=0; i<PLAYER_NUMBER_IN_ROOM; i++){
|
||||
playerArray[i] = matchingPlayerList.remove(0);
|
||||
}
|
||||
return playerArray;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Room createRoom(ISession iSession, RoomProto.RoomMatchRequest roomMatchRequest, int[] playerArray)
|
||||
{
|
||||
//int roomId = incrRoomId.getAndIncrement();
|
||||
int roomId = incrRoomId++;
|
||||
Room room = new Room(roomId, roomMatchRequest.getType(), playerArray);
|
||||
roomMap.put(roomId, room);
|
||||
|
||||
for(int i=0; i<playerArray.length; i++)
|
||||
{
|
||||
User user = UserManager.getUserInMem(playerArray[i]);
|
||||
user.getRoomInfo().setMatching(false);
|
||||
user.getRoomInfo().setRoomId(roomId);
|
||||
MessageUtil.sendRoomMatchSuccessIndication(playerArray[i], room.getType(), roomId);
|
||||
}
|
||||
|
||||
return room;
|
||||
}
|
||||
|
||||
public static synchronized Room getRoomById(int roomId) {
|
||||
return roomMap.get(roomId);
|
||||
}
|
||||
|
||||
public static synchronized void timerRun(long timestamp){
|
||||
Iterator<Map.Entry<Integer, Room>> entries = roomMap.entrySet().iterator();
|
||||
while(entries.hasNext()){
|
||||
Map.Entry<Integer, Room> entry = entries.next();
|
||||
Room room = entry.getValue();
|
||||
if(room.timerRunIsEndGame(timestamp)){
|
||||
//当前游戏,技术删除房间
|
||||
entries.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void ready(ISession iSession, RoomProto.RoomStartGameReadyRequest request, int responseMsgId) throws Exception {
|
||||
User user = UserManager.getUserInMem(iSession.getUid());
|
||||
int roomId = user.getRoomInfo().getRoomId();
|
||||
if(roomId == -1){
|
||||
//没有在房间中
|
||||
MessageUtil.sendErrorResponse(iSession, 0, responseMsgId, "没有在房间中");
|
||||
return;
|
||||
}
|
||||
|
||||
Room room = getRoomById(user.getRoomInfo().getRoomId());
|
||||
if(room == null){
|
||||
//房间已经销毁,如果走到这一步,说明其他地方有错了
|
||||
MessageUtil.sendErrorResponse(iSession, 0, responseMsgId, "房间已经销毁");
|
||||
return;
|
||||
}
|
||||
|
||||
MessageUtil.sendRoomStartGameReadyResponse(iSession, responseMsgId, room.getType());
|
||||
room.ready(user.getId());
|
||||
}
|
||||
//测试使用
|
||||
public static List getMatchingPlayerList()
|
||||
{
|
||||
return matchingPlayerList;
|
||||
}
|
||||
|
||||
//客户端发起请求,获取开始游戏时的初始数据
|
||||
public static synchronized void startGameRequset(ISession iSession, RoomFightProto.RoomGameStartRequest request, int responseMsgId) throws Exception {
|
||||
User user = UserManager.getUserInMem(iSession.getUid());
|
||||
int roomId = user.getRoomInfo().getRoomId();
|
||||
if(roomId == -1){
|
||||
//没有在房间中
|
||||
MessageUtil.sendErrorResponse(iSession, 0, responseMsgId, "没有在房间中");
|
||||
return;
|
||||
}
|
||||
|
||||
Room room = getRoomById(user.getRoomInfo().getRoomId());
|
||||
if(room == null){
|
||||
//房间已经销毁,如果走到这一步,说明其他地方有错了
|
||||
MessageUtil.sendErrorResponse(iSession, 0, responseMsgId, "房间已经销毁");
|
||||
return;
|
||||
}
|
||||
|
||||
room.startGameRequset(iSession, responseMsgId, request);
|
||||
}
|
||||
|
||||
//客户端发起请求,获取开始游戏时的初始数据
|
||||
public static synchronized void roomSyncMyselfMoveRequest(ISession iSession, RoomFightProto.RoomSyncMyselfMoveRequest request, int responseMsgId) throws Exception {
|
||||
User user = UserManager.getUserInMem(iSession.getUid());
|
||||
int roomId = user.getRoomInfo().getRoomId();
|
||||
if(roomId == -1){
|
||||
//没有在房间中
|
||||
MessageUtil.sendErrorResponse(iSession, 0, responseMsgId, "没有在房间中");
|
||||
return;
|
||||
}
|
||||
|
||||
Room room = getRoomById(user.getRoomInfo().getRoomId());
|
||||
if(room == null){
|
||||
//房间已经销毁,如果走到这一步,说明其他地方有错了
|
||||
MessageUtil.sendErrorResponse(iSession, 0, responseMsgId, "房间已经销毁");
|
||||
return;
|
||||
}
|
||||
|
||||
MessageUtil.sendRoomSyncMyselfMoveResponse(iSession, responseMsgId, request.getClientTimestamp());
|
||||
|
||||
room.roomSyncMyselfMove(iSession, request, responseMsgId);
|
||||
}
|
||||
//测试使用
|
||||
public static Map<Integer, Room> getRoomMap()
|
||||
{
|
||||
return roomMap;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
package com.ljsd.jieling.logic.room;
|
||||
|
||||
import com.ljsd.jieling.protocols.RoomFightProto;
|
||||
import com.ljsd.jieling.util.MapPoint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RoomPlayerInfo {
|
||||
private int uid;
|
||||
|
||||
public static final int UNREADY_STATE = 0; //还未准备
|
||||
public static final int READYED_STATE = 1; //已准备
|
||||
public static final int GAMING_STATE = 2; //游戏中
|
||||
public static final int OFFLINE_STATE = 3; //掉线了
|
||||
private int state = UNREADY_STATE; //见上面的定义
|
||||
private int curPos;
|
||||
private long preMoveTimestamp; //上次移动时间
|
||||
private int speed; //移动速度
|
||||
private List<Integer> path; //根据目标点计算出来的移动路径
|
||||
|
||||
public RoomPlayerInfo(int uid) {
|
||||
this.uid = uid;
|
||||
state = UNREADY_STATE;
|
||||
}
|
||||
|
||||
//用户移动到新位置, 返回下一个点的坐标,用户展示客户端的转向,及模拟移动
|
||||
public boolean move(List<Integer> path){
|
||||
int nextXY = path.get(0); //移动时的下一个点
|
||||
if(MapPoint.getX(curPos) == MapPoint.getX(nextXY)){
|
||||
if(MapPoint.getY(curPos) != MapPoint.getY(nextXY) + 1 || MapPoint.getY(curPos) != MapPoint.getY(nextXY) -1){
|
||||
//下一个点不是相邻的格子,作弊了
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(MapPoint.getY(curPos) == MapPoint.getY(nextXY)){
|
||||
if(MapPoint.getX(curPos) != MapPoint.getX(nextXY) + 1 || MapPoint.getX(curPos) != MapPoint.getX(nextXY) -1){
|
||||
//下一个点不是相邻的格子,作弊了
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
this.path = path;
|
||||
return true;
|
||||
}
|
||||
|
||||
//服务器模拟移动,如果前端模拟,则不需要服务气端调用这个函数,服务器端在事件触发的时候做校验即可
|
||||
//如果位置发生变化,则返回新的当前点
|
||||
public int move(long timestamp){
|
||||
if(path.size() <= 0){
|
||||
return -1;
|
||||
}
|
||||
int nextPoint = path.get(0);
|
||||
int moveDistance = (int)((timestamp - preMoveTimestamp)*speed);
|
||||
if(moveDistance < 1){
|
||||
//格子没有发生变化
|
||||
return -1;
|
||||
}
|
||||
for(int i=0; i<moveDistance && i<path.size(); i++){
|
||||
curPos = path.remove(0);
|
||||
}
|
||||
return curPos;
|
||||
}
|
||||
public int getUid() {
|
||||
return uid;
|
||||
}
|
||||
|
||||
public void setUid(int uid) {
|
||||
this.uid = uid;
|
||||
}
|
||||
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(int state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public int getCurPos() {
|
||||
return curPos;
|
||||
}
|
||||
|
||||
public void setCurPos(int curPos) {
|
||||
this.curPos = curPos;
|
||||
}
|
||||
|
||||
//todo ……
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.ljsd.jieling.logic.room;
|
||||
|
||||
public class RoomThread extends Thread{
|
||||
private static long MAX_RUN_INTERVAL = 100;
|
||||
private static long MIN_CRITICAL_CHECK_TIME = 10;
|
||||
|
||||
private void hotfixRun()
|
||||
{
|
||||
try {
|
||||
long startTimestamp = System.currentTimeMillis();
|
||||
RoomManager.timerRun(startTimestamp);
|
||||
long endTimestamp = System.currentTimeMillis();
|
||||
|
||||
//保障运行时间间隔是100毫秒
|
||||
long sleepTime = MAX_RUN_INTERVAL-(endTimestamp-startTimestamp);
|
||||
if(sleepTime <= MIN_CRITICAL_CHECK_TIME){
|
||||
//单次运行时间过长
|
||||
}
|
||||
if(sleepTime > 0){
|
||||
Thread.sleep(sleepTime);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(true){
|
||||
hotfixRun();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package com.ljsd.jieling.util;
|
||||
|
||||
public class MapPoint {
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public MapPoint(){
|
||||
|
||||
}
|
||||
public MapPoint(int x, int y){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public static int getX(int xy){
|
||||
return xy&0xff;
|
||||
}
|
||||
public static int getY(int xy){
|
||||
return xy&0xff00>>8;
|
||||
}
|
||||
public MapPoint(int xy){
|
||||
setPoint(xy);
|
||||
}
|
||||
|
||||
public void setPoint(int xy){
|
||||
this.x = getX(xy);
|
||||
this.y = getY(xy);
|
||||
}
|
||||
|
||||
public boolean equate(MapPoint mapPoint){
|
||||
if(this.x != mapPoint.x){
|
||||
return false;
|
||||
}
|
||||
if(this.y != mapPoint.y){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean equate(int xy){
|
||||
if(this.x != getX(xy)){
|
||||
return false;
|
||||
}
|
||||
if(this.y != getY(xy)){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
}
|
|
@ -3,16 +3,16 @@ package com.ljsd.jieling.util;
|
|||
import com.google.protobuf.GeneratedMessage;
|
||||
import com.googlecode.protobuf.format.JsonFormat;
|
||||
import com.ljsd.jieling.db.mongo.MongoUtil;
|
||||
import com.ljsd.jieling.logic.OnlineUserManager;
|
||||
import com.ljsd.jieling.netty.PackageConstant;
|
||||
import com.ljsd.jieling.netty.cocdex.Tea;
|
||||
import com.ljsd.jieling.network.session.ISession;
|
||||
import com.ljsd.jieling.protocols.CommonProto;
|
||||
import com.ljsd.jieling.protocols.MessageTypeProto;
|
||||
import com.ljsd.jieling.protocols.PlayerInfoProto;
|
||||
import com.ljsd.jieling.protocols.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
|
||||
public class MessageUtil {
|
||||
|
||||
|
@ -50,6 +50,10 @@ public class MessageUtil {
|
|||
return Tea.encrypt2(bytes, secretKey);
|
||||
}
|
||||
|
||||
public static void sendMessage(int uid, int result, int msgId, GeneratedMessage generatedMessage, boolean flush) {
|
||||
ISession sessionByUid = OnlineUserManager.getSessionByUid(uid);
|
||||
sendMessage(sessionByUid, result, msgId, generatedMessage, flush);
|
||||
}
|
||||
|
||||
public static void sendMessage(ISession session, int result, int msgId, GeneratedMessage generatedMessage, boolean flush) {
|
||||
byte[] byteBuf = wrappedBuffer(session.getUid(), session.getToken(), session.getIndex(), result, msgId, generatedMessage);
|
||||
|
@ -191,5 +195,56 @@ public class MessageUtil {
|
|||
// }
|
||||
// }
|
||||
|
||||
public static void sendRoomMatchResponse(ISession session, int responseMsgId, int type, boolean result) {
|
||||
RoomProto.RoomMatchResponse.Builder builder = RoomProto.RoomMatchResponse.newBuilder();
|
||||
builder.setResult(result);
|
||||
builder.setType(type);
|
||||
sendMessage(session, 1, responseMsgId, builder.build(), true);
|
||||
}
|
||||
|
||||
public static void sendRoomMatchSuccessIndication(int uid, int type, int roomId) {
|
||||
RoomProto.RoomMatchSuccessIndication.Builder builder = RoomProto.RoomMatchSuccessIndication.newBuilder();
|
||||
builder.setRoomId(roomId);
|
||||
builder.setType(type);
|
||||
sendMessage(uid, 1, MessageTypeProto.MessageType.ROOM_MATCH_SUCCESS_INDICATION_VALUE, builder.build(), true);
|
||||
}
|
||||
|
||||
public static void sendRoomCancelMatchResponse(ISession session, int responseMsgId, int type, boolean result) {
|
||||
RoomProto.RoomMatchResponse.Builder builder = RoomProto.RoomMatchResponse.newBuilder();
|
||||
builder.setResult(result);
|
||||
builder.setType(type);
|
||||
sendMessage(session, 1, responseMsgId, builder.build(), true);
|
||||
}
|
||||
|
||||
public static void sendRoomGameEndIndication(int uid, int roomId) {
|
||||
RoomFightProto.RoomGameEndIndication.Builder builder = RoomFightProto.RoomGameEndIndication.newBuilder();
|
||||
builder.setRoomId(roomId);
|
||||
sendMessage(uid, 1, MessageTypeProto.MessageType.ROOM_GAME_END_INDICATION_VALUE, builder.build(), true);
|
||||
}
|
||||
|
||||
public static void sendRoomStartGameReadyResponse(ISession session, int responseMsgId, int type) {
|
||||
RoomProto.RoomStartGameReadyRequest.Builder builder = RoomProto.RoomStartGameReadyRequest.newBuilder();
|
||||
builder.setType(type);
|
||||
sendMessage(session, 1, responseMsgId, builder.build(), true);
|
||||
}
|
||||
|
||||
public static void sendRoomStartGameIndication(int uid, int roomId, int type) {
|
||||
RoomProto.RoomStartGameIndication.Builder builder = RoomProto.RoomStartGameIndication.newBuilder();
|
||||
builder.setRoomId(roomId);
|
||||
builder.setType(type);
|
||||
sendMessage(uid, 1, MessageTypeProto.MessageType.ROOM_START_GAME_INDICATION_VALUE, builder.build(), true);
|
||||
}
|
||||
|
||||
public static void sendRoomSyncMyselfMoveResponse(ISession session, int responseMsgId, long clientTimestamp) {
|
||||
RoomFightProto.RoomSyncMyselfMoveResponse.Builder builder = RoomFightProto.RoomSyncMyselfMoveResponse.newBuilder();
|
||||
builder.setClientTimestamp(clientTimestamp);
|
||||
builder.setServerTimestamp(System.currentTimeMillis());
|
||||
sendMessage(session, 1, responseMsgId, builder.build(), true);
|
||||
}
|
||||
public static void sendRoomSyncOtherMoveIndiction(int uid, List<Integer> path) {
|
||||
RoomFightProto.RoomSyncOtherMoveIndiction.Builder builder = RoomFightProto.RoomSyncOtherMoveIndiction.newBuilder();
|
||||
builder.setUid(uid);
|
||||
builder.addAllTartXYs(path);
|
||||
sendMessage(uid, 1, MessageTypeProto.MessageType.ROOM_SYNC_OTHER_MOVE_INDICTION_VALUE, builder.build(), true);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue