-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.cpp
238 lines (206 loc) · 5.07 KB
/
db.cpp
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
#include <io.h>
#include <string>
#include <dialogs.hpp>
#include "MyStrUtil.h"
#include "db.h"
extern string MaglibPath;
string MaglibDatabase = "data/cardslist.dat";
TCardPile* db = NULL;
TCardPile* GetAllCards()
{
if (db == NULL)
{
db = new TCardPile();
}
return db;
}
void FreeCards()
{
if (db == NULL) return;
for (TCardBrowser i = db->begin(); i != db->end(); i++)
{
delete (*i);
}
delete db;
db = NULL;
}
string ExtractWordDelim(string& s, char Delimiter)
{
if (s.empty()) return "";
string Result;
int i = s.find(Delimiter);
if (i < 0)
{
Result = s;
s.resize(0);
}
else
{
Result = s.substr(0, i);
s.erase(0, i+1);
}
return Result;
}
TCardData* ParseMaglibLine(string Line, string SetName)
{
string Name = ExtractWordDelim(Line, '\t');
string Set = ExtractWordDelim(Line, '\t');
if (!SetName.empty() && (Set != SetName)) return NULL;
string RarityStr = ExtractWordDelim(Line, '\t') + " ";
// êîððåêöèÿ àïîñòðîôîâ
int i;
while ((i = Name.find(char(0x92))) != string::npos) Name[i] = 0x27;
TCardData::TRarity Rarity;
switch (RarityStr[0])
{
default:
case 'C':
Rarity = TCardData::ryCommon;
break;
case 'U':
Rarity = TCardData::ryUncommon;
break;
case 'R':
Rarity = TCardData::ryRare;
break;
}
string Manacost = ExtractWordDelim(Line, '\t');
string Type = ExtractWordDelim(Line, '\t');
string SubType = "";
i = Type.find("Creature");
if (i != string::npos)//((Type == "Creature") || (Type == "Artifact Creature"))
{
SubType = Type.substr(i+8);
Type.erase(i+8);
//Type = "Creature";
}
i = Type.find("Summon");
if (i != string::npos)
{
SubType = Type.substr(i+6);
Type = "Creature";
}
if (!SubType.empty())
{
string Dash = ExtractWord(SubType, " ");
}
string CardText = ExtractWordDelim(Line, '\t');
while ((i = CardText.find("#P")) >= 0)
{
CardText.replace(i, 2, "\n");
}
string Flavor = ExtractWordDelim(Line, '\t');
string PowerToughness = ExtractWordDelim(Line, '\t');
TCardData* Result = new TCardData(Name, Type, SubType, PowerToughness,
Manacost, Rarity, CardText, Set);
return Result;
}
TCardPile CreateSet(string SetName)
{
TCardPile Result;
TCardPile* p = GetAllCards();
//if (!SetName.empty())
// åñëè ñåò óæå âû÷èòàí
for (TCardBrowser i = p->begin(); i != p->end(); i++)
{
if ((*i)->Edition == SetName) Result.push_back(*i);
}
if (Result.size() > 0) return Result;
FILE* fp = fopen((MaglibPath + MaglibDatabase).c_str(), "rt");
if (fp == NULL)
{
MessageDlg(AnsiString("File ") + MaglibPath.c_str() +
MaglibDatabase.c_str() + " not found",
mtError, TMsgDlgButtons() << mbOK, 0);
return Result;
}
char buf[4096];
while (!feof(fp))
{
fgets(buf, 4096, fp);
TCardData* Card = ParseMaglibLine(buf, SetName);
if (Card != NULL)
{
Result.push_back(Card);
p->push_back(Card);
}
}
if (Result.empty())
{
MessageDlg(AnsiString("Set ") + SetName.c_str() +
" not found in database file",
mtError, TMsgDlgButtons() << mbOK, 0);
}
fclose(fp);
return Result;
}
TCardPile GenerateBooster(TCardPile& Set)
{
TCardPile Result;
//randomize();
int i;
int Iterations = 0;
do
{
i = random(Set.size());
Iterations++;
if (Iterations > 1000) break;
}
while (Set[i]->Rarity != TCardData::ryRare);
Result.push_back(Set[i]);
Iterations = 0;
int UncommonColors[7] = { 0,0,0,0,0 };
for (int j=0; j<3; j++)
{
for(;;)
{
i = random(Set.size());
Iterations++;
if (Iterations > 4000) break;
if (Set[i]->Rarity != TCardData::ryUncommon) continue;
if (UncommonColors[Set[i]->GetColor()] > 0) continue;
if (std::find(Result.begin(), Result.end(), Set[i]) != Result.end()) continue;
break;
}
UncommonColors[Set[i]->GetColor()]++;
Result.push_back(Set[i]);
}
Iterations = 0;
int CommonColors[7] = { 0,0,0,0,0 };
for (int j=0; j<11; j++)
{
for(;;)
{
i = random(Set.size());
Iterations++;
if (Iterations > 8000) break;
if (IsBasicLand(Set[i])) continue;
if (Set[i]->Rarity != TCardData::ryCommon) continue;
if (CommonColors[Set[i]->GetColor()] >= 3) continue;
if (std::find(Result.begin(), Result.end(), Set[i]) != Result.end()) continue;
break;
}
CommonColors[Set[i]->GetColor()]++;
Result.push_back(Set[i]);
}
return Result;
}
bool IsBasicLand(TCardData* c)
{
return
(c->Name == "Island") ||
(c->Name == "Forest") ||
(c->Name == "Swamp") ||
(c->Name == "Mountain") ||
(c->Name == "Plains");
// ïîäêðóòèòü äëÿ Snow-covereds
}
TCardData* GetCard(string Name)
{
TCardPile* p = GetAllCards();
for (TCardBrowser i = p->begin(); i != p->end(); i++)
{
if (!stricmp((*i)->Name.c_str(), Name.c_str())) return *i;
}
return NULL;
}