bool ConfigurationManager::configure(std::string name, int& variable, int defaultValue) { if(!hasInt(name)) { setInt(name, defaultValue); variable = defaultValue; return false; } variable = getInt(name); return true; }
int ConfigurationManager::getInt(std::string name) { if (hasInt(name)) { return intBoolMap[name]; } else { LOG("Settings Error", "Variable not found, use the \"hasInt\" function first"); return 0; } }
void BenchmarkTcpClient::handle(Event &event, long long) { if (&event == &eventRead) { int prevSize = readBuffer.size(); readBuffer.reserve(1024); readBuffer.push(socket->read(readBuffer.end(), 1024)); receviedSize += readBuffer.size() - prevSize; eventRead.setTimeRelativeNow(); if (readBuffer.empty()) return; if (!writeBuffer.empty()) { formatError(); return; } if (server) { if (hasInt() && !requestsCount) { requestsCount = popInt(); if (requestsCount <= 0) { formatError(); return; } requestsCountRemain = requestsCount; } if (hasInt() && !requestSize) { requestSize = popInt(); if (requestSize <= 0) { formatError(); return; } if (requestsCountRemain <= 0) { formatError(); return; } --requestsCountRemain; } if (hasInt() && !answerSize) { answerSize = popInt(); if (answerSize <= 0) { formatError(); return; } requestSizeRemain = requestSize; } if (requestSizeRemain > 0) { currentCrc32 = Packet::crc32(readBuffer.begin(), readBuffer.size(), currentCrc32); requestSizeRemain -= readBuffer.size(); readBuffer.clear(); if (requestSizeRemain < 0) { formatError(); return; } if (requestSizeRemain == 0) { currentSeed = currentCrc32; currentCrc32 = 0; answerSizeRemain = answerSize; buildWriteChunk(); } } } else { while(!empty()) { if (answerSizeRemain <= 0 || pop() != (char)((int)random(currentSeed)%256 - 128)) { formatError(); return; } --answerSizeRemain; } if (answerSizeRemain == 0) { if (requestsCountRemain == 0) { socket->close(); return; } requestSizeRemain = requestSize; buildWriteChunk(); } } } else if (&event == &eventWrite) { if (writeBuffer.empty()) return; if (!readBuffer.empty()) { formatError(); return ; } if (!writeBuffer.empty()) writeBuffer.pop(socket->write(writeBuffer.begin(), writeBuffer.size())); buildWriteChunk(); if (!writeBuffer.empty()) eventWrite.setTimeRelativeNow(); } else if (&event == &eventClose) { if ( !socket->wasError() && requestsCountRemain == 0 && requestSizeRemain == 0 && answerSizeRemain == 0 && readBuffer.empty() && writeBuffer.empty()) endUs = Platform::nowUs(); delete this; } }
bool Player::getProperty(const std::string_view prop, Variable& var) const { if (prop.empty() == true) { return false; } auto props = Utils::splitStringIn2(prop, '.'); auto propHash = str2int16(props.first); if (getLevelObjProp(propHash, props.second, var) == true) { return true; } switch (propHash) { case str2int16("name"): var = Variable(Name()); break; case str2int16("simpleName"): var = Variable(SimpleName()); break; case str2int16("d"): case str2int16("description"): { updateNameAndDescriptions(); size_t idx = Utils::strtou(props.second); if (idx >= descriptions.size()) { idx = 0; } var = Variable(descriptions[idx]); break; } case str2int16("eval"): var = Variable((int64_t)Formula::evalString(props.second, *this)); break; case str2int16("evalMin"): var = Variable((int64_t)Formula::evalMinString(props.second, *this)); break; case str2int16("evalMax"): var = Variable((int64_t)Formula::evalMaxString(props.second, *this)); break; case str2int16("evalf"): var = Variable(Formula::evalString(props.second, *this)); break; case str2int16("evalMinf"): var = Variable(Formula::evalMinString(props.second, *this)); break; case str2int16("evalMaxf"): var = Variable(Formula::evalMaxString(props.second, *this)); break; case str2int16("totalKills"): var = Variable((int64_t)Class()->TotalKills()); break; case str2int16("hasMaxStats"): var = Variable(hasMaxStats()); break; case str2int16("hasProperty"): var = Variable(hasInt(props.second)); break; case str2int16("canUseSelectedItem"): { if (selectedItem == nullptr) { return false; } var = Variable(canUseObject(*selectedItem)); break; } case str2int16("canUseSelectedSpell"): { if (selectedSpell == nullptr) { return false; } var = Variable(canUseObject(*selectedSpell->spell)); break; } case str2int16("canUseItem"): { std::string_view props2; size_t invIdx; size_t itemIdx; if (inventories.parseInventoryAndItem( props.second, props2, invIdx, itemIdx) == true) { auto item = inventories[invIdx].get(itemIdx); if (item != nullptr) { var = Variable(canUseObject(*item)); break; } } return false; } case str2int16("hasEquipedItemType"): var = Variable(hasEquipedItemType(props.second)); break; case str2int16("hasEquipedItemSubType"): var = Variable(hasEquipedItemSubType(props.second)); break; case str2int16("hasSelectedItem"): var = Variable(selectedItem != nullptr); break; case str2int16("hasItem"): { std::string_view props2; size_t invIdx; size_t itemIdx; if (inventories.parseInventoryAndItem( props.second, props2, invIdx, itemIdx) == true) { var = Variable(inventories[invIdx].get(itemIdx) != nullptr); break; } return false; } case str2int16("hasItemClass"): var = Variable(inventories.hasItem(str2int16(props.second))); break; case str2int16("hasSpell"): { var = Variable(getSpell(std::string(props.second)) != nullptr); break; } case str2int16("hasSelectedSpell"): { var = Variable(selectedSpell != nullptr); break; } case str2int16("isItemSlotInUse"): { std::string_view props2; size_t invIdx; size_t itemIdx; if (inventories.parseInventoryAndItem( props.second, props2, invIdx, itemIdx) == true) { var = Variable(inventories[invIdx].isSlotInUse(itemIdx)); break; } return false; } case str2int16("isStanding"): var = Variable(playerStatus == PlayerStatus::Stand); break; case str2int16("isWalking"): var = Variable(playerStatus == PlayerStatus::Walk); break; case str2int16("isAttacking"): var = Variable(playerStatus == PlayerStatus::Attack); break; case str2int16("isDead"): var = Variable(playerStatus == PlayerStatus::Dead); break; case str2int16("selectedItem"): { if (selectedItem != nullptr) { return selectedItem->getProperty(props.second, var); } return false; } case str2int16("item"): { std::string_view props2; size_t invIdx; size_t itemIdx; if (inventories.parseInventoryAndItem( props.second, props2, invIdx, itemIdx) == true) { auto item = inventories[invIdx].get(itemIdx); if (item != nullptr) { return item->getProperty(props2, var); } } return false; } case str2int16("inventory"): { auto props2 = Utils::splitStringIn2(props.second, '.'); auto invIdx = GameUtils::getPlayerInventoryIndex(props2.first); if (invIdx < inventories.size()) { return inventories[invIdx].getProperty(props2.second, var); } return false; } case str2int16("itemQuantity"): { auto classIdHash16 = str2int16(props.second); uint32_t itemQuantity = 0; if (itemQuantityCache.getValue(classIdHash16, itemQuantity) == false) { if (inventories.getQuantity(classIdHash16, itemQuantity) == true) { itemQuantityCache.updateValue(classIdHash16, itemQuantity); } } var = Variable((int64_t)itemQuantity); break; } case str2int16("selectedSpell"): { if (selectedSpell != nullptr) { return selectedSpell->getProperty(props.second, var); } return false; } case str2int16("spell"): { auto props2 = Utils::splitStringIn2(props.second, '.'); auto spell = getSpellInstance(std::string(props2.first)); if (spell != nullptr) { return spell->getProperty(props2.second, var); } return false; } default: { Number32 value; if (getNumberByHash(propHash, props.second, value) == true) { var = Variable(value.getInt64()); break; } return false; } } return true; }