/// <summary> /// 开始游戏 /// </summary> /// <param name="fightRoom"></param> private void startGame(FightRoom fightRoom)//List<int> uidList) { SingleExecute.Instance.processSingle( () => { //开始游戏广播 fightRoom.Broadcast(OpCode.MATCH, MatchCode.START_GAME_BOD, "0"); //创建牌库 fightRoom.CreateCardLibrary(); //初始化玩家手牌(发牌) fightRoom.InitPlayerCards(); foreach (var item in fightRoom.playerDtos) { //给每个客户端发送自己的手牌信息 List <CardDto> cardList = fightRoom.GetUserCard(item.UserID); ClientPeer clientPeer = UserCache.Instance.GetClientPeer(item.UserID); clientPeer.StartSend(OpCode.FIGHT, FightCode.GET_CARD_SRES, cardList); } //由第一个进入房间的玩家首先开始回合 PlayerDto firstPlayer = fightRoom.GetFirstPlayer(); fightRoom.Broadcast(OpCode.FIGHT, FightCode.NEXT_TURN_SBOD, firstPlayer.UserID); } ); }
/// <summary> /// 处理非指令卡 /// </summary> /// <param name="clientPeer"></param> /// <param name="cardDto"></param> private void processOtherCard(ClientPeer clientPeer, CardDto cardDto) { SingleExecute.Instance.processSingle(() => { if (!UserCache.Instance.IsOnline(clientPeer)) { return; } int uid = UserCache.Instance.GetId(clientPeer); FightRoom fightRoom = FightRoomCache.Instance.GetRoomByUid(uid); switch (cardDto.Race) { case RaceType.ORC: switch (cardDto.Name) { case OrcOtherCardType.Ancestor_Helmets: //先祖头盔 fightRoom.Broadcast(OpCode.FIGHT, FightCode.USE_OTHERCARD_SBOD, cardDto, clientPeer); //在服务器上移除玩家相应非指令卡 fightRoom.RemoveCard(uid, cardDto); break; case OrcOtherCardType.Totem_summon: //召唤图腾 List <CardDto> cardDtos = fightRoom.DispathCard(uid, CardType.ARMYCARD); if (cardDtos.Count > 0) { //给玩家发送发牌消息 //fightRoom.Broadcast(OpCode.FIGHT, FightCode.ADD_CARD_SRES, cardlist); clientPeer.StartSend(OpCode.FIGHT, FightCode.ADD_CARD_SRES, cardDtos); //给房间内除其他玩家发送发牌消息 fightRoom.Broadcast(OpCode.FIGHT, FightCode.ADD_CARD_SBOD, cardDtos.Count, clientPeer); } else { //牌库内没有其他中高阶单位 clientPeer.StartSend(OpCode.FIGHT, FightCode.USE_OTHERCARD_SRES, cardDto); } break; } break; } }); }
/// <summary> /// 客户端跟注请求的处理 /// </summary> /// <param name="client"></param> private void Follow(ClientPeer client) { SingleExecute.Instance.Execute(() => { if (fightCache.IsFighting(client.Id) == false) { return; } FightRoom room = fightCache.GetFightRoomByUserId(client.Id); room.stakesSum += room.lastPlayerStakesCount; int stakesSum = room.UpdatePlayerStakesSum(client.Id, room.lastPlayerStakesCount); int remainCoin = DatabaseManager.UpdateCoinCount(client.Id, -(room.lastPlayerStakesCount)); stakesDto.Change(client.Id, remainCoin, room.lastPlayerStakesCount, stakesSum, StakesDto.StakesType.NoLook); room.Broadcast(OpCode.Fight, FightCode.PutStakes_BRO, stakesDto); if (remainCoin < room.lastPlayerStakesCount) { GiveUpCard(client); return; } //轮到下一个玩家下注 Turn(client); }); }
/// <summary> /// 客户端弃牌的请求处理 /// </summary> /// <param name="client"></param> private void GiveUpCard(ClientPeer client) { SingleExecute.Instance.Exeecute(() => { if (fightCache.IsFighting(client.Id) == false) { return; } FightRoom room = fightCache.GetFightRoomByUserId(client.Id); room.giveUpUserIdList.Add(client.Id); room.Broadcast(OpCode.Fight, FightCode.GiveUpCard_BRO, client.Id); // 离开的玩家是本次下注的玩家,这样的需转换下一个玩家下注 if (room.roundModel.CurrentStakesUserId == client.Id) { //轮换下注 Turn(client); } // 一个弃牌,一个逃跑 if (room.giveUpUserIdList.Count >= 1 && room.leaveUserIdList.Count >= 1) { GameOver(room); } // 两个逃跑 if (room.giveUpUserIdList.Count == 2) { GameOver(room); } }); }
/// <summary> /// 轮换下注 /// </summary> /// <param name="client"></param> private void Turn(ClientPeer client, int turnUserId = -1) { // 清空计时器任务 TimerManager.Instance.Clear(); if (fightCache.IsFighting(client.Id) == false) { return; } FightRoom room = fightCache.GetFightRoomByUserId(client.Id); int nextID = room.Turn(); if (room.IsGiveUpCard(nextID) || room.IsLeaveRoom(nextID) || (turnUserId != -1 && nextID != turnUserId)) { // 如果下一位玩家离开或弃牌了,就继续轮换下注,直到改玩家不离开也不弃牌为止 Turn(client, turnUserId); } else { timerClient = DatabaseManager.GetClientPeerByUserId(nextID); // 添加计时器任务 TimerManager.Instance.AddTimerEvent(60, TimerDelegate); Console.WriteLine("当前下注者" + client.UserName); room.Broadcast(OpCode.Fight, FightCode.StartStakes_BRO, nextID); } }
/// <summary> /// 游戏结束 /// </summary> /// <param name="room"></param> private void GameOver(FightRoom room) { PlayerDto winPlayer = null; List <PlayerDto> loseList = new List <PlayerDto>(); foreach (var player in room.playerList) { // 胜利的玩家 if (!room.IsGiveUpCard(player.id) && !room.IsLeaveRoom(player.id)) { winPlayer = player; } // 失败的玩家们 else { loseList.Add(player); } } DatabaseManager.UpdateCoin(winPlayer.id, room.stakesSum); gameOverDto.Change(winPlayer, loseList, room.stakesSum); room.Broadcast(OpCode.Fight, FightCode.GameOver_BRO, gameOverDto); //销毁房间 fightCache.DesRoom(room); //清空定时任务 TimerManager.Instance.Clear(); }
/// <summary> /// 客户端的加注处理 /// </summary> /// <param name="client"></param> /// <param name="value"></param> private void AddStakes(ClientPeer client, int multiple) { SingleExecute.Instance.Exeecute(() => { if (fightCache.IsFighting(client.Id) == false) { return; } FightRoom room = fightCache.GetFightRoomByUserId(client.Id); room.lastPlayerStakes *= multiple; if (room.lastPlayerStakes > room.topStakes) // 顶注 { room.lastPlayerStakes = room.topStakes; } room.stakesSum += room.lastPlayerStakes; int stakesSum = room.UpdatePlayerStakesSum(client.Id, room.lastPlayerStakes); int remainCoin = DatabaseManager.UpdateCoin(client.Id, -room.lastPlayerStakes); stakesDto.Change(client.Id, remainCoin, room.lastPlayerStakes, stakesSum, StakesDto.StakesType.NoLook); room.Broadcast(OpCode.Fight, FightCode.PutStakes_BRO, stakesDto); // 剩余金币不够 if (remainCoin < room.lastPlayerStakes) { GiveUpCard(client); return; } Turn(client); }); }
/// <summary> /// 比牌结果 /// </summary> private void CompareResult(FightRoom room, ClientPeer c1, PlayerDto winDto, PlayerDto loseDto, ClientPeer otherClient) { // 转到胜利的玩家下注 Turn(c1, winDto.id); // 输了的玩家弃牌 GiveUpCard(loseDto.id); resultDto.Change(winDto, loseDto); Console.WriteLine("胜利者:" + winDto.name + " 失败者: " + loseDto.name); room.Broadcast(OpCode.Fight, FightCode.CompareCard_BRO, resultDto, otherClient); }
/// <summary> /// 客户端看牌请求 /// </summary> /// <param name="client"></param> private void LookCard(ClientPeer client) { SingleExecute.Instance.Exeecute(() => { if (fightCache.IsFighting(client.Id) == false) { return; } FightRoom room = fightCache.GetFightRoomByUserId(client.Id); room.Broadcast(OpCode.Fight, FightCode.LookCard_BRO, client.Id); }); }
/// <summary> /// 处理下一回合请求 /// </summary> /// <param name="clientPeer"></param> private void processNextTurn(ClientPeer clientPeer) { SingleExecute.Instance.processSingle( () => { if (!UserCache.Instance.IsOnline(clientPeer)) { return; } int uid = UserCache.Instance.GetId(clientPeer); //获得战斗房间 FightRoom fightRoom = FightRoomCache.Instance.GetRoomByUid(uid); int nextuid = -1; //下一回合的玩家ID foreach (var item in fightRoom.playerDtos) { if (item.UserID != uid) { nextuid = item.UserID; break; } } //下一回玩家的套接字连接对象 ClientPeer nextClientpeer = UserCache.Instance.GetClientPeer(nextuid); //广播通知下一回合玩家 fightRoom.Broadcast(OpCode.FIGHT, FightCode.NEXT_TURN_SBOD, nextuid); //给下一回合的玩家发牌 List <CardDto> cardlist = fightRoom.DispathCard(nextuid); //给玩家发送发牌消息 //fightRoom.Broadcast(OpCode.FIGHT, FightCode.ADD_CARD_SRES, cardlist); nextClientpeer.StartSend(OpCode.FIGHT, FightCode.ADD_CARD_SRES, cardlist); //给房间内除了下一回合的玩家发送发牌消息 fightRoom.Broadcast(OpCode.FIGHT, FightCode.ADD_CARD_SBOD, cardlist.Count, nextClientpeer); } ); }
private void processGameOver(ClientPeer clientPeer) { SingleExecute.Instance.processSingle(() => { if (!UserCache.Instance.IsOnline(clientPeer)) { return; } int uid = UserCache.Instance.GetId(clientPeer); //获得战斗房间 FightRoom fightRoom = FightRoomCache.Instance.GetRoomByUid(uid); //玩家数据更新 UserModel userModel = UserCache.Instance.GetModelByClientPeer(clientPeer); userModel.Exp += 20; userModel.Money += 100; if (userModel.Exp >= 100) { userModel.Lv++; } MysqlPeer.Instance.UpdateUserInfo(userModel); //向其他人广播游戏结束 fightRoom.Broadcast(OpCode.FIGHT, FightCode.GAME_OVER_SBOD, "游戏结束", clientPeer); //更新其他人的数据 int otheruid = -1; foreach (var item in fightRoom.playerDtos) { if (item.UserID != uid) { otheruid = item.UserID; break; } } ClientPeer otherclientPeer = UserCache.Instance.GetClientPeer(otheruid); userModel = UserCache.Instance.GetModelByClientPeer(otherclientPeer); userModel.Exp += 5; userModel.Money += 30; if (userModel.Exp >= 100) { userModel.Lv++; } MysqlPeer.Instance.UpdateUserInfo(userModel); //TODO 销毁战斗房间 }); }
/// <summary> /// 开始战斗 /// </summary> /// <param name="clientList"></param> /// <param name="roomType"></param> public void StartFight(List <ClientPeer> clientList, int roomType) { SingleExecute.Instance.Execute(() => { FightRoom room = fightCache.CreatRoom(clientList); switch (roomType) { case 0: room.bottomStakes = 10; room.topStakes = 100; room.lastPlayerStakesCount = 10; break; case 1: room.bottomStakes = 20; room.topStakes = 200; room.lastPlayerStakesCount = 20; break; case 2: room.bottomStakes = 50; room.topStakes = 500; room.lastPlayerStakesCount = 50; break; default: break; } foreach (var client in clientList) { room.UpdatePlayerStakesSum(client.Id, room.bottomStakes); } //选择庄家 ClientPeer bankerClient = room.SetBanker(); //重置位置,排序 room.ResetPosition(bankerClient.Id); //发牌 room.DealCards(); //对手牌排序 room.SortAllPlayerCard(); //获得牌型 room.GetAllPlayerCardType(); room.Broadcast(OpCode.Fight, FightCode.StartFight_BRO, room.playerList); //转换下注,换到庄家下一位玩家下注 Turn(bankerClient); }); }
/// <summary> /// 处理兵种攻击请求 /// </summary> private void processArmyAttack(ClientPeer clientPeer, MapAttackDto mapAttackDto) { SingleExecute.Instance.processSingle( () => { if (!UserCache.Instance.IsOnline(clientPeer)) { return; } int uid = UserCache.Instance.GetId(clientPeer); FightRoom fightRoom = FightRoomCache.Instance.GetRoomByUid(uid); fightRoom.Broadcast(OpCode.FIGHT, FightCode.ARMY_ATTACK_SBOD, mapAttackDto, clientPeer); //向房间内其他人发送攻击请求 } ); }
/// <summary> /// 处理兵种移动请求 /// </summary> private void processArmyMove(ClientPeer clientPeer, MapMoveDto mapMoveDto) { SingleExecute.Instance.processSingle( () => { if (!UserCache.Instance.IsOnline(clientPeer)) { return; } int uid = UserCache.Instance.GetId(clientPeer); FightRoom fightRoom = FightRoomCache.Instance.GetRoomByUid(uid); //向房间内其他人传送移动信息 fightRoom.Broadcast(OpCode.FIGHT, FightCode.MAP_ARMY_MOVE_SBOD, mapMoveDto, clientPeer); } ); }
/// <summary> /// 处理攻击卡请求 /// </summary> /// <param name="clientPeer"></param> private void processAttackCard(ClientPeer clientPeer) { SingleExecute.Instance.processSingle(() => { if (!UserCache.Instance.IsOnline(clientPeer)) { return; } int uid = UserCache.Instance.GetId(clientPeer); FightRoom fightRoom = FightRoomCache.Instance.GetRoomByUid(uid); //发送攻击广播 fightRoom.Broadcast(OpCode.FIGHT, FightCode.DEAL_ATTACK_SBOD, "使用了攻击卡", clientPeer); //在服务器上移除玩家攻击手牌 fightRoom.RemoveCard(uid, CardType.ORDERCARD, OrderCardType.ATTACK); }); }
/// <summary> /// 处理修养请求 /// </summary> /// <param name="clientPeer"></param> /// <param name="active"></param> private void processDealRestAttack(ClientPeer clientPeer, MapPointDto mapPointDto) { SingleExecute.Instance.processSingle( () => { if (!UserCache.Instance.IsOnline(clientPeer)) { return; } int uid = UserCache.Instance.GetId(clientPeer); FightRoom fightRoom = FightRoomCache.Instance.GetRoomByUid(uid); fightRoom.Broadcast(OpCode.FIGHT, FightCode.DEAL_REST_SBOD, mapPointDto, clientPeer); //向房间内其他人发送修养广播 //在服务器移除修养指令卡 fightRoom.RemoveCard(uid, CardType.ORDERCARD, OrderCardType.REST); } ); }
/// <summary> /// 处理单位技能 /// </summary> private void processArmySkill(ClientPeer clientPeer, SkillDto skillDto) { SingleExecute.Instance.processSingle( () => { if (!UserCache.Instance.IsOnline(clientPeer)) { return; } int uid = UserCache.Instance.GetId(clientPeer); FightRoom fightRoom = FightRoomCache.Instance.GetRoomByUid(uid); //向房间内其他人发送兵种技能消息 fightRoom.Broadcast(OpCode.FIGHT, FightCode.ARMY_USE_SKILL_SBOD, skillDto, clientPeer); // } ); }
/// <summary> /// 处理地图放置兵种请求 /// </summary> /// <param name="mapPointDto"></param> private void processMapSetArmy(ClientPeer clientPeer, MapPointDto mapPointDto) { SingleExecute.Instance.processSingle( () => { if (!UserCache.Instance.IsOnline(clientPeer)) { return; } int uid = UserCache.Instance.GetId(clientPeer); FightRoom fightRoom = FightRoomCache.Instance.GetRoomByUid(uid); //向房间内其他人发送兵种放置消息 fightRoom.Broadcast(OpCode.FIGHT, FightCode.MAP_SET_ARMY_SBOD, mapPointDto, clientPeer); // } ); }
/// <summary> /// 处理出牌请求 /// </summary> /// <param name="clientPeer"></param> private void processDealCard(ClientPeer clientPeer, CardDto cardDto) { SingleExecute.Instance.processSingle( () => { if (!UserCache.Instance.IsOnline(clientPeer)) { return; } int uid = UserCache.Instance.GetId(clientPeer); FightRoom fightRoom = FightRoomCache.Instance.GetRoomByUid(uid); //在服务器上移除玩家兵种卡 fightRoom.RemoveCard(uid, cardDto); //在其他玩家客户端移除手牌 fightRoom.Broadcast(OpCode.FIGHT, FightCode.DEAL_ARMYCARD_SBOD, 1, clientPeer); } ); }
/// <summary> /// 处理闪避请求 /// </summary> /// <param name="clientPeer"></param> /// <param name="active"></param> private void processDealDodge(ClientPeer clientPeer, bool active) { SingleExecute.Instance.processSingle( () => { if (!UserCache.Instance.IsOnline(clientPeer)) { return; } int uid = UserCache.Instance.GetId(clientPeer); FightRoom fightRoom = FightRoomCache.Instance.GetRoomByUid(uid); //向房间内其他人发送闪避广播 fightRoom.Broadcast(OpCode.FIGHT, FightCode.DEAL_DODGE_SBOD, active, clientPeer); //在服务器上移除玩家闪避手牌 fightRoom.RemoveCard(uid, CardType.ORDERCARD, OrderCardType.DODGE); } ); }
/// <summary> /// 客户端离开请求的处理 /// </summary> /// <param name="client"></param> private void LeaveRoom(ClientPeer client) { SingleExecute.Instance.Execute(() => { //不在战斗房间,忽略 if (fightCache.IsFighting(client.Id) == false) { return; } FightRoom room = fightCache.GetFightRoomByUserId(client.Id); room.leaveUserIdList.Add(client.Id); DatabaseManager.UpdateCoinCount(client.Id, -(room.bottomStakes * 20)); room.Broadcast(OpCode.Fight, FightCode.Leave_BRO, client.Id); //离开的玩家是本次下注的玩家 //这样的需转换下一个玩家下注 if (room.roundModel.CurrentStakesUserId == client.Id) { //轮换下注 Turn(client); } if (room.giveUpCardUserIdList.Count >= 1 && room.leaveUserIdList.Count >= 1) { GameOver(room); } //游戏结束 if (room.leaveUserIdList.Count == 2) { GameOver(room); return; } if (room.leaveUserIdList.Count == 3) { fightCache.DestoryRoom(room); } }); }
/// <summary> /// 客户端聊天的请求处理 /// </summary> /// <param name="client"></param> /// <param name="msg"></param> private void Chat(ClientPeer client, string msg) { SingleExecute.Instance.Exeecute(() => { foreach (var matchCache in matchCaches) { if (matchCache.IsMatching(client.Id)) // 客户端在匹配房间里 { MatchRoom room = matchCache.GetRoom(client.Id); chatDto.Change(client.Id, client.UserName, msg); room.Broadcast(OpCode.Chat, ChatCode.BRO, chatDto); } } if (fightCache.IsFighting(client.Id) == false) { return; } FightRoom fightRoom = fightCache.GetFightRoomByUserId(client.Id); chatDto.Change(client.Id, client.UserName, msg); fightRoom.Broadcast(OpCode.Chat, ChatCode.BRO, chatDto); }); }
/// <summary> /// 客户端离开请求的处理 /// </summary> /// <param name="client"></param> private void LeaveRoom(ClientPeer client) { SingleExecute.Instance.Exeecute(() => { // 不在战斗房间 if (fightCache.IsFighting(client.Id) == false) { return; } FightRoom room = fightCache.GetFightRoomByUserId(client.Id); room.leaveUserIdList.Add(client.Id); // 离开房间的惩罚 DatabaseManager.UpdateCoin(client.Id, -(room.bottomStakes * 20)); room.Broadcast(OpCode.Fight, FightCode.Leave_BRO, client.Id); // 离开的玩家是本次下注的玩家,这样的话需要转换下一个玩家下注 if (room.roundModel.CurrentStakesUserId == client.Id) { // 轮换下注 Turn(client); } // 一个弃牌,一个逃跑 if (room.giveUpUserIdList.Count >= 1 && room.leaveUserIdList.Count >= 1) { GameOver(room); } // 2 个离开 if (room.leaveUserIdList.Count == 2) { GameOver(room); return; } // 3 个离开 if (room.leaveUserIdList.Count == 3) { fightCache.DesRoom(room); // 销毁房间 } }); }
/// <summary> /// 弃牌,不需要转换下注 /// </summary> /// <param name="userId"></param> private void GiveUpCard(int userId) { SingleExecute.Instance.Exeecute(() => { if (fightCache.IsFighting(userId) == false) { return; } FightRoom room = fightCache.GetFightRoomByUserId(userId); room.giveUpUserIdList.Add(userId); room.Broadcast(OpCode.Fight, FightCode.GiveUpCard_BRO, userId); // 一个弃牌,一个逃跑 if (room.giveUpUserIdList.Count >= 1 && room.leaveUserIdList.Count >= 1) { GameOver(room); } //游戏结束 if (room.giveUpUserIdList.Count == 2) { GameOver(room); } }); }
/// <summary> /// 客户端比牌的处理 /// </summary> /// <param name="compareClient"></param> /// <param name="comparedID"></param> private void CompareCard(ClientPeer compareClient, int comparedID) { SingleExecute.Instance.Exeecute(() => { if (fightCache.IsFighting(compareClient.Id) == false) { return; } FightRoom room = fightCache.GetFightRoomByUserId(compareClient.Id); room.stakesSum += room.lastPlayerStakes; int stakesSum = room.UpdatePlayerStakesSum(compareClient.Id, room.lastPlayerStakes); int remainCoin = DatabaseManager.UpdateCoin(compareClient.Id, -room.lastPlayerStakes); stakesDto.Change(compareClient.Id, remainCoin, room.lastPlayerStakes, stakesSum, StakesDto.StakesType.Look); room.Broadcast(OpCode.Fight, FightCode.PutStakes_BRO, stakesDto); // 拿到 3 个玩家的 DTO PlayerDto c1Dto = null, c2Dto = null, otherDto = null; foreach (var player in room.playerList) { if (player.id == compareClient.Id) { c1Dto = player; } else if (player.id == comparedID) { c2Dto = player; } else { otherDto = player; } } ClientPeer otherClient = DatabaseManager.GetClientPeerByUserId(otherDto.id); // 比牌 CompareCard(room, compareClient, c1Dto, c2Dto, otherClient); }); }