업적 - Dictionary/File Read Write
2020. 4. 22. 14:45ㆍC#/수업내용
<App.cs>
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace Study_013
{
class App
{
// Dic 객체 선언
Dictionary<int, AchievementData> dicAchievementDatas;
Dictionary<int, AchievementInfo> dicAchievementInfos;
public App()
{
// Dic 객체 생성
this.dicAchievementDatas = new Dictionary<int, AchievementData>();
this.dicAchievementInfos = new Dictionary<int, AchievementInfo>();
// 테이블 데이터 Read
// 역직렬화 (String -> 배열객체)
AchievementData[] arrAchievementDatas = JsonConvert.DeserializeObject<AchievementData[]>(dataJson);
// Dic 객체에 역직렬화한 배열객체 할당 (옮기기)
foreach(AchievementData data in arrAchievementDatas)
{
}
// Info 데이터 Read
// 변화 가능한 데이터이므로 데이터 변경 여부를 조건 분할
if (File.Exists(infoPath)) // 기존 유저
{
string InfoJson = File.ReadAllText(infoPath);
AchievementInfo[] arrAchievementInfos = JsonConvert.DeserializeObject<AchievementInfo[]>(InfoJson);
foreach(AchievementInfo info in arrAchievementInfos)
{
}
}
else // 신규 유저
{
foreach(KeyValuePair<int, AchievementData> pair in this.dicAchievementDatas)
{
AchievementData data = pair.Value;
}
this.SaveAchievements();
}
PrintAchievement(1000);
DoAchievement(1000);
SaveAchievements();
}
public void PrintAchievement(int id)
{
AchievementData data = this.dicAchievementDatas[id];
AchievementInfo info = this.dicAchievementInfos[id];
Console.WriteLine($"id:{data.id}, name:{data.name}, goal:({info.count}/{data.goal}), reward:({data.reward}), desc:{data.desc}");
}
public void DoAchievement(int id)
{
AchievementData data = this.dicAchievementDatas[id];
AchievementInfo info = this.dicAchievementInfos[id];
}
public void SaveAchievements()
{
AchievementInfo[] arrAchievementInfos = new AchievementInfo[length];
int idx = 0;
foreach(KeyValuePair<int, AchievementInfo> pair in this.dicAchievementInfos)
{
arrAchievementInfos[idx] = pair.Value;
idx++;
}
string infoJson = JsonConvert.SerializeObject(arrAchievementInfos);
File.WriteAllText(path, infoJson);
}
}
}
|
<AchievementData.cs> - 변경되면 안되는 테이블 데이터
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_013
{
class AchievementData
{
public int id;
public string name;
public int goal;
public int reward;
public string desc;
}
}
|
<AchievementInfo.cs> - 변경될 수 있는 데이터
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;
namespace Study_013
{
class AchievementInfo
{
public int id;
public int count;
public AchievementInfo(int id, int count)
{
this.id = id;
this.count = count;
}
}
}
|
'C# > 수업내용' 카테고리의 다른 글
업적 달성 - #Dictionary #Json #Table (0) | 2020.04.24 |
---|---|
Json을 이용하여 Excel 파일을 역직렬화 하기 (0) | 2020.04.21 |
배열 - 캐릭터의 무기 습득 및 장착 (0) | 2020.04.20 |
배열의 복사(Deep/Shallow) (0) | 2020.04.16 |
스타크래프트 - 배럭 (0) | 2020.04.14 |