본문 바로가기
c#/수업내용

2020.04.24 수업내용 업적과 % 출력

by Luna_O 2020. 4. 27.

업적 이미지

실행 코드

classApp

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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace study_016
{
    class App
    {
        Dictionary<int, AchievementData> dicAchievementDatas;
        Dictionary<int, AchievementReward> dicAchievementRewards;
        Dictionary<int, AchievementInfo> achievementInfo;
 
        public App()
        {
            string dataPath = "./Achievements_data.json";
            string dataJson = File.ReadAllText(dataPath);
 
            string rewardPath = "./Achievements_reward.json";
            string rewardJson = File.ReadAllText(rewardPath);
 
            AchievementData[] arrAchievementDatas = JsonConvert.DeserializeObject<AchievementData[]>(dataJson);
 
            AchievementReward[] arrAchievementRewards = JsonConvert.DeserializeObject<AchievementReward[]>(rewardJson);
 
            dicAchievementDatas = new Dictionary<int, AchievementData>();
            dicAchievementRewards = new Dictionary<int, AchievementReward>();
 
            foreach (AchievementReward reward in arrAchievementRewards)
            {
                this.dicAchievementRewards.Add(reward.id, reward);
            }
 
            foreach (AchievementData data in arrAchievementDatas)
            {
                this.dicAchievementDatas.Add(data.id, data);
            }
 
            //foreach (KeyValuePair<int, AchievementData> arrdata in this.dicAchievementDatas)
            //{
            //    AchievementData data = arrdata.Value;
            //    string format = string.Format(data.desc, data.goal);
            //    Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7}",
            //}
            //foreach (KeyValuePair<int, AchievementReward> arrReward in this.dicAchievementRewards)
            //{
            //    AchievementReward reward = arrReward.Value;
            //    Console.WriteLine("{0} {1} {2} {3}",
            //        reward.id, reward.name,reward.type, reward.icon_name);
            //}
 
            achievementInfo = new Dictionary<int, AchievementInfo>();
            achievementInfo.Add(this.dicAchievementDatas[1000].id, new AchievementInfo(this.dicAchievementDatas[1000].id, 40000));
            achievementInfo.Add(this.dicAchievementDatas[1001].id, new AchievementInfo(this.dicAchievementDatas[1001].id, 72456));
            achievementInfo.Add(this.dicAchievementDatas[1002].id, new AchievementInfo(this.dicAchievementDatas[1002].id, 14084));
            achievementInfo.Add(this.dicAchievementDatas[1003].id, new AchievementInfo(this.dicAchievementDatas[1003].id, 45));
            this.PrintAchievement(1000);
            this.PrintAchievement(1001);
            this.PrintAchievement(1002);
            this.PrintAchievement(1003);
 
        }
 
        public void PrintAchievement(int id)
        {
            AchievementData data = this.dicAchievementDatas[id];
            int sum = achievementInfo[id].count * 100 / data.goal;
 
            if (sum >= 100)
            {
                sum = 100;
                string desc = string.Format(data.desc, data.goal);
                Console.WriteLine("name : {0}, desc : {1} {2}% ({3}/{4}) "data.name, desc, sum, achievementInfo[id].count, data.goal);
            }
            else
            {
                string format = string.Format("{0:f2}%", sum);
                string desc = string.Format(data.desc, data.goal);
                Console.WriteLine("name : {0}, desc : {1} {2}% ({3}/{4}) "data.name, desc, sum, achievementInfo[id].count, data.goal);
            }
 
        }
    }
}
 
cs

class AchievementData

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace study_016
{
    class AchievementData
    {
        public int id;
        public string name;
        public string desc;
        public int goal;
        public int reward1_id;
        public int reward1_amount;
        public int reward2_id;
        public int reward2_amount;
 
        public AchievementData()
        {
 
        }
    }
}
 
cs

class AchievementReward

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace study_016
{
    class AchievementReward
    {
        public int id;
        public string name;
        public int type;
        public string icon_name;
 
    }
}
 
 
cs

class AchievementInfo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace study_016
{
    class AchievementInfo
    {
        public int id;
        public int count;
 
        public AchievementInfo(int id, int count)
        {
            this.id = id;
            this.count = count;
        }
    }
}
 
cs

실행 창