示例#1
0
 public void InsertOrUpdateBlock(Data.BlockData block)
 {
     lock (BlockInsertOrUpdateQueue)
     {
         BlockInsertOrUpdateQueue.Enqueue(block);
         TerrainCommunication.SendBlockUpdate(block);
     }
 }
        public static bool Insert(Data.BlockData data)
        {
            if (data == null || data.DataBaseID > -1)
            {
                return(false);
            }

            var chunk = GameApplication.Current.Terrain.ChunkByMemoryID(data.ChunkMemoryID);

            if (chunk == null || chunk.DataBaseID <= -1)
            {
                return(false);
            }

            SqlConnection conn = GameApplication.Current.ConnectionManager.OpenConnection();

            try
            {
                SqlCommand comm = new SqlCommand(@"INSERT INTO dbo.BlockData(ChunkID,X,Y,Z,BlockID,IsoValue,IsPathBlocked,IsDestroyed) OUTPUT INSERTED.ID
                VALUES(@ChunkID, @X,@Y,@Z,@BlockID,@IsoValue,@IsPathBlocked,@IsDestroyed)", conn);
                comm.Parameters.AddWithValue("ChunkID", chunk.DataBaseID);
                comm.Parameters.AddWithValue("Y", data.Position.y);
                comm.Parameters.AddWithValue("X", data.Position.x);
                comm.Parameters.AddWithValue("Z", data.Position.z);
                comm.Parameters.AddWithValue("BlockID", data.Block.ID);
                comm.Parameters.AddWithValue("IsoValue", data.Isovalue);
                comm.Parameters.AddWithValue("IsPathBlocked", data.IsPathBlocked);
                comm.Parameters.AddWithValue("IsDestroyed", data.IsDestroyed);

                long ID = (long)comm.ExecuteScalar();
                lock (data)
                {
                    data.DataBaseID = ID;
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                GameApplication.Current.ConnectionManager.CloseConnection(conn);
            }
        }
示例#3
0
        private static bool LoadAllChunkBlocks(out string error)
        {
            error = null;
            SqlConnection conn = GameApplication.Current.ConnectionManager.OpenConnection();

            try
            {
                SqlCommand comm = new SqlCommand("select * from dbo.BlockData", conn);
                //comm.Parameters.AddWithValue("ChunkID", chunkDataBaseID);
                comm.CommandTimeout = 10000;
                using (var reader = comm.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        long  ID            = (long)reader["ID"];
                        long  ChunkID       = (long)reader["ChunkiD"];
                        byte  X             = (byte)reader["X"];
                        byte  Y             = (byte)reader["Y"];
                        byte  Z             = (byte)reader["Z"];
                        int   BlockID       = (int)reader["BlockID"];
                        float isovalue      = Convert.ToSingle(reader["IsoValue"]);
                        bool  IsDestroyed   = (bool)reader["IsDestroyed"];
                        bool  IsPathBlocked = (bool)reader["IsPathBlocked"];
                        var   chunkData     = GameApplication.Current.Terrain.ChunkByDataBaseID(ChunkID);
                        if (chunkData == null)
                        {
                            error = string.Format("Invalid ChunkData DataBaseID [DataBaseID:{0}]", ChunkID);
                            return(false);
                        }
                        var blockData = new Data.BlockData(chunkData.MemoryID, ID, GameApplication.Current.Terrain.BlockSet.Blocks[BlockID], new SandBoxEngine.Terrain.Utils.Vector3i(X, Y, Z), isovalue, IsPathBlocked, IsDestroyed);
                        chunkData.Blocks[X][Y][Z] = blockData;
                    }
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                GameApplication.Current.ConnectionManager.CloseConnection(conn);
            }
        }
        public static bool Update(Data.BlockData data)
        {
            if (data == null || data.DataBaseID <= -1)
            {
                return(false);
            }

            SqlConnection conn = GameApplication.Current.ConnectionManager.OpenConnection();

            try
            {
                SqlCommand comm = new SqlCommand(@"UPDATE dbo.BlockData
                                                   SET Y = @Y,
                                                       X = @X,
                                                       Z = @Z,
                                                       BlockID = @BlockID,
                                                       IsoValue = @IsoValue,
                                                       IsPathBlocked = @IsPathBlocked,
                                                       IsDestroyed = @IsDestroyed
                                                    WHERE ID = @ID", conn);
                comm.Parameters.AddWithValue("ID", data.DataBaseID);
                comm.Parameters.AddWithValue("Y", data.Position.y);
                comm.Parameters.AddWithValue("X", data.Position.x);
                comm.Parameters.AddWithValue("Z", data.Position.z);
                comm.Parameters.AddWithValue("BlockID", data.Block.ID);
                comm.Parameters.AddWithValue("IsoValue", data.Isovalue);
                comm.Parameters.AddWithValue("IsPathBlocked", data.IsPathBlocked);
                comm.Parameters.AddWithValue("IsDestroyed", data.IsDestroyed);

                int affected = comm.ExecuteNonQuery();

                return(affected > 0);
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                GameApplication.Current.ConnectionManager.CloseConnection(conn);
            }
        }
示例#5
0
        public static Data.ChunkData Load(long DataBaseID, out string error)
        {
            error = null;
            SqlConnection conn = GameApplication.Current.ConnectionManager.OpenConnection();

            try
            {
                if (DataBaseID <= -1)
                {
                    error = string.Format("Invalid chunk database id [DataBaseID: {0}]", DataBaseID);
                    return(null);
                }
                StringBuilder query = new StringBuilder();
                query.AppendLine("select * from dbo.Chunk where ID = @chunkID");
                query.AppendLine(@"select * from dbo.BlockData where ChunkID = @chunkID");

                SqlCommand comm = new SqlCommand(query.ToString(), conn);
                comm.Parameters.AddWithValue("chunkID", DataBaseID);

                using (var reader = comm.ExecuteReader())
                {
                    if (!reader.Read())
                    {
                        error = string.Format("Invalid chunk not found [DataBaseID: {0}]", DataBaseID);
                        return(null);
                    }
                    int x     = (int)reader["X"];
                    int y     = (int)reader["Y"];
                    int z     = (int)reader["Z"];
                    int MapID = (int)reader["MapID"];


                    Data.ChunkData chunkData = new Data.ChunkData(MapID, DataBaseID, new SandBoxEngine.Terrain.Utils.Vector3i(x, y, z));
                    long           MemoryID  = GameApplication.Current.Terrain.AddChunk(chunkData);
                    lock (chunkData)
                    {
                        if (MemoryID == -1)
                        {
                            error = string.Format("Failed to add chunk to memory [DataBaseID: {0}]", DataBaseID);
                            return(null);
                        }

                        if (reader.NextResult())
                        {
                            while (reader.Read())
                            {
                                long  ID            = (long)reader["ID"];
                                long  ChunkID       = (long)reader["ChunkiD"];
                                byte  X             = (byte)reader["X"];
                                byte  Y             = (byte)reader["Y"];
                                byte  Z             = (byte)reader["Z"];
                                int   BlockID       = (int)reader["BlockID"];
                                float isovalue      = Convert.ToSingle(reader["IsoValue"]);
                                bool  IsDestroyed   = (bool)reader["IsDestroyed"];
                                bool  IsPathBlocked = (bool)reader["IsPathBlocked"];

                                var blockData = new Data.BlockData(MemoryID, ID, GameApplication.Current.Terrain.BlockSet.Blocks[BlockID], new SandBoxEngine.Terrain.Utils.Vector3i(X, Y, Z), isovalue, IsPathBlocked, IsDestroyed);
                                chunkData.Blocks[X][Y][Z] = blockData;
                            }
                        }
                    }
                    return(chunkData);
                }
            }
            catch (Exception ex)
            {
                error = ex.ToString();
                return(null);
            }
            finally
            {
                GameApplication.Current.ConnectionManager.CloseConnection(conn);
            }
        }
示例#6
0
        private bool DequeueBlock(out string error)
        {
            error = null;
            try
            {
                Data.BlockData block = null;
                lock (BlockInsertOrUpdateQueue)
                {
                    try
                    {
                        block = BlockInsertOrUpdateQueue.Dequeue();
                    }
                    //empty queue
                    catch (InvalidOperationException) { }
                }
                if (block != null)
                {
                    lock (block)
                    {
                        bool           enqueueAgain;
                        Data.ChunkData chunk = ChunkByMemoryID(block.ChunkMemoryID);
                        bool           chunkSaved;
                        lock (chunk)
                        {
                            chunkSaved = chunk.DataBaseID > -1;
                        }
                        //verifica se o chunk foi salvo no banco
                        if (chunkSaved)
                        {
                            if (block.DataBaseID <= -1)
                            {
                                enqueueAgain = (!DAO.Terrain.BlockData.Insert(block) && block.DataBaseID <= -1);
                            }
                            else
                            {
                                enqueueAgain = (!DAO.Terrain.BlockData.Update(block));
                            }
                        }
                        else// aguarda na fila ate que seu chunk esteja salvo no banco de dados
                        {
                            enqueueAgain = true;
                        }

                        if (enqueueAgain)
                        {
                            lock (BlockInsertOrUpdateQueue)
                            {
                                //try again later
                                BlockInsertOrUpdateQueue.Enqueue(block);
                            }
                        }
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                error = ex.ToString();
                return(false);
            }
        }