Beispiel #1
0
        protected override void HandleRequest()
        {
            rand = Query["ignore"] != null ? new Random(int.Parse(Query["ignore"])) : new Random();

            if (Query["guid"] == null || Query["password"] == null)
            {
                WriteErrorLine("Error.incorrectEmailOrPassword");
            }
            else
            {
                DbAccount   acc;
                LoginStatus status = Database.Verify(Query["guid"], Query["password"], out acc);

                if (status == LoginStatus.OK)
                {
                    if (Query["boxId"] == null)
                    {
                        WriteLine("<Error>Box ID not declared.</Error>");
                        return;
                    }

                    SerializeMiniGames box = SerializeMiniGames.GetBox(int.Parse(Query["boxId"]), CONSTANTS.MYSTERY_BOX);

                    if (box == null)
                    {
                        WriteLine($"<Error>Box ID {Query["boxId"]} not found.</Error>");
                        return;
                    }

                    if (box.Sale != null && DateTime.UtcNow <= box.Sale.SaleEnd)
                    {
                        switch (box.Sale.Currency)
                        {
                        case CONSTANTS.GOLD:
                            if (acc.Credits < box.Sale.Price)
                            {
                                WriteLine("<Error>Not enough gold.</Error>");
                                return;
                            }
                            break;

                        case CONSTANTS.FAME:
                            if (acc.Fame < box.Sale.Price)
                            {
                                WriteLine("<Error>Not enough fame.</Error>");
                                return;
                            }
                            break;

                        default:
                        {
                            WriteLine($"<Error>Invalid currency type ID {box.Sale.Currency}.</Error>");
                        }
                            return;
                        }
                    }
                    else
                    {
                        switch (box.PriceMB.Currency)
                        {
                        case CONSTANTS.GOLD:
                            if (acc.Credits < box.PriceMB.Amount)
                            {
                                WriteLine("<Error>Not enough gold.</Error>");
                                return;
                            }
                            break;

                        case CONSTANTS.FAME:
                            if (acc.Fame < box.PriceMB.Amount)
                            {
                                WriteLine("<Error>Not enough fame.</Error>");
                                return;
                            }
                            break;

                        default:
                        {
                            WriteLine($"<Error>Invalid currency type ID {box.PriceMB.Currency}.</Error>");
                        }
                            return;
                        }
                    }

                    if (Database.CheckMysteryBox(acc, box.BoxId, box.Total))
                    {
                        WriteLine($"<Error>You can only purchase {box.Title} {box.Total} time{((box.Total <= 1) ? "" : "s")}.</Error>");
                        return;
                    }

                    if (box.Total != -1)
                    {
                        Database.AddMysteryBox(acc, box.BoxId);
                    }

                    MysteryBoxResult res = new MysteryBoxResult
                    {
                        Awards = Utils.GetCommaSepString(GetAwards(box.Contents))
                    };

                    if (box.Sale != null && DateTime.UtcNow <= box.Sale.SaleEnd)
                    {
                        switch (box.Sale.Currency)
                        {
                        case CONSTANTS.GOLD:
                        {
                            Database.UpdateCredit(acc, -box.Sale.Price);
                            res.GoldLeft = acc.Credits;
                        }
                        break;

                        case CONSTANTS.FAME:
                        {
                            Database.UpdateFame(acc, -box.Sale.Price);
                            res.GoldLeft = acc.Fame;
                        }
                        break;

                        default:
                        {
                            WriteLine($"<Error>Invalid currency type ID {box.Sale.Currency}.</Error>");
                        }
                            return;
                        }
                    }
                    else
                    {
                        switch (box.PriceMB.Currency)
                        {
                        case CONSTANTS.GOLD:
                        {
                            Database.UpdateCredit(acc, box.PriceMB.Amount < 0 ? 0 : -box.PriceMB.Amount);
                            res.GoldLeft = acc.Credits;
                        }
                        break;

                        case CONSTANTS.FAME:
                        {
                            Database.UpdateFame(acc, box.PriceMB.Amount < 0 ? 0 : -box.PriceMB.Amount);
                            res.GoldLeft = acc.Fame;
                        }
                        break;

                        default:
                        {
                            WriteLine($"<Error>Invalid currency type ID {box.PriceMB.Currency}.</Error>");
                        }
                            return;
                        }
                    }
                    if (box.Sale != null && DateTime.UtcNow <= box.Sale.SaleEnd)
                    {
                        res.Currency = box.Sale.Currency;
                    }
                    else
                    {
                        res.Currency = box.PriceMB.Currency;
                    }

                    sendMysteryBoxResult(Context.Response.OutputStream, res);

                    int[] gifts = Utils.FromCommaSepString32(res.Awards);

                    List <int> giftsList = acc.Gifts.ToList();

                    foreach (int item in gifts)
                    {
                        giftsList.Add(item);
                    }

                    acc.Gifts = giftsList.ToArray();

                    acc.Flush();
                    acc.Reload();
                }
                else
                {
                    WriteLine("<Error>Account not found</Error>");
                }
            }
        }
Beispiel #2
0
        protected override void HandleRequest()
        {
            rand = Query["ignore"] != null ? new Random(int.Parse(Query["ignore"])) : new Random();

            if (Query["guid"] == null || Query["password"] == null)
            {
                WriteErrorLine("Error.incorrectEmailOrPassword");
            }
            else
            {
                DbAccount   acc;
                LoginStatus status = Database.Verify(Query["guid"], Query["password"], out acc);

                currency = -1;
                int.TryParse(Query["currency"], out currency);
                rows = -1;
                int.TryParse(Query["status"], out rows);
                price = -1;
                data  = null;

                if (status == LoginStatus.OK)
                {
                    List <int>    giftsList  = acc.Gifts.ToList();
                    int[]         gifts      = null;
                    List <string> candidates = new List <string>(3);

                    if (Query["gameId"] == null)
                    {
                        WriteLine("<Error>Fortune Game ID not declared.</Error>");
                        return;
                    }

                    SerializeMiniGames box = SerializeMiniGames.GetBox(int.Parse(Query["gameId"]), CONSTANTS.FORTUNE_GAME);

                    if (box == null)
                    {
                        WriteLine($"<Error>Fortune Game ID {Query["gameId"]} not found.</Error>");
                        return;
                    }

                    switch (currency)
                    {
                    case CONSTANTS.GOLD:
                        if (acc.Credits < box.PriceFG.firstInGold || acc.Credits < box.PriceFG.secondInGold)
                        {
                            WriteLine("<Error>Not enough gold.</Error>");
                            return;
                        }
                        break;

                    case CONSTANTS.FORTUNETOKENS:
                        if (acc.FortuneTokens < box.PriceFG.firstInToken)
                        {
                            WriteLine("<Error>Not enough fortune tokens.</Error>");
                            return;
                        }
                        break;

                    default:
                    {
                        WriteLine($"<Error>Invalid currency type ID {currency}.</Error>");
                    }
                        return;
                    }

                    do
                    {
                        string item = Utils.GetCommaSepString(GetAwards(box.Contents));
                        if (!candidates.Contains(item))
                        {
                            candidates.Add(item);
                        }
                    } while (candidates.Count < 3);
                    switch (rows)
                    {
                    case 0:
                    {
                        switch (currency)
                        {
                        case CONSTANTS.GOLD:
                            if (acc.Credits < box.PriceFG.firstInGold)
                            {
                                WriteLine("<Error>Not enough gold.</Error>");
                                return;
                            }
                            if (CurrentGames.ContainsKey(acc.AccountId))
                            {
                                CurrentGames.Remove(acc.AccountId);
                            }
                            CurrentGames.Add(acc.AccountId, candidates.ToArray());
                            price = box.PriceFG.firstInGold;
                            data  =
                                string.Format(@"<Success>
                                            <Candidates>{0}</Candidates>
                                            <Gold>{1}</Gold>
                                        </Success>", Utils.GetCommaSepString(candidates.ToArray()), acc.Credits - price);
                            break;

                        case CONSTANTS.FORTUNETOKENS:
                            if (acc.FortuneTokens < box.PriceFG.firstInToken)
                            {
                                WriteLine("<Error>Not enough fortune tokens.</Error>");
                                return;
                            }
                            if (CurrentGames.ContainsKey(acc.AccountId))
                            {
                                CurrentGames.Remove(acc.AccountId);
                            }
                            CurrentGames.Add(acc.AccountId, candidates.ToArray());
                            price = box.PriceFG.firstInToken;
                            data  =
                                string.Format(@"<Success>
                                            <Candidates>{0}</Candidates>
                                            <FortuneToken>{1}</FortuneToken>
                                        </Success>", Utils.GetCommaSepString(candidates.ToArray()), acc.FortuneTokens - price);
                            break;

                        default:
                        {
                            WriteLine($"<Error>Invalid currency type ID {currency}.</Error>");
                        }
                            return;
                        }
                    }
                    break;

                    case 1:
                    {
                        switch (currency)
                        {
                        case CONSTANTS.GOLD:
                        {
                            if (CurrentGames.ContainsKey(acc.AccountId))
                            {
                                candidates = CurrentGames[acc.AccountId].ToList();
                                candidates.Shuffle();
                                data =
                                    string.Format(@"<Success>
                                                    <Awards>{0}</Awards>
                                                </Success>", candidates[int.Parse(Query["choice"])]);
                                gifts = Utils.FromCommaSepString32(candidates[int.Parse(Query["choice"])]);
                                foreach (int i in gifts)
                                {
                                    giftsList.Add(i);
                                }
                                candidates.Remove(candidates[int.Parse(Query["choice"])]);
                                CurrentGames[acc.AccountId] = candidates.ToArray();
                            }
                        }
                        break;

                        case CONSTANTS.FORTUNETOKENS:
                        {
                            if (CurrentGames.ContainsKey(acc.AccountId))
                            {
                                candidates = CurrentGames[acc.AccountId].ToList();
                                candidates.Shuffle();
                                data =
                                    string.Format(@"<Success>
                                                    <Awards>{0}</Awards>
                                                </Success>", candidates[int.Parse(Query["choice"])]);
                                gifts = Utils.FromCommaSepString32(candidates[int.Parse(Query["choice"])]);
                                foreach (int i in gifts)
                                {
                                    giftsList.Add(i);
                                }
                                candidates.Remove(candidates[int.Parse(Query["choice"])]);
                                CurrentGames[acc.AccountId] = candidates.ToArray();
                            }
                        }
                        break;

                        default:
                        {
                            WriteLine($"<Error>Invalid currency type ID {currency}.</Error>");
                        }
                            return;
                        }
                    }
                    break;

                    case 2:
                    {
                        switch (currency)
                        {
                        case CONSTANTS.GOLD:
                        {
                            if (CurrentGames.ContainsKey(acc.AccountId))
                            {
                                if (acc.Credits < box.PriceFG.secondInGold)
                                {
                                    WriteLine("<Error>Not enough gold.</Error>");
                                    return;
                                }
                                candidates = CurrentGames[acc.AccountId].ToList();
                                candidates.Shuffle();
                                price = box.PriceFG.secondInGold;
                                data  =
                                    string.Format(@"<Success>
                                                    <Awards>{0}</Awards>
                                                </Success>", candidates[int.Parse(Query["choice"])]);
                                gifts = Utils.FromCommaSepString32(candidates[int.Parse(Query["choice"])]);
                                foreach (int i in gifts)
                                {
                                    giftsList.Add(i);
                                }
                                candidates.Remove(candidates[int.Parse(Query["choice"])]);
                                CurrentGames.Remove(acc.AccountId);
                            }
                        }
                        break;

                        case CONSTANTS.FORTUNETOKENS:
                        {
                            WriteLine($"<Error>You can not play twiche with a Fortune Token.</Error>");
                        }
                            return;

                        default:
                        {
                            WriteLine($"<Error>Invalid currency type ID {currency}.</Error>");
                        }
                            return;
                        }
                    }
                    break;

                    default:
                    {
                        WriteLine($"<Error>Unreachable data stage {rows}.</Error>");
                    }
                        return;
                    }

                    switch (currency)
                    {
                    case CONSTANTS.GOLD:
                    {
                        Database.UpdateCredit(acc, price < 0 ? 0 : -price);
                    }
                    break;

                    case CONSTANTS.FORTUNETOKENS:
                    {
                        Database.UpdateTokens(acc, price < 0 ? 0 : -price);
                    }
                    break;

                    default:
                    {
                        WriteLine($"<Error>Invalid currency type ID {currency}.</Error>");
                    }
                        return;
                    }

                    acc.Gifts = giftsList.ToArray();

                    acc.Flush();
                    acc.Reload();

                    WriteLine(data);
                }
                else
                {
                    WriteErrorLine(status.GetInfo());
                }
            }
        }