[MMO 컨텐츠 구현] 2-4. 서버 연동: Data

scarleter99·2023년 11월 14일

Server

Config Manager

  • 데이터를 저장하고 있는 파일의 위치를 로드/관리한다.

📄 ConfigManager.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Server.Data
{
	[Serializable]
	public class ServerConfig
	{
		public string dataPath;
	}

	public class ConfigManager
	{
        // Singleton
        public static ServerConfig Config { get; private set; }

        // Config 데이터 로드 후 ServerConfig로 변환
        public static void LoadConfig()
		{
			string text = File.ReadAllText("config.json");
			Config = Newtonsoft.Json.JsonConvert.DeserializeObject<ServerConfig>(text);
		}
	}
}

📄 config.json

{
  "dataPath": "../../../../../Client/Assets/Resources/Data"
}

Data Manager

  • 데이터 로드 후 Dict 형태로 관리한다.

📄 DataManager.cs

using Google.Protobuf.Protocol;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Server.Data
{
    public interface ILoader<Key, Value>
    {
        Dictionary<Key, Value> MakeDict();
    }

    public class DataManager
    {
        // Singleton
        public static Dictionary<int, StatInfo> StatDict { get; private set; } = new Dictionary<int, StatInfo>();
        public static Dictionary<int, Data.Skill> SkillDict { get; private set; } = new Dictionary<int, Data.Skill>();

		// JSON 데이터를 Dict로 변환
        public static void LoadData()
        {
            StatDict = LoadJson<Data.StatData, int, StatInfo>("StatData").MakeDict();
            SkillDict = LoadJson<Data.SkillData, int, Data.Skill>("SkillData").MakeDict();
        }

        static Loader LoadJson<Loader, Key, Value>(string path) where Loader : ILoader<Key, Value>
        {
            string text = File.ReadAllText($"{ConfigManager.Config.dataPath}/{path}.json");
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Loader>(text);
        }
    }
}


Data.Contents

  • 데이터를 객체로 관리하기 위한 class를 제공한다.
  • 데이터를 List로 관리하며 Dict로 변환하는 함수를 제공한다.

📄 Data.Contents.cs

using Google.Protobuf.Protocol;
using System;
using System.Collections.Generic;
using System.Text;

namespace Server.Data
{
    #region Stat
    [Serializable]
    public class StatData : ILoader<int, StatInfo>
    {
        public List<StatInfo> stats = new List<StatInfo>();

        public Dictionary<int, StatInfo> MakeDict()
        {
            Dictionary<int, StatInfo> dict = new Dictionary<int, StatInfo>();
            foreach (StatInfo stat in stats)
            {
                stat.Hp = stat.MaxHp;
                dict.Add(stat.Level, stat);
            }
            return dict;
        }
    }
    #endregion

    #region Skill
    [Serializable]
    public class Skill
    {
        public int id;
        public string name;
        public float cooldown;
        public int damage;
        public SkillType skillType;
        public ProjectileInfo projectile;
    }

    public class ProjectileInfo
    {
        public string name;
        public float speed;
        public int range;
        public string prefab;
    }

    [Serializable]
    public class SkillData : ILoader<int, Skill>
    {
        public List<Skill> skills = new List<Skill>();

        public Dictionary<int, Skill> MakeDict()
        {
            Dictionary<int, Skill> dict = new Dictionary<int, Skill>();
            foreach (Skill skill in skills)
                dict.Add(skill.id, skill);
            return dict;
        }
    }
    #endregion
}

Client

Data.Contents

  • 데이터를 객체로 관리하기 위한 class를 제공한다.
  • 데이터를 List로 관리하며 Dict로 변환하는 함수를 제공한다.

📄 Data.Contents.cs

using Google.Protobuf.Protocol;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Data
{
    #region Skill
    [Serializable]
    public class Skill
    {
        public int id;
        public string name;
        public float cooldown;
        public int damage;
        public SkillType skillType;
        public ProjectileInfo projectile;
    }

    public class ProjectileInfo
    {
        public string name;
        public float speed;
        public int range;
        public string prefab;
    }

    [Serializable]
    public class SkillData : ILoader<int, Skill>
    {
        public List<Skill> skills = new List<Skill>();

        public Dictionary<int, Skill> MakeDict()
        {
            Dictionary<int, Skill> dict = new Dictionary<int, Skill>();
            foreach (Skill skill in skills)
                dict.Add(skill.id, skill);
            return dict;
        }
    }
    #endregion
}

📄 StatData.json

{
  "stats": [
    {
      "level": "1",
      "maxHp": "200",
      "attack": "20",
      "speed": "10.0",
      "totalExp": "0"
    },
    {
      "level": "2",
      "maxHp": "250",
      "attack": "25",
      "speed": "10.0",
      "totalExp": "10"
    },
    {
      "level": "3",
      "maxHp": "300",
      "attack": "30",
      "speed": "10.0",
      "totalExp": "20"
    }
  ]
}

📄 SkillData.json

{
    "skills": [
        {
            "id": "1",
            "name": "평타",
            "cooldown": "0.5",
            "damage": "10",
            "skillType": "SkillAuto"
        },
        {
            "id": "2",
            "name": "화살 공격",
            "cooldown": "0.5",
            "damage": "5",
            "skillType": "SkillProjectile",
            "projectile": {
                "name": "화살",
                "speed": "20.0",
                "range": "10",
                "prefab": "Creature/Arrow"
            }
        }
    ]
} 

0개의 댓글