コード例 #1
0
ファイル: DBExtensionItems.cs プロジェクト: kamilion/WISP
        public static bool Item_Create(this DB db, ServerGameObject go, string owner, out string msg, out SqlTransaction tran, out SqlConnection con)
        {
            if(go.IsTransient)
            {
                con = null;
                tran = null;
                msg = "Transient objects can't be saved to the database.";
                return false;
            }

            tran = null;
            bool result = true;
            msg = "";

            con = DB.GameDataConnection;
            SqlCommand cmd = DB.GetCommand(con, "Items_Create", true);

            SqlParameter pout = new SqlParameter("@resultCode", 0);
            pout.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(pout);

            cmd.Parameters.Add(new SqlParameter("@GOT", (int)go.GameObjectType));
            cmd.Parameters.Add(new SqlParameter("@UID", go.UID));
            cmd.Parameters.Add(new SqlParameter("@createdOn", go.CreatedOn));
            cmd.Parameters.Add(new SqlParameter("@template", go.ItemTemplate));
            cmd.Parameters.Add(new SqlParameter("@owner", owner));
            cmd.Parameters.Add(new SqlParameter("@context", go.Context));
            cmd.Parameters.Add(new SqlParameter("@typeHash", (long)go.TypeHash));

            Pointer dataPointer = new Pointer();
            byte[] bindata = new byte[1024];
            go.Serialize(ref bindata, dataPointer);

            // Combine envelope and body into final data gram
            byte[] trimData = new byte[dataPointer.Position];
            Util.Copy(bindata, 0, trimData, trimData.Length, dataPointer.Position);

            cmd.Parameters.Add(new SqlParameter("@binData", trimData));
            cmd.Parameters.Add(new SqlParameter("@stackCount", go.StackCount));
            cmd.Parameters.Add(new SqlParameter("@isStatic", go.IsStatic));
            cmd.Parameters.Add(new SqlParameter("@objectOwner", go.Owner));

            if (!go.IsStatic)
            {
                SqlParameter ints = new SqlParameter("@intProperties", ItemIntPropertiesToTable(go.Properties.GetAllPropertiesOfKind(PropertyKind.Int32), go.UID));
                ints.SqlDbType = SqlDbType.Structured;
                cmd.Parameters.Add(ints);

                SqlParameter floats = new SqlParameter("@floatProperties", ItemFloatPropertiesToTable(go.Properties.GetAllPropertiesOfKind(PropertyKind.Single), go.UID));
                floats.SqlDbType = SqlDbType.Structured;
                cmd.Parameters.Add(floats);

                SqlParameter longs = new SqlParameter("@longProperties", ItemLongPropertiesToTable(go.Properties.GetAllPropertiesOfKind(PropertyKind.Int64), go.UID));
                longs.SqlDbType = SqlDbType.Structured;
                cmd.Parameters.Add(longs);

                SqlParameter strings = new SqlParameter("@stringProperties", ItemStringPropertiesToTable(go.Properties.GetAllPropertiesOfKind(PropertyKind.String), go.UID));
                strings.SqlDbType = SqlDbType.Structured;
                cmd.Parameters.Add(strings);

                SqlParameter statsParm = new SqlParameter("@stats", ItemStatsToTable(go.Stats.AllStats, go.UID));
                statsParm.SqlDbType = SqlDbType.Structured;
                cmd.Parameters.Add(statsParm);
            }

            try
            {
                con.Open();
                tran = con.BeginTransaction(IsolationLevel.ReadCommitted);
                cmd.Connection = con;
                cmd.Transaction = tran;

                cmd.ExecuteNonQuery();
                long val = (long)cmd.Parameters[0].Value;
                result = val > 0;

                switch (val)
                {
                    case -1:
                    case 0:
                        msg = "Server was unable to created Item.";
                        break;
                    case 1:
                        msg = "Item created.";
                        break;
                }

                // -1 = unknown error creating Item
                //  0 = unknown error crating Item starting stats
                //  1 = Item created successfully
            }
            catch (Exception e)
            {
                Log1.Logger("Server").Error("[DATABASE ERROR] : " + e.Message);
                result = false;
            }
            finally
            {
                /*
                if (con != null)
                {
                    con.Close();
                    con.Dispose();
                    con = null;
                }

                 * Calling method must close connection. this is to allow API users to append to the Item creation method and commit the transaction
                 before closing the connection.*/
            }

            return result;
        }
コード例 #2
0
ファイル: DBExtensionItems.cs プロジェクト: kamilion/WISP
        private static DataTable GameObjectToTable(ServerGameObject input, DataTable table = null)
        {
            if (table == null)
            {
                table = new DataTable("ItemTable");

                table.Columns.Add("Template", typeof(string));
                table.Columns.Add("CreatedOn", typeof(DateTime));
                table.Columns.Add("GOT", typeof(int));
                table.Columns.Add("UID", typeof(Guid));
                table.Columns.Add("Owner", typeof(string));
                table.Columns.Add("Context", typeof(Guid));
                table.Columns.Add("TypeHash", typeof(long));
                table.Columns.Add("BinData", typeof(byte[]));
                table.Columns.Add("IsStatic", typeof(byte));
                table.Columns.Add("StackCount", typeof(int));
                table.Columns.Add("ObjectOwner", typeof(Guid));
            }

            DataRow r = table.NewRow();
            r["Template"] = input.ItemTemplate;
            r["CreatedOn"] = input.CreatedOn;
            r["GOT"] = (int)input.GameObjectType;
            r["UID"] = input.UID;
            r["Owner"] = input.OwningServer;
            r["Context"] = input.Context;
            r["TypeHash"] = input.TypeHash;

            Pointer dataPointer = new Pointer();
            byte[] bindata = new byte[1024];
            input.Serialize(ref bindata, dataPointer);

            // Combine envelope and body into final data gram
            byte[] trimData = new byte[dataPointer.Position];
            Util.Copy(bindata, 0, trimData, trimData.Length, dataPointer.Position);

            r["BinData"] = trimData;

            r["StackCount"] = input.StackCount;
            r["IsStatic"] = input.IsStatic;
            r["ObjectOwner"] = input.Owner;
            table.Rows.Add(r);

            return table;
        }