Example #1
0
        private void TransmitRecCMD(string ipname, RemoteCMD_Data recData)
        {
            string json = JsonMapper.ToJson(recData);

            byte[] buf = parser.EncoderData(json);
            TransmitMsg(roomManager[player_Room[ipname]].Members.ToArray(), buf, null);
        }
Example #2
0
        private void SendRecCMD(string ipname, RemoteCMD_Data recData)
        {
            string json = JsonMapper.ToJson(recData);

            byte[] buf = parser.EncoderData(json);
            SendDataBegin(ipname, buf);
        }
Example #3
0
 public void HandleCallLandlord(string ipname, RemoteCMD_Data recData)
 {
     Console.WriteLine("玩家" + recData.player.Name + "叫地主");
     TransmitRecCMD(ipname, recData);
     DecideLandlord(ipname, true);
     SendTurn(ipname);
 }
Example #4
0
 public void HandleNotClaim(string ipname, RemoteCMD_Data recData)
 {
     Console.WriteLine("玩家" + recData.player.Name + "不抢地主");
     TransmitRecCMD(ipname, recData);
     DecideLandlord(ipname, false);
     SendTurn(ipname);
 }
Example #5
0
        public void HandleGenerateCards(Card[] cards, string ipname)
        {
            Console.WriteLine("开始发牌...");
            Card[] cards1 = new Card[17];
            Array.Copy(cards, 0, cards1, 0, 17);
            Card[] cards2 = new Card[17];
            Array.Copy(cards, 17, cards2, 0, 17);
            Card[] cards3 = new Card[17];
            Array.Copy(cards, 34, cards3, 0, 17);
            Card[] baseCards = new Card[3];
            Array.Copy(cards, 51, baseCards, 0, 3);
            //手牌1
            RemoteCMD_Data recData = new RemoteCMD_Data();

            recData.cmd   = RemoteCMD_Const.DealCards;
            recData.cards = cards1;
            SendRecCMD(roomManager[player_Room[ipname]].Members[0], recData);
            //手牌2
            recData.cmd   = RemoteCMD_Const.DealCards;
            recData.cards = cards2;
            SendRecCMD(roomManager[player_Room[ipname]].Members[1], recData);
            //手牌3
            recData.cmd   = RemoteCMD_Const.DealCards;
            recData.cards = cards3;
            SendRecCMD(roomManager[player_Room[ipname]].Members[2], recData);
            //底牌
            recData.cmd   = RemoteCMD_Const.BaseCards;
            recData.cards = baseCards;
            TransmitRecCMD(ipname, recData);
            //开始游戏
            SendTurn(ipname);
        }
Example #6
0
 public void HandleMatch(string ipname, PlayerInfo p)
 {
     lock (curFreeRoom)
     {
         Console.WriteLine("玩家:" + p.Name + "加入房间,开始匹配");
         curFreeRoom.AddMember(ipname); //加入当前房间
                                        //绑定ip和昵称
         if (!ipname_PlayerName.ContainsKey(ipname))
         {
             ipname_PlayerName.Add(ipname, p.Name);
         }
         ipname_PlayerName[ipname] = p.Name;
         //p.RoomId curFreeRoom.RoomId;
         //绑定玩家和房间号
         if (!player_Room.ContainsKey(ipname))
         {
             player_Room.Add(ipname, curFreeRoom.RoomId);
         }
         player_Room[ipname] = curFreeRoom.RoomId;
         //当前房间满了
         if (curFreeRoom.IsFull)
         {
             Console.WriteLine("房间满员,开始游戏");
             //确定玩家顺序
             int startPlayer = rand.Next(0, 3);//随机起始
             curFreeRoom.CurrentTurn = startPlayer;
             int player2 = (startPlayer + 1) % 3;
             int player3 = (startPlayer + 2) % 3;
             //发送匹配成功命令
             RemoteCMD_Data rec = new RemoteCMD_Data();
             rec.cmd = RemoteCMD_Const.MatchSuccess;
             TransmitRecCMD(ipname, rec);
             //将顺序转发给房间内的玩家
             rec.cmd         = RemoteCMD_Const.StartPlayer;
             rec.player.Name = ipname_PlayerName[curFreeRoom.Members[startPlayer]];
             TransmitRecCMD(ipname, rec);
             //
             rec.cmd         = RemoteCMD_Const.Player2;
             rec.player.Name = ipname_PlayerName[curFreeRoom.Members[player2]];
             TransmitRecCMD(ipname, rec);
             //
             rec.cmd         = RemoteCMD_Const.Player3;
             rec.player.Name = ipname_PlayerName[curFreeRoom.Members[player3]];
             TransmitRecCMD(ipname, rec);
             ////新开一个房间
             curFreeRoom = roomManager.GetRoom();
         }
     }
 }
Example #7
0
        private void SendTurn(string ipname)
        {
            RemoteCMD_Data recData = new RemoteCMD_Data();

            recData.cmd = RemoteCMD_Const.GameTurn;
            int index = roomManager[player_Room[ipname]].CurrentTurn;//当前回合索引

            recData.player.Name = ipname_PlayerName[
                roomManager[player_Room[ipname]].Members[index]
                                  ];                        //当前玩家昵称
            //string targetIP = roomManager[player_Room[ipname]].Members[index];//IP
            TransmitRecCMD(ipname, recData);                //转发回合
            roomManager[player_Room[ipname]].CurrentTurn++; //当前回合数增加
            roomManager[player_Room[ipname]].CurrentTurn %= 3;
        }
Example #8
0
        static void Main(string[] args)
        {
            //IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
            //string ipaddr = "";
            //foreach(var ip in ips)
            //{
            //    Console.WriteLine(ip.ToString());
            //    if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            //    {
            //        ipaddr = ip.ToString();
            //        break;
            //    }
            //}
            Console.WriteLine("请输入服务器IP地址:");
            string ip = Console.ReadLine();

            Console.WriteLine("请输入服务器端口号:");
            string       port   = Console.ReadLine();
            MyGameServer server = new MyGameServer(ip, port);

            server.StartListen();
            while (true)
            {
                string cmd = Console.ReadLine();
                if (cmd.Equals("exit"))
                {
                    break;
                }
                if (cmd.Equals("test"))
                {
                    RemoteCMD_Data recData = new RemoteCMD_Data();
                    recData.player.Name = "test";
                    MyParser parser = new MyParser();
                    string   msg    = LitJson.JsonMapper.ToJson(recData);
                    byte[]   buf    = parser.EncoderData(msg);
                    server.BroadcastMsg(null, buf);
                }
            }
        }
Example #9
0
 public void HandleGameOver(string ipname, RemoteCMD_Data recData)
 {
     Console.WriteLine("玩家" + recData.player.Name + "获得胜利,游戏结束");
     TransmitRecCMD(ipname, recData);
     ReleaseRoom(ipname);//释放房间
 }
Example #10
0
 public void HandlePass(string ipname, RemoteCMD_Data recData)
 {
     Console.WriteLine("玩家" + recData.player.Name + "跳过");
     TransmitRecCMD(ipname, recData);
     SendTurn(ipname);
 }
Example #11
0
 private void HandleCancelMatch(string ipname, RemoteCMD_Data recData)
 {
     roomManager[player_Room[ipname]].DelMember(ipname);//移除房间中的玩家
     Console.WriteLine("玩家:" + ipname_PlayerName[ipname] + "退出匹配");
 }
Example #12
0
        public override void HandleRecievedData(string ipname, string data)
        {
            Console.WriteLine(data);
            RemoteCMD_Data recData = JsonMapper.ToObject <RemoteCMD_Data>(data);

            switch (recData.cmd)
            {
            case RemoteCMD_Const.Match:
            {
                HandleMatch(ipname, recData.player);
            } break;

            case RemoteCMD_Const.CallLandlord:
            {
                HandleCallLandlord(ipname, recData);
            }
            break;

            case RemoteCMD_Const.NotCall:
            {
                HandleNotCall(ipname, recData);
            }
            break;

            case RemoteCMD_Const.Claim:
            {
                HandleClaim(ipname, recData);
            }
            break;

            case RemoteCMD_Const.NotClaim:
            {
                HandleNotClaim(ipname, recData);
            }
            break;

            case RemoteCMD_Const.GenerateCards:
            {
                HandleGenerateCards(recData.cards, ipname);
            }
            break;

            case RemoteCMD_Const.Discards:
            {
                HandleDiscards(ipname, recData);
            }
            break;

            case RemoteCMD_Const.Pass:
            {
                HandlePass(ipname, recData);
            }
            break;

            case RemoteCMD_Const.GamerOver:
            {
                HandleGameOver(ipname, recData);
            }
            break;

            case RemoteCMD_Const.CancelMatch:
            {
                HandleCancelMatch(ipname, recData);
            }
            break;
            }
        }