Exemplo n.º 1
0
 /// <summary>
 /// 添加词汇进入消息队列
 /// </summary>
 /// <param name="keyword"></param>
 private static void AddQueue(string keyword)
 {
     if (keywordQueue.Contains(keyword))
     {
         return;
     }
     lock (LockHelper.GetLoker(Const.LANGUAGE))
     {
         Console.WriteLine("加入词汇:{0}", keyword);
         keywordQueue.Push(keyword);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// 获取缓存值(如果不存在则创建)
 /// </summary>
 /// <typeparam name="TValue"></typeparam>
 /// <param name="key"></param>
 /// <param name="timeSpan">过期时间</param>
 /// <param name="createValue"></param>
 /// <param name="isSleep">重新创建缓存之后是否暂停进程</param>
 /// <returns></returns>
 public static TValue Get <TValue>(string key, TimeSpan timeSpan, Func <TValue> createValue)
 {
     if (_cache.TryGetValue(key, out TValue result))
     {
         return(result);
     }
     lock (LockHelper.GetLoker(key))
     {
         if (_cache.TryGetValue(key, out result))
         {
             return(result);
         }
         result = createValue();
         _cache.Set(key, result, timeSpan);
         return(result);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// 查询转账信息
        /// </summary>
        /// <param name="site">商户</param>
        /// <param name="game">游戏配置</param>
        /// <param name="orderId">商户的转账订单号</param>
        /// <returns></returns>
        public QueryTransferResult QueryTransfer(Site site, GameSetting game, string orderId)
        {
            // 加锁,不允许对同一个订单号进行并发查询
            lock (LockHelper.GetLoker($"{game.ID}-{orderId}"))
            {
                UserTransfer order = TransferAgent.Instance().GetUserTransfer(site.ID, game.ID, orderId);
                if (order == null)
                {
                    return(new QueryTransferResult(ResultStatus.NoOrder));
                }
                if (order.Status == TransferStatus.Paying)
                {
                    return(new QueryTransferResult(ResultStatus.OrderPaying));
                }

                if (order.Status == TransferStatus.Exception)
                {
                    UserGame user = UserAgent.Instance().GetUserGameInfo(order.UserID, game.ID);
                    // 调用API接口
                    ResultStatus status = game.Setting.QueryTransfer(new QueryTransferInfo()
                    {
                        UserName = user.Account,
                        OrderID  = orderId,
                        Currency = site.Currency
                    });
                    if (status == ResultStatus.Exception)
                    {
                        return(new QueryTransferResult(ResultStatus.Exception));
                    }

                    order.Status = status == ResultStatus.Success ? TransferStatus.Success : TransferStatus.Faild;
                    this.WriteDB.Update(order, t => t.Status);
                }

                if (order.Status == TransferStatus.Success)
                {
                    return(new QueryTransferResult(order.Money, order.CreateAt, UserAgent.Instance().GetUserName(order.UserID), order.Action, site.Currency));
                }

                return(new QueryTransferResult(ResultStatus.OrderFaild));
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// 获取实体类下所有的数据库字段
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 internal static List <ColumnProperty> GetColumns(Type type)
 {
     if (columnCache.ContainsKey(type))
     {
         return(columnCache[type]);
     }
     lock (LockHelper.GetLoker(type.Name))
     {
         List <ColumnProperty> columns = new List <ColumnProperty>();
         foreach (PropertyInfo property in type.GetProperties().Where(t => t.HasAttribute <ColumnAttribute>()))
         {
             columns.Add(new ColumnProperty(property));
         }
         if (!columnCache.ContainsKey(type))
         {
             columnCache.Add(type, columns);
         }
     }
     return(columnCache[type]);
 }
Exemplo n.º 5
0
        /// <summary>
        /// 转账
        /// </summary>
        /// <param name="siteId">所属商户</param>
        /// <param name="game">当前游戏配置</param>
        /// <param name="user">用户信息</param>
        /// <param name="action">转入转出类型</param>
        /// <param name="orderID">转账订单号(本地)</param>
        /// <param name="currency">货币类型</param>
        /// <param name="money">金额</param>
        /// <returns></returns>
        public TransferResult Transfer(Site site, GameSetting game, UserGame user, TransferAction action, string orderID, decimal money)
        {
            if (user == null)
            {
                return(new TransferResult(ResultStatus.NoUser));
            }
            // 订单格式的判断
            if (WebAgent.IsUserName(orderID, 2, 16))
            {
                return(new TransferResult(ResultStatus.OrderIDFormat));
            }

            // 金额判断(默认是2位小数,如果游戏接口的特别要求,则在游戏接口中返回金额错误)
            if (money <= 0M || Math.Round(money, 2) != money)
            {
                return(new TransferResult(ResultStatus.BadMoney));
            }

            // 本地锁(如果部署集群则需要修改成为分布式锁)
            lock (LockHelper.GetLoker($"{user.ToString()}"))
            {
                //同一个商户订单重复,不允许操作
                if (this.ReadDB.Exists <UserTransfer>(t => t.SiteID == site.ID && t.SourceID == orderID))
                {
                    return(new TransferResult(ResultStatus.ExistsOrder));
                }

                //添加转账记录,把状态设置为转账中
                UserTransfer userTransfer = new UserTransfer()
                {
                    SiteID   = site.ID,
                    GameID   = game.ID,
                    UserID   = user.UserID,
                    Money    = money,
                    Action   = action,
                    CreateAt = DateTime.Now,
                    FinishAt = DateTime.MinValue,
                    SystemID = string.Empty,
                    SourceID = orderID,
                    Status   = TransferStatus.Paying
                };
                this.WriteDB.InsertIdentity(userTransfer);

                // 调用API接口
                TransferResult result = game.Setting.Transfer(new TransferInfo()
                {
                    Prefix   = site.Prefix,
                    UserName = user.Account,
                    Action   = action,
                    OrderID  = orderID,
                    Currency = site.Currency,
                    Money    = money
                });

                userTransfer.SystemID = result.SystemID;
                userTransfer.FinishAt = DateTime.Now;
                userTransfer.Status   = result.Status switch
                {
                    ResultStatus.Exception => TransferStatus.Exception,
                    ResultStatus.Success => TransferStatus.Success,
                    _ => TransferStatus.Faild
                };
                this.WriteDB.Update(userTransfer, t => t.SystemID, t => t.FinishAt, t => t.Status);

                if (!result)
                {
                    return(new TransferResult(result.Status));
                }

                if (result.Balance != null)
                {
                    UserAgent.Instance().UpdateBalance(user, result.Balance.Value);
                }
                else
                {
                    BalanceResult balanceResult = this.GetBalance(site, game, user);
                    if (balanceResult)
                    {
                        result.Balance = balanceResult.Balance;
                    }
                }
                return(result);
            }
        }