[NR-CORE] Json shop system.

Posts 1–9 of 9 · Page 1 of 1
[NR-CORE] Json shop system.
Replace MerchantsList.cs with this.
Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using common;
using common.resources;
using log4net;
using Newtonsoft.Json;
using wServer.realm.terrain;

namespace wServer.realm.entities.vendors
{
    public class ShopItem : ISellableItem
    {
        public ushort ItemId { get; private set; }
        public int Price { get; }
        public int Count { get; }
        public string Name { get; }

        public ShopItem(string name, ushort price, int count = -1)
        {
            ItemId = ushort.MaxValue;
            Price = price;
            Count = count;
            Name = name;
        }

        public void SetItem(ushort item)
        {
            if (ItemId != ushort.MaxValue)
                throw new AccessViolationException("Can't change item after it has been set.");

            ItemId = item;
        }
    }
    
    internal static class MerchantLists
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(MerchantLists));
        
        public static readonly Dictionary<TileRegion, Tuple<List<ISellableItem>, CurrencyType, /*
            Req*/int>> Shops = new Dictionary<TileRegion, Tuple<List<ISellableItem>, CurrencyType, int>>();
        
        public static void Init(RealmManager manager)
        {
            var json = JsonConvert.DeserializeObject<List<ShopSettings>>(File.ReadAllText("shop.json"));

            foreach (var item in json)
            {
                var list = new List<ISellableItem>();
                var currency = (CurrencyType)Enum.Parse(typeof(CurrencyType), item.Currency);
                var region = (TileRegion)Enum.Parse(typeof(TileRegion), item.ShopId);

                list.Add(new ShopItem(item.Item, item.Price));
                Shops.Add(region, new Tuple<List<ISellableItem>, CurrencyType, int>(list, currency, item.Price));
            }

            foreach (var shop in Shops)
                foreach (var shopItem in shop.Value.Item1.OfType<ShopItem>())
                {
                    ushort id;
                    if (!manager.Resources.GameData.IdToObjectType.TryGetValue(shopItem.Name, out id))
                        Log.WarnFormat("Item name: {0}, not found.", shopItem.Name);
                    else
                        shopItem.SetItem(id);
                }
        }
        public partial class ShopSettings
        {
            public string Currency { get; set; }
            public string Item { get; set; }
            public ushort Price { get; set; }
            [JsonProperty("Shop_Id")]
            public string ShopId { get; set; }
        }
    }
}
And all you need to add now is a file in to bin called shop it needs to be json... This is how you would structure your shops.

Code:
[
    {
        "Currency": "Fame",
        "Item": "Sword of Acclaim",
        "Price": 100,
        "Shop_Id": "Store_1"
    },
    {
        "Currency": "Fame",
        "Item": "Demon Blade",
        "Price": 100,
        "Shop_Id": "Store_2"
    }
]
@MikeRaarupBirk it's up now bitch
Nice, but the current system is already very simplified, but good job anyways
Quote Originally Posted by DevilRotMG View Post
Nice, but the current system is already very simplified, but good job anyways
This doesnt require a rebuild tho, this you can only open the WServer up and changes will be made, even make a command to reload the shops.
Quote Originally Posted by SeXZeFPS View Post
This doesnt require a rebuild tho, this you can only open the WServer up and changes will be made, even make a command to reload the shops.
Oh true, I didn't realise that. But doesn't that also mean that it would be fetching data outside the system, and maybe cause increased lag?

(Sort of like Remote Textures)
Quote Originally Posted by DevilRotMG View Post
Oh true, I didn't realise that. But doesn't that also mean that it would be fetching data outside the system, and maybe cause increased lag?

(Sort of like Remote Textures)
What? Did u read the code i posted?
This is pretty neat, but the shops aren't structured properly. It would be a lot better if the name of the store was a block you would then add the items into, like how it is in server sources.

Example:
Code:
[
    {
        "StoreId": "Store_1",
        {
            "Item": "Sword of Acclaim", "Currency": "Fame", "Price": 500,
            "Item": "Archon Sword", "Currency": "Fame", "Price": 100
        }
    },
    {
        "StoreId": "Store_2",
        {
            "Item": "Potion of Defense", "Currency": "Gold", "Price": 50,
            "Item": "Potion of Wisdom", "Currency": "Gold", "Price": 15
        }
    }
]
Quote Originally Posted by Invader_Zim View Post
This is pretty neat, but the shops aren't structured properly. It would be a lot better if the name of the store was a block you would then add the items into, like how it is in server sources.

Example:
Code:
[
    {
        "StoreId": "Store_1",
        {
            "Item": "Sword of Acclaim", "Currency": "Fame", "Price": 500,
            "Item": "Archon Sword", "Currency": "Fame", "Price": 100
        }
    },
    {
        "StoreId": "Store_2",
        {
            "Item": "Potion of Defense", "Currency": "Gold", "Price": 50,
            "Item": "Potion of Wisdom", "Currency": "Gold", "Price": 15
        }
    }
]
My xml system has this
Code:
  
<Shops>
  <Shop currency="Fame" region="Store_2">
    <Item id="Sword of Splendor" price="100"/>
    <Item id="Helm of the Jack-o'-naut" price="100"/>
    <Item id="Breastplate of New Life" price="100"/>
    <Item id="Test Ring" price="100"/>
    <Item id="Sword of Splendor" price="100"/>
    <Item id="Shield of Ogmur" price="100"/>
    <Item id="Abyssal Armor" price="100"/>
    <Item id="Test Ring" price="100"/>
    <Item id="Coral Bow" price="100"/>
  </Shop>
</Shops>
Quote Originally Posted by SeXZeFPS View Post
My xml system has this
Code:
  
<Shops>
  <Shop currency="Fame" region="Store_2">
    <Item id="Sword of Splendor" price="100"/>
    <Item id="Helm of the Jack-o'-naut" price="100"/>
    <Item id="Breastplate of New Life" price="100"/>
    <Item id="Test Ring" price="100"/>
    <Item id="Sword of Splendor" price="100"/>
    <Item id="Shield of Ogmur" price="100"/>
    <Item id="Abyssal Armor" price="100"/>
    <Item id="Test Ring" price="100"/>
    <Item id="Coral Bow" price="100"/>
  </Shop>
</Shops>
So how do you set currency with that, or are they all just default fame. (I mean if you want for example test ring to be gold, do you just make a new <Shop> with the same region)?
Quote Originally Posted by DevilRotMG View Post
So how do you set currency with that, or are they all just default fame. (I mean if you want for example test ring to be gold, do you just make a new <Shop> with the same region)?
Haven't really tried but i dont see the reason why it should'nt work.
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us