给你完整代码,替换原有函数即可
//向帐户的仓库导入物品。一行一条,可同时导入多行
void ImportItem()
{
char pathSucess[256];
char temp[256];
char pathError[256];
strncpy_s(temp, "..\\DATA\\ImportItem\\*.txt", sizeof(temp));
struct _finddata_t file;
INT32 findFile = _findfirst(temp, &file);
if (findFile == -1)
return;
INT32 serverId = 0;
while (true)
{
serverId++;
if (serverId > MAX_SERVERGROUP)
break;
if (file.name[0] == '.')
{
INT32 tryFindNext = _findnext(findFile, &file);
if (tryFindNext != 0)
break;
continue;
}
sprintf_s(pathSucess, sizeof(pathSucess), "..\\DATA\\ImportItem\\%s", file.name);
sprintf_s(pathError, sizeof(pathError), "..\\DATA\\ImportItem\\err\\%s", file.name);
FILE* pFile;
fopen_s(&pFile, pathSucess, "rt");
if (pFile == nullptr)
{
INT32 tryFindNext = _findnext(findFile, &file);
if (tryFindNext != 0)
break;
continue;
}
char accountName[256];
INT32 itemId, ef1, ef2, ef3, efv1, efv2, efv3;
while (fgets(temp, sizeof(temp), pFile))
{
sscanf_s(temp, "%s %d %d %d %d %d %d %d", accountName, sizeof(accountName), &itemId, &ef1, &efv1, &ef2, &efv2, &ef3, &efv3);
// 将账号名称转换为大写
_strupr_s(accountName);
bool runTimeSent = false;
if (itemId < 0)
{
// Invalid itemId, move file to error
MoveFileA(pathSucess, pathError);
continue;
}
INT32 idxName = cFileDB.GetIndex(accountName);
if (idxName > 0 && idxName < MAX_DBACCOUNT && cFileDB.Account[idxName].Slot >= 0 && cFileDB.Account[idxName].Slot <= 3)
{
INT32 srv = idxName / MAX_PLAYER;
INT32 id = idxName % MAX_PLAYER;
pCOF packet;
memset(&packet, 0, sizeof(packet));
packet.Header.PacketId = 0xC0F;
packet.Header.Size = sizeof(pCOF);
packet.Header.ClientId = id;
strncpy_s(packet.Username, accountName, sizeof(packet.Username));
packet.item.Index = itemId;
packet.item.Effect[0].Index = ef1;
packet.item.Effect[0].Value = efv1;
packet.item.Effect[1].Index = ef2;
packet.item.Effect[1].Value = efv2;
packet.item.Effect[2].Index = ef3;
packet.item.Effect[2].Value = efv3;
pUser[srv].Sock.SendOneMessage((char*)&packet, sizeof(pCOF));
runTimeSent = true;
std::stringstream str;
str << "通过DBsrv接收到物品 " << packet.item.toString().c_str();
}
if (!runTimeSent)
{
STRUCT_ACCOUNT account{};
memcpy(account.Username, accountName, sizeof(account.Username));
INT32 readedAccount = cFileDB.DBReadAccount(&account);
if (readedAccount == 0)
{
MoveFileA(pathSucess, pathError);
continue;
}
STRUCT_ITEM item{};
item.Index = itemId;
item.Effect[0].Index = ef1;
item.Effect[0].Value = efv1;
item.Effect[1].Index = ef2;
item.Effect[1].Value = efv2;
item.Effect[2].Index = ef3;
item.Effect[2].Value = efv3;
INT32 pos = -1;
for (INT32 i = 0; i < 160; i++)
{
if (account.Storage.Item.Index != 0)
continue;
pos = i;
break;
}
if (pos == -1)
{
INT32 moveRet = MoveFileA(pathSucess, pathError);
Log(accountName, "空间不足,无法接收物品 %s。", item.toString().c_str());
if (moveRet == 0)
{
Log("空间不足,移动失败。文件: %s。物品: %s。GetLastError: %d。\n", pathSucess, item.toString().c_str(), GetLastError());
Log(accountName, "空间不足,移动失败。文件 %s。物品: %s。GetLastError %s。", pathSucess, item.toString().c_str(), GetLastError());
}
else
{
Log("空间不足,移动失败。文件: %s。物品: %s。GetLastError: %d。\n", pathSucess, item.toString().c_str(), GetLastError());
}
continue;
}
account.Storage.Item[pos] = item;
Log(accountName, "物品 %s 已交付给角色", item.toString().c_str());
INT32 tryReadAccount = cFileDB.DBWriteAccount(&account);
if (tryReadAccount == 0)
{
if (!runTimeSent)
MoveFileA(pathSucess, pathError);
}
}
}
fclose(pFile);
auto tryReadAccount = DeleteFileA(pathSucess);
if (tryReadAccount == 0)
{
if (!MoveFileA(pathSucess, pathError))
{
printf("导入物品成功 - 文件未删除\n");
}
else
{
printf("导入物品成功 - 文件已移动,警告\n");
}
}
INT32 tryFindNext = _findnext(findFile, &file);
if (tryFindNext != 0)
break;
}
if (findFile != -1)
_findclose(findFile);
}