Exemplo n.º 1
0
        private static int CapsuleMachineGetItem(Account User, int MachineItemNum, int MachineKindID, CapsuleMachineInfo infos, List <CapsuleMachineItem> CapsuleMachineItem, out byte ret)
        {
            int TotalCount = infos.TotalItemCount;
            //int itemnum = 0;
            CapsuleMachineItem ItemInfo = new CapsuleMachineItem();

            foreach (var item in CapsuleMachineItem.OrderBy(o => o.ItemMax))
            {
                Random rnd    = new Random(Guid.NewGuid().GetHashCode());
                int    rndnum = rnd.Next(TotalCount + 1);
                if (rndnum <= item.ItemMax)
                {
                    if (item.ItemCount <= 0)
                    {
                        ItemInfo = CapsuleMachineItem.Where(w => w.ItemCount > 0).OrderBy(_ => Guid.NewGuid()).FirstOrDefault();
                    }
                    else
                    {
                        ItemInfo = item;
                    }
                    //ItemInfo = item;
                    break;
                }
                else
                {
                    TotalCount -= item.ItemMax;
                }
            }

            //ItemInfo = CapsuleMachineItem.Where(w => w.Level == 1).FirstOrDefault();

            ret = 0;
            bool isReset       = false;
            int  resultItemNum = 0;
            int  level         = 0;

            using (var con = new MySqlConnection(Conf.Connstr))
            {
                con.Open();
                var cmd = new MySqlCommand(string.Empty, con);
                cmd.Parameters.Clear();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_capsuleMachineSelect";
                cmd.Parameters.Add("machineNum", MySqlDbType.Int32).Value    = infos.RealMachineNum;
                cmd.Parameters.Add("machineKind", MySqlDbType.Int32).Value   = infos.RealMachineNumKind;
                cmd.Parameters.Add("itemNum", MySqlDbType.Int32).Value       = ItemInfo.ItemNum;
                cmd.Parameters.Add("userNum", MySqlDbType.Int32).Value       = User.UserNum;
                cmd.Parameters.Add("selectTime", MySqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("resetCount", MySqlDbType.Int32).Value    = infos.ResetCount;
                MySqlDataReader reader = cmd.ExecuteReader();
                reader.Read();
                ret = (byte)reader.GetInt32("retval");
                if (ret == 0)
                {
                    resultItemNum = reader.GetInt32("resultItemNum");
                    level         = reader.GetInt32("level");
                    isReset       = reader.GetBoolean("isReset");
                    reader.GetInt32("cost");
                    reader.GetInt32("myAsset");
                    //reader.GetInt32("isTRItem");
                }
                else
                {
                    Console.WriteLine("RET: {0}", ret);
                }
                cmd.Dispose();
                reader.Close();
                con.Close();
            }
            if (ret == 0)
            {
                if (level < 3)
                {
                    Task.Run(() =>
                    {
                        foreach (var ac in ClientConnection.CurrentAccounts.Values)
                        {
                            ac.Connection.SendAsync(new CapsuleMachineNotice(ac, (byte)level, MachineItemNum, resultItemNum, User.NickName));
                        }
                    });
                }

                if (isReset)
                {
                    string MachineNum = CapsuleMachineHolder.UpdateCapsuleMachineInfo(MachineItemNum, infos.RealMachineNum, infos.isRotate);
                    if (infos.isRotate)
                    {
                        Task.Run(() =>
                        {
                            foreach (var ac in ClientConnection.CurrentAccounts.Values)
                            {
                                ac.Connection.SendAsync(new RoatateMachineNotice(ac, MachineNum));
                            }
                        });
                    }
                }
                else
                {
                    CapsuleMachineHolder.CapsuleMachineItems[MachineItemNum].Where(w => w.ItemNum == ItemInfo.ItemNum).ToList()
                    .ForEach(f => f.ItemCount -= 1);
                }
            }
            return(resultItemNum);
        }
Exemplo n.º 2
0
        private static int CapsuleMachineGetItem(Account User, int MachineItemNum, int MachineKindID, CapsuleMachineInfo infos, List <CapsuleMachineItem> CapsuleMachineItem, out byte ret)
        {
            int TotalCount = infos.TotalItemCount;
            //int itemnum = 0;
            CapsuleMachineItem ItemInfo = new CapsuleMachineItem();
            //ItemInfo = CapsuleMachineItem.Where(w => w.Level == 1).FirstOrDefault();

            double UserLucky = (double)User.Luck;
            double CapsuleMachineRateLuckyDiv = 200; //DB
            int    CapsuleMachineMaxDice      = 10;  //DB

            int ItemLevel       = 6;
            int ItemLevelBefore = 0;
            int DrawTime        = 1;

            DrawTime = (int)(UserLucky / CapsuleMachineRateLuckyDiv + 1.0);
            if (DrawTime > CapsuleMachineMaxDice)
            {
                DrawTime = CapsuleMachineMaxDice;
            }

            while (DrawTime > 0)
            {
drawtart:
                Random rnd = new Random(Guid.NewGuid().GetHashCode());
                int rndnum = rnd.Next(1, TotalCount + 1);
                int min = 1, max = 1;
                foreach (var item in CapsuleMachineItem.OrderBy(o => o.ItemMax))
                {
                    max += item.ItemMax;
                    if (min <= rndnum && rndnum < max)
                    {
                        if (item.ItemCount <= 0)
                        {
                            goto drawtart; //redraw
                        }
                        ItemLevelBefore = ItemLevel;
                        if (ItemLevelBefore >= item.Level)
                        {
                            ItemLevel = item.Level;
                            ItemInfo  = item;
                        }
                        break;
                    }
                    min = max;
                }

                DrawTime--;
            }

            ret = 0;
            bool isReset = false;
            int  resultItemNum = 0;
            int  level = 0;
            int  PriceType = 0, cost = 0;

            try
            {
                using (var con = new MySqlConnection(Conf.Connstr))
                {
                    con.Open();
                    var cmd = new MySqlCommand(string.Empty, con);
                    cmd.Parameters.Clear();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "usp_capsuleMachineSelect";
                    cmd.Parameters.Add("machineNum", MySqlDbType.Int32).Value    = infos.RealMachineNum;
                    cmd.Parameters.Add("machineKind", MySqlDbType.Int32).Value   = infos.RealMachineNumKind;
                    cmd.Parameters.Add("itemNum", MySqlDbType.Int32).Value       = ItemInfo.ItemNum;
                    cmd.Parameters.Add("userNum", MySqlDbType.Int32).Value       = User.UserNum;
                    cmd.Parameters.Add("selectTime", MySqlDbType.DateTime).Value = DateTime.Now;
                    cmd.Parameters.Add("resetCount", MySqlDbType.Int32).Value    = infos.ResetCount;
                    MySqlDataReader reader = cmd.ExecuteReader();
                    reader.Read();
                    ret = (byte)reader.GetInt32("retval");
                    if (ret == 0)
                    {
                        resultItemNum = reader.GetInt32("resultItemNum");
                        level         = reader.GetInt32("level");
                        isReset       = reader.GetBoolean("isReset");
                        cost          = reader.GetInt32("cost");
                        //myAsset = reader.GetInt32("myAsset");
                        PriceType = Convert.ToInt32(reader["PriceType"]);
                    }
                    else
                    {
                        Console.WriteLine("RET: {0}", ret);
                    }
                    cmd.Dispose();
                    reader.Close();
                    con.Close();
                }
                if (ret == 0)
                {
                    if (level < 3)
                    {
                        Task.Run(() =>
                        {
                            foreach (var ac in ClientConnection.CurrentAccounts.Values)
                            {
                                ac.Connection.SendAsync(new CapsuleMachineNotice(ac, (byte)level, MachineItemNum, resultItemNum, User.NickName));
                            }
                        });
                    }

                    if (isReset)
                    {
                        string MachineNum = CapsuleMachineHolder.UpdateCapsuleMachineInfo(MachineItemNum, infos.RealMachineNum, infos.isRotate);
                        if (infos.isRotate)
                        {
                            Task.Run(() =>
                            {
                                foreach (var ac in ClientConnection.CurrentAccounts.Values)
                                {
                                    ac.Connection.SendAsync(new RoatateMachineNotice(ac, MachineNum));
                                }
                            });
                        }
                    }
                    else
                    {
                        CapsuleMachineHolder.CapsuleMachineItems[MachineItemNum].Where(w => w.ItemNum == ItemInfo.ItemNum).ToList()
                        .ForEach(f => f.ItemCount -= 1);
                    }

                    if (PriceType == 0) //TR
                    {
                        User.TR -= cost;
                    }
                    else if (PriceType == 1) //點數
                    {
                        User.Cash -= cost;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error on capsule machine get item: {0}", ex.Message);
                ret = 1;
            }
            return(resultItemNum);
        }