From fbc9b8a201397b876464998e9925f45fa527962f Mon Sep 17 00:00:00 2001 From: rpaciorek Date: Mon, 14 Aug 2023 13:03:00 +0000 Subject: [PATCH] Farm expansions (#13) * support for farm expansions * add farm expansion to store - replace farm expansion bundle by raw farm expansion - add dreadfall farm expansion * bugfix - set room name in SetUserRoom on new room * support for multiple farm rooms of the same type * bugfix - missing CommonInventoryID in SetCommonInventory reply to farm expansion request --- src/Controllers/Common/ContentController.cs | 97 ++++++++++++---- src/Resources/items.xml | 19 ++++ src/Resources/store.xml | 117 +++++++++++++++++++- 3 files changed, 209 insertions(+), 24 deletions(-) diff --git a/src/Controllers/Common/ContentController.cs b/src/Controllers/Common/ContentController.cs index fc183f1..c3f6606 100644 --- a/src/Controllers/Common/ContentController.cs +++ b/src/Controllers/Common/ContentController.cs @@ -182,30 +182,50 @@ public class ContentController : Controller { // SetCommonInventory can remove any number of items from the inventory, this checks if it's possible foreach (var req in request) { if (req.Quantity >= 0) continue; - InventoryItem? item = viking.Inventory.InventoryItems.FirstOrDefault(e => e.ItemId == req.ItemID); - if (item is null || item.Quantity < req.Quantity) + int inventorySum = viking.Inventory.InventoryItems.Sum(e => {if (e.ItemId == req.ItemID) return e.Quantity; return 0;}); + if (inventorySum < -req.Quantity) return Ok(new CommonInventoryResponse { Success = false }); } // Now that we know the request is valid, update the inventory foreach (var req in request) { if (req.ItemID == 0) continue; // Do not save a null item - InventoryItem? item = viking.Inventory.InventoryItems.FirstOrDefault(e => e.ItemId == req.ItemID); - if (item is null) { - item = new InventoryItem { ItemId = (int)req.ItemID, Quantity = 0 }; - viking.Inventory.InventoryItems.Add(item); + + if (IsFarmExpansion((int)req.ItemID)) { + // if req.Quantity < 0 remove unique items + for (int i=req.Quantity; i<0; ++i) { + InventoryItem? item = viking.Inventory.InventoryItems.FirstOrDefault(e => e.ItemId == req.ItemID && e.Quantity>0); + item.Quantity--; + } + // if req.Quantity > 0 add unique items + for (int i=0; i e.ItemId == req.ItemID); + if (item is null) { + item = new InventoryItem { ItemId = (int)req.ItemID, Quantity = 0 }; + viking.Inventory.InventoryItems.Add(item); + } + int updateQuantity = 0; // The game expects 0 if quantity got updated by just 1 + if (req.Quantity > 1) + updateQuantity = req.Quantity; // Otherwise it expects the quantity from the request + item.Quantity += req.Quantity; + ctx.SaveChanges(); // We need to get the ID of the newly created item + if (req.Quantity > 0) + responseItems.Add(new CommonInventoryResponseItem { + CommonInventoryID = item.Id, + ItemID = item.ItemId, + Quantity = updateQuantity + }); } - int updateQuantity = 0; // The game expects 0 if quantity got updated by just 1 - if (req.Quantity > 1) - updateQuantity = req.Quantity; // Otherwise it expects the quantity from the request - item.Quantity += req.Quantity; - ctx.SaveChanges(); // We need to get the ID of the newly created item - if (req.Quantity > 0) - responseItems.Add(new CommonInventoryResponseItem { - CommonInventoryID = item.Id, - ItemID = item.ItemId, - Quantity = updateQuantity - }); } CommonInventoryResponse response = new CommonInventoryResponse { @@ -662,7 +682,9 @@ public class ContentController : Controller { PurchaseStoreItemRequest request = XmlUtil.DeserializeXml(purchaseItemRequest); CommonInventoryResponseItem[] items = new CommonInventoryResponseItem[request.Items.Length]; for (int i = 0; i < request.Items.Length; i++) { - InventoryItem? item = viking.Inventory.InventoryItems.FirstOrDefault(e => e.ItemId == request.Items[i]); + InventoryItem? item = null; + if (!IsFarmExpansion(request.Items[i])) + item = viking.Inventory.InventoryItems.FirstOrDefault(e => e.ItemId == request.Items[i]); if (item is null) { item = new InventoryItem { ItemId = request.Items[i], Quantity = 0 }; viking.Inventory.InventoryItems.Add(item); @@ -788,11 +810,23 @@ public class ContentController : Controller { return Ok(response); foreach (var room in rooms) { if (room.RoomId == "MyRoomINT" || room.RoomId == "StaticFarmItems") continue; + + int itemID = 0; + if (room.RoomId != "") { + // farm expansion room: RoomId is Id for expansion item + if (Int32.TryParse(room.RoomId, out int inventoryItemId)) { + InventoryItem? item = room.Viking.Inventory.InventoryItems.FirstOrDefault(e => e.Id == inventoryItemId); + if (item != null) { + itemID = item.ItemId; + } + } + } + UserRoom ur = new UserRoom { RoomID = room.RoomId, CategoryID = 541, // Placeholder CreativePoints = 0, // Placeholder - ItemID = 0, + ItemID = itemID, Name = room.Name }; response.UserRoomList.Add(ur); @@ -806,9 +840,16 @@ public class ContentController : Controller { public IActionResult SetUserRoom([FromForm] string apiToken, [FromForm] string request) { UserRoom roomRequest = XmlUtil.DeserializeXml(request); Room? room = ctx.Sessions.FirstOrDefault(e => e.ApiToken == apiToken)?.Viking?.Rooms.FirstOrDefault(x => x.RoomId == roomRequest.RoomID); - if (room is null) - return Ok(new UserItemPositionList { UserItemPosition = new UserItemPosition[0] }); - room.Name = roomRequest.Name; + if (room is null) { + // setting farm room name can be done before call SetUserRoomItemPositions + room = new Room { + RoomId = roomRequest.RoomID, + Name = roomRequest.Name + }; + ctx.Sessions.FirstOrDefault(e => e.ApiToken == apiToken)?.Viking?.Rooms.Add(room); + } else { + room.Name = roomRequest.Name; + } ctx.SaveChanges(); return Ok(new UserRoomSetResponse { Success = true, @@ -922,4 +963,16 @@ public class ContentController : Controller { TemplateName = image.TemplateName, }; } + + private bool IsFarmExpansion(int itemId) { + ItemData? itemData = itemService.GetItem(itemId); + if (itemData != null && itemData.Category != null) { + foreach (ItemDataCategory itemCategory in itemData.Category) { + if (itemCategory.CategoryId == 541) { // if item is farm expansion + return true; + } + } + } + return false; + } } diff --git a/src/Resources/items.xml b/src/Resources/items.xml index 875dfd9..791f481 100644 --- a/src/Resources/items.xml +++ b/src/Resources/items.xml @@ -249671,6 +249671,12 @@ This bundle contains 10 Accurate Powerups. 20785 + + 544 + Farm Expansion Store + + 0 + 0 500 700 @@ -415743,6 +415749,12 @@ Cool down 1.5 seconds 20432 + + 544 + Farm Expansion Store + + 0 + 0 500 700 @@ -415751,6 +415763,7 @@ Cool down 1.5 seconds -1 20432 Dread Farm + Dread Farm false @@ -1018744,6 +1018757,12 @@ Used in Food Flick and Spot What's Not. 0 + + 544 + Farm Expansion Store + + 0 + 0 500 400 diff --git a/src/Resources/store.xml b/src/Resources/store.xml index ecc290c..3f0cd8b 100644 --- a/src/Resources/store.xml +++ b/src/Resources/store.xml @@ -539341,6 +539341,59 @@ + FarmingOceanDO + + 2D + 1 + 0 + + + Preview + RS_DATA/FarmDWFarmIcons.unity3d/AniDWDragonsStorePreviewFarmOcean + 0 + + + 2D Size + 330,440 + 0 + + + 541 + Farm Expansions + + 0 + + + 544 + Farm Expansion Store + + 0 + + 0 + 500 + 400 + Grow more crops, have more animals, and more decorations in this farm expansion by the ocean! + RS_DATA/FarmDWFarmIcons.unity3d/IcoDWFarmOcean + -1 + 13099 + Farm by the Ocean + false + + + false + false + 10 + 1 + + 0 + 0 +

+ + + + + + RS_DATA/PfDWDecorHHHauntedTree02.unity3d/PfDWDecorHHHauntedTree02 @@ -547066,6 +547119,12 @@ 0 + + 544 + Farm Expansion Store + + 0 + 0 500 700 @@ -547091,7 +547150,7 @@ - + + + FarmingDreadfallDO + + 2D + 1 + 20432 + + + Preview + RS_DATA/FarmDWFarmIcons.unity3d/AniDWDragonsStorePreviewFarmDreadfall + 20432 + + + 2D Size + 330,440 + 20432 + + + 541 + Farm Expansions + + 20432 + + + 544 + Farm Expansion Store + + 0 + + 0 + 500 + 700 + Grow more Crops, raise more Animals, and add more Decorations in this Spooky Farm! Now featuring more bones! + RS_DATA/FarmDWFarmIcons.unity3d/IcoDWFarmDreadfall + -1 + 20432 + Dread Farm + Dread Farm + false + + + false + false + 10 + 1 + + 0 + 0 +

+ + + + RS_DATA/pfdwdecorstatuesod10do/PfDWDecorStatueSOD10DO