Beispiel #1
0
        /// <summary>
        /// 开奖
        /// </summary>
        public void OpenGamble()
        {
            try
            {
                #region deal case
                //获取已经到了开奖时间,还没有开奖的主题
                List <GambleTitleEntity> titleList = GambleTitleMgr.GetNeedOpenGambleTitles();
                if (titleList == null)
                {
                    return;
                }
                if (titleList.Count == 0)
                {
                    return;
                }

                for (int titleIndex = 0, titleCount = titleList.Count; titleIndex < titleCount; titleIndex++)
                {
                    //如果还没有在后台设置最终哪个选项是获胜选项,就报错
                    if (titleList[titleIndex].ResultFlagId == Guid.Empty)
                    {
                        //SystemlogMgr.Error("Gamble.OpenGamble", "titleList[titleIndex].ResultFlagId == 0");
                        continue;
                    }

                    //List<GambleOptionEntity> optionList = GambleOptionMgr.GetByTitleId(titleList[titleIndex].Idx);
                    //if (optionList == null || optionList.Count == 0)
                    //{
                    //    SystemlogMgr.Error("Gamble.OpenGamble", "optionList == null || optionList.Count == 0");
                    //    continue;
                    //}
                    List <GambleHostEntity> hostList = GambleHostMgr.GetByTitleId(titleList[titleIndex].Idx);
                    if (hostList == null || hostList.Count == 0)
                    {
                        //更新状态为已开奖
                        titleList[titleIndex].Status = 2;
                        if (!GambleTitleMgr.Update(titleList[titleIndex]))
                        {
                            string msg = "GambleTitleId:" + titleList[titleIndex].Idx.ToString() + "Update status to 2 error!";
                            SystemlogMgr.Error("Gamble.OpenGamble", msg);
                        }
                        continue;
                    }

                    for (int hostIndex = 0, hostCount = hostList.Count; hostIndex < hostCount; hostIndex++)
                    {
                        List <GambleHostoptionrateEntity> rateList =
                            GambleHostoptionrateMgr.GetByHostId(hostList[hostIndex].Idx);
                        if (rateList == null)
                        {
                            continue;
                        }
                        if (rateList.Count == 0)
                        {
                            continue;
                        }
                        decimal winRate          = 0.00m;
                        int     gambleTotalMoney = 0;
                        for (int rateIndex = 0, rateCount = rateList.Count; rateIndex < rateCount; rateIndex++)
                        {
                            List <GambleDetailEntity> detailList = GambleDetailMgr.GetByOptionId(rateList[rateIndex].Idx);
                            if (detailList == null || detailList.Count == 0)
                            {
                                continue;
                            }
                            if (titleList[titleIndex].ResultFlagId == Guid.Empty)
                            {
                                SystemlogMgr.Error("Gamble.OpenGamble", "ResultFlagId is 0");
                                continue;
                            }
                            GambleOptionEntity optionRight = GambleOptionMgr.GetById(titleList[titleIndex].ResultFlagId);
                            #region 结算玩家的竞猜点券
                            //猜中的玩家
                            if (rateList[rateIndex].OptionId == titleList[titleIndex].ResultFlagId)
                            {
                                winRate          = rateList[rateIndex].WinRate;
                                gambleTotalMoney = rateList[rateIndex].GambleMoney;
                                //按照赔率进行结算,发送邮件
                                for (int detailIndex = 0, detailCount = detailList.Count; detailIndex < detailCount; detailIndex++)
                                {
                                    //该玩家的竞猜已经开过奖了
                                    if (detailList[detailIndex].Status != 0)
                                    {
                                        continue;
                                    }

                                    //给玩家结算奖金
                                    int returnMoney = Convert.ToInt32(
                                        (decimal)detailList[detailIndex].GambleMoney *
                                        rateList[rateIndex].WinRate * 0.95m);
                                    //更新状态为猜中
                                    detailList[detailIndex].Status      = 1;
                                    detailList[detailIndex].ResultMoney = returnMoney;
                                    if (!GambleDetailMgr.Update(detailList[detailIndex]))
                                    {
                                        continue;
                                    }
                                    //string mailContent = "恭喜你,在参与"+titleList[titleIndex].Title +
                                    //    "的竞猜中,成功猜中" + optionRight.OptionContent + ",获得奖励"
                                    //    + returnMoney +"点券";
                                    MailBuilder mailGambler = new MailBuilder(EnumMailType.GambleReturnToGambler,
                                                                              detailList[detailIndex].ManagerId, titleList[titleIndex].Title, optionRight.OptionContent, EnumCurrencyType.Point, returnMoney);
                                    mailGambler.Save();
                                    //更新竞猜排行榜数据
                                    int subMoney = returnMoney - detailList[detailIndex].GambleMoney;
                                    GambleRankMgr.UpdateData(detailList[detailIndex].ManagerId, detailList[detailIndex].ManagerName, subMoney);
                                }
                            }
                            //没猜中
                            else
                            {
                                for (int detailIndex = 0, detailCount = detailList.Count; detailIndex < detailCount; detailIndex++)
                                {
                                    //更新状态为未猜中
                                    detailList[detailIndex].Status = 2;
                                    GambleDetailMgr.Update(detailList[detailIndex]);
                                    //更新竞猜排行榜数据

                                    GambleRankMgr.UpdateData(detailList[detailIndex].ManagerId, detailList[detailIndex].ManagerName, 0 - detailList[detailIndex].GambleMoney);
                                }
                            }
                            #endregion
                        }

                        #region 结算庄家盈亏
                        if (hostList[hostIndex].ManagerId != LeagueConst.GambleNpcId)
                        {
                            int hostReturnMoney = Convert.ToInt32(winRate * (decimal)gambleTotalMoney);
                            int hostGambleMoney = hostList[hostIndex].TotalMoney - hostReturnMoney;
                            int tax             = 0;
                            int subMoney        = hostGambleMoney - hostList[hostIndex].HostMoney;
                            if (hostGambleMoney > hostList[hostIndex].HostMoney)
                            {
                                tax       = Convert.ToInt32((decimal)subMoney * 0.05m);
                                subMoney -= tax;
                            }
                            int hostMoney = hostGambleMoney - tax;
                            hostList[hostIndex].HostWinMoney = subMoney;
                            //string mailToHost = "你发起的竞猜"+ titleList[titleIndex].Title+"已完成奖励派发,奖池还剩余"+hostMoney
                            //    +"点券,本次竞猜你共盈利"+subMoney+"点券";
                            MailBuilder mailHost = new MailBuilder(EnumMailType.GambleReturnToHost,
                                                                   hostList[hostIndex].ManagerId, titleList[titleIndex].Title,
                                                                   EnumCurrencyType.Point, hostMoney, subMoney);
                            mailHost.Save();
                            //更新竞猜排行榜数据

                            string hostName = ManagerCore.Instance.GetManager(hostList[hostIndex].ManagerId).Name;
                            GambleRankMgr.UpdateData(hostList[hostIndex].ManagerId, hostName, subMoney);
                        }
                        #endregion
                        #region 更新Host状态为已开奖
                        hostList[hostIndex].Status = 2;
                        GambleHostMgr.Update(hostList[hostIndex]);
                        #endregion
                    }

                    //更新状态为已开奖
                    titleList[titleIndex].Status = 2;
                    if (!GambleTitleMgr.Update(titleList[titleIndex]))
                    {
                        string msg = "GambleTitleId:" + titleList[titleIndex].Idx.ToString() + "Update status to 2 error!";
                        SystemlogMgr.Error("Gamble.OpenGamble", msg);
                    }
                }
                #endregion

                #region 更新排行榜
                GambleRankMgr.UpdateRank();
                #endregion
            }
            catch (Exception ex)
            {
                SystemlogMgr.Error("Gamble.OpenGamble", ex);
            }
        }