Exemple #1
0
        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (string item in dicClientAndName.Values)
            {
                string str = string.Format("Update UserT set State=0 where UserName='******'", item);
                db.getsqlcom(str);
            }
            Dictionary <string, string> quit = new Dictionary <string, string>();

            quit.Add("Type", "Quit");
            byte[] send = mp.SendCon(quit);
            MsgProcess.SendAllClient(comboBoxClient, dicSocket, send);
            Server.Client.Shutdown(SocketShutdown.Both);
            System.Environment.Exit(0);
        }
Exemple #2
0
        void TCPServer(object o)
        {
            Socket SocketClient = o as Socket;

            while (true)
            {
                byte[] jbyte = new byte[1024];
                int    r     = SocketClient.Receive(jbyte);
                Dictionary <string, string> data = mp.ReCon(jbyte, 0, r);
                if (data["Type"] == "Userdata")
                {
                    string        sqlstr     = string.Format("select ID,UserName,GameCount,Exp,Coin,CurrentLevel from UserT where UserName='******'", data["UserName"]);
                    SqlDataReader dataReader = db.getcom(sqlstr);
                    string        sql2       = string.Format("update UserT set State=1 where UserName='******'", data["UserName"]);
                    db.getsqlcom(sql2);
                    Dictionary <string, string> result = new Dictionary <string, string>();
                    if (dataReader.HasRows)
                    {
                        dataReader.Read();
                        result.Add("ID", dataReader[0].ToString().Trim());
                        result.Add("UserName", dataReader[1].ToString().Trim());
                        result.Add("GameCount", dataReader[2].ToString().Trim());
                        result.Add("Exp", dataReader[3].ToString().Trim());
                        result.Add("Coin", dataReader[4].ToString().Trim());
                        result.Add("CurrentLevel", dataReader[5].ToString().Trim());
                        byte[] newsend = mp.SendCon(result);
                        SocketClient.Send(newsend);
                        string sqlstr3 = string.Format("update UserT set Lastlogin='******' where UserName = '******'", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), data["UserName"]);
                        db.getsqlcom(sqlstr3);
                    }
                    else
                    {
                        result.Add("Error", "404");
                        string send    = JsonConvert.SerializeObject(result);
                        byte[] newsend = Encoding.UTF8.GetBytes(send);
                        SocketClient.Send(newsend);
                    }
                    dicNameAndClient.Add(data["UserName"], SocketClient.RemoteEndPoint.ToString());
                    dicClientAndName.Add(SocketClient.RemoteEndPoint.ToString(), data["UserName"]);
                }
                else if (data["Type"] == "Message")
                {
                    if (data.ContainsKey("ToUser"))
                    {
                        string sqlstr = string.Format("insert into ChatT values({0},{1},{2},'{3}','{4}','{5}')", NameID[data["UserName"]], NameID[data["TOUser"]], data["RoomID"], DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), data["Chat"], data["Bgroup"]);
                        db.getsqlcom(sqlstr);
                        Dictionary <string, string> send = new Dictionary <string, string>();
                        send.Add("Type", "Message2");
                        send.Add("From", data["UserName"]);
                        send.Add("TO", data["TOUser"]);
                        send.Add("Message", data["Chat"]);
                        send.Add("Time", DateTime.Now.ToString());
                        byte[] newsend = mp.SendCon(send);
                        dicSocket[dicNameAndClient[data["TOUser"]]].Send(newsend);
                        SocketClient.Send(newsend);
                        ShowGroupMsg(DateTime.Now.ToString() + ":转发消息:" + data["UserName"] + "→" + data["TOUser:"******"Chat"]);
                    }
                    else
                    {
                        string sqlstr = string.Format("insert into ChatT values({0},{1},{2},'{3}','{4}','{5}')", NameID[data["UserName"]], "null", data["RoomID"], DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), data["Chat"], data["Bgroup"]);
                        db.getsqlcom(sqlstr);
                        Dictionary <string, string> send = new Dictionary <string, string>();
                        send.Add("Type", "Message1");
                        send.Add("From", data["UserName"]);
                        send.Add("Message", data["Chat"]);
                        send.Add("Time", DateTime.Now.ToString());
                        byte[] newsend = mp.SendCon(send);
                        MsgProcess.SendAllClient(comboBoxClient, dicSocket, newsend);
                        ShowGroupMsg(DateTime.Now.ToString() + ":" + data["UserName"] + ":" + data["Chat"]);
                    }
                }
                else if (data["Type"] == "JoinRoom")
                {
                    Dictionary <string, string> room = new Dictionary <string, string>();
                    string        str = string.Format("select * from RoomT where RoomID={0}", data["RoomID"]);
                    SqlDataReader sdr = db.getcom(str);
                    if (sdr.HasRows)
                    {
                        while (sdr.Read())
                        {
                            while (sdr.Read())
                            {
                                for (int i = 0; i < sdr.FieldCount; i++)
                                {
                                    room.Add(sdr.GetName(i), sdr[i].ToString().Trim());
                                }
                            }
                        }
                    }
                    if (room["RoomPName"].Length == 0)
                    {
                        room["RoomPName"]  = data["UserName"];
                        room["RoomPID"]    = data["ID"];
                        room["RoomPState"] = "1";
                        string sqlstr = string.Format("update RoomT set RoomPName={0},RoomPID={1},RoomPState={2} where RoomID={3}", room["RoomPName"], room["RoomPID"], room["RoomPState"], room["RoomID"]);
                        db.getsqlcom(sqlstr);
                    }
                    else if (room["RoomP2Name"].Length == 0)
                    {
                        room["RoomP2Name"]  = data["UserName"];
                        room["RoomP2ID"]    = data["ID"];
                        room["RoomP2State"] = "1";
                        string sqlstr = string.Format("update RoomT set RoomP2Name={0},RoomP2ID={1},RoomP2State={2} where RoomID={3}", room["RoomPName"], room["RoomPID"], room["RoomPState"], room["RoomID"]);
                        db.getsqlcom(sqlstr);
                    }
                    else
                    {
                        Dictionary <string, string> failsend = new Dictionary <string, string>();
                        failsend.Add("Type", "JoinRoom");
                        failsend.Add("State", "Failed");
                        failsend.Add("RoomID", data["RoomID"]);
                        failsend.Add("Reason", "full");
                        byte[] fsend = mp.SendCon(failsend);
                    }
                    if (room["RoomPName"] != null && room["RoomP2Name"] != null)
                    {
                        room["GameStatus"] = "0";
                        string sqlstr = string.Format("update RoomT set GameStatus={0} where RoomID={1}", room["RoomID"], room["GameStatus"]);
                        db.getsqlcom(sqlstr);
                    }
                    Dictionary <string, string> send = new Dictionary <string, string>();
                    send.Add("Type", "JoinRoom");
                    send.Add("State", "Success");
                    send.Add("RoomID", data["RoomID"]);
                    string        sqlstr3 = string.Format("select * from UserT where ID={0}", room["RoomCID"]);
                    SqlDataReader src     = db.getcom(sqlstr3);
                    while (src.Read())
                    {
                        Dictionary <string, string> Uinfo = new Dictionary <string, string>();
                        for (int i = 0; i < src.FieldCount; i++)
                        {
                            Uinfo.Add(src.GetName(i), src[i].ToString().Trim());
                        }
                        send.Add(room["RoomCID"], JsonConvert.SerializeObject(Uinfo));
                    }
                    if (room["RoomPName"] != null)
                    {
                        string        sql2  = string.Format("select * from UserT where ID={0}", room["RoomPID"]);
                        SqlDataReader sqlr2 = db.getcom(sql2);
                        while (sqlr2.Read())
                        {
                            Dictionary <string, string> Uinfo = new Dictionary <string, string>();
                            for (int i = 0; i < sqlr2.FieldCount; i++)
                            {
                                Uinfo.Add(sqlr2.GetName(i), sqlr2[i].ToString().Trim());
                            }
                            send.Add(room["RoomPID"], JsonConvert.SerializeObject(Uinfo));
                        }
                    }
                    if (room["RoomP2Name"] != null)
                    {
                        string        sql3  = string.Format("select * from UserT where ID={0}", room["RoomP2ID"]);
                        SqlDataReader sqlr3 = db.getcom(sql3);
                        while (sqlr3.Read())
                        {
                            Dictionary <string, string> Uinfo = new Dictionary <string, string>();
                            for (int i = 0; i < sqlr3.FieldCount; i++)
                            {
                                Uinfo.Add(sqlr3.GetName(i), sqlr3[i].ToString().Trim());
                            }
                            send.Add(room["RoomP2ID"], JsonConvert.SerializeObject(Uinfo));
                        }
                    }
                    byte[]        newsend    = mp.SendCon(send);
                    string        sqlstr2    = string.Format("select RoomCName,RoomPName,RoomP2Name from RoomT where RoomID = {0}", data["RoomID"]);
                    SqlDataReader dataReader = db.getcom(sqlstr2);
                    dataReader.Read();
                    if (dataReader.HasRows)
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            if (dataReader[i] != System.DBNull.Value)
                            {
                                dicSocket[dicNameAndClient[dataReader[i].ToString().Trim()]].Send(newsend);
                            }
                        }
                    }
                }
                else if (data["Type"] == "QuitRoom")
                {
                    string sqlstr = string.Format("update RoomT set {0}= null,{1}= null,{2}= 0 where RoomID = {3}", data["Position"], data["PositionID"], data["PositionState"], data["RoomID"]);
                    db.getsqlcom(sqlstr);
                    Dictionary <string, string> send = new Dictionary <string, string>();
                    send.Add("Type", "QuitRoom");
                    send.Add("State", "Success");
                    send.Add("RoomID", data["RoomID"]);
                    send.Add("UserName", data["UserName"]);
                    byte[]        newsend    = mp.SendCon(send);
                    string        sqlstr3    = string.Format("select RoomCName,RoomPName,RoomP2Name from RoomT where RoomID = {0}", data["RoomID"]);
                    SqlDataReader dataReader = db.getcom(sqlstr3);
                    dataReader.Read();
                    if (dataReader.HasRows)
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            if (dataReader[i] != System.DBNull.Value)
                            {
                                dicSocket[dicNameAndClient[dataReader[i].ToString().Trim()]].Send(newsend);
                            }
                        }
                    }
                }
                else if (data["Type"] == "ChangeState")
                {
                    string sqlstr = string.Format("update RoomT set {0}={1} where RoomID = {2}", data["Position"], data["State"], data["RoomID"]);
                    db.getsqlcom(sqlstr);
                    Dictionary <string, string> send = new Dictionary <string, string>();
                    send.Add("Type", "CState");
                    send.Add("State", data["State"]);
                    byte[]        newsend    = mp.SendCon(send);
                    string        sqlstr2    = string.Format("select RoomCName,RoomPName,RoomP2Name from RoomT where RoomID = {0}", data["RoomID"]);
                    SqlDataReader dataReader = db.getcom(sqlstr2);
                    dataReader.Read();
                    if (dataReader.HasRows)
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            if (dataReader[i] != System.DBNull.Value)
                            {
                                dicSocket[dicNameAndClient[dataReader[i].ToString().Trim()]].Send(newsend);
                            }
                        }
                    }
                }
                else if (data["Type"] == "NewGame")
                {
                    string sqlstr  = string.Format("insert into GameT values()");
                    string sqlstr2 = string.Format("insert into GameT values()");
                    string sqlstr3 = string.Format("insert into GameT values()");
                    string sqlstr4 = string.Format("update RoomT set RoomCState=17,RoomPState=17,RoomP2State=17 where RoomID = {0}", data["RoomID"]);
                    string sqlstr5 = string.Format("select RoomCName,RoomPName,RoomP2Name from RoomT where RoomID = {0}", data["RoomID"]);
                    db.getsqlcom(sqlstr);
                    db.getsqlcom(sqlstr2);
                    db.getsqlcom(sqlstr3);
                    db.getsqlcom(sqlstr4);
                    SqlDataReader reader = db.getcom(sqlstr5);
                    if (reader.HasRows)
                    {
                        Dictionary <string, IPEndPoint> newgame = new Dictionary <string, IPEndPoint>();
                        Thread th1 = new Thread(GameProcess);
                        th1.IsBackground = true;
                        th1.Start(newgame);
                    }
                    else
                    {
                        Dictionary <string, string> send = new Dictionary <string, string>();
                        send.Add("Type", "GState");
                        send.Add("State", "Failed");
                        byte[] newsend = mp.SendCon(send);
                        SocketClient.Send(newsend);
                    }
                }
                else if (data["Type"] == "Logout")
                {
                    IDName.Remove(NameID[data["UserName"]]);
                    NameID.Remove(data["UserName"]);
                    NameOnline.Remove(data["UserName"]);
                    if (data["RoomPosition"] != "Null")
                    {
                        string sqlstr  = string.Format("update RoomT set {0}={1},{2}={3} where RoomID={4}", data["RoomPosition"], "null", data["PositionID"], "null", data["RoomID"]);
                        string sqlstr2 = string.Format("exec RoomCheck");
                        string sqlstrx = string.Format("delete from RoomT where RoomCName is null and RoomPName is null and RoomP2Name is null");
                        db.getsqlcom(sqlstr);
                        db.getsqlcom(sqlstr2);
                        db.getsqlcom(sqlstrx);
                        Dictionary <string, string> send = new Dictionary <string, string>();
                        send.Add("Type", "QuitRoom");
                        send.Add("State", "Success");
                        send.Add("RoomID", data["RoomID"]);
                        send.Add("UserName", data["UserName"]);
                        byte[]        newsend    = mp.SendCon(send);
                        string        sqlstr5    = string.Format("select RoomCName,RoomPName,RoomP2Name from RoomT where RoomID = {0}", data["RoomID"]);
                        SqlDataReader dataReader = db.getcom(sqlstr5);
                        dataReader.Read();
                        if (dataReader.HasRows)
                        {
                            for (int i = 0; i < 3; i++)
                            {
                                if (dataReader[i] != System.DBNull.Value)
                                {
                                    dicSocket[dicNameAndClient[dataReader[i].ToString().Trim()]].Send(newsend);
                                }
                            }
                        }
                    }
                    string sqlstr3 = string.Format("update UserT set State=0 where UserName='******'", data["UserName"]);
                    db.getsqlcom(sqlstr3);
                    dicNameAndClient.Remove(data["UserName"]);
                    dicClientAndName.Remove(SocketClient.RemoteEndPoint.ToString());
                    showlogmsg(DateTime.Now.ToString() + ":" + data["UserName"] + ":已退出!");
                    break;
                }
                else if (data["Type"] == "OnlineCheck")
                {
                    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    Dictionary <string, string> send = new Dictionary <string, string>();
                    send.Add("Type", "OnlineCheck");
                    send.Add("State", "1");
                    byte[] newsend = mp.SendCon(send);
                    socket.SendTo(newsend, endPoint);
                    if (NameOnline.ContainsKey(data["UserName"]))
                    {
                        NameOnline[data["UserName"]] = DateTime.Now;
                    }
                }
                else if (data["Type"] == "Request")
                {
                    Dictionary <string, string> dat = new Dictionary <string, string>();
                    dat.Add("LastUpdate", DateTime.Now.ToString());
                    string        sqlstr   = "select ID,UserName from UserT where State=1";
                    SqlDataReader iuReader = db.getcom(sqlstr);
                    while (iuReader.Read())
                    {
                        dat.Add(iuReader[0].ToString().Trim(), iuReader[1].ToString().Trim());
                    }
                    string        sqlstr2 = "select * from RoomT";
                    SqlDataReader rreader = db.getcom(sqlstr2);
                    if (rreader.HasRows)
                    {
                        Dictionary <string, string> room = new Dictionary <string, string>();
                        int count = 0;
                        while (rreader.Read())
                        {
                            for (int i = 0; i < rreader.FieldCount; i++)
                            {
                                room.Add(rreader.GetName(i), rreader[i].ToString().Trim());
                            }
                            dat.Add("RoomData" + count.ToString(), JsonConvert.SerializeObject(room));
                            room.Clear();
                            count++;
                        }
                    }
                    byte[] send = mp.SendCon(dat);
                    SocketClient.Send(send);
                }
            }
        }