public bool HandlePacket(Peer peer, byte[] data, Game game) { var request = new BuyItemReq(data); var itemTemplate = game.ItemManager.SafeGetItemType(request.id); if (itemTemplate == null) { return(false); } var recipeParts = game.GetPeerInfo(peer).GetChampion().getInventory().GetAvailableItems(itemTemplate.Recipe); var price = itemTemplate.TotalPrice; Item i; if (recipeParts.Count == 0) { if (game.GetPeerInfo(peer).GetChampion().GetStats().Gold < price) { return(true); } i = game.GetPeerInfo(peer).GetChampion().getInventory().AddItem(itemTemplate); if (i == null) { // Slots full return(false); } } else { foreach (var instance in recipeParts) { price -= instance.TotalPrice; } if (game.GetPeerInfo(peer).GetChampion().GetStats().Gold < price) { return(false); } foreach (var instance in recipeParts) { game.GetPeerInfo(peer).GetChampion().GetStats().RemoveBuff(instance.ItemType); var champion = game.GetPeerInfo(peer).GetChampion(); var inventory = champion.Inventory; game.PacketNotifier.notifyRemoveItem(champion, inventory.GetItemSlot(instance), 0); inventory.RemoveItem(instance); } i = game.GetPeerInfo(peer).GetChampion().getInventory().AddItem(itemTemplate); } game.GetPeerInfo(peer).GetChampion().GetStats().Gold = game.GetPeerInfo(peer).GetChampion().GetStats().Gold - price; game.GetPeerInfo(peer).GetChampion().GetStats().AddBuff(itemTemplate); game.PacketNotifier.notifyItemBought(game.GetPeerInfo(peer).GetChampion(), i); return(true); }
public async Task <BuyItemRes> BuyItem(BuyItemReq request) { return(await Task.Run(() => { try { /*Will hardcode response, but in production, data could be stored in db and * payment reference generated after payment is confirmed */ BuyItemRes buyItemRes = new BuyItemRes() { ReferenceNo = "XXXXXX", ResponseCode = $"0{((int)InternalResponseCode.Succesful).ToString()}", ResponseMessage = "Payment Successful" }; return buyItemRes; } catch (Exception ex) { logger.Error(ex); return null; } })); }
public unsafe bool HandlePacket(ENetPeer *peer, byte[] data, Game game) { var request = new BuyItemReq(data); var itemTemplate = ItemManager.getInstance().getItemTemplateById(request.id); if (itemTemplate == null) { return(false); } var recipeParts = game.getPeerInfo(peer).getChampion().getInventory().getAvailableRecipeParts(itemTemplate); var price = itemTemplate.getTotalPrice(); ItemInstance i; if (recipeParts.Count == 0) { if (game.getPeerInfo(peer).getChampion().getStats().getGold() < price) { return(true); } i = game.getPeerInfo(peer).getChampion().getInventory().addItem(itemTemplate); if (i == null) { // Slots full return(false); } } else { foreach (var instance in recipeParts) { price -= instance.getTemplate().getTotalPrice(); } if (game.getPeerInfo(peer).getChampion().getStats().getGold() < price) { return(false); } foreach (var instance in recipeParts) { game.getPeerInfo(peer).getChampion().getStats().unapplyStatMods(instance.getTemplate().getStatMods()); PacketNotifier.notifyRemoveItem(game.getPeerInfo(peer).getChampion(), instance.getSlot(), 0); game.getPeerInfo(peer).getChampion().getInventory().removeItem(instance.getSlot()); } i = game.getPeerInfo(peer).getChampion().getInventory().addItem(itemTemplate); } game.getPeerInfo(peer).getChampion().getStats().setGold(game.getPeerInfo(peer).getChampion().getStats().getGold() - price); game.getPeerInfo(peer).getChampion().getStats().applyStatMods(itemTemplate.getStatMods()); PacketNotifier.notifyItemBought(game.getPeerInfo(peer).getChampion(), i); return(true); }
public async Task <HttpResponseMessage> BuyItem(BuyItemReq req) { BuyItemRes res = new BuyItemRes(); try { if (req == null) { res.ResponseCode = ((int)InternalResponseCode.InvalidRequestBody).ToString(); res.ResponseMessage = "Request body cannot be null"; return(Request.CreateResponse(HttpStatusCode.BadRequest, res)); } CommonLib commonLib = new CommonLib(); var identity = (ClaimsIdentity)User.Identity; //get merchantname from identity var merchantname = identity.Name; //get merchant profile from name var profile = commonLib.GetUserProfileByMerchantName(merchantname); if (profile == null) { //invalid profile res.ResponseCode = ((int)InternalResponseCode.InvalidCredential).ToString(); res.ResponseMessage = "Invalid Credential"; return(Request.CreateResponse(HttpStatusCode.Forbidden, res)); } //generate hash and compare with what was included in the requestbody var mygeneratedhash = commonLib.GenerateSHA256Hash(profile.SecretKey, req.timestamp, profile.ApiKey, req.ReceiptRef); //compare with hash sent by client if (mygeneratedhash != req.HashValue || string.IsNullOrEmpty(req.HashValue)) { //unrecognised user res.ResponseCode = ((int)InternalResponseCode.InvalidHash).ToString(); res.ResponseMessage = "Invalid Hash"; return(Request.CreateResponse(HttpStatusCode.Forbidden, res)); } res = await commonLib.BuyItem(req); return(Request.CreateResponse(HttpStatusCode.OK, res)); } catch (Exception ex) { logger.Error(ex); res.ResponseCode = ((int)InternalResponseCode.Exception).ToString(); res.ResponseMessage = "Exception Occurred"; return(Request.CreateResponse(HttpStatusCode.ExpectationFailed, res)); } }
public BuyItemReq GetBuyItemReqData() { var buyitemreq = new BuyItemReq() { DeliveryLocation = "Chicago", ItemName = "Planks", Quantity = 300, ReceiptRef = "Ref1234", timestamp = "1553644809379", HashValue = "jeB9FrZbmREq0BkvV+1Upb3huITcO9Vq2ChGTPTu1Is=" }; return(buyitemreq); }