private static bool UpdateUserPoints(int userID, int[] points, int count, TryUpdateUserPointCallback beforeUpdate, PointActionItemAttribute attribute, string operateName, string remarks) { bool throwOverMaxValue = false, throwOverMinValue = false; if (attribute == null) { if (beforeUpdate != null) { beforeUpdate(TryUpdateUserPointState.CheckFailed); } return(false); } throwOverMaxValue = attribute.ThrowOverMaxValue; throwOverMinValue = attribute.ThrowOverMinValue; return(UserBO.Instance.UpdateUserPoints(userID, throwOverMaxValue, throwOverMinValue, points, count, beforeUpdate, operateName, remarks)); }
//public static bool UpdateUsersPoint<T1, T2>(Dictionary<int,Dictionary<PointActionType,T1> actions) //{ //} /* * public static bool UpdateUsersPointValue<T>(Dictionary<int, int> userPoints, string type, T actionType, TryUpdateUserPointCallback beforeUpdate, TryUpdateUserPointCallback2 beforeUpdate2) where T : struct * { * using (BbsContext context = new BbsContext()) * { * context.BeginTransaction(); * try * { * * bool updatePoint = true; * if (beforeUpdate2 != null) * { * updatePoint = beforeUpdate2(TryUpdateUserPointState.CheckSucceed, out userPoints); * } * else if (beforeUpdate != null) * updatePoint = beforeUpdate(TryUpdateUserPointState.CheckSucceed); * * if (updatePoint) * { * bool success = true; * foreach (KeyValuePair<int, int> pair in userPoints) * { * success = UpdateUserPointValue<T>(pair.Key, type, actionType, pair.Value, null); * if (!success) * break; * } * if (success) * { * context.CommitTransaction(); * return true; * } * } * context.RollbackTransaction(); * return false; * } * catch (Exception ex) * { * context.RollbackTransaction(); * throw ex; * } * } * return true; * } * */ public static bool UpdateUserPointValue <T>(int userId, string type, T actionType, int nodeID, int value, TryUpdateUserPointCallback beforeUpdate) where T : struct { PointAction pointAction; UserPoint userPoint = GetUserPoint <T>(userId, type, actionType, nodeID, out pointAction); int?minRemaining, maxValue; int minValue; pointAction.GetActionPointValueSetting(actionType.ToString(), userId, out minRemaining, out minValue, out maxValue); int absValue = Math.Abs(value); if (absValue < minValue)//小于最低值 { Context.ThrowError <UserPointTradeMinValueError>(new UserPointTradeMinValueError("UserPointTradeMinValueError", userPoint.Name, absValue, minValue)); if (beforeUpdate != null) { beforeUpdate(TryUpdateUserPointState.CheckFailed); } return(false); } if (maxValue != null && absValue > maxValue.Value)//大于最高值 { Context.ThrowError <UserPointTradeMaxValueError>(new UserPointTradeMaxValueError("UserPointTradeMaxValueError", userPoint.Name, absValue, maxValue.Value)); if (beforeUpdate != null) { beforeUpdate(TryUpdateUserPointState.CheckFailed); } return(false); } PointActionItemAttribute attribute = GetPointActionItemAttribute(type, actionType, true); if (attribute == null) { if (beforeUpdate != null) { beforeUpdate(TryUpdateUserPointState.CheckFailed); } return(false); } if (attribute.IgnoreTax == false) { value = GetPointValue(value); } User user = UserBO.Instance.GetUser(userId); int[] points = new int[Consts.PointCount]; points[(int)userPoint.Type] = value; PointActionItemAttribute tempAttribute; if (minRemaining != null && minRemaining.Value > userPoint.MinValue) { int[] minValues = new int[8]; minValues[(int)userPoint.Type] = minRemaining.Value; lock (user.UpdateUserPointLocker) { int point; int result = UserBO.Instance.CheckUserPoint(userId, true, false, points, minValues, null, out point); if (result != 0) { int remaning = point + value; Context.ThrowError <UserPointTradeRemainingError>(new UserPointTradeRemainingError("UserPointTradeRemainingError", userPoint.Name, remaning, minRemaining.Value)); if (beforeUpdate != null) { beforeUpdate(TryUpdateUserPointState.CheckFailed); } return(false); } } if (beforeUpdate != null) { bool success = beforeUpdate(TryUpdateUserPointState.CheckSucceed); if (success == false) { return(false); } } beforeUpdate = null; //由于上面已经检查过积分 所以下面不再检查 tempAttribute = new PointActionItemAttribute(attribute.ActionName, false, false, attribute.IgnoreTax, attribute.IsShowInList); } else { tempAttribute = attribute; } /* * lock (user.UpdateUserPointLocker) * { * if (minRemaining != null && minRemaining.Value > userPoint.MinValue) * { * int remaning = user.ExtendedPoints[(int)userPoint.Type] + value; * if (remaning < minRemaining.Value)//交易后小于最低余额 * { * Context.ThrowError<UserPointTradeRemainingError>(new UserPointTradeRemainingError("UserPointTradeRemainingError", userPoint.Name, remaning, minRemaining.Value)); * if (beforeUpdate != null) * { * beforeUpdate(TryUpdateUserPointState.CheckFailed); * } * return false; * } * } * } */ return(UpdateUserPoints(userId, points, 1, beforeUpdate, tempAttribute, tempAttribute.ActionName, tempAttribute.ActionName)); }
private static bool UpdateUserPoints <T>(string type, T actionType, bool isNeedValue, int userID, int[] points, int count, TryUpdateUserPointCallback beforeUpdate) where T : struct { PointActionItemAttribute attribute = GetPointActionItemAttribute(type, actionType, isNeedValue); return(UpdateUserPoints(userID, points, count, beforeUpdate, attribute, attribute.ActionName, attribute.ActionName)); }
/// <summary> /// 针对不同的用户 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="userActions">key:UserID,Value:actionType</param> /// <param name="type"></param> /// <param name="count"></param> /// <param name="nodeID"></param> /// <param name="beforeUpdate"></param> /// <returns></returns> public static bool UpdateUsersPoint <T>(Dictionary <int, T> userActions, string type, int count, int nodeID, TryUpdateUserPointCallback beforeUpdate) where T : struct { bool success = false; Dictionary <int, int[]> allPoints = new Dictionary <int, int[]>(); foreach (KeyValuePair <int, T> pair in userActions) { if (pair.Key == 0) { continue; } int[] points; success = CheckPoints <T>(pair.Key, type, pair.Value, count, true, nodeID, out points); if (success == false) { beforeUpdate(TryUpdateUserPointState.CheckFailed); return(false); } if (points != null) { allPoints.Add(pair.Key, points); } //UpdateUserPoint<T>(pair.Key, type, pair.Value, 1, true, nodeID, null); } success = beforeUpdate(TryUpdateUserPointState.CheckSucceed); if (success == false) { return(false); } foreach (KeyValuePair <int, int[]> pair in allPoints) { string action = userActions[pair.Key].ToString(); string name = GetActionName(type, action); UserBO.Instance.UpdateUserPoint(pair.Key, false, false, pair.Value, name, name); } return(true); /* * using (BbsContext context = new BbsContext()) * { * context.BeginTransaction(IsolationLevel.ReadUncommitted); * try * { * bool success = beforeUpdate(TryUpdateUserPointState.CheckSucceed); * if (success == false) * { * context.RollbackTransaction(); * return false; * } * * List<int> userIDs = new List<int>(); * foreach (KeyValuePair<int, T> pair in userActions) * { * userIDs.Add(pair.Key); * } * //先缓存一下用户 * UserBO.Instance.GetUsers(userIDs); * * foreach (KeyValuePair<int, T> pair in userActions) * { * if (pair.Key == 0) * continue; * * UpdateUserPoint<T>(pair.Key, type, pair.Value, 1, true, nodeID, null); * * } * context.CommitTransaction(); * } * catch(Exception ex) * { * context.RollbackTransaction(); * throw ex; * } * } * return true; */ }
/// <summary> /// 对同一个用户的不同动作 操作积分 /// </summary> /// <typeparam name="T1">不需要传值的积分动作</typeparam> /// <typeparam name="T2">需要用户填值的积分动作</typeparam> /// <param name="userID">操作该用户的积分</param> /// <param name="noNeedValueActions">(key:动作类型 value:操作倍数) 如果没有用null</param> /// <param name="needValueActions">(key:动作类型 value:积分值) 需要用户填值的积分动作类型 如果没有用null</param> /// <param name="type"></param> /// <param name="nodeID"></param> /// <param name="beforeUpdate"></param> /// <returns></returns> public static bool UpdateUserPoints <T1, T2>(int userID, Dictionary <T1, int> noNeedValueActions, Dictionary <T2, int> needValueActions, string type, int nodeID, TryUpdateUserPointCallback beforeUpdate) where T1 : struct where T2 : struct { using (BbsContext context = new BbsContext()) { context.BeginTransaction(IsolationLevel.ReadUncommitted); try { bool success = beforeUpdate(TryUpdateUserPointState.CheckSucceed); if (success == false) { context.RollbackTransaction(); return(success); } if (noNeedValueActions != null) { foreach (KeyValuePair <T1, int> pair in noNeedValueActions) { success = UpdateUserPoint <T1>(userID, type, pair.Key, pair.Value, true, nodeID, null); if (success == false) { break; } } } if (success == false) { context.RollbackTransaction(); return(success); } if (needValueActions != null) { foreach (KeyValuePair <T2, int> pair in needValueActions) { success = UpdateUserPointValue <T2>(userID, type, pair.Key, nodeID, pair.Value, null); if (success == false) { break; } } } if (success == false) { context.RollbackTransaction(); return(success); } context.CommitTransaction(); } catch (Exception ex) { context.RollbackTransaction(); throw ex; } } return(true); }
public static bool UpdateUserPoint <T>(int userID, string type, T actionType, int count, bool isNormal, int nodeId, TryUpdateUserPointCallback beforeUpdate) where T : struct { if (userID == 0)//游客 不操作积分 { if (beforeUpdate != null) { beforeUpdate(TryUpdateUserPointState.CheckSucceed); } return(true); } int[] points = null; PointAction pointAction = AllSettings.Current.PointActionSettings.PointActions.GetPointAction(type, nodeId); //foreach (PointAction pointAction in AllSettings.Current.PointActionSettings.PointActions) //{ // if (string.Compare(pointAction.Type, type) == 0) // { // points = pointAction.GetPoints(actionType.ToString(), userID); // break; // } //} points = pointAction.GetPoints(actionType.ToString(), userID); if (points == null) { if (beforeUpdate != null) { beforeUpdate(TryUpdateUserPointState.CheckSucceed); } return(true); } if (isNormal == false)//取相反的值 { for (int i = 0; i < points.Length; i++) { points[i] = -points[i]; } } return(UpdateUserPoints <T>(type, actionType, false, userID, points, count, beforeUpdate)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="targetUserIds">key:用户ID;value:更新倍数</param> /// <param name="type"></param> /// <param name="actionType"></param> /// <param name="beforeUpdate"></param> /// <returns></returns> private static bool UpdateUserPoint <T>(Dictionary <int, int> targetUserIds, string type, T actionType, bool isNormal, int nodeId, TryUpdateUserPointCallback beforeUpdate, TryUpdateUserPointCallback2 beforeUpdate2) where T : struct { using (BbsContext context = new BbsContext()) { context.BeginTransaction(IsolationLevel.ReadUncommitted); try { bool updatePoint = true; if (beforeUpdate2 != null) { updatePoint = beforeUpdate2(TryUpdateUserPointState.CheckSucceed, out targetUserIds); } else if (beforeUpdate != null) { updatePoint = beforeUpdate(TryUpdateUserPointState.CheckSucceed); } if (updatePoint) { //List<int> userIDs = new List<int>(); //foreach (KeyValuePair<int, int> pair in targetUserIds) //{ // userIDs.Add(pair.Key); //} ////先缓存一下用户 //UserBO.Instance.GetUsers(userIDs); bool success = true; foreach (KeyValuePair <int, int> pair in targetUserIds) { //throw new Exception("test error"); success = UpdateUserPoint <T>(pair.Key, type, actionType, pair.Value, isNormal, nodeId, null); if (!success) { break; } } if (success) { context.CommitTransaction(); return(true); } } context.RollbackTransaction(); return(false); } catch (Exception ex) { context.RollbackTransaction(); throw ex; } } }
public static bool UpdateUserPoint <T>(Dictionary <int, int> targetUserIds, string type, T actionType, bool isNormal, int nodeID, TryUpdateUserPointCallback beforeUpdate) where T : struct { return(UpdateUserPoint <T>(targetUserIds, type, actionType, isNormal, nodeID, beforeUpdate, null)); }