public void RandomUtilTest() { var randoms1 = RandomUtil.GetRandomList(1, 10); var randoms2 = RandomUtil.GetRandomList(1, 10); var str1 = string.Join(",", randoms1); var str2 = string.Join(",", randoms2); Assert.AreNotEqual(str1, str2); }
public void Run() { var pkService = new PKService(); var pks = pkService.GetNotCalculatePKs(); foreach (var pk in pks) { if (pk != null && !pk.IsRanked) { try { var now = DateTime.Now; if (now >= pk.BeginTime.AddSeconds(pk.OpeningSeconds))// 封盘 { var desks = new List <PKRoomDesk>(); // 计算名次 foreach (var room in pk.PKRooms) { foreach (var desk in room.PKRoomDesks) { var ranks = RandomUtil.GetRandomList(1, 10); desk.Ranks = string.Join(",", ranks); } desks.AddRange(room.PKRoomDesks); } // 保存名次 pkService.UpdateRanks(desks); // 更新 名次生成标志 pkService.UpdateIsRanked(pk.PKId, true); var msg = string.Format("[PkGameRankJob] Calculate Ranks - PKId : {0} - Time : {1}", pk.PKId, now.ToString(DateFormatConst.yMd_Hms)); _logger.Info(msg); } } catch (Exception ex) { var msg = string.Format("[PkGameRankJob] Calculate Ranks Failed - PKId : {0} - Time : {1}", pk.PKId, DateTime.Now.ToString(DateFormatConst.yMd_Hms)); _logger.Info(msg); _logger.Info(ex); } } } }
/// <summary> /// 计算名次车号 /// 既第一名是几号车, 第二名是几号车... /// </summary> /// <param name="pkId">期号</param> public List <int> CalculateRanks(int pkId) { // 下注金额按(名次+车号)求和 var betAmounts = GetBetAmounts(pkId); // 没有人下注, 返回随机名次 if (betAmounts.Sum(a => a.Amount) == 0) { var ranks = RandomUtil.GetRandomList(1, 10); return(ranks); } // 计算下注百分比 var betRates = CalculateBetRates(betAmounts); // 奖池百分比 转换成 矩阵, 用于计算最小中奖名次 [TODO]大于1必不中 var matrix = GetMatrix(betRates); //// 名次下注百分比 //var rankRates = GetRankRates(betRates); /////////////////////test////////////////////////////// //var matrixStr = GetMatrixStr(matrix); ////////////////////test/////////////////////////////// // 计算最小中奖名次 var minCostMatrix = new HungarianAlgorithm(matrix).Run(); // 验证最小中奖名次的 [奖池百分比] 之和 是否小于 100%, 小于则返回, 否则返回null if (IsValidRanks(matrix, minCostMatrix)) { // 计算名次:比赛结果:车号顺序 var ranks = GetRanks(minCostMatrix, betRates); return(ranks); } else { return(null); } }