Your serverless inventory that actually works
firebase deploy
AddItem
await Nventory.AddItem("legendary-sword", new {
Attack = 10,
Speed = 5
});
Since we have generalized the interfaces to fit most of games, there're no unique items in Nventory.
Every item has its quantity and can be counted more than 2.
We don't have native support for it, you should implement your own stuffs.
Good news, it's pretty simple.
Unique equipments
Think about equipments, they cannot be stacked in most cases even if they have excatly same options.
GUID
would be the best practice for this case.
Even if you add 2 swords, they never be stacked.
var key = $"sword.{(new Guid()).ToString()}";
await Nventory.AddItem(key, sword);
Consumables with variants
In some cases, consumables also have options.
Please see example below:
{
"grade": "good",
"heal" : 10
},
{
"grade": "poor",
"heal" : 5
}
Each items can be stacked with same options.
You may carry 2 good bananas, 3 poor bananas and so on.
In this case, just use GetHashCode
method.
var key = $"banana.{banana.GetHashCode()}";
await Nventory.AddItem(key, banana);
Consumables without variantes
Nothing to do!
UpdateItem
Creates or Replaces item with given data.
(Always overwrites the item include quantity
.)
await Nventory.UpdateItem("banana", banana);
ConsumeItem
await Nventory.ConsumeItem("banana", 1);
GetItem
await Nventory.GetItem("banana");
await Nventory.GetAllItems();
You can implement period-specific items in few lines.
// Add subscription
await Nventory.AddItem(
"one-year-premium",
new {},
Nventory.OneYear);
// Check premium status
var item = await Nventory.GetItem("one-year-premium");
if (item == null)
Console.WriteLine("Your are not a premium member");
else
Console.WriteLine("Welcome customer!");
It's okay, most of singleplay games doesn't care about manipulations.
Just use Nventory for online storage. All items will be avaliable on any devices.
You should not use AddItem or UpdateItem on client. Items are always issued by server.
ConsumeItem is safe, it's always decreasing the quantity.
Most important part is firestore rules.
Hackers still have write access to your databases unless you blocked it.
{
// firestore rules goes here
}