Series.cs 3.1 KB
Newer Older
yqian1000's avatar
yqian1000 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public class Series
{
    public string Name;
    public string Url;
    public int Version;
    string SeriesPath
    {
        get { return $"{CUtil.DataSavePath}data/{Name.Replace("/", "")}/"; }
    }
    public List<string> BoxLst = new List<string>();
    public List<string> Tags = new List<string>();
    public List<string> Rare = new List<string>();
    Dictionary<string, Card> Cards = new Dictionary<string, Card>();

    public Dictionary<string, Card> GetCards()
    {
        return Cards;
    }

    public Series(string name, string url, int time)
    {
        Name = name;
        Url = url;
        Version = time;
    }

    public Series(string name, string url)
    {
        Name = name;
        Url = url;
        Version = 0;
    }

    public void SetVersion(int version)
    {
        Version = version;
    }

    public void AddCard(Card card)
    {
        Cards[card.Num] = card;
        if (!BoxLst.Contains(card.Box))
            BoxLst.Add(card.Box);
        for(int i = 0; i < card.Tag.Length; i++)
        {
            if (!Tags.Contains(card.Tag[i]))
                Tags.Add(card.Tag[i]);
        }
        if (!Rare.Contains(card.Rare))
            Rare.Add(card.Rare);
    }



    public string ToJson()
    {
        string json = $"\"Name\":\"{Name}\", \"Url\":\"{Url}\", \"Version\":{Version}";
        return "{" + json + "},";
    }

    string ToCardLstJson()
    {
        string json = "";
        foreach (var item in Cards)
        {
            json += item.Value.ToJson() + ",\n";
        }
        json = json.Remove(json.LastIndexOf(","));
        return $"[\n{json}\n]";
    }

    public void SaveCardLst()
    {
        if (!Directory.Exists($"{SeriesPath}"))
            Directory.CreateDirectory($"{SeriesPath}");
        string path = $"{SeriesPath}/Cards.json";
        string json = ToCardLstJson();
        FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
        StreamWriter sw = new StreamWriter(fs);
        sw.Write(json);
        sw.Close();
        fs.Close();
    }

    /// <summary>
    /// 加载卡片列表
    /// </summary>
    public void LoadCardsFromJson()
    {
        if (!Directory.Exists($"{SeriesPath}"))
            Directory.CreateDirectory($"{SeriesPath}");
        string path = $"{SeriesPath}/Cards.json";
        string json = null;
        if (!File.Exists(path))
        {
            //尝试读取本地的
yqian1000's avatar
yqian1000 已提交
105
            TextAsset tx = Resources.Load<TextAsset>($"data/{Name.Replace("/", "")}/Cards");
yqian1000's avatar
yqian1000 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            if (tx == null)
                return;
            else
                json = tx.text;
        }
        else
            //读取存档
            json = File.ReadAllText(path);
        LoadFromJsonText(json);
    }

    public void LoadFromJsonText(string json)
    {
        JsonData data = JsonMapper.ToObject(json);
        for (int i = 0; i < data.Count; i++)
        {
            Card card = new Card(data[i]);
yqian1000's avatar
yqian1000 已提交
123
            //card.CheckImgFile();
yqian1000's avatar
yqian1000 已提交
124 125 126 127
            AddCard(card);
        }
    }
}