예제 #1
0
        public static object Deserialize(byte[] data)
        {
            var result = new GeneralData();

            using (MemoryStream m = new MemoryStream(data))
            {
                using (BinaryReader reader = new BinaryReader(m))
                {
                    result.Pos        = reader.ReadString();
                    result.ItemName   = reader.ReadString();
                    result.PlayerName = reader.ReadString();
                    result.FriendName = reader.ReadString();
                }
            }
            return(result);
        }
        public override void OnRaiseEvent(IRaiseEventCallInfo info)
        {
            try
            {
                base.OnRaiseEvent(info);
            }
            catch (Exception e)
            {
                this.PluginHost.BroadcastErrorInfoEvent(e.ToString(), info);
                return;
            }

            switch (info.Request.EvCode)
            {
            case 1:
            {
                //return msg (0 - fail, 1 - create, 2 - success)
                recvdMessage = Encoding.Default.GetString((byte[])info.Request.Data);
                //string playerName = Encoding.Default.GetString((byte[])info.Request.Data);

                string playerName     = GetStringDataFromMessage("PlayerName");
                string playerPassword = GetStringDataFromMessage("Password");
                string ReturnMessage  = "2";

                if (!CheckUserDatabase(playerName, playerPassword)) //first check to see if login fail or there exist not such user
                {
                    if (!CheckForUser(playerName))                  // check if user exist (true->password failed)(false->user does not exist)
                    {                                               //create
                        string       sql = "INSERT INTO users (name,password,date_created) VALUES('" + playerName + "', '" + playerPassword + "', now())";
                        MySqlCommand cmd = new MySqlCommand(sql, conn);
                        cmd.ExecuteNonQuery();
                        ReturnMessage = "1";
                    }
                    else         //failed
                    {
                        ReturnMessage = "0";
                    }
                }

                //++this.CallsCount;
                //int cnt = this.CallsCount;
                //string ReturnMessage = info.Nickname + " clicked the button. Now the count is " + cnt.ToString();
                this.PluginHost.BroadcastEvent(target: ReciverGroup.Group, senderActor: 0, targetGroup: 1, data: new Dictionary <byte, object>()
                    {
                        {
                            (byte)245, ReturnMessage
                        }
                    }, evCode: info.Request.EvCode, cacheOp: 0);

                //this.PluginHost.BroadcastEvent(target: ReciverGroup.All, senderActor: 0, targetGroup: 0, evCode: info.Request.EvCode,
                //    data: new Dictionary<byte, object>() { { (byte)245, ReturnMessage } }, cacheOp: 0);
            }
            break;

            case 2:
            {
                //recvdMessage = Encoding.Default.GetString((byte[])info.Request.Data);

                //string playerName = GetStringDataFromMessage("PlayerName");
                //string json = GetStringDataFromMessage("Json");
                //PluginHost.LogDebug(json);
                //string itemName = GetStringDataFromMessage("ItemName");
                //string pos = GetStringDataFromMessage("Pos");

                GeneralData gData      = (GeneralData)GeneralData.Deserialize((byte[])info.Request.Data);
                string      playerName = gData.PlayerName;
                string      json       = gData.FriendName;
                PluginHost.LogDebug(json);
                string itemName = gData.ItemName;
                string pos      = gData.Pos;

                if (CheckForUser(playerName))         // check if user exist in db, if exist take data
                {
                    //string sql = "SELECT name, data FROM users WHERE name = '" + playerName + "' AND data = '" + _password + "'";
                    //MySqlCommand cmd = new MySqlCommand(sql, conn);
                    //cmd.Parameters.Add(new MySqlParameter("name", _username));
                    //cmd.Parameters.Add(new MySqlParameter("password", _password));
                    //MySqlDataReader rdr = cmd.ExecuteReader();
                    //bool bLogin = rdr.HasRows;
                    //rdr.Close();


                    //string sql = "UPDATE users SET data = '" + json + "' WHERE name = '" + playerName + "' AND data IS NULL";
                    //PluginHost.LogDebug(sql);
                    //MySqlCommand cmd = new MySqlCommand(sql, conn);
                    //int rowAffected = cmd.ExecuteNonQuery();

                    //PluginHost.LogDebug("Updated data");

                    //if(rowAffected > 0)
                    //{
                    //    sql = "UPDATE users SET data = '" + json + "' WHERE name = '" + playerName + "'"
                    //}

                    string sql = "UPDATE users SET data = '" + json + "', pos = '" + pos + "', item = '" + itemName + "' WHERE name = '" + playerName + "'";
                    PluginHost.LogDebug(sql);
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.ExecuteNonQuery();

                    PluginHost.LogDebug("Updated data");
                }
            }
            break;

            case 3:     // Get json data
            {
                recvdMessage = Encoding.Default.GetString((byte[])info.Request.Data);
                string playerName = GetStringDataFromMessage("PlayerName");
                string json       = "";
                string pos        = "";
                string item       = "";

                string       sql = "SELECT data, pos, item FROM users WHERE name = '" + playerName + "'";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                PluginHost.LogDebug(playerName);
                //cmd.Parameters.Add(new MySqlParameter("name", playerName));
                using (MySqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        json = rdr["data"].ToString();
                        pos  = rdr["pos"].ToString();
                        item = rdr["item"].ToString();
                    }

                    PluginHost.LogDebug(pos);
                    rdr.Close();
                }

                //json = json.Trim('{','}');
                //pos = pos.Trim('{', '}');

                //if (item == null || item.Length <= 0)
                //    item = "";

                //item = "\"" + item + "\"";

                //string itemPrefix = @"""item"":";
                //string jsonData = "{" + json + "," + pos + "," + itemPrefix + item + "}";

                GeneralData gData = new GeneralData();
                gData.PlayerName = playerName;
                gData.FriendName = json;
                gData.ItemName   = item;
                gData.Pos        = pos;

                this.PluginHost.BroadcastEvent(target: ReciverGroup.All, senderActor: 0, targetGroup: 0, data: new Dictionary <byte, object>()
                    {
                        {
                            (byte)245, GeneralData.Serialize(gData)
                        }
                    }, evCode: info.Request.EvCode, cacheOp: 0);
                //this.PluginHost.BroadcastEvent(target: ReciverGroup.All, senderActor: 0, targetGroup: 0, data: new Dictionary<byte, object>() { {
                //(byte)245, jsonData } }, evCode: info.Request.EvCode, cacheOp: 0);
            }
            break;
            }

            #region Commented_Out
            //try
            //{
            //    base.OnRaiseEvent(info);
            //}
            //catch (Exception e)
            //{
            //    this.PluginHost.BroadcastErrorInfoEvent(e.ToString(), info);
            //    return;
            //}

            //if (info.Request.EvCode == 1)
            //{
            //    recvdMessage = Encoding.Default.GetString((byte[])info.Request.Data);
            //    //string playerName = Encoding.Default.GetString((byte[])info.Request.Data);

            //    string playerName = GetStringDataFromMessage("PlayerName");
            //    string playerPassword = GetStringDataFromMessage("Password");
            //    string ReturnMessage = "Login in success";

            //    if (!CheckUserDatabase(playerName, playerPassword)) //first check to see if login fail or there exist not such user
            //    {
            //        if (!CheckForUser(playerName)) // check if user exist (true->password failed)(false->user does not exist)
            //        {
            //            string sql = "INSERT INTO users (name,password,date_created) VALUES('" + playerName + "', '" + playerPassword + "', now())";
            //            MySqlCommand cmd = new MySqlCommand(sql, conn);
            //            cmd.ExecuteNonQuery();
            //            ReturnMessage = "New account created!";
            //        }
            //        else
            //            ReturnMessage = "Password is wrong please try again!";

            //    }

            //    //++this.CallsCount;
            //    //int cnt = this.CallsCount;
            //    //string ReturnMessage = info.Nickname + " clicked the button. Now the count is " + cnt.ToString();
            //    // this.PluginHost.BroadcastEvent(target: ReciverGroup.All, senderActor: 0, targetGroup: 0, data: new Dictionary<byte, object>() { {
            //    //(byte)245, ReturnMessage } }, evCode: info.Request.EvCode, cacheOp: 0);

            //    this.PluginHost.BroadcastEvent(target: ReciverGroup.All, senderActor: 0, targetGroup: 0, evCode: info.Request.EvCode,
            //        data: new Dictionary<byte, object>() { { (byte)245, ReturnMessage } }, cacheOp: 0);
            //}
            #endregion
        }