protected override void HandleRequest() { StreamWriter wtr = new StreamWriter(Context.Response.OutputStream); if (Query.AllKeys.Length > 0) { using (Database db = new Database()) { Package package = Package.GetPackage(int.Parse(Query["packageId"])); if (package == null) { wtr.Write("<Error>This package is not available any more</Error>"); return; } JsonSerializer s = new JsonSerializer(); var contents = s.Deserialize<PackageContent>(new JsonTextReader(new StringReader(package.Contents))); Account acc = db.Verify(Query["guid"], Query["password"], Program.GameData); if (CheckAccount(acc, db, false)) { if (acc.Credits < package.Price) { wtr.Write("<Error>Not enough gold.<Error/>"); return; } var cmd = db.CreateQuery(); if (contents.items?.Count > 0) { foreach (var i in contents.items) { Dictionary<string, int> itemDic = new Dictionary<string, int>(); List<int> gifts = acc.Gifts; gifts.Add(i); cmd = db.CreateQuery(); cmd.CommandText = "UPDATE accounts SET gifts=@gifts WHERE uuid=@uuid AND password=SHA1(@password);"; cmd.Parameters.AddWithValue("@gifts", Utils.GetCommaSepString<int>(gifts.ToArray())); cmd.Parameters.AddWithValue("@uuid", Query["guid"]); cmd.Parameters.AddWithValue("@password", Query["password"]); cmd.ExecuteNonQuery(); } } if (contents.charSlots > 0) { cmd = db.CreateQuery(); cmd.CommandText = "UPDATE accounts SET maxCharSlot=maxCharSlot + @amount WHERE uuid=@uuid AND password=SHA1(@password);"; cmd.Parameters.AddWithValue("@amount", contents.charSlots); cmd.Parameters.AddWithValue("@uuid", Query["guid"]); cmd.Parameters.AddWithValue("@password", Query["password"]); if (cmd.ExecuteNonQuery() == 0) return; } if (contents.vaultChests > 0) { for (int j = 0; j < contents.vaultChests; j++) db.CreateChest(acc); } db.UpdateCredit(acc, -package.Price); wtr.Write("<Success/>"); } } } }
private bool ParseContents(Account acc, string json) { try { using (var db = new Database()) { var code = GiftCode.FromJson(json); if (code == null) return false; var cmd = db.CreateQuery(); if (code.Gifts.Count > 0) { List<int> gifts = acc.Gifts; foreach (var i in code.Gifts) gifts.Add(i); cmd = db.CreateQuery(); cmd.CommandText = "UPDATE accounts SET gifts=@gifts WHERE uuid=@uuid AND password=SHA1(@password);"; cmd.Parameters.AddWithValue("@gifts", Utils.GetCommaSepString<int>(gifts.ToArray())); cmd.Parameters.AddWithValue("@uuid", Query["guid"]); cmd.Parameters.AddWithValue("@password", Query["password"]); cmd.ExecuteNonQuery(); } if (code.CharSlots > 0) { cmd = db.CreateQuery(); cmd.CommandText = "UPDATE accounts SET maxCharSlot=maxCharSlot + @amount WHERE uuid=@uuid AND password=SHA1(@password);"; cmd.Parameters.AddWithValue("@amount", code.CharSlots); cmd.Parameters.AddWithValue("@uuid", Query["guid"]); cmd.Parameters.AddWithValue("@password", Query["password"]); cmd.ExecuteNonQuery(); } if (code.VaultChests > 0) for (int j = 0; j < code.VaultChests; j++) db.CreateChest(acc); if (code.Gold > 0) db.UpdateCredit(acc, code.Gold); if (code.Fame > 0) db.UpdateFame(acc, code.Fame); } } catch (Exception) { return false; } return true; }