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
This commit is contained in:
rpaciorek 2023-08-14 13:03:00 +00:00 committed by GitHub
parent 0ef87a61ff
commit fbc9b8a201
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 209 additions and 24 deletions

View File

@ -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<req.Quantity; ++i) {
InventoryItem item = new InventoryItem { ItemId = (int)req.ItemID, Quantity = 1 };
viking.Inventory.InventoryItems.Add(item);
ctx.SaveChanges(); // We need to get the ID of the newly created item
responseItems.Add(new CommonInventoryResponseItem {
CommonInventoryID = item.Id,
ItemID = item.ItemId,
Quantity = 0
});
}
} else {
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);
}
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<PurchaseStoreItemRequest>(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<UserRoom>(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;
}
}

View File

@ -249671,6 +249671,12 @@ This bundle contains 10 Accurate Powerups.</d>
<i xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<id>20785</id>
</c>
<c>
<cid>544</cid>
<cn>Farm Expansion Store</cn>
<i xsi:nil="true" />
<id>0</id>
</c>
<ct>0</ct>
<ct2>500</ct2>
<cp>700</cp>
@ -415743,6 +415749,12 @@ Cool down 1.5 seconds</d>
<i xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<id>20432</id>
</c>
<c>
<cid>544</cid>
<cn>Farm Expansion Store</cn>
<i xsi:nil="true" />
<id>0</id>
</c>
<ct>0</ct>
<ct2>500</ct2>
<cp>700</cp>
@ -415751,6 +415763,7 @@ Cool down 1.5 seconds</d>
<im>-1</im>
<id>20432</id>
<itn>Dread Farm</itn>
<itnp>Dread Farm</itnp>
<l>false</l>
<ro xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<rid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
@ -1018744,6 +1018757,12 @@ Used in Food Flick and Spot What's Not.</d>
<i xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<id>0</id>
</c>
<c>
<cid>544</cid>
<cn>Farm Expansion Store</cn>
<i xsi:nil="true" />
<id>0</id>
</c>
<ct>0</ct>
<ct2>500</ct2>
<cp>400</cp>

View File

@ -539341,6 +539341,59 @@
<bp xsi:nil="true" />
</is>
<is>
<an>FarmingOceanDO</an>
<at>
<k>2D</k>
<v>1</v>
<id>0</id>
</at>
<at>
<k>Preview</k>
<v>RS_DATA/FarmDWFarmIcons.unity3d/AniDWDragonsStorePreviewFarmOcean</v>
<id>0</id>
</at>
<at>
<k>2D Size</k>
<v>330,440</v>
<id>0</id>
</at>
<c>
<cid>541</cid>
<cn>Farm Expansions</cn>
<i xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<id>0</id>
</c>
<c>
<cid>544</cid>
<cn>Farm Expansion Store</cn>
<i xsi:nil="true" />
<id>0</id>
</c>
<ct>0</ct>
<ct2>500</ct2>
<cp>400</cp>
<d>Grow more crops, have more animals, and more decorations in this farm expansion by the ocean!</d>
<icn>RS_DATA/FarmDWFarmIcons.unity3d/IcoDWFarmOcean</icn>
<im>-1</im>
<id>13099</id>
<itn>Farm by the Ocean</itn>
<l>false</l>
<ro xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<rid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<s>false</s>
<as>false</as>
<sf>10</sf>
<u>1</u>
<g xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<rf>0</rf>
<rtid>0</rtid>
<p xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<ir xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<ipsm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<ism xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<bp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</is>
<!-- <is>
<an>FarmingOceanDO</an>
<at>
<k>2D</k>
@ -539402,7 +539455,7 @@
<ipsm xsi:nil="true" />
<ism xsi:nil="true" />
<bp xsi:nil="true" />
</is>
</is>-->
<is>
<an>RS_DATA/PfDWDecorHHHauntedTree02.unity3d/PfDWDecorHHHauntedTree02</an>
<c>
@ -547066,6 +547119,12 @@
<i xsi:nil="true" />
<id>0</id>
</c>
<c>
<cid>544</cid>
<cn>Farm Expansion Store</cn>
<i xsi:nil="true" />
<id>0</id>
</c>
<ct>0</ct>
<ct2>500</ct2>
<cp>700</cp>
@ -547091,7 +547150,7 @@
<ism xsi:nil="true" />
<bp xsi:nil="true" />
</is>
<is>
<!-- <is>
<an>FarmingThawfestDO</an>
<at>
<k>2D</k>
@ -547154,6 +547213,60 @@
<ipsm xsi:nil="true" />
<ism xsi:nil="true" />
<bp xsi:nil="true" />
</is>-->
<is>
<an>FarmingDreadfallDO</an>
<at>
<k>2D</k>
<v>1</v>
<id>20432</id>
</at>
<at>
<k>Preview</k>
<v>RS_DATA/FarmDWFarmIcons.unity3d/AniDWDragonsStorePreviewFarmDreadfall</v>
<id>20432</id>
</at>
<at>
<k>2D Size</k>
<v>330,440</v>
<id>20432</id>
</at>
<c>
<cid>541</cid>
<cn>Farm Expansions</cn>
<i xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<id>20432</id>
</c>
<c>
<cid>544</cid>
<cn>Farm Expansion Store</cn>
<i xsi:nil="true" />
<id>0</id>
</c>
<ct>0</ct>
<ct2>500</ct2>
<cp>700</cp>
<d>Grow more Crops, raise more Animals, and add more Decorations in this Spooky Farm! Now featuring more bones!</d>
<icn>RS_DATA/FarmDWFarmIcons.unity3d/IcoDWFarmDreadfall</icn>
<im>-1</im>
<id>20432</id>
<itn>Dread Farm</itn>
<itnp>Dread Farm</itnp>
<l>false</l>
<ro xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<rid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<s>false</s>
<as>false</as>
<sf>10</sf>
<u>1</u>
<g xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<rf>0</rf>
<rtid>0</rtid>
<p xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<ir xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<ipsm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<ism xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<bp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</is>
<is>
<an>RS_DATA/pfdwdecorstatuesod10do/PfDWDecorStatueSOD10DO</an>