Exemplo n.º 1
0
        public Task <bool> InsertAsync(PlayerShopItem shopItem, ITransaction transaction = null)
        {
            var trans = transaction ?? _db.CreateTransaction();

            var buff = new byte[18];

            Buffer.BlockCopy(BitConverter.GetBytes(shopItem.Id), 0, buff, 0, 4);
            Buffer.BlockCopy(BitConverter.GetBytes(shopItem.ItemId), 0, buff, 4, 2);
            Buffer.BlockCopy(BitConverter.GetBytes(shopItem.Price), 0, buff, 6, 4);
            Buffer.BlockCopy(BitConverter.GetBytes(shopItem.InsertTime), 0, buff, 10, 4);
            Buffer.BlockCopy(BitConverter.GetBytes(shopItem.AccountId), 0, buff, 14, 4);

            trans.AddCondition(Condition.HashNotExists(_key, shopItem.Id));
            var task = trans.HashSetAsync(_key, shopItem.Id, buff)
                       .ContinueWith(t => Item2List(shopItem, t, true));

            task.ContinueWith(e =>
                              Log.Error(e.Exception.InnerException.ToString()),
                              TaskContinuationOptions.OnlyOnFaulted);

            if (transaction == null)
            {
                trans.ExecuteAsync();
            }

            return(task);
        }
Exemplo n.º 2
0
        public Task <bool> RemoveAsync(PlayerShopItem shopItem, ITransaction transaction = null)
        {
            var trans = transaction ?? _db.CreateTransaction();

            trans.AddCondition(Condition.HashExists(_key, shopItem.Id));
            var task = trans.HashDeleteAsync(_key, shopItem.Id)
                       .ContinueWith(t => Item2List(shopItem, t, false));

            task.ContinueWith(e =>
                              Log.Error(e.Exception.InnerException.ToString()),
                              TaskContinuationOptions.OnlyOnFaulted);

            if (transaction == null)
            {
                trans.ExecuteAsync();
            }

            return(task);
        }
Exemplo n.º 3
0
        private bool Item2List(PlayerShopItem shopItem, Task <bool> t, bool add)
        {
            var success = !t.IsCanceled && t.Result;

            if (success)
            {
                using (TimedLock.Lock(ListLock))
                {
                    if (add)
                    {
                        _entries.Add(shopItem);
                    }
                    else
                    {
                        _entries.Remove(shopItem);
                    }
                }
            }

            return(success);
        }