Ejemplo n.º 1
0
        //检查断开的用户是否是游戏房间内成员
        private bool CheckFangZhu(user index)
        {
            bool falg = false;

            foreach (int roomids in rooms.Keys)
            {
                if (rooms[roomids].fangzhu.username == index.username)
                {
                    rooms[roomids].QuitRoom(null);
                    rooms.Remove(roomids);
                    ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n系统:房间号:" + roomids + ",已关闭\r\n";
                    HZHUtils.RemoveRoom(roomids);
                    falg = true;
                    break;
                }
            }
            return(falg);
        }
Ejemplo n.º 2
0
 private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         string uname = username.Text;
         string pwd   = password.Text;
         string lname = lastname.Text;
         if (!String.IsNullOrEmpty(uname) && !String.IsNullOrEmpty(pwd) && !String.IsNullOrEmpty(lname))
         {
             bool flag = HZHUtils.AddUser(uname, pwd, lname);
             MessageBox.Show("添加成功!");
             this.Close();
         }
         else
         {
             MessageBox.Show("用户名、密码、昵称不能为空");
         }
     }
     catch
     {
         MessageBox.Show("数据库操作异常!");
     }
 }
Ejemplo n.º 3
0
        // ================================================================================================================
        // ===========================================上方为变量===========================================================
        // ================================================================================================================
        // ================================================================================================================
        // ===========================================下方为函数===========================================================
        // ================================================================================================================
        // ================================================================================================================

        //处理消息函数
        public void MsgHandler(string msg, Socket client_socket)
        {
            string index = "";

            //如果当前用户未断开连接,则取出用户名
            if (client_socket != null && client_socket.Connected)
            {
                index = usersBack[client_socket].username;
            }
            string[] data;
            //解析用户发送的消息函数
            string command = HZHUtils.JieXiMsg(msg, out data);

            switch (command)
            {
            //登录消息处理
            case "login":
                string lastname;
                //判断是否登录
                bool flag = HZHUtils.IsLogin(data[0], data[1], out lastname);
                if (flag)
                {
                    bool ff = false;
                    foreach (user usernames in users.Keys)
                    {
                        if (usernames.username == data[0])
                        {
                            ff = true;
                            break;
                        }
                    }
                    if (ff)
                    {
                        //用户已在线,发送用户在线消息
                        SendMsg("Online", client_socket);
                    }
                    else
                    {
                        SendMsg(lastname, client_socket);
                        ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n" + data[0] + ":登录操作,用户名:" + data[0] + ",成功\r\n";
                        //更改连接用户的用户名
                        string lastusername = usersBack[client_socket].username;
                        foreach (user userss in users.Keys)
                        {
                            if (userss.username == lastusername)
                            {
                                userss.username = data[0];
                                userss.lastname = lastname;
                                break;
                            }
                        }
                        usersBack[client_socket].username = data[0];
                        usersBack[client_socket].lastname = lastname;
                        RealOnlineNum++;
                        userText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n系统:" + data[0] + ":上线\r\n";
                    }
                }
                else
                {
                    //用户登录失败
                    SendMsg("NO", client_socket);
                    ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n" + data[0] + ":登录操作,用户名:" + data[0] + ",失败\r\n";
                }
                break;

            //房间内玩家发送消息处理
            case "msg":
                ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n房间:" + data[2] + "," + index + ":" + data[1] + "\r\n";
                int roomidss = int.Parse(data[2]);
                //在房间内对其他玩家发送消息
                rooms[roomidss].SendMsg("msg|" + data[0] + "," + data[1], null);
                break;

            //创建房间消息处理
            case "create":
                bool f = HZHUtils.CreateRoom(data[0], int.Parse(data[1]), int.Parse(data[2]), int.Parse(data[3]));
                int  roomid;
                f              = HZHUtils.GetRoomId(data[0], out roomid);
                ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n" + roomid + ":创建房间操作,房间号:" + roomid + ",成功\r\n";
                if (int.Parse(data[2]) == 0)    //创建一夜狼房间
                {
                    List <string> shenfen  = new List <string>();
                    string[]      shenfens = data[5].Split('.');
                    foreach (string str in shenfens)
                    {
                        shenfen.Add(str);
                    }
                    bool fff = false;
                    while (!fff)
                    {
                        shenfen = HZHUtils.DaLuanList <string>(shenfen);
                        for (int i = 0; i < shenfen.Count - 3; i++)
                        {
                            if (shenfen[i] == "狼人1" || shenfen[i] == "狼人2" || shenfen[i] == "狼王" || shenfen[i] == "爪牙")
                            {
                                fff = true;
                            }
                        }
                    }

                    int ii = 1;
                    Dictionary <int, string> shenfenss = new Dictionary <int, string>();
                    foreach (string strss in shenfen)
                    {
                        shenfenss.Add(ii++, strss);
                    }
                    Room room = new Room(new user(data[0], data[4]), client_socket, int.Parse(data[1]), roomid, this, shenfenss, 0);
                    rooms.Add(roomid, room);
                }
                else if (int.Parse(data[2]) == 1)    //创建扇子狼房间
                {
                    List <string> shenfen = new List <string>();
                    shenfen.Add("警长");
                    shenfen.Add("狼王");
                    shenfen.Add("预言家");
                    shenfen.Add("女巫");
                    shenfen.Add("猎人");
                    shenfen.Add("白痴");
                    shenfen = HZHUtils.DaLuanList <string>(shenfen);
                    List <string> Finalshenfen = new List <string>();
                    for (int i = 0; i < 3; i++)
                    {
                        Finalshenfen.Add(shenfen[i]);
                    }
                    Finalshenfen.Add("狼人1");
                    Finalshenfen.Add("狼人2");
                    Finalshenfen.Add("狼人3");
                    Finalshenfen.Add("平民1");
                    Finalshenfen.Add("平民2");
                    Finalshenfen.Add("平民3");
                    Finalshenfen = HZHUtils.DaLuanList <string>(Finalshenfen);
                    int ii = 1;
                    Dictionary <int, string> shenfenss = new Dictionary <int, string>();
                    foreach (string strss in Finalshenfen)
                    {
                        shenfenss.Add(ii++, strss);
                    }
                    Room room = new Room(new user(data[0], data[4]), client_socket, int.Parse(data[1]), roomid, this, shenfenss, 1);
                    rooms.Add(roomid, room);
                }
                else if (int.Parse(data[2]) == 2)    //创建新游戏1房间
                {
                }
                else if (int.Parse(data[2]) == 3)    //创建新游戏2房间
                {
                }
                ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n系统:" + rooms[roomid].RoomInfo() + "\r\n";
                SendMsg(roomid.ToString(), client_socket);
                break;

            //用户能否登录消息处理
            case "Isjoin":
                string f1 = HZHUtils.IsRoom(int.Parse(data[0]), int.Parse(data[1]));
                ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n" + index + ":查询房间操作,房间号:" + data[0] + ",查询状态:" + f1 + "\r\n";
                SendMsg(f1, client_socket);
                break;

            //用户加入房间消息处理
            case "join":
                ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n" + index + ":加入房间操作,房间号:" + data[1] + ",成功\r\n";
                rooms[int.Parse(data[1])].join(new user(data[0], data[2]), client_socket);

                break;

            //用户退出房间消息处理
            case "Quitroom":
                int f2       = HZHUtils.QuitRoom(data[0]);
                int roomidid = int.Parse(data[1]);
                if (f2 == -1)
                {
                    rooms[roomidid].QuitRoom(data[0]);
                    ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n" + index + ":退出房间操作,状态:房间成员\r\n";
                }
                else
                {
                    rooms[roomidid].QuitRoom(null);
                    ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n" + index + ":退出房间操作,状态:房主\r\n";
                    QuitRoom(f2);
                }
                break;

            //房间信息消息处理
            case "RoomInfo":
                int room_id = int.Parse(data[0]);
                ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n系统:" + rooms[room_id].RoomInfo() + "\r\n";
                SendMsg(rooms[room_id].RoomInfo(), client_socket);
                break;

            //开始游戏消息处理 start|1
            case "startGame":
                int  room_id1 = int.Parse(data[0]);
                Room room1    = rooms[room_id1];
                room1.style = true;
                bool t = HZHUtils.UpdateRoom(room_id1.ToString());
                if (t)
                {
                    room1.StartGame();
                    ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n系统:房间号:" + room_id1 + ",游戏开始\r\n";
                }
                else
                {
                    ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n系统:房间号:" + room_id1 + ",游戏开始失败\r\n";
                }
                ShowText.Text += DateTime.Now.ToLocalTime().ToString() + ":\r\n系统:" + rooms[room_id1].RoomInfo() + "\r\n";
                break;

            //一夜狼游戏内容消息处理 game|预言家,1 or game|预言家,-1 or game|小女孩,2.3
            //扇子狼游戏内容消息处理 game|预言家,1 or game|预言家,-1 or game|平民,3 or game|平民,-1
            case "game":
                int      room_id2 = int.Parse(data[1]);
                Room     room2    = rooms[room_id2];
                string   shenfen2 = data[0];
                string[] index2   = data[2].Split('.');
                int[]    index22  = new int[2];
                int      j        = 0;
                foreach (string str in index2)
                {
                    index22[j++] = int.Parse(str);
                }
                room2.GameHandler(shenfen2, index22);
                break;

            //游戏结束消息处理 endGame|1
            case "endGame":
                int  room_id3 = int.Parse(data[0]);
                Room room3    = rooms[room_id3];
                room3.SendMsg(room3.RetrunFuPan(), null);
                break;
            }
        }