본문 바로가기
NOTE/SaveSource

[SaveSource] StageRankManager

by DevAthena 2017. 1. 20.

스테이지별 랭킹관리, Json 파일 입출력 

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using System.IO;
 
public enum StageType
{
    Cpt,
    Desert,
    Desert2,
    Ruin,
    Ruin2,
    Hope,
    Hope2,
    None
}
 
[System.Serializable]
public class ClearData
{
    public string stage;
    public int score;
    public float time;
 
    public ClearData(string stage, int score, float time)
    {
        this.stage = stage;
        this.score = score;
        this.time = time;
    }
}
 
[System.Serializable]
public class ClearDataList
{
    public ClearData[] ranks;   
}
 
public class StageRankManager : SingleTon<StageRankManager>
{
    private StageType           m_stageType = StageType.None;
    private List<ClearData>     m_ranksAll = new List<ClearData>();
    private string              m_jsonData = null;
    private ClearDataList       m_ranksToArray = new ClearDataList();
 
 
    public StageType CurrentStage
    {
        get
        {
            SetStage();
            return m_stageType;
        }
    }
    private StageRankManager()
    {
 
    }
    private int CompareClass(ClearData c1, ClearData c2)
    {
        if (c1.score < c2.score) return 1;
        if (c1.score > c2.score) return -1;
        return 0;
    }
 
    /// <summary>
    /// json에 저장된(될) string형 값을 리턴 / 
    /// 스테이지 추가시 내용도 추가해야함 (지혜 01.19)
    /// </summary>
    /// <param name="stage">받은 stageType에 대한 string을 리턴받음</param>
    public string SetStage(StageType stage = StageType.None)
    {
        string m_sceneName = SceneManager.GetActiveScene().name;
 
        if (m_sceneName.Equals("Cpt_Stage"|| stage == StageType.Cpt)
        {
            m_stageType = StageType.Cpt;
            return "cpt";
        }
        else if (m_sceneName.Equals("Operation_Restore_Hope"|| stage == StageType.Hope)
        {
            m_stageType = StageType.Hope;
            return "hope";
        }
        else if (m_sceneName.Equals("Operation_Restore_Hope_Night"|| stage == StageType.Hope2)
        {
            m_stageType = StageType.Hope2;
            return "hope2";
        }
        else if (m_sceneName.Equals("DesertStorm_1"|| stage == StageType.Desert)
        {
            m_stageType = StageType.Desert;
            return "desert";
        }
        else if (m_sceneName.Equals("DesertStorm_Night"|| stage == StageType.Desert2)
        {
            m_stageType = StageType.Desert2;
            return "desert2";
        }
        else if(m_sceneName.Equals("Ruin"|| stage == StageType.Ruin)
        {
            m_stageType = StageType.Ruin;
            return "ruin";
        }
        else if (m_sceneName.Equals("Ruin_Night"|| stage == StageType.Ruin2)
        {
            m_stageType = StageType.Ruin2;
            return "ruin2";
        }
        else
        {
            m_stageType = StageType.None;
            return "none";
        }
    }
 
    ///<summary>
    ///게임 시작하고서 로비들어갈 때 한 번만 호출 (지혜 01.19)
    ///</summary>
    public void ReadAllRanking()
    {
        if(File.Exists(Application.dataPath + "/Resources/Rank.json"))
        {
            string m_jsonRead = File.ReadAllText(Application.dataPath + "/Resources/Rank.json");
            if (string.IsNullOrEmpty(m_jsonRead) == false)
            {
                m_ranksToArray = JsonUtility.FromJson<ClearDataList>(m_jsonRead);
                for (int i = 0; i < m_ranksToArray.ranks.Length; i++)
                {
                    ClearData clearData = new ClearData(m_ranksToArray.ranks[i].stage, m_ranksToArray.ranks[i].score, m_ranksToArray.ranks[i].time);
                    m_ranksAll.Add(clearData);
                }
            }
        }        
    }
 
    /// <summary>
    /// 한 스테이지 클리어 후 호출 (지혜 01.19)
    /// </summary>
    /// <param name="m_score">클리어 점수</param>
    /// <param name="m_time">클리어 시간</param>
    public void AddClearData(int m_score, float m_time)
    {
        ClearData clearData = new ClearData(SetStage(), m_score, m_time);
        m_ranksAll.Add(clearData);
 
        //Add된 데이터와 합산해서 Json 파일에 덮어쓰기
        m_ranksToArray.ranks = m_ranksAll.ToArray();
        m_jsonData = JsonUtility.ToJson(m_ranksToArray);
        File.WriteAllText(Application.dataPath + "/Resources/Rank.json", m_jsonData);
    }
 
    /// <summary>
    /// 클리어한 스테이지의 랭킹 (지혜 01.19)
    /// </summary>
    /// <param name="m_stageType">스테이지 종류</param>
    /// <param name="m_score">자신의 점수</param>
    /// <param name="m_time">자신의 시간</param>
    /// <returns></returns>
    public List<ClearData> GetStageRanking(string m_stageType, int m_score = 0float m_time = 0)
    {
        List<ClearData> m_rankList = new List<ClearData>();
        
        for(int i = 0; i < m_ranksAll.Count; i++)
        {
            if(m_ranksAll[i].stage == m_stageType)
            {
                m_rankList.Add(new ClearData(m_ranksAll[i].stage, m_ranksAll[i].score, m_ranksAll[i].time));
            }
        }
 
        m_rankList.Sort(CompareClass); // 보내기전에 점수별로 진짜 랭킹화
        return m_rankList;
    }
 
 
 
 
 
}
 
 
 
 
 
cs


'NOTE > SaveSource' 카테고리의 다른 글

[SaveSource] TutorialManager  (0) 2017.02.08
[Save Source] UIResult  (0) 2017.01.26
[SaveSource] CameraOrbit  (0) 2017.01.12
[SaveSource] WaveMoveTrigger  (0) 2017.01.12
[SaveSource] SpeicalWeapon  (0) 2017.01.12