forked from SoDOff-Project/sodoff
support for items and inventories
This commit is contained in:
parent
2fa769fda8
commit
94da3ff337
@ -21,6 +21,8 @@ methods = [
|
|||||||
'GetUserProfile',
|
'GetUserProfile',
|
||||||
'GetQuestions',
|
'GetQuestions',
|
||||||
'GetCommonInventory',
|
'GetCommonInventory',
|
||||||
|
'SetCommonInventory',
|
||||||
|
'GetItem',
|
||||||
'GetAuthoritativeTime',
|
'GetAuthoritativeTime',
|
||||||
'SetAvatar',
|
'SetAvatar',
|
||||||
'GetPetAchievementsByUserID',
|
'GetPetAchievementsByUserID',
|
||||||
|
@ -12,9 +12,11 @@ public class ContentController : Controller {
|
|||||||
|
|
||||||
private readonly DBContext ctx;
|
private readonly DBContext ctx;
|
||||||
private KeyValueService keyValueService;
|
private KeyValueService keyValueService;
|
||||||
public ContentController(DBContext ctx, KeyValueService keyValueService) {
|
private ItemService itemService;
|
||||||
|
public ContentController(DBContext ctx, KeyValueService keyValueService, ItemService itemService) {
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
this.keyValueService = keyValueService;
|
this.keyValueService = keyValueService;
|
||||||
|
this.itemService = itemService;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
@ -118,7 +120,6 @@ public class ContentController : Controller {
|
|||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Produces("application/xml")]
|
[Produces("application/xml")]
|
||||||
[Route("V2/ContentWebService.asmx/GetCommonInventory")]
|
|
||||||
[Route("ContentWebService.asmx/GetCommonInventory")]
|
[Route("ContentWebService.asmx/GetCommonInventory")]
|
||||||
public IActionResult GetCommonInventory([FromForm] string apiToken) {
|
public IActionResult GetCommonInventory([FromForm] string apiToken) {
|
||||||
// TODO, this is a placeholder
|
// TODO, this is a placeholder
|
||||||
@ -129,44 +130,72 @@ public class ContentController : Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Ok(new CommonInventoryData {
|
return Ok(new CommonInventoryData {
|
||||||
UserID = Guid.Parse(user is not null ? user.Id : viking.Id),
|
UserID = Guid.Parse(user is not null ? user.Id : viking.Id)
|
||||||
Item = new UserItemData[] {
|
|
||||||
new UserItemData {
|
|
||||||
UserInventoryID = 1099730701,
|
|
||||||
ItemID = 8977,
|
|
||||||
Quantity = 1,
|
|
||||||
Uses = -1,
|
|
||||||
ModifiedDate = DateTime.Now,
|
|
||||||
Item = new ItemData {
|
|
||||||
AssetName = "DragonStableINTDO",
|
|
||||||
Cost = 100000,
|
|
||||||
CashCost = -1,
|
|
||||||
CreativePoints = 0,
|
|
||||||
Description = "Any dragon would be glad to make one of the two available nests home!",
|
|
||||||
IconName = "RS_DATA/DragonsStablesDO.unity3d/IcoDWDragonStableDefault",
|
|
||||||
InventoryMax = 1,
|
|
||||||
ItemID = 8977,
|
|
||||||
ItemName = "Dragon Stable",
|
|
||||||
Locked = false,
|
|
||||||
Stackable = false,
|
|
||||||
AllowStacking = false,
|
|
||||||
SaleFactor = 10,
|
|
||||||
Uses = -1,
|
|
||||||
Attribute = new ItemAttribute[] {
|
|
||||||
new ItemAttribute { Key = "2D", Value = "1" },
|
|
||||||
new ItemAttribute { Key = "NestCount", Value = "2" },
|
|
||||||
new ItemAttribute { Key = "StableType", Value = "All" }
|
|
||||||
},
|
|
||||||
Category = new ItemDataCategory[] {
|
|
||||||
new ItemDataCategory { CategoryId = 455, CategoryName = "Dragons Dragon Stable", IconName = "8977"}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[Produces("application/xml")]
|
||||||
|
[Route("V2/ContentWebService.asmx/GetCommonInventory")]
|
||||||
|
public IActionResult GetCommonInventoryV2([FromForm] string apiToken) {
|
||||||
|
Viking? viking = ctx.Sessions.FirstOrDefault(e => e.ApiToken == apiToken)?.Viking;
|
||||||
|
if (viking is null || viking.Inventory is null) return Ok();
|
||||||
|
|
||||||
|
List<InventoryItem> items = viking.Inventory.InventoryItems.ToList();
|
||||||
|
List<UserItemData> userItemData = new();
|
||||||
|
foreach (InventoryItem item in items) {
|
||||||
|
ItemData itemData = itemService.GetItem(item.ItemId);
|
||||||
|
UserItemData uid = new UserItemData {
|
||||||
|
UserInventoryID = viking.Inventory.Id,
|
||||||
|
ItemID = itemData.ItemID,
|
||||||
|
Quantity = item.Quantity,
|
||||||
|
Uses = itemData.Uses,
|
||||||
|
ModifiedDate = DateTime.Now,
|
||||||
|
Item = itemData
|
||||||
|
};
|
||||||
|
userItemData.Add(uid);
|
||||||
|
}
|
||||||
|
|
||||||
|
CommonInventoryData invData = new CommonInventoryData {
|
||||||
|
UserID = Guid.Parse(viking.UserId),
|
||||||
|
Item = userItemData.ToArray()
|
||||||
|
};
|
||||||
|
return Ok(invData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[Produces("application/xml")]
|
||||||
|
[Route("ContentWebService.asmx/SetCommonInventory")]
|
||||||
|
public IActionResult SetCommonInventory([FromForm] string apiToken, [FromForm] string commonInventoryRequestXml) {
|
||||||
|
CommonInventoryRequest[] request = XmlUtil.DeserializeXml<CommonInventoryRequest[]>(commonInventoryRequestXml);
|
||||||
|
Viking? viking = ctx.Sessions.FirstOrDefault(e => e.ApiToken == apiToken)?.Viking;
|
||||||
|
if (viking is null || viking.Inventory is null) return Ok();
|
||||||
|
|
||||||
|
// Set inventory items
|
||||||
|
List<CommonInventoryResponseItem> responseItems = new();
|
||||||
|
foreach (var req in request) {
|
||||||
|
InventoryItem? item = viking.Inventory.InventoryItems.FirstOrDefault(e => e.ItemId == req.ItemID);
|
||||||
|
if (item is null) item = new InventoryItem { ItemId = (int)req.ItemID, Quantity = 0 };
|
||||||
|
int origQuantity = item.Quantity;
|
||||||
|
item.Quantity = request[0].Quantity;
|
||||||
|
responseItems.Add(new CommonInventoryResponseItem {
|
||||||
|
CommonInventoryID = viking.InventoryId,
|
||||||
|
ItemID = item.ItemId,
|
||||||
|
Quantity = origQuantity
|
||||||
|
});
|
||||||
|
viking.Inventory.InventoryItems.Add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
CommonInventoryResponse response = new CommonInventoryResponse {
|
||||||
|
Success = true,
|
||||||
|
CommonInventoryIDs = responseItems.ToArray()
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.SaveChanges();
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Produces("application/xml")]
|
[Produces("application/xml")]
|
||||||
[Route("ContentWebService.asmx/GetAuthoritativeTime")]
|
[Route("ContentWebService.asmx/GetAuthoritativeTime")]
|
||||||
@ -174,6 +203,13 @@ public class ContentController : Controller {
|
|||||||
return Ok(new DateTime(DateTime.Now.Ticks));
|
return Ok(new DateTime(DateTime.Now.Ticks));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[Produces("application/xml")]
|
||||||
|
[Route("ItemStoreWebService.asmx/GetItem")] // NOTE: Should be in a separate controler, but it's inventory related, so I'll leave it here for now
|
||||||
|
public IActionResult GetItem([FromForm] int itemId) {
|
||||||
|
return Ok(itemService.GetItem(itemId));
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Produces("application/xml")]
|
[Produces("application/xml")]
|
||||||
[Route("V2/ContentWebService.asmx/SetAvatar")]
|
[Route("V2/ContentWebService.asmx/SetAvatar")]
|
||||||
|
@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using sodoff.Attributes;
|
using sodoff.Attributes;
|
||||||
using sodoff.Model;
|
using sodoff.Model;
|
||||||
using sodoff.Schema;
|
using sodoff.Schema;
|
||||||
|
using sodoff.Services;
|
||||||
using sodoff.Util;
|
using sodoff.Util;
|
||||||
|
|
||||||
namespace sodoff.Controllers.Common;
|
namespace sodoff.Controllers.Common;
|
||||||
@ -10,9 +11,11 @@ namespace sodoff.Controllers.Common;
|
|||||||
public class RegistrationController : Controller {
|
public class RegistrationController : Controller {
|
||||||
|
|
||||||
private readonly DBContext ctx;
|
private readonly DBContext ctx;
|
||||||
|
private ItemService itemService;
|
||||||
|
|
||||||
public RegistrationController(DBContext ctx) {
|
public RegistrationController(DBContext ctx, ItemService itemService) {
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
|
this.itemService = itemService;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
@ -79,10 +82,13 @@ public class RegistrationController : Controller {
|
|||||||
return Ok(new RegistrationResult { Status = MembershipUserStatus.DuplicateUserName });
|
return Ok(new RegistrationResult { Status = MembershipUserStatus.DuplicateUserName });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Inventory inv = new Inventory { InventoryItems = new List<InventoryItem>() };
|
||||||
|
inv.InventoryItems.Add(new InventoryItem { ItemId = 8977, Quantity = 1 }); // DragonStableINTDO - Dragons Dragon Stable
|
||||||
Viking v = new Viking {
|
Viking v = new Viking {
|
||||||
Id = Guid.NewGuid().ToString(),
|
Id = Guid.NewGuid().ToString(),
|
||||||
Name = data.ChildName,
|
Name = data.ChildName,
|
||||||
User = user,
|
User = user,
|
||||||
|
Inventory = inv
|
||||||
};
|
};
|
||||||
ctx.Vikings.Add(v);
|
ctx.Vikings.Add(v);
|
||||||
ctx.SaveChanges();
|
ctx.SaveChanges();
|
||||||
|
@ -10,6 +10,9 @@ public class DBContext : DbContext {
|
|||||||
public DbSet<Pair> Pairs { get; set; } = null!;
|
public DbSet<Pair> Pairs { get; set; } = null!;
|
||||||
public DbSet<PairData> PairData { get; set; } = null!;
|
public DbSet<PairData> PairData { get; set; } = null!;
|
||||||
public DbSet<TaskStatus> TaskStatuses { get; set; } = null!;
|
public DbSet<TaskStatus> TaskStatuses { get; set; } = null!;
|
||||||
|
public DbSet<Inventory> Inventories { get; set; } = null!;
|
||||||
|
public DbSet<InventoryItem> InventoryItems { get; set; } = null!;
|
||||||
|
|
||||||
public string DbPath { get; }
|
public string DbPath { get; }
|
||||||
|
|
||||||
public DBContext() {
|
public DBContext() {
|
||||||
@ -38,6 +41,10 @@ public class DBContext : DbContext {
|
|||||||
.WithMany(e => e.Vikings)
|
.WithMany(e => e.Vikings)
|
||||||
.HasForeignKey(e => e.UserId);
|
.HasForeignKey(e => e.UserId);
|
||||||
|
|
||||||
|
builder.Entity<Viking>().HasOne(v => v.Inventory)
|
||||||
|
.WithOne(e => e.Viking)
|
||||||
|
.HasForeignKey<Viking>(e => e.InventoryId);
|
||||||
|
|
||||||
builder.Entity<User>().HasMany(u => u.Vikings)
|
builder.Entity<User>().HasMany(u => u.Vikings)
|
||||||
.WithOne(e => e.User);
|
.WithOne(e => e.User);
|
||||||
|
|
||||||
@ -63,6 +70,9 @@ public class DBContext : DbContext {
|
|||||||
builder.Entity<Viking>().HasMany(u => u.Images)
|
builder.Entity<Viking>().HasMany(u => u.Images)
|
||||||
.WithOne(e => e.Viking);
|
.WithOne(e => e.Viking);
|
||||||
|
|
||||||
|
builder.Entity<Viking>().HasOne(v => v.Inventory)
|
||||||
|
.WithOne(e => e.Viking);
|
||||||
|
|
||||||
builder.Entity<PairData>()
|
builder.Entity<PairData>()
|
||||||
.HasKey(e => e.Id);
|
.HasKey(e => e.Id);
|
||||||
|
|
||||||
@ -80,5 +90,22 @@ public class DBContext : DbContext {
|
|||||||
builder.Entity<TaskStatus>()
|
builder.Entity<TaskStatus>()
|
||||||
.HasOne(t => t.Viking)
|
.HasOne(t => t.Viking)
|
||||||
.WithMany();
|
.WithMany();
|
||||||
|
|
||||||
|
builder.Entity<Inventory>().HasKey(e => e.Id);
|
||||||
|
|
||||||
|
builder.Entity<Inventory>()
|
||||||
|
.HasOne(i => i.Viking)
|
||||||
|
.WithOne(e => e.Inventory)
|
||||||
|
.HasForeignKey<Inventory>(e => e.VikingId);
|
||||||
|
|
||||||
|
builder.Entity<Inventory>()
|
||||||
|
.HasMany(i => i.InventoryItems)
|
||||||
|
.WithOne(e => e.Inventory);
|
||||||
|
|
||||||
|
builder.Entity<InventoryItem>()
|
||||||
|
.HasOne(e => e.Inventory)
|
||||||
|
.WithMany(e => e.InventoryItems)
|
||||||
|
.HasForeignKey(e => e.InventoryId);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
src/Model/Inventory.cs
Normal file
13
src/Model/Inventory.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace sodoff.Model;
|
||||||
|
public class Inventory {
|
||||||
|
[Key]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
public string VikingId { get; set; }
|
||||||
|
|
||||||
|
public virtual Viking? Viking { get; set; }
|
||||||
|
|
||||||
|
public virtual ICollection<InventoryItem> InventoryItems { get; set; } = null!;
|
||||||
|
}
|
16
src/Model/InventoryItem.cs
Normal file
16
src/Model/InventoryItem.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace sodoff.Model {
|
||||||
|
public class InventoryItem {
|
||||||
|
[Key]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
public int ItemId { get; set; }
|
||||||
|
|
||||||
|
public int InventoryId { get; set; }
|
||||||
|
|
||||||
|
public virtual Inventory Inventory { get; set; }
|
||||||
|
|
||||||
|
public int Quantity;
|
||||||
|
}
|
||||||
|
}
|
@ -20,4 +20,8 @@ public class Viking {
|
|||||||
public virtual ICollection<Dragon> Dragons { get; set; } = null!;
|
public virtual ICollection<Dragon> Dragons { get; set; } = null!;
|
||||||
public virtual ICollection<Image> Images { get; set; } = null!;
|
public virtual ICollection<Image> Images { get; set; } = null!;
|
||||||
public virtual Dragon? SelectedDragon { get; set; }
|
public virtual Dragon? SelectedDragon { get; set; }
|
||||||
|
|
||||||
|
public int InventoryId { get; set; }
|
||||||
|
|
||||||
|
public virtual Inventory Inventory { get; set; } = null!;
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ builder.Services.AddControllers(options => {
|
|||||||
});
|
});
|
||||||
builder.Services.AddDbContext<DBContext>();
|
builder.Services.AddDbContext<DBContext>();
|
||||||
builder.Services.AddScoped<KeyValueService>();
|
builder.Services.AddScoped<KeyValueService>();
|
||||||
|
builder.Services.AddSingleton<ItemService>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
647863
src/Resources/items.xml
Normal file
647863
src/Resources/items.xml
Normal file
File diff suppressed because it is too large
Load Diff
12
src/Schema/ServerItemArray.cs
Normal file
12
src/Schema/ServerItemArray.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System.Xml.Serialization;
|
||||||
|
|
||||||
|
namespace sodoff.Schema {
|
||||||
|
|
||||||
|
// NOTE: This schema is NOT used by the client
|
||||||
|
// This is a schema specific to the sodoff server
|
||||||
|
[XmlRoot(ElementName = "Items", Namespace = "")]
|
||||||
|
public class ServerItemArray {
|
||||||
|
[XmlElement(ElementName = "I", IsNullable = true)]
|
||||||
|
public ItemData[] ItemDataArray;
|
||||||
|
}
|
||||||
|
}
|
23
src/Services/ItemService.cs
Normal file
23
src/Services/ItemService.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
using sodoff.Schema;
|
||||||
|
using sodoff.Util;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Xml;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
|
namespace sodoff.Services {
|
||||||
|
public class ItemService {
|
||||||
|
|
||||||
|
Dictionary<int, ItemData> items = new();
|
||||||
|
|
||||||
|
public ItemService() {
|
||||||
|
ServerItemArray itemArray = XmlUtil.DeserializeXml<ServerItemArray>(XmlUtil.ReadResourceXmlString("items"));
|
||||||
|
foreach (var item in itemArray.ItemDataArray) {
|
||||||
|
items.Add(item.ItemID, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemData GetItem(int id) {
|
||||||
|
return items[id];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<None Remove="Resources\items.xml" />
|
||||||
<None Remove="Resources\store.xml" />
|
<None Remove="Resources\store.xml" />
|
||||||
<None Remove="Resources\allranks.xml" />
|
<None Remove="Resources\allranks.xml" />
|
||||||
<None Remove="Resources\tutorialmission.xml" />
|
<None Remove="Resources\tutorialmission.xml" />
|
||||||
@ -23,6 +24,9 @@
|
|||||||
<None Update="Resources\store.xml">
|
<None Update="Resources\store.xml">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="Resources\items.xml">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
<None Update="Resources\tutorialmission.xml">
|
<None Update="Resources\tutorialmission.xml">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
@ -33,6 +37,9 @@
|
|||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
<EmbeddedResource Include="Resources\allranks.xml">
|
<EmbeddedResource Include="Resources\allranks.xml">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</EmbeddedResource>
|
||||||
|
<EmbeddedResource Include="Resources\items.xml">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
<EmbeddedResource Include="Resources\tutorialmission.xml">
|
<EmbeddedResource Include="Resources\tutorialmission.xml">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user