-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerDataMgr.cs
251 lines (208 loc) · 7.62 KB
/
PlayerDataMgr.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
public class LevelRecord
{
public LevelRecord(int id)
{
LevelID = id;
Passed = false;
Times = 0;
HighestStar = 0;
if (id == 1)
Locked = false;
else
Locked = true;
}
public int LevelID{get;set;}
public bool Passed { get; set; }
public int Times{get;set;}
public int HighestStar { get; set; }
public bool Locked { get; set; }
}
public class PlayerDataMgr
{
private static PlayerDataMgr instance;
private static object syncRoot = new Object();
private static PlayerInfo player = new PlayerInfo();
private Dictionary<SlotType,Dictionary<int, Slot>> playerBags = new Dictionary<SlotType,Dictionary<int, Slot>>();
private List<int> playerFinishedChapters = new List<int>();
private Dictionary<int, LevelRecord> playerLevelRecords = new Dictionary<int, LevelRecord>();
private PlayerDataMgr() { }
public static PlayerDataMgr Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if(instance == null)
instance = new PlayerDataMgr();
}
}
return instance;
}
}
public void Init()
{
InitPlayerBag();
InitPlayerLevels();
}
public PlayerInfo GetPlayer()
{
return player;
}
public Dictionary<int, LevelRecord> GetPlayerLevelRecords()
{
return playerLevelRecords;
}
public Dictionary<int, Slot> GetPlayerBag(SlotType type)
{
return playerBags[type];
}
#region Chapter and Level
private void InitPlayerLevels()
{
foreach(int i in DBConfigMgr.Instance.MapLevel.Keys)
{
playerLevelRecords.Add(i, new LevelRecord(i));
}
}
public bool IsThisChapterPlayerFinished(int chapterID)
{
return playerFinishedChapters.Exists(x => x == chapterID);
}
public void PlayerFinishedChapter(int chapterID)
{
if(!IsThisChapterPlayerFinished(chapterID))
playerFinishedChapters.Add(chapterID);
}
public void PlayerFinishedLevel(int levelID, int star)
{
if (playerLevelRecords.ContainsKey(levelID))
{
playerLevelRecords[levelID].Passed = true;
playerLevelRecords[levelID].Times += 1;
playerLevelRecords[DBConfigMgr.Instance.MapLevel[levelID].UnlockNextLevelID].Locked = false;
if (playerLevelRecords[levelID].HighestStar < star)
playerLevelRecords[levelID].HighestStar = star;
}
else
{
LevelRecord record = new LevelRecord(levelID);
record.LevelID = levelID;
record.Passed = true;
record.Times = 1;
record.HighestStar = star;
}
}
public int GetPlayerLevelTodayTimes(int id)
{
if (playerLevelRecords.ContainsKey(id))
return playerLevelRecords[id].Times;
else
return 0;
}
#endregion
#region 背包和仓库
private void AddInitialGeneralForPlayer()
{
AddGeneral(1);
AddGeneral(44);
AddGeneral(45);
AddGeneral(46);
AddGeneral(47);
AddGeneral(65);
// 一开始都要上阵
MakeGeneralOnBattle(1, FormationPosition.B1);
MakeGeneralOnBattle(2, FormationPosition.B2);
MakeGeneralOnBattle(3, FormationPosition.B3);
MakeGeneralOnBattle(4, FormationPosition.C1);
MakeGeneralOnBattle(5, FormationPosition.C2);
MakeGeneralOnBattle(6, FormationPosition.C3);
}
private void InitPlayerBag()
{
playerBags.Add(SlotType.SlotType_General, new Dictionary<int, Slot>());
playerBags.Add(SlotType.SlotType_Soldier, new Dictionary<int, Slot>());
playerBags.Add(SlotType.SlotType_Armor, new Dictionary<int, Slot>());
playerBags.Add(SlotType.SlotType_Item, new Dictionary<int, Slot>());
playerBags.Add(SlotType.SlotType_SoldierMaterial, new Dictionary<int, Slot>());
playerBags.Add(SlotType.SlotType_ArmorMaterial, new Dictionary<int, Slot>());
AddInitialGeneralForPlayer();
}
public void AddGeneral(int id)
{
// Find index
int index = 1;
if (playerBags[SlotType.SlotType_General].Count > 0)
{
index = playerBags[SlotType.SlotType_General].Keys.Max() + 1;
}
SlotGeneral s = new SlotGeneral(SlotType.SlotType_General, index, id);
s.FormationPosition = 0;
playerBags[SlotType.SlotType_General].Add(s.Index, s);
// Add default soldier for general
AddSoldier(DBConfigMgr.Instance.MapGeneral[id].InitialSoldier, s.Index);
}
public void AddSoldier(int id,int generalIndex)
{
int index = 1;
if (playerBags[SlotType.SlotType_Soldier].Count > 0)
{
index = playerBags[SlotType.SlotType_Soldier].Keys.Max() + 1;
}
SlotSoldier s = new SlotSoldier(SlotType.SlotType_Soldier, index, id);
s.ExtraData = generalIndex;
playerBags[SlotType.SlotType_Soldier].Add(s.Index, s);
}
#endregion
#region 阵型
private void MakeGeneralOnBattle(int slotIndex, FormationPosition formationPosition)
{
SlotGeneral slot = (SlotGeneral)playerBags[SlotType.SlotType_General][slotIndex];
slot.FormationPosition = formationPosition;
slot.ExtraData = (int)formationPosition;
}
private void MakeGeneralOffBattle(int slotIndex)
{
SlotGeneral slot = (SlotGeneral)playerBags[SlotType.SlotType_General][slotIndex];
slot.FormationPosition = FormationPosition.OFF;
slot.ExtraData = 0;
}
private bool IsFormationPositionOccupied(FormationPosition formationPosition)
{
foreach (int i in playerBags[SlotType.SlotType_General].Keys)
{
if (playerBags[SlotType.SlotType_General][i].ExtraData != 0)
return true;
}
return false;
}
public void SavePlayerFormation(Dictionary<FormationPosition,int> formationDict)
{
foreach (FormationPosition position in formationDict.Keys)
{
MakeGeneralOnBattle(formationDict[position],position);
}
}
public Dictionary<int,SlotGeneral> GetOnBattleGenerals()
{
Dictionary<int, SlotGeneral> onBattleGenerals = new Dictionary<int,SlotGeneral>();
foreach (KeyValuePair<int, Slot> kv in playerBags[SlotType.SlotType_General])
{
if (kv.Value.ExtraData >= 0)
{
onBattleGenerals.Add(kv.Key, (SlotGeneral)kv.Value);
}
}
return onBattleGenerals;
}
#endregion
}
}