public void ClearTitle(SocketConnection sConn)
 {
     type = "_over";
     titleList.Clear();
     userList.Clear();
     Send("ReviceClearTitle|" + sConn.Name);
 }
        public void StartGame(JObject jo, SocketConnection sConn)
        {
            userList.Remove(sConn.Name);
            judge = sConn.Name;

            string msg;
            if (titleList.Count != userList.Count)
            {
                msg = string.Format("ReviceStartGame|False|抱歉,出题要求{0}人参加,但现在只有{1}人,所以游戏启动失败!", titleList.Count, userList.Count);
                Send(msg);
            }
            else
            {
                var num = Rank();
                speak = userList[0];

                var i = 0;
                foreach (var item in connectionSocketList)
                {
                    if (item.Name != sConn.Name)
                    {
                        var title = titleList[num[i] - 1];
                        msg = string.Format("ReviceStartGame|True|游戏开始,{1}开始发言!|{0}|{1}", title, speak);

                        if (!userTitle.ContainsKey(item.Name))
                            userTitle.Add(item.Name, title);
                        i++;
                    }
                    else
                    {
                        type = "_game";
                        msg = string.Format("ReviceStartGame|True|游戏开始,{0}开始发言!|你是法官|{0}", speak);
                    }
                    var dr = new DataFrame(msg);
                    item.ConnectionSocket.Send(dr.GetBytes());
                }
            }
        }
        public void VoteUser(JObject jo, SocketConnection sConn)
        {
            var userName = jo["UserName"].ToString();
            var voteUser = jo["VoteUser"].ToString();

            if (!voteList.ContainsKey(userName))
            {
                voteList.Add(userName, voteUser);
            }
            else
            {
                const string message = "ReviceVote|Flase|你已经投过票了,请不要重复投票!";
                var dr = new DataFrame(message);
                sConn.ConnectionSocket.Send(dr.GetBytes());
                return;
            }

            if (userList.Count != voteList.Count) return;

            var result = voteList.GroupBy(d => d.Value).Select(
                n => new
                {
                    Count = n.Count(),
                    value = n.FirstOrDefault().Value
                }).OrderByDescending(n => n.Count).ToList();

            var voteTxt = voteList.Aggregate("", (current, vote) => current + (":" + vote.Key + ":" + vote.Value));

            string resultVote = null;
            for (var i = 0; i < result.Count - 1; i++)
            {
                if (result[i].Count == result[i + 1].Count)
                {
                    resultVote += "," + result[i + 1].value;
                }
                else
                {
                    break;
                }
            }

            string style;
            string isOver;
            if (resultVote == null)
            {
                userList.Remove(result[0].value);
                resultVote = string.Format("{0}获得了{1}票,所以{0}出局!", result[0].value, result[0].Count);
                isOver = IsOver(result[0].value, userTitle[result[0].value]);
                style = "Kill";
            }
            else
            {
                resultVote = string.Format("{0}等人票数相同,都是{1}票,所以进行PK!", result[0].value + resultVote, result[0].Count);
                style = "Pk";
                isOver = "continue";
            }

            voteList.Clear();

            var msg = string.Format("ReviceVote|True|{0}|{1}|{2}|{3}|{4}|{5}", voteTxt, style, resultVote,
                                    result[0].value, userList[0], isOver);
            Send(msg);

            speak = userList[0];

            if (isOver == "_over")
            {
                GameOver();
            }
        }
        public void SendTitle(JObject jo, SocketConnection sConn)
        {
            var userName = jo["UserName"].ToString();
            string msg;
            if (titleList.Count == 0)
            {
                var requestPeople = jo["People"].ToString();
                var requestGhost = jo["Ghost"].ToString();
                var requestPeopleNum = Convert.ToInt32(jo["PeopleNum"].ToString());
                var requestGhostNum = Convert.ToInt32(jo["GhostNum"].ToString());
                var requestXiaoBai = Convert.ToInt32(jo["XiaoBai"].ToString());

                titleUserName = userName;
                people = requestPeople;
                ghost = requestGhost;
                peopleNum = requestPeopleNum;
                ghostNum = requestGhostNum;

                for (var i = 0; i < requestPeopleNum; i++)
                {
                    titleList.Add(requestPeople);
                }
                for (var i = 0; i < requestGhostNum; i++)
                {
                    titleList.Add(requestGhost);
                }
                for (var i = 0; i < requestXiaoBai; i++)
                {
                    titleList.Add("你是小白,伤不起");
                }
                type = "_plan";
                msg = string.Format("ReviceTitle|{0}已出题,请要参加的人员,赶快准备!|{0}", userName);
                Send(msg);
            }
            else
            {
                msg = string.Format("ReviceTitle|出题失败!{0}已出题,请先完成当前题目!!", titleUserName);
                var dr = new DataFrame(msg);
                sConn.ConnectionSocket.Send(dr.GetBytes());
            }
        }
 public void Plan(JObject jo, SocketConnection sConn)
 {
     var user = jo["UserName"].ToString();
     string msg;
     if (!userList.Contains(user))
     {
         userList.Add(user);
         msg = string.Format("RevicePlan|True|{0}", user);
         Send(msg);
     }
     else
     {
         msg = string.Format("RevicePlan|False|你要爪子,准备了还点。。");
         var dr = new DataFrame(msg);
         sConn.ConnectionSocket.Send(dr.GetBytes());
     }
 }
        public void Login(JObject jo, SocketConnection sConn)
        {
            string msg;
            var user = jo["UserName"].ToString();

            var name = connectionSocketList.Where(b => b.Name == user);
            if (name.Count() != 0)
            {
                msg = "Login|False|用户名已被使用!";
                SendUser(msg, sConn);
                Logger.Log(Enums.LogType.Error, "用户名【" + user + "】已经被使用");

                sConn.ConnectionSocket.Close();
                connectionSocketList.Remove(sConn);
            }
            else
            {
                sConn.Name = user;
                msg = "Login|True|" + user + "|";
                msg += connectionSocketList.Aggregate("", (current, list) => current + ("," + list.Name));
                msg += "|";
                msg += userList.Aggregate("", (current, list) => current + ("," + list));
                msg += "|" + type;
                msg += "|" + titleUserName;
                Send(msg);

                Logger.Log(Enums.LogType.Login, user);
            }
        }
예제 #7
0
        public void StartServer()
        {
            listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            //var address = IPAddress.Parse(ServerIp);
            listener.Bind(new IPEndPoint(GetLocalmachineIpAddress(), ServerPort));
            listener.Listen(connectionsQueueLength);

            Logger.Log(Enums.LogType.Start, string.Format("聊天服务器启动。监听地址:{0}, 端口:{1}", GetLocalmachineIpAddress(), ServerPort));
            Logger.Log(Enums.LogType.Start, string.Format("WebSocket服务器地址: ws://{0}:{1}/chat", GetLocalmachineIpAddress(), ServerPort));

            while (true)
            {
                var sc = listener.Accept();
                System.Threading.Thread.Sleep(100);

                var socketConn = new SocketConnection { ConnectionSocket = sc };
                socketConn.DataReceived += socketConn_DataReceived;
                socketConn.Disconnected += socketConn_Disconnected;

                socketConn.ConnectionSocket.BeginReceive(socketConn.ReceivedDataBuffer,
                                                         0, socketConn.ReceivedDataBuffer.Length,
                                                         0, socketConn.ManageHandshake,
                                                         socketConn.ConnectionSocket.Available);
                connectionSocketList.Add(socketConn);
            }
            // ReSharper disable FunctionNeverReturns
        }
예제 #8
0
 // ReSharper restore FunctionNeverReturns
 public void SendUser(string message, SocketConnection item)
 {
     if (!item.ConnectionSocket.Connected) return;
     Logger.Log(Enums.LogType.Msg, message);
     try
     {
         if (item.IsDataMasked)
         {
             var dr = new DataFrame(message);
             item.ConnectionSocket.Send(dr.GetBytes());
         }
         else
         {
             item.ConnectionSocket.Send(firstByte);
             item.ConnectionSocket.Send(Encoding.UTF8.GetBytes(message));
             item.ConnectionSocket.Send(lastByte);
         }
     }
     catch (Exception ex)
     {
         Logger.Log(Enums.LogType.Error, ex.Message);
     }
 }