示例#1
0
        public static void UnRegister(ServerGameObjectManager som)
        {
            ServerGameObjectManager somt = null;

            somt.StopSaveCycle();
            ManagersByContext.TryRemove(som.Context, out somt);
        }
示例#2
0
        /// <summary>
        /// Gets a reference to a server game object currently in memory, i.e. previously loaded from the DB.
        /// </summary>
        /// <param name="id">id of the object being returned</param>
        /// <returns></returns>
        public ServerGameObject GetItem(Guid id, ServerGameObjectManager gom, bool includeDeleted = false)
        {
            ServerGameObject sgo = gom.GetGameObjectFromId(id) as ServerGameObject;

            if (sgo == null || (!includeDeleted && sgo.IsDeleted))
            {
                return(null);
            }

            return(sgo);
        }
示例#3
0
        /// <summary>
        /// creates a Item object, but does not persist it.
        /// </summary>
        /// <param name="properties">Item properties to add</param>
        /// <param name="owner">the owning user</param>
        /// <param name="context">Any user defined context - could be a game room, an owning server, etc.  GameObjectManager keeps sub-lists of games based on context ID. Use GUID.Empty for none. </param>
        /// <returns></returns>
        public ServerGameObject CreateNewItem(string itemTemplate, Guid context, ServerGameObjectManager gom, string owningServer)
        {
            ServerGameObject sgo = CreateItemShell(itemTemplate);

            if (sgo == null)
            {
                return(null);
            }

            sgo.IsGhost = true;
            // Instantiate the inner GameObject
            sgo.Context = context;
            // Add the needed bits
            sgo.UID          = Guid.NewGuid();
            sgo.CreatedOn    = DateTime.UtcNow;
            sgo.OwningServer = owningServer;
            gom.RegisterGameObject(sgo, context);
            return(sgo);
        }
示例#4
0
        /// <summary>
        /// Delete an object from the world.
        /// </summary>
        /// <param name="id"></param>
        public void DeleteItem(Guid id, Guid accountDeleting, string reason, ServerGameObjectManager gom)
        {
            ServerGameObject sgo = null;

            if (gom != null)
            {
                GetItem(id, gom);
            }
            else
            {
                string msg = "";
                sgo = LoadItem(id, true, "", ref msg, null);
            }

            if (sgo != null)
            {
                sgo.IsDeleted      = true;
                sgo.DeleteReason   = reason;
                sgo.AccountDeleted = accountDeleting;
            }
        }
示例#5
0
        public static int Item_BatchUpdate(this DB db, List<IGameObject> objects, out string msg, ServerGameObjectManager gom)
        {
            /*
                WHEN NOT MATCHED [BY TARGET] -- row exists in source but not in target
                WHEN NOT MATCHED BY SOURCE -- row exists in target but not in source
            */

            msg = "";

            int processed = 0;
            // Gather the data tables
            DataTable intTable = null;
            DataTable floatTable = null;
            DataTable longTable = null;
            DataTable stringTable = null;
            DataTable statTable = null;
            DataTable itemTable = null;
            DataTable deleteTable = null;

            List<ServerGameObject> done = new List<ServerGameObject>();
            int maxCount = GetMaxBatchItemCount();
            for (int i = 0; i < objects.Count; i++)
            {
                ServerGameObject sgo = (ServerGameObject) objects[i];

                // Transient objects only exist in memory and are not persisted in the database
                if(sgo == null || sgo.IsTransient)
                {
                    continue;
                }

                bool added = false;

                // if the object is deleted, add it to the deleted table and add it to the list of objects that have been processed (are @done)
                if (sgo.IsDeleted)
                {
                    // add to deleted table
                    deleteTable = GameObjectToDeleteRow(sgo);
                    done.Add(sgo);
                    added = true; // we added something to the done list in the delete block
                }

                // if the object has new/changed data (isDirty), or the object is a ghost, i.e. only exists in memory because it has not been written to the db yet and the
                // item is a valid item (ValidateITemCreateRequest), then we add this item's information to the data that will be scheduled to be written to the db
                if (sgo.IsDirty || (sgo.IsGhost && ItemUtil.Instance.ValidateItemCreateRequest(sgo, ref msg))) // never before written to DB
                {
                    // Add object data to the item data table
                    itemTable = GameObjectToTable(sgo, itemTable);

                    // if th item is not static, it might have individual, i.e. instance data like properties and stats.  Therefore, if the item is not static
                    // we write the object's instance data to the tables which will be cached to be written to the DB
                    if (!sgo.IsStatic)
                    {
                        intTable = ItemIntPropertiesToTable(sgo.Properties.GetAllPropertiesOfKind(PropertyKind.Int32), sgo.UID, intTable);
                        floatTable = ItemFloatPropertiesToTable(sgo.Properties.GetAllPropertiesOfKind(PropertyKind.Single), sgo.UID, floatTable);
                        longTable = ItemLongPropertiesToTable(sgo.Properties.GetAllPropertiesOfKind(PropertyKind.Int64), sgo.UID, longTable);
                        stringTable = ItemStringPropertiesToTable(sgo.Properties.GetAllPropertiesOfKind(PropertyKind.String), sgo.UID, stringTable);
                        statTable = ItemStatsToTable(sgo.Stats.AllStats, sgo.UID, statTable);
                        sgo.IsSaving = true;
                    }

                    // if the object has not been previously added to the "@done", i.e. processed, item list, add it now.
                    if (!added)
                    {
                        done.Add(sgo);
                    }
                }

                // If we've processed the maximum number of objects that we are configured to process in one pass, or if we are the end of the list of objects to be processed,
                // then we issue a command to the DB to write all of the item's data that we have thus far cached.
                if(done.Count >= maxCount || i >= objects.Count-1)
                {
                    // do we have anything to process?  Could have passed in an object list with zero objects.
                    if (done.Count > 0)
                    {
                        // Issue the DB persist command
                        processed += ItemBatchUpdateStep(done, itemTable, intTable, floatTable, longTable, stringTable, statTable, deleteTable, gom);
                    }

                    // Clear out the item table caches and the "@done", i.e. processed items list.
                    itemTable = intTable = floatTable = longTable = stringTable = statTable = null;
                    done.Clear();

                    maxCount = GetMaxBatchItemCount();
                }

                if(sgo.IsZombie & gom != null)
                {
                    gom.RemoveGameObject(sgo);
                }
            }

            return processed;
        }
示例#6
0
        private static int ItemBatchUpdateStep(List<ServerGameObject> done, DataTable itemTable, DataTable intTable, DataTable floatTable, DataTable longTable, DataTable stringTable, DataTable statTable, DataTable deleteTable, ServerGameObjectManager gom)
        {
            int processed = 0;
            SqlConnection con = DB.GameDataConnection;
            SqlCommand cmd = DB.GetCommand(con, "Items_BatchUpdateOrInsert", true, 60);

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

            SqlParameter items = new SqlParameter("@Items", itemTable);
            items.SqlDbType = SqlDbType.Structured;
            cmd.Parameters.Add(items);

            if (intTable != null)
            {
                SqlParameter ints = new SqlParameter("@ItemPropertyInts", intTable);
                ints.SqlDbType = SqlDbType.Structured;
                cmd.Parameters.Add(ints);
            }

            if (floatTable != null)
            {
                SqlParameter floats = new SqlParameter("@ItemPropertyFloats", floatTable);
                floats.SqlDbType = SqlDbType.Structured;
                cmd.Parameters.Add(floats);
            }

            if (longTable != null)
            {
                SqlParameter longs = new SqlParameter("@ItemPropertyLongs", longTable);
                longs.SqlDbType = SqlDbType.Structured;
                cmd.Parameters.Add(longs);
            }

            if (stringTable != null)
            {
                SqlParameter strings = new SqlParameter("@ItemPropertyStrings", stringTable);
                strings.SqlDbType = SqlDbType.Structured;
                cmd.Parameters.Add(strings);
            }

            if (statTable != null)
            {
                SqlParameter statsParm = new SqlParameter("@ItemPropertyStats", statTable);
                statsParm.SqlDbType = SqlDbType.Structured;
                cmd.Parameters.Add(statsParm);
            }

            if (deleteTable != null)
            {
                SqlParameter deleteParm = new SqlParameter("@DeleteItems", deleteTable);
                deleteParm.SqlDbType = SqlDbType.Structured;
                cmd.Parameters.Add(deleteParm);
            }

            try
            {
                con.Open();
                cmd.Connection = con;

                cmd.ExecuteNonQuery();
                long val = (long)cmd.Parameters[0].Value;
                bool result = val > 0;
                if (!result)
                {
                    processed = 0;
                }
                else
                {
                    foreach (ServerGameObject go in done)
                    {
                        processed++;
                        if(go.IsDeleted)
                        {
                            gom.RemoveGameObject(go);
                        }
                        go.IsGhost = false;
                        go.IsDirty = false;
                        go.IsSaving = false;
                    }
                }

                BatchTimeoutErrs--;
                if(MaxBatchItemCountMod < 0 && BatchTimeoutErrs < 0)
                {
                    MaxBatchItemCountMod += 0.5f;
                }
                // -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);
                if(e.Message.ToLower().Contains("timeout"))
                {
                    BatchTimeoutErrs+=20;
                    MaxBatchItemCountMod -= 0.1f;
                }
                processed = 0;
                foreach (ServerGameObject go in done)
                {
                    go.IsSaving = false;
                }
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                    con.Dispose();
                    con = null;
                }
            }
            return processed;
        }
示例#7
0
 public static void UnRegister(ServerGameObjectManager som)
 {
     ServerGameObjectManager somt = null;
     somt.StopSaveCycle();
     ManagersByContext.TryRemove(som.Context, out somt);
 }
示例#8
0
 public static void Register(ServerGameObjectManager som)
 {
     ManagersByContext.TryAdd(som.Context, som);
     som.RunSaveCycle(ConfigHelper.GetStringConfig("ServerUserID", "EnterServerID"));
 }
示例#9
0
 public TelegramDispatcher(ServerGameObjectManager gamesActorManager)
 {
     m_ActorManager = gamesActorManager;
 }
示例#10
0
        /// <summary>
        /// Loads an Item from the DB
        /// </summary>
        /// <param name="owner">owning account</param>
        /// <param name="id">the id for the Item to get</param>
        /// <returns></returns>
        public ServerGameObject LoadItem(Guid id, bool includeDeleted, string lockedByServerId, ref string rsultMsg, ServerGameObjectManager gom)
        {
            if (id == Guid.Empty)
            {
                return(null);
            }



            SqlConnection    con  = null;
            SqlTransaction   tran = null;
            ServerGameObject sgo  = null;

            if (gom != null)
            {
                sgo = gom.GetGameObjectFromId(id) as ServerGameObject;
                if (sgo != null)
                {
                    return(sgo);
                }
            }


            try
            {
                Guid cown = Guid.Empty;
                if (DB.Instance.Item_Load(out sgo, id, includeDeleted, lockedByServerId, out tran, out con))
                {
                    ((ServerGameObject)sgo).IsDirty = false;
                    ((ServerGameObject)sgo).IsGhost = false;

                    tran.Commit();

                    bool isDeleted = false;
                    isDeleted = ((ServerGameObject)sgo).IsDeleted;

                    if (!isDeleted && lockedByServerId != null && lockedByServerId.Length > 0)
                    {
                        if (gom != null)
                        {
                            gom.RegisterGameObject(sgo, sgo.Context);
                        }
                    }
                }
                else
                {
                    rsultMsg = "Item doesn't exist.";
                    return(null);
                }
            }
            catch (Exception e)
            {
                return(null);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                    con.Dispose();
                    con = null;
                }
                if (tran != null)
                {
                    tran.Dispose();
                    tran = null;
                }
            }

            return(sgo);
        }
示例#11
0
        /// <summary>
        /// Loads an Item from the DB
        /// </summary>
        /// <param name="owner">owning account</param>
        /// <param name="id">the id for the Item to get</param>
        /// <returns></returns>
        public List <IGameObject> LoadItems(List <Guid> ids, bool includeDeleted, string lockedByServerId, ref string rsultMsg, ServerGameObjectManager gom)
        {
            SqlConnection      con   = null;
            SqlTransaction     tran  = null;
            List <IGameObject> items = new List <IGameObject>();

            try
            {
                Guid cown = Guid.Empty;
                items = DB.Instance.Item_LoadBatch(ids, includeDeleted, lockedByServerId, out tran, out con);
                if (items.Count > 0)
                {
                    foreach (IGameObject sgo in items)
                    {
                        ((ServerGameObject)sgo).IsDirty = false;
                        ((ServerGameObject)sgo).IsGhost = false;
                        if (!((ServerGameObject)sgo).IsDeleted && lockedByServerId != null && lockedByServerId.Length > 0)
                        {
                            gom.RegisterGameObject(sgo, sgo.Context);
                        }
                    }
                    tran.Commit();
                }
                else
                {
                    rsultMsg = "Item doesn't exist.";
                    return(null);
                }
            }
            catch (Exception e)
            {
                return(null);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                    con.Dispose();
                    con = null;
                }
                if (tran != null)
                {
                    tran.Dispose();
                    tran = null;
                }
            }
            return(items);
        }
示例#12
0
 public static void Register(ServerGameObjectManager som)
 {
     ManagersByContext.TryAdd(som.Context, som);
     som.RunSaveCycle(ConfigHelper.GetStringConfig("ServerUserID", "EnterServerID"));
 }