Exemple #1
0
        private void sendMysteryBoxResult(Stream stream, MysteryBoxResult res)
        {
            XmlDocument doc     = new XmlDocument();
            XmlNode     success = doc.CreateElement("Success");

            doc.AppendChild(success);

            XmlNode awards = doc.CreateElement("Awards");

            awards.InnerText = res.Awards.Replace(" ", string.Empty);
            success.AppendChild(awards);

            XmlNode goldLeft = doc.CreateElement(res.Currency == 0 ? "Gold" : "Fame");

            goldLeft.InnerText = res.GoldLeft.ToString();
            success.AppendChild(goldLeft);

            StringWriter wtr = new StringWriter();

            doc.Save(wtr);
            using (StreamWriter output = new StreamWriter(stream))
                output.WriteLine(wtr.ToString());
        }
Exemple #2
0
        protected override void HandleRequest()
        {
            rand = Query["ignore"] != null ? new Random(int.Parse(Query["ignore"])) : new RRandom();

            using (Database db = new Database())
            {
                Account acc = db.Verify(Query["guid"], Query["password"], Program.GameData);
                if (CheckAccount(acc, db, false))
                {
                    if (Query["boxId"] == null)
                    {
                        using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                            wtr.WriteLine("<Error>Box not found</Error>");
                        return;
                    }
                    MysteryBox box = MysteryBox.GetBox(int.Parse(Query["boxId"]));
                    if (box == null)
                    {
                        using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                            wtr.WriteLine("<Error>Box not found</Error>");
                        return;
                    }
                    if (box.Sale != null && DateTime.UtcNow <= box.Sale.SaleEnd)
                    {
                        switch (box.Sale.Currency)
                        {
                        case 0:
                            if (acc.Credits < box.Sale.Price)
                            {
                                using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                                    wtr.WriteLine("<Error>Not Enough Gold</Error>");
                                return;
                            }
                            break;

                        case 1:
                            if (acc.Stats.Fame < box.Sale.Price)
                            {
                                using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                                    wtr.WriteLine("<Error>Not Enough Fame</Error>");
                                return;
                            }
                            break;
                        }
                    }
                    else
                    {
                        switch (box.Price.Currency)
                        {
                        case 0:
                            if (acc.Credits < box.Price.Amount)
                            {
                                using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                                    wtr.WriteLine("<Error>Not Enough Gold</Error>");
                                return;
                            }
                            break;

                        case 1:
                            if (acc.Stats.Fame < box.Price.Amount)
                            {
                                using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                                    wtr.WriteLine("<Error>Not Enough Fame</Error>");
                                return;
                            }
                            break;
                        }
                    }

                    MysteryBoxResult res = new MysteryBoxResult
                    {
                        Awards = Utils.GetCommaSepString(GetAwards(box.Contents))
                    };
                    if (box.Sale != null && DateTime.UtcNow <= box.Sale.SaleEnd)
                    {
                        res.GoldLeft = box.Sale.Currency == 0
                            ? db.UpdateCredit(acc, -box.Sale.Price)
                            : db.UpdateFame(acc, -box.Sale.Price);
                    }
                    else
                    {
                        res.GoldLeft = box.Price.Currency == 0
                            ? db.UpdateCredit(acc, -box.Price.Amount)
                            : db.UpdateFame(acc, -box.Price.Amount);
                    }

                    if (box.Sale != null && DateTime.UtcNow <= box.Sale.SaleEnd)
                    {
                        res.Currency = box.Sale.Currency;
                    }
                    else
                    {
                        res.Currency = box.Price.Currency;
                    }

                    sendMysteryBoxResult(Context.Response.OutputStream, res);

                    int[] gifts = Utils.FromCommaSepString32(res.Awards);
                    foreach (int item in gifts)
                    {
                        acc.Gifts.Add(item);
                    }

                    MySqlCommand cmd = db.CreateQuery();
                    cmd.CommandText =
                        "UPDATE accounts SET gifts=@gifts WHERE uuid=@uuid AND password=SHA1(@password);";
                    cmd.Parameters.AddWithValue("@uuid", Query["guid"]);
                    cmd.Parameters.AddWithValue("@password", Query["password"]);
                    cmd.Parameters.AddWithValue("@gifts", Utils.GetCommaSepString(acc.Gifts.ToArray()));
                    cmd.ExecuteNonQuery();
                }
                else
                {
                    using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                        wtr.WriteLine("<Error>Account not found</Error>");
                }
            }
        }
        protected override void HandleRequest()
        {
            rand = Query["ignore"] != null ? new Random(int.Parse(Query["ignore"])) : new Random();

            using (Database db = new Database())
            {
                Account acc = db.Verify(Query["guid"], Query["password"], Program.GameData);
                if (CheckAccount(acc, db, false))
                {
                    if (Query["boxId"] == null)
                    {
                        using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                            wtr.WriteLine("<Error>Box not found</Error>");
                        return;
                    }
                    MysteryBox box = MysteryBox.GetBox(int.Parse(Query["boxId"]));
                    if (box == null)
                    {
                        using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                            wtr.WriteLine("<Error>Box not found</Error>");
                        return;
                    }
                    if (box.Sale != null && DateTime.UtcNow <= box.Sale.SaleEnd)
                    {
                        switch (box.Sale.Currency)
                        {
                            case 0:
                                if (acc.Credits < box.Sale.Price)
                                {
                                    using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                                        wtr.WriteLine("<Error>Not Enough Gold</Error>");
                                    return;
                                }
                                break;

                            case 1:
                                if (acc.Stats.Fame < box.Sale.Price)
                                {
                                    using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                                        wtr.WriteLine("<Error>Not Enough Fame</Error>");
                                    return;
                                }
                                break;
                        }
                    }
                    else
                    {
                        switch (box.Price.Currency)
                        {
                            case 0:
                                if (acc.Credits < box.Price.Amount)
                                {
                                    using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                                        wtr.WriteLine("<Error>Not Enough Gold</Error>");
                                    return;
                                }
                                break;

                            case 1:
                                if (acc.Stats.Fame < box.Price.Amount)
                                {
                                    using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                                        wtr.WriteLine("<Error>Not Enough Fame</Error>");
                                    return;
                                }
                                break;
                        }
                    }

                    MysteryBoxResult res = new MysteryBoxResult
                    {
                        Awards = Utils.GetCommaSepString(GetAwards(box.Contents))
                    };
                    if (box.Sale != null && DateTime.UtcNow <= box.Sale.SaleEnd)
                        res.GoldLeft = box.Sale.Currency == 0
                            ? db.UpdateCredit(acc, -box.Sale.Price)
                            : db.UpdateFame(acc, -box.Sale.Price);
                    else
                        res.GoldLeft = box.Price.Currency == 0
                            ? db.UpdateCredit(acc, -box.Price.Amount)
                            : db.UpdateFame(acc, -box.Price.Amount);

                    if (box.Sale != null && DateTime.UtcNow <= box.Sale.SaleEnd)
                        res.Currency = box.Sale.Currency;
                    else
                        res.Currency = box.Price.Currency;

                    sendMysteryBoxResult(Context.Response.OutputStream, res);

                    int[] gifts = Utils.FromCommaSepString32(res.Awards);
                    foreach (int item in gifts)
                        acc.Gifts.Add(item);

                    MySqlCommand cmd = db.CreateQuery();
                    cmd.CommandText =
                        "UPDATE accounts SET gifts=@gifts WHERE uuid=@uuid AND password=SHA1(@password);";
                    cmd.Parameters.AddWithValue("@uuid", Query["guid"]);
                    cmd.Parameters.AddWithValue("@password", Query["password"]);
                    cmd.Parameters.AddWithValue("@gifts", Utils.GetCommaSepString(acc.Gifts.ToArray()));
                    cmd.ExecuteNonQuery();
                }
                else
                    using (StreamWriter wtr = new StreamWriter(Context.Response.OutputStream))
                        wtr.WriteLine("<Error>Account not found</Error>");
            }
        }
Exemple #4
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>");
                }
            }
        }
        private void sendMysteryBoxResult(Stream stream, MysteryBoxResult res)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode success = doc.CreateElement("Success");
            doc.AppendChild(success);

            XmlNode awards = doc.CreateElement("Awards");
            awards.InnerText = res.Awards.Replace(" ", String.Empty);
            success.AppendChild(awards);

            XmlNode goldLeft = doc.CreateElement(res.Currency == 0 ? "Gold" : "Fame");
            goldLeft.InnerText = res.GoldLeft.ToString();
            success.AppendChild(goldLeft);

            StringWriter wtr = new StringWriter();
            doc.Save(wtr);
            using (StreamWriter output = new StreamWriter(stream))
                output.WriteLine(wtr.ToString());
        }