public void MessageReceive(UserToken token, SocketModel message) { if (!CacheProxy.User.IsOnline(token)) { return; } switch (message.command) { //请求开始匹配 case MatchProtocol.StartMatch_CReq: { ResponseStartMatchModel result = BizProxy.Match.StartMatch(token, (SeverConst.GameType)message.GetMessage <int>()); token.write(TypeProtocol.Match, MatchProtocol.StartMatch_SRes, result); } break; //请求离开匹配 case MatchProtocol.StartMatch_SRes: { int result = BizProxy.Match.LevelMatch(token); token.write(TypeProtocol.Match, MatchProtocol.LevelMatch_SRes, result); } break; ; } }
/// <summary> /// 添加一个匹配,添加到匹配队列 /// </summary> /// <param name="userid"></param> /// <param name="type"></param> public void AddMatch(int userid, SeverConst.GameType type, ref ResponseStartMatchModel info) { //创建一个新的匹配模型 Lazy <MatchModel> model = null; if (matchDict.Count > 0) { //创建一条匹配队列的房间号列表 List <int> roomID = new List <int>(matchDict.Keys); for (int i = 0; i < roomID.Count; i++) { //当目前游戏类型和匹配的类型相同 if (matchDict[roomID[i]].Value.Type == type) { //当房间人数未满 if (matchDict[roomID[i]].Value.Team.Count < matchDict[roomID[i]].Value.MaxPlayer) { //添加返回给客户端的信息 matchDict[roomID[i]].Value.Team.Add(userid); info.PlayerCount = model.Value.Team.Count; info.MaxPlayer = model.Value.MaxPlayer; info.Type = model.Value.Type; //添加玩家到房间的映射 user2MatchDict.TryAdd(userid, model.Value.RoomID); DebugUtil.Instance.Log2Time(string.Format("{0} 请求链接匹配失败,队列号", model.Value.RoomID)); IsFnish(roomID[i]); return; } } } } else//创建一个新的队列 { model = new Lazy <MatchModel>(); //设定当前开局的数量 model.Value.MaxPlayer = 2; model.Value.RoomID = GetMatchId(); model.Value.Type = type; model.Value.Team.Add(userid); //添加返回给客户端的信息 info.PlayerCount = model.Value.Team.Count; info.MaxPlayer = model.Value.MaxPlayer; info.Type = model.Value.Type; //添加房间号和房间的映射 matchDict.TryAdd(model.Value.RoomID, model); //添加玩家和房间号的映射 user2MatchDict.TryAdd(userid, model.Value.RoomID); //游戏是否开始的映射 if (!isStartGameDict.ContainsKey(model.Value.RoomID)) { isStartGameDict.TryAdd(model.Value.RoomID, false); } IsFnish(model.Value.RoomID); } }
/// <summary> /// 请求开始匹配 /// </summary> /// <param name="token">用户连接对象</param> /// <param name="type">房间类型</param> /// <returns> 0 请求开始匹配成功 </returns> /// <returns>-1 当前金币余额不足 </returns> /// <returns>-2 当前玩家已在匹配列表 </returns> public ResponseStartMatchModel StartMatch(UserToken token, SeverConst.GameType type) { ResponseStartMatchModel rsmm = new ResponseStartMatchModel(); RoleInfo user = CacheProxy.User.Get(token); if (user == null) { rsmm.Status = -3; DebugUtil.Instance.Log2Time(string.Format("{0} 请求链接匹配失败,连接失败", token.conn.RemoteEndPoint)); return(rsmm); } int uid = user.Id; int uCoin = user.Coin; //获取进去房间需要的最新金币 int coin = CacheProxy.Match.GetRoomCoinAtType(type); if (uCoin < coin) { rsmm.Status = -1; DebugUtil.Instance.Log2Time(string.Format("{0} 请求链接匹配失败,余额不足", token.conn.RemoteEndPoint)); return(rsmm); } //获取是否在匹配队列中 int matchid = CacheProxy.Match.IsInMatchLine(user.Id); if (matchid > 0) { rsmm.Status = -2; DebugUtil.Instance.Log2Time(string.Format("{0} 请求链接匹配失败,当前已在队列中", token.conn.RemoteEndPoint)); return(rsmm); } rsmm.Status = 0; CacheProxy.Match.AddMatch(uid, type, ref rsmm); DebugUtil.Instance.Log2Time(string.Format("{0} 匹配成功", token.conn.RemoteEndPoint)); return(rsmm); }
public void MessageReceive(SocketModel model) { switch (model.command) { case MatchProtocol.LevelMatch_SRes: { switch (model.GetMessage <int>()) { case 0: case -2: GameApp.Instance.GameLevelMngr.CloseSystemUI(GameResource.SystemUIType.MatchPanel); break; case -1: GameApp.Instance.CommonHintDlg.OpenHint("游戏即将开始"); break; } } break; ; case MatchProtocol.MatchInfo_BRQ: { MatchModel m = model.GetMessage <MatchModel>(); if (GameApp.Instance.UI_Match != null) { GameApp.Instance.UI_Match.UpdateRoomRoleInfo(m); } } break; case MatchProtocol.MacchInfoFinsh_BRQ: { //TODO: LoadBattle GameApp.Instance.GameLevelMngr.LoadScene(GameResource.SceneName.Main); } break; case MatchProtocol.StartMatch_SRes: { ResponseStartMatchModel m = model.GetMessage <ResponseStartMatchModel>(); switch (m.Status) { case 0: GameApp.Instance.GameLevelMngr.LoadSystemUI(GameResource.SystemUIType.MatchPanel, () => { GameObject go; if (!GameApp.Instance.GameLevelMngr.SystemUIGODict.TryGetValue( GameResource.SystemUIType.MatchPanel, out go)) { if (!go.GetComponent <UI_Match>()) { go.AddComponent <UI_Match>(); } go.GetComponent <UI_Match>().StartMatch(m); } }); break; case -1: GameApp.Instance.CommonHintDlg.OpenHint("当前余额不足"); break; case -2: GameApp.Instance.CommonHintDlg.OpenHint("当前已在匹配队列中"); break; ; } } break; } }
public void StartMatch(ResponseStartMatchModel info) { mMatchContent.text = string.Format("正在匹配中...... {0}/{1}", info.PlayerCount, info.MaxPlayer); mGameType.text = GameApp.Instance.GameConst.GameName[(int)info.Type]; mTimeCount = 0; }