-
Notifications
You must be signed in to change notification settings - Fork 645
Expand file tree
/
Copy pathdeck_manager.cpp
More file actions
401 lines (398 loc) · 12.2 KB
/
Copy pathdeck_manager.cpp
File metadata and controls
401 lines (398 loc) · 12.2 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <algorithm>
#include "deck_manager.h"
#include "data_manager.h"
#include "game.h"
#include "myfilesystem.h"
#include "network.h"
namespace ygo {
DeckManager deckManager;
void DeckManager::LoadLFListSingle(const char* path) {
auto cur = _lfList.rend();
FILE* fp = myfopen(path, "r");
char linebuf[1024]{};
wchar_t strBuffer[256]{};
if(fp) {
while(std::fgets(linebuf, sizeof linebuf, fp)) {
if(linebuf[0] == '#')
continue;
if(linebuf[0] == '!') {
auto len = std::strcspn(linebuf, "\r\n");
linebuf[len] = 0;
BufferIO::DecodeUTF8(&linebuf[1], strBuffer);
LFList newlist;
newlist.listName = strBuffer;
newlist.hash = 0x7dfcee6a;
_lfList.push_back(newlist);
cur = _lfList.rbegin();
continue;
}
if (cur == _lfList.rend())
continue;
char* pos = linebuf;
errno = 0;
auto result = std::strtoul(pos, &pos, 10);
if (errno || result > UINT32_MAX)
continue;
if (pos == linebuf || *pos != ' ')
continue;
uint32_t code = static_cast<uint32_t>(result);
errno = 0;
int count = std::strtol(pos, &pos, 10);
if (errno)
continue;
if (count < 0 || count > 2)
continue;
cur->content[code] = count;
cur->hash = cur->hash ^ ((code << 18) | (code >> 14)) ^ ((code << (27 + count)) | (code >> (5 - count)));
}
std::fclose(fp);
}
}
void DeckManager::LoadLFList() {
LoadLFListSingle("expansions/lflist.conf");
LoadLFListSingle("lflist.conf");
LFList nolimit;
nolimit.listName = L"N/A";
nolimit.hash = 0;
_lfList.push_back(nolimit);
}
const wchar_t* DeckManager::GetLFListName(unsigned int lfhash) {
auto lit = std::find_if(_lfList.begin(), _lfList.end(), [lfhash](const ygo::LFList& list) {
return list.hash == lfhash;
});
if(lit != _lfList.end())
return lit->listName.c_str();
return dataManager.unknown_string;
}
const LFList* DeckManager::GetLFList(unsigned int lfhash) {
auto lit = std::find_if(_lfList.begin(), _lfList.end(), [lfhash](const ygo::LFList& list) {
return list.hash == lfhash;
});
if (lit != _lfList.end())
return &(*lit);
return nullptr;
}
static unsigned int checkAvail(unsigned int ot, unsigned int avail) {
if((ot & avail) == avail)
return 0;
if((ot & AVAIL_OCG) && (avail != AVAIL_OCG))
return DECKERROR_OCGONLY;
if((ot & AVAIL_TCG) && (avail != AVAIL_TCG))
return DECKERROR_TCGONLY;
return DECKERROR_NOTAVAIL;
}
uint32_t DeckManager::CheckDeck(const Deck& deck, unsigned int lfhash, size_t rule) {
std::unordered_map<uint32_t, int> ccount;
// rule
if(deck.main.size() < DECK_MIN_SIZE || deck.main.size() > DECK_MAX_SIZE)
return (DECKERROR_MAINCOUNT << 28) | (unsigned)deck.main.size();
if(deck.extra.size() > EXTRA_MAX_SIZE)
return (DECKERROR_EXTRACOUNT << 28) | (unsigned)deck.extra.size();
if(deck.side.size() > SIDE_MAX_SIZE)
return (DECKERROR_SIDECOUNT << 28) | (unsigned)deck.side.size();
auto lflist = GetLFList(lfhash);
if (!lflist)
return 0;
auto& list = lflist->content;
const unsigned int rule_map[6] = { AVAIL_OCG, AVAIL_TCG, AVAIL_SC, AVAIL_CUSTOM, AVAIL_OCGTCG, 0 };
unsigned int avail = 0;
if (rule < sizeof rule_map / sizeof rule_map[0])
avail = rule_map[rule];
for (auto& cit : deck.main) {
auto gameruleDeckError = checkAvail(cit->ot, avail);
if(gameruleDeckError)
return (gameruleDeckError << 28) | cit->code;
if (cit->type & (TYPES_EXTRA_DECK | TYPE_TOKEN))
return (DECKERROR_MAINCOUNT << 28);
auto code = cit->get_duel_code();
ccount[code]++;
int dc = ccount[code];
if(dc > 3)
return (DECKERROR_CARDCOUNT << 28) | cit->code;
auto it = list.find(code);
if(it != list.end() && dc > it->second)
return (DECKERROR_LFLIST << 28) | cit->code;
}
for (auto& cit : deck.extra) {
auto gameruleDeckError = checkAvail(cit->ot, avail);
if(gameruleDeckError)
return (gameruleDeckError << 28) | cit->code;
if (!(cit->type & TYPES_EXTRA_DECK) || cit->type & TYPE_TOKEN)
return (DECKERROR_EXTRACOUNT << 28);
auto code = cit->get_duel_code();
ccount[code]++;
int dc = ccount[code];
if(dc > 3)
return (DECKERROR_CARDCOUNT << 28) | cit->code;
auto it = list.find(code);
if(it != list.end() && dc > it->second)
return (DECKERROR_LFLIST << 28) | cit->code;
}
for (auto& cit : deck.side) {
auto gameruleDeckError = checkAvail(cit->ot, avail);
if(gameruleDeckError)
return (gameruleDeckError << 28) | cit->code;
if (cit->type & TYPE_TOKEN)
return (DECKERROR_SIDECOUNT << 28);
auto code = cit->get_duel_code();
ccount[code]++;
int dc = ccount[code];
if(dc > 3)
return (DECKERROR_CARDCOUNT << 28) | cit->code;
auto it = list.find(code);
if(it != list.end() && dc > it->second)
return (DECKERROR_LFLIST << 28) | cit->code;
}
return 0;
}
uint32_t DeckManager::LoadDeck(Deck& deck, uint32_t dbuf[], uint32_t mainc, uint32_t sidec, bool is_packlist) {
deck.clear();
uint32_t errorcode = 0;
auto& _datas = dataManager.GetDataTable();
for(uint32_t i = 0; i < mainc; ++i) {
auto code = dbuf[i];
auto it = _datas.find(code);
if(it == _datas.end()) {
errorcode = code;
continue;
}
auto& cd = it->second;
if (cd.type & TYPE_TOKEN) {
errorcode = code;
continue;
}
if(is_packlist) {
deck.main.push_back(&cd);
continue;
}
if (cd.type & TYPES_EXTRA_DECK) {
if (deck.extra.size() < EXTRA_MAX_SIZE)
deck.extra.push_back(&cd);
}
else {
if (deck.main.size() < DECK_MAX_SIZE)
deck.main.push_back(&cd);
}
}
for(uint32_t i = 0; i < sidec; ++i) {
auto code = dbuf[mainc + i];
auto it = _datas.find(code);
if(it == _datas.end()) {
errorcode = code;
continue;
}
auto& cd = it->second;
if (cd.type & TYPE_TOKEN) {
errorcode = code;
continue;
}
if(deck.side.size() < SIDE_MAX_SIZE)
deck.side.push_back(&cd);
}
return errorcode;
}
uint32_t DeckManager::LoadDeckFromStream(Deck& deck, std::istringstream& deckStream, bool is_packlist) {
int ct = 0;
int mainc = 0, sidec = 0;
uint32_t cardlist[PACK_MAX_SIZE]{};
bool is_side = false;
std::string linebuf;
while (std::getline(deckStream, linebuf) && ct < PACK_MAX_SIZE) {
if (linebuf[0] == '!') {
is_side = true;
continue;
}
if (linebuf[0] < '0' || linebuf[0] > '9')
continue;
errno = 0;
auto code = std::strtoul(linebuf.c_str(), nullptr, 10);
if (errno || code > UINT32_MAX)
continue;
cardlist[ct++] = code;
if (is_side)
++sidec;
else
++mainc;
}
return LoadDeck(deck, cardlist, mainc, sidec, is_packlist);
}
bool DeckManager::LoadSide(Deck& deck, uint32_t dbuf[], uint32_t mainc, uint32_t sidec) {
std::unordered_map<uint32_t, int> pcount;
std::unordered_map<uint32_t, int> ncount;
for(auto card : deck.main)
pcount[card->code]++;
for(auto card : deck.extra)
pcount[card->code]++;
for(auto card : deck.side)
pcount[card->code]++;
Deck ndeck;
LoadDeck(ndeck, dbuf, mainc, sidec);
if (ndeck.main.size() != deck.main.size() || ndeck.extra.size() != deck.extra.size() || ndeck.side.size() != deck.side.size())
return false;
for(auto card : ndeck.main)
ncount[card->code]++;
for(auto card : ndeck.extra)
ncount[card->code]++;
for(auto card : ndeck.side)
ncount[card->code]++;
for (auto& cdit : ncount)
if (cdit.second != pcount[cdit.first])
return false;
deck = ndeck;
return true;
}
void DeckManager::GetCategoryPath(wchar_t* ret, int index, const wchar_t* text) {
wchar_t catepath[256];
switch(index) {
case DECK_CATEGORY_PACK:
myswprintf(catepath, L"./pack");
break;
case DECK_CATEGORY_BOT:
BufferIO::CopyWideString(mainGame->gameConf.bot_deck_path, catepath);
break;
case -1:
case DECK_CATEGORY_NONE:
case DECK_CATEGORY_SEPARATOR:
myswprintf(catepath, L"./deck");
break;
default:
myswprintf(catepath, L"./deck/%ls", text);
}
BufferIO::CopyWStr(catepath, ret, 256);
}
void DeckManager::GetDeckFile(wchar_t* ret, int category_index, const wchar_t* category_name, const wchar_t* deckname) {
wchar_t filepath[256];
wchar_t catepath[256];
if(deckname != nullptr) {
GetCategoryPath(catepath, category_index, category_name);
myswprintf(filepath, L"%ls/%ls.ydk", catepath, deckname);
BufferIO::CopyWStr(filepath, ret, 256);
}
else {
BufferIO::CopyWStr(L"", ret, 256);
}
}
FILE* DeckManager::OpenDeckFile(const wchar_t* file, const char* mode) {
FILE* fp = mywfopen(file, mode);
return fp;
}
irr::io::IReadFile* DeckManager::OpenDeckReader(const wchar_t* file) {
char file2[256];
BufferIO::EncodeUTF8(file, file2);
auto reader = dataManager.IrrFileSystem->createAndOpenFile(file2);
return reader;
}
bool DeckManager::LoadCurrentDeck(std::istringstream& deckStream, bool is_packlist) {
LoadDeckFromStream(current_deck, deckStream, is_packlist);
return true; // the above LoadDeck has return value but we ignore it here for now
}
bool DeckManager::LoadCurrentDeck(const wchar_t* file, bool is_packlist) {
current_deck.clear();
if (!file[0])
return false;
char deckBuffer[MAX_YDK_SIZE]{};
auto reader = OpenDeckReader(file);
if(!reader) {
wchar_t localfile[256];
myswprintf(localfile, L"./deck/%ls.ydk", file);
reader = OpenDeckReader(localfile);
}
if(!reader && !mywcsncasecmp(file, L"./pack", 6)) {
wchar_t zipfile[256];
myswprintf(zipfile, L"%ls", file + 2);
reader = OpenDeckReader(zipfile);
}
if(!reader)
return false;
int size = reader->read(deckBuffer, sizeof deckBuffer);
reader->drop();
if (size >= (int)sizeof deckBuffer) {
return false;
}
std::istringstream deckStream(deckBuffer);
LoadDeckFromStream(current_deck, deckStream, is_packlist);
return true; // the above function has return value but we ignore it here for now
}
bool DeckManager::LoadCurrentDeck(int category_index, const wchar_t* category_name, const wchar_t* deckname) {
wchar_t filepath[256];
GetDeckFile(filepath, category_index, category_name, deckname);
bool is_packlist = (category_index == DECK_CATEGORY_PACK);
if(!LoadCurrentDeck(filepath, is_packlist))
return false;
if (mainGame->is_building)
mainGame->deckBuilder.RefreshPackListScroll();
return true;
}
void DeckManager::SaveDeck(const Deck& deck, std::stringstream& deckStream) {
deckStream << "#created by ..." << std::endl;
deckStream << "#main" << std::endl;
for(size_t i = 0; i < deck.main.size(); ++i)
deckStream << deck.main[i]->code << std::endl;
deckStream << "#extra" << std::endl;
for(size_t i = 0; i < deck.extra.size(); ++i)
deckStream << deck.extra[i]->code << std::endl;
deckStream << "!side" << std::endl;
for(size_t i = 0; i < deck.side.size(); ++i)
deckStream << deck.side[i]->code << std::endl;
}
bool DeckManager::SaveDeck(const Deck& deck, const wchar_t* file) {
if(!FileSystem::IsDirExists(L"./deck") && !FileSystem::MakeDir(L"./deck"))
return false;
FILE* fp = OpenDeckFile(file, "w");
if(!fp)
return false;
std::stringstream deckStream;
SaveDeck(deck, deckStream);
std::fputs(deckStream.str().c_str(), fp);
std::fclose(fp);
return true;
}
bool DeckManager::DeleteDeck(const wchar_t* file) {
return FileSystem::RemoveFile(file);
}
bool DeckManager::CreateCategory(const wchar_t* name) {
if(!FileSystem::IsDirExists(L"./deck") && !FileSystem::MakeDir(L"./deck"))
return false;
if(name[0] == 0)
return false;
wchar_t localname[256];
myswprintf(localname, L"./deck/%ls", name);
return FileSystem::MakeDir(localname);
}
bool DeckManager::RenameCategory(const wchar_t* oldname, const wchar_t* newname) {
if(!FileSystem::IsDirExists(L"./deck") && !FileSystem::MakeDir(L"./deck"))
return false;
if(newname[0] == 0)
return false;
wchar_t oldlocalname[256];
wchar_t newlocalname[256];
myswprintf(oldlocalname, L"./deck/%ls", oldname);
myswprintf(newlocalname, L"./deck/%ls", newname);
return FileSystem::Rename(oldlocalname, newlocalname);
}
bool DeckManager::DeleteCategory(const wchar_t* name) {
wchar_t localname[256];
myswprintf(localname, L"./deck/%ls", name);
if(!FileSystem::IsDirExists(localname))
return false;
return FileSystem::DeleteDir(localname);
}
bool DeckManager::SaveDeckArray(const DeckArray& deck, const wchar_t* name) {
if (!FileSystem::IsDirExists(L"./deck") && !FileSystem::MakeDir(L"./deck"))
return false;
FILE* fp = OpenDeckFile(name, "w");
if (!fp)
return false;
std::fprintf(fp, "#created by ...\n#main\n");
for (const auto& code : deck.main)
std::fprintf(fp, "%u\n", code);
std::fprintf(fp, "#extra\n");
for (const auto& code : deck.extra)
std::fprintf(fp, "%u\n", code);
std::fprintf(fp, "!side\n");
for (const auto& code : deck.side)
std::fprintf(fp, "%u\n", code);
std::fclose(fp);
return true;
}
}