void HarvesterFactory::handleObjectReady(Object* object,DispatchClient* client) { //*ONLY* used to load resource containers //never used .... InLoadingContainer* ilc = _getObject(object->getParentId()); if (! ilc) {//ILC sanity check... LOG(warning) << "Could not locate InLoadingContainer for object parent [" << object->getParentId() << "]"; return; } HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(ilc->mObject); //add res containers gWorldManager->addObject(object,true); if(harvester->decLoadCount() == 0) { if(!(_removeFromObjectLoadMap(harvester->getId()))) LOG(warning) << "Failed removing object from loadmap"; ilc->mOfCallback->handleObjectReady(harvester,ilc->mClient); mILCPool.free(ilc); } }
void ObjectController::_handleHarvesterDeActivate(uint64 targetId,Message* message,ObjectControllerCmdProperties* cmdProperties) { CreatureObject* creature = dynamic_cast<CreatureObject*>(mObject); PlayerObject* player = creature->GetGhost(); if(!player) { return; } //do we have a valid structure ??? uint64 id = targetId; Object* object = gWorldManager->getObjectById(id); PlayerStructure* structure = dynamic_cast<PlayerStructure*>(object); if(!structure) { //gMessageLib->sendSystemMessage(player,L"","player_structure","command_no_building"); return; } //is the structure in Range??? float fTransferDistance = gWorldConfig->getConfiguration<float>("Player_Structure_Operate_Distance",(float)10.0); if(glm::distance(player->GetCreature()->mPosition, structure->mPosition) > fTransferDistance) { return; } HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(structure); harvester->setActive(false); //send the respective delta gMessageLib->sendHarvesterActive(harvester); //send the db update mDatabase->executeSqlAsync(0,0,"UPDATE %s.harvesters SET active = 0 WHERE id=%"PRIu64" ",mDatabase->galaxy(),harvester->getId()); }
//====================================================================================================================== // // Selects the resource for extraction // void ObjectController::_handleHarvesterSelectResource(uint64 targetId,Message* message,ObjectControllerCmdProperties* cmdProperties) { CreatureObject* creature = dynamic_cast<CreatureObject*>(mObject); PlayerObject* player = creature->GetGhost(); if(!player) { return; } //do we have a valid structure ??? uint64 id = targetId; Object* object = gWorldManager->getObjectById(id); PlayerStructure* structure = dynamic_cast<PlayerStructure*>(object); if(!structure) { //gMessageLib->sendSystemMessage(player,L"","player_structure","command_no_building"); return; } //is the structure in Range??? float fTransferDistance = gWorldConfig->getConfiguration<float>("Player_Structure_Operate_Distance",(float)10.0); if(glm::distance(player->GetCreature()->mPosition, structure->mPosition) > fTransferDistance) { DLOG(info) << " ObjectController::_handleHarvesterSelectResource Structure not in Range"; return; } HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(structure); //get the relevant Resource BString dataStr; message->getStringUnicode16(dataStr); uint64 resourceId; swscanf(dataStr.getUnicode16(),L"%"WidePRIu64,&resourceId); Resource* tmpResource = gResourceManager->getResourceById(resourceId); if((!tmpResource)||(!tmpResource->getCurrent())) { DLOG(info) << " ObjectController::_handleHarvesterSelectResource No valid resource!"; return; } harvester->setCurrentResource(resourceId); // update the current resource in the db mDatabase->executeSqlAsync(0,0,"UPDATE %s.harvesters SET ResourceID=%"PRIu64" WHERE id=%"PRIu64" ",mDatabase->galaxy(),resourceId,harvester->getId()); CurrentResource* cR = reinterpret_cast<CurrentResource*>(tmpResource); //resource = reinterpret_cast<CurrentResource*>(gResourceManager->getResourceByNameCRC(resourceName.getCrc())); float posX, posZ; float ratio = 0.0; posX = harvester->mPosition.x; posZ = harvester->mPosition.z; if(cR) { ratio = cR->getDistribution((int)posX + 8192,(int)posZ + 8192); if(ratio > 1.0) { ratio = 1.0; } } float ber = harvester->getSpecExtraction(); harvester->setCurrentExtractionRate(ber*ratio); // now enter the new resource in the hoppers resource list if its isnt already in there // TODO keep the list up to date by removing unnecessary resources // to this end read the list anew and delete every resource with zero amount // have a stored function do this if(!harvester->checkResourceList(resourceId)) { //do *not* add to list - otherwise we get a racecondition with the asynch update from db !!! //harvester->getResourceList()->push_back(std::make_pair(resourceId,float(0.0))); //add to db mDatabase->executeSqlAsync(0,0,"INSERT INTO %s.harvester_resources VALUES(%"PRIu64",%"PRIu64",0,0)",mDatabase->galaxy(),harvester->getId(),resourceId); } // update the current extractionrate in the db for the stored procedure handling the harvesting mDatabase->executeSqlAsync(0,0,"UPDATE %s.harvesters SET rate=%f WHERE id=%"PRIu64" ",mDatabase->galaxy(),(ber*ratio),harvester->getId()); //now send the updates gMessageLib->sendCurrentResourceUpdate(harvester,player); gMessageLib->sendCurrentExtractionRate(harvester,player); }
void HarvesterFactory::handleDatabaseJobComplete(void* ref,DatabaseResult* result) { QueryContainerBase* asyncContainer = reinterpret_cast<QueryContainerBase*>(ref); switch(asyncContainer->mQueryType) { case HFQuery_ResourceData: { uint32 count = (uint32)result->getRowCount(); HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(asyncContainer->mObject); if(count) { DataBinding* binding = mDatabase->createDataBinding(2); binding->addField(DFT_uint64,offsetof(HarvesterHopperItem,ResourceID),8,0); binding->addField(DFT_float,offsetof(HarvesterHopperItem,Quantity),4,1); HResourceList* hRList = harvester->getResourceList(); hRList->resize(hRList->size()+count); HarvesterHopperItem hopperTemp; for(uint64 i=0; i <count; i++) { result->getNextRow(binding,&hopperTemp); hRList->push_back(std::make_pair(hopperTemp.ResourceID,hopperTemp.Quantity)); } } QueryContainerBase* asynContainer = new(mQueryContainerPool.ordered_malloc()) QueryContainerBase(asyncContainer->mOfCallback,HFQuery_AttributeData,asyncContainer->mClient,asyncContainer->mId); asynContainer->mId = harvester->getId(); asynContainer->mObject = harvester; mDatabase->executeSqlAsync(this,asynContainer,"SELECT attributes.name,sa.value,attributes.internal" " FROM %s.structure_attributes sa" " INNER JOIN %s.attributes ON (sa.attribute_id = attributes.id)" " WHERE sa.structure_id = %" PRIu64 " ORDER BY sa.order", mDatabase->galaxy(),mDatabase->galaxy(),harvester->getId()); } break; case HFQuery_AttributeData: { HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(asyncContainer->mObject); //_buildAttributeMap(harvester,result); Attribute_QueryContainer attribute; uint64 count = result->getRowCount(); //int8 str[256]; //BStringVector dataElements; for(uint64 i = 0; i < count; i++) { result->getNextRow(mAttributeBinding,(void*)&attribute); harvester->addInternalAttribute(attribute.mKey,std::string(attribute.mValue.getAnsi())); } harvester->setLoadState(LoadState_Loaded); LOG(info) << "Loaded harvester with id [" << harvester->getId() << "]"; asyncContainer->mOfCallback->handleObjectReady(harvester,asyncContainer->mClient); } break; case HFQuery_MainData: { QueryContainerBase* asynContainer = new(mQueryContainerPool.ordered_malloc()) QueryContainerBase(asyncContainer->mOfCallback,HFQuery_ResourceData,asyncContainer->mClient,asyncContainer->mId); HarvesterObject* harvester = new(HarvesterObject); _createHarvester(result,harvester); asynContainer->mObject = harvester; asynContainer->mClient = asyncContainer->mClient; //asyncContainer->mOfCallback->handleObjectReady(harvester,asyncContainer->mClient); //now request the associated resource container count int8 sql[250]; sprintf(sql,"SELECT hr.resourceID, hr.quantity FROM %s.harvester_resources hr WHERE hr.ID = '%" PRIu64 "' ",mDatabase->galaxy(),harvester->getId()); mDatabase->executeSqlAsync(this,asynContainer,sql); } break; default: break; } mQueryContainerPool.free(asyncContainer); }
void HarvesterFactory::handleDatabaseJobComplete(void* ref,swganh::database::DatabaseResult* result) { QueryContainerBase* asyncContainer = reinterpret_cast<QueryContainerBase*>(ref); switch(asyncContainer->mQueryType) { case HFQuery_ResourceData: { uint32 count = (uint32)result->getRowCount(); HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(asyncContainer->mObject); if(count) { swganh::database::DataBinding* binding = mDatabase->createDataBinding(2); binding->addField(swganh::database::DFT_uint64,offsetof(HarvesterHopperItem,ResourceID),8,0); binding->addField(swganh::database::DFT_float,offsetof(HarvesterHopperItem,Quantity),4,1); HResourceList* hRList = harvester->getResourceList(); hRList->resize(hRList->size()+count); HarvesterHopperItem hopperTemp; for(uint64 i=0; i <count; i++) { result->getNextRow(binding,&hopperTemp); hRList->push_back(std::make_pair(hopperTemp.ResourceID,hopperTemp.Quantity)); } } QueryContainerBase* asynContainer = new(mQueryContainerPool.ordered_malloc()) QueryContainerBase(asyncContainer->mOfCallback,HFQuery_AttributeData,asyncContainer->mClient,asyncContainer->mId); asynContainer->mId = harvester->getId(); asynContainer->mObject = harvester; mDatabase->executeSqlAsync(this,asynContainer,"SELECT attributes.name,sa.value,attributes.internal" " FROM %s.structure_attributes sa" " INNER JOIN %s.attributes ON (sa.attribute_id = attributes.id)" " WHERE sa.structure_id = %"PRIu64" ORDER BY sa.order", mDatabase->galaxy(),mDatabase->galaxy(),harvester->getId()); } break; case HFQuery_AttributeData: { HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(asyncContainer->mObject); //_buildAttributeMap(harvester,result); Attribute_QueryContainer attribute; uint64 count = result->getRowCount(); //int8 str[256]; //BStringVector dataElements; for(uint64 i = 0; i < count; i++) { result->getNextRow(mAttributeBinding,(void*)&attribute); harvester->addInternalAttribute(BString(attribute.mKey.c_str()),attribute.mValue); } QueryContainerBase* asynContainer = new(mQueryContainerPool.ordered_malloc()) QueryContainerBase(asyncContainer->mOfCallback,HFQuery_AdminData,asyncContainer->mClient,asyncContainer->mId); asynContainer->mId = harvester->getId(); asynContainer->mObject = harvester; std::stringstream sql; sql << "SELECT sad.PlayerID, sad.AdminType, c.firstname FROM " << mDatabase->galaxy() << ".structure_admin_data sad INNER JOIN " << mDatabase->galaxy() << ".characters c ON (c.id = sad.PlayerID) WHERE StructureID = " << harvester->getId() << ";"; mDatabase->executeSqlAsync(this, asynContainer, sql.str()); } break; case HFQuery_AdminData: { HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(asyncContainer->mObject); std::unique_ptr<sql::ResultSet>& result_set = result->getResultSet(); while (result_set->next()) { std::string type = result_set->getString(2); if(type == "ADMIN") { harvester->admin_data_.admin_add_(result_set->getInt64(1), result_set->getString(3)); LOG(info) << " added Admin to harvester : " << harvester->getId(); } else if(type == "BAN") { harvester->admin_data_.ban_add_(result_set->getInt64(1), result_set->getString(3)); LOG(info) << " added ban to harvester : " << harvester->getId(); } else if(type == "ENTRY") { harvester->admin_data_.entry_add_(result_set->getInt64(1), result_set->getString(3)); LOG(info) << " added entry to harvester : " << harvester->getId(); } else if(type == "HOPPER") { harvester->admin_data_.entry_add_(result_set->getInt64(1), result_set->getString(3)); LOG(info) << " added entry to harvester : " << harvester->getId(); } } harvester->admin_data_.structure_id_ = harvester->getId(); harvester->admin_data_.owner_id_ = harvester->getOwner(); harvester->setLoadState(LoadState_Loaded); LOG(info) << "Loaded harvester with id [" << harvester->getId() << "]"; asyncContainer->mOfCallback->handleObjectReady(harvester,asyncContainer->mClient); } break; case HFQuery_MainData: { QueryContainerBase* asynContainer = new(mQueryContainerPool.ordered_malloc()) QueryContainerBase(asyncContainer->mOfCallback,HFQuery_ResourceData,asyncContainer->mClient,asyncContainer->mId); HarvesterObject* harvester = new(HarvesterObject); _createHarvester(result,harvester); asynContainer->mObject = harvester; asynContainer->mClient = asyncContainer->mClient; //asyncContainer->mOfCallback->handleObjectReady(harvester,asyncContainer->mClient); //now request the associated resource container count int8 sql[250]; sprintf(sql,"SELECT hr.resourceID, hr.quantity FROM %s.harvester_resources hr WHERE hr.ID = '%"PRIu64"' ",mDatabase->galaxy(),harvester->getId()); mDatabase->executeSqlAsync(this,asynContainer,sql); } break; default: break; } mQueryContainerPool.free(asyncContainer); }
void HarvesterObject::handleDatabaseJobComplete(void* ref,swganh::database::DatabaseResult* result) { StructureManagerAsyncContainer* asynContainer = (StructureManagerAsyncContainer*)ref; switch(asynContainer->mQueryType) { case Structure_ResourceRetrieve: { PlayerObject* player = dynamic_cast<PlayerObject*>(gWorldManager->getObjectById(asynContainer->mPlayerId)); HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(gWorldManager->getObjectById(asynContainer->mStructureId)); swganh::database::DataBinding* binding = gWorldManager->getKernel()->GetDatabase()->createDataBinding(1); binding->addField(swganh::database::DFT_uint32,0,4); uint64 count; uint32 error; count = result->getRowCount(); if(!count) { assert(false && "HarvesterObject::handleDatabaseJobComplete Structure_ResourceRetrieve did not find any resources"); return; } result->getNextRow(binding,&error); if(error > 0) { //mmh there was something fishy ... no changes db side gMessageLib->sendResourceEmptyHopperResponse(harvester,player,error, asynContainer->command.b1, asynContainer->command.b2); return; } createResourceContainer(asynContainer->command.ResourceId, player, asynContainer->command.Amount); StructureManagerAsyncContainer* asyncContainer = new StructureManagerAsyncContainer(Structure_ResourceDiscardUpdateHopper,player->getClient()); asyncContainer->mStructureId = asynContainer->mStructureId; asyncContainer->mPlayerId = asynContainer->mPlayerId; asyncContainer->command = asynContainer->command; gWorldManager->getKernel()->GetDatabase()->executeSqlAsync(harvester,asyncContainer,"SELECT hr.resourceID, hr.quantity FROM %s.harvester_resources hr WHERE hr.ID = '%"PRIu64"' ",gWorldManager->getKernel()->GetDatabase()->galaxy(), harvester->getId()); } break; case Structure_ResourceDiscard: { PlayerObject* player = dynamic_cast<PlayerObject*>(gWorldManager->getObjectById(asynContainer->mPlayerId)); HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(gWorldManager->getObjectById(asynContainer->mStructureId)); swganh::database::DataBinding* binding = gWorldManager->getKernel()->GetDatabase()->createDataBinding(1); binding->addField(swganh::database::DFT_uint32,0,4); uint64 count; uint32 error; count = result->getRowCount(); if(!count) { assert(false && "HarvesterObject::handleDatabaseJobComplete Structure_ResourceDiscard did not find resource"); return; } result->getNextRow(binding,&error); if(result > 0) { //mmh there was something fishy ... no changes db side gMessageLib->sendResourceEmptyHopperResponse(harvester,player,error, asynContainer->command.b1, asynContainer->command.b2); return; } StructureManagerAsyncContainer* asyncContainer = new StructureManagerAsyncContainer(Structure_ResourceDiscardUpdateHopper,player->getClient()); asyncContainer->mStructureId = asynContainer->mStructureId; asyncContainer->mPlayerId = asynContainer->mPlayerId; asyncContainer->command = asyncContainer->command; gWorldManager->getKernel()->GetDatabase()->executeSqlAsync(harvester,asyncContainer,"SELECT hr.resourceID, hr.quantity FROM %s.harvester_resources hr WHERE hr.ID = '%"PRIu64"' ",gWorldManager->getKernel()->GetDatabase()->galaxy(),harvester->getId()); } break; case Structure_ResourceDiscardUpdateHopper: { uint64 count = result->getRowCount(); HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(gWorldManager->getObjectById(asynContainer->mStructureId)); PlayerObject* player = dynamic_cast<PlayerObject*>(gWorldManager->getObjectById(asynContainer->mPlayerId)); swganh::database::DataBinding* binding = gWorldManager->getKernel()->GetDatabase()->createDataBinding(2); binding->addField(swganh::database::DFT_uint64,offsetof(HarvesterHopperItem,ResourceID),8,0); binding->addField(swganh::database::DFT_float,offsetof(HarvesterHopperItem,Quantity),4,1); HResourceList* hRList = harvester->getResourceList(); hRList->clear(); HarvesterHopperItem hopperTemp; for(uint64 i=0; i <count; i++) { result->getNextRow(binding,&hopperTemp); hRList->push_back(std::make_pair(hopperTemp.ResourceID,hopperTemp.Quantity)); } //now send the update to the client gMessageLib->SendHarvesterHopperUpdate(harvester,player); gMessageLib->sendResourceEmptyHopperResponse(harvester,player,0, asynContainer->command.b2, asynContainer->command.b2); gWorldManager->getKernel()->GetDatabase()->destroyDataBinding(binding); } break; case Structure_GetResourceData: { uint64 count = result->getRowCount(); HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(gWorldManager->getObjectById(asynContainer->mStructureId)); PlayerObject* player = dynamic_cast<PlayerObject*>(gWorldManager->getObjectById(asynContainer->mPlayerId)); swganh::database::DataBinding* binding = gWorldManager->getKernel()->GetDatabase()->createDataBinding(2); binding->addField(swganh::database::DFT_uint64,offsetof(HarvesterHopperItem,ResourceID),8,0); binding->addField(swganh::database::DFT_float,offsetof(HarvesterHopperItem,Quantity),4,1); HResourceList* hRList = harvester->getResourceList(); hRList->clear(); HarvesterHopperItem hopperTemp; for(uint64 i=0; i <count; i++) { result->getNextRow(binding,&hopperTemp); hRList->push_back(std::make_pair(hopperTemp.ResourceID,hopperTemp.Quantity)); } //now send the update to the client gMessageLib->sendHarvesterResourceData(harvester,player); gMessageLib->sendBaselinesHINO_7(harvester,player); gWorldManager->getKernel()->GetDatabase()->destroyDataBinding(binding); } break; //read in the current resource hoppers contents case Structure_HopperUpdate: { uint64 count = result->getRowCount(); HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(gWorldManager->getObjectById(asynContainer->mStructureId)); PlayerObject* player = dynamic_cast<PlayerObject*>(gWorldManager->getObjectById(asynContainer->mPlayerId)); swganh::database::DataBinding* binding = gWorldManager->getKernel()->GetDatabase()->createDataBinding(2); binding->addField(swganh::database::DFT_uint64,offsetof(HarvesterHopperItem,ResourceID),8,0); binding->addField(swganh::database::DFT_float,offsetof(HarvesterHopperItem,Quantity),4,1); HResourceList* hRList = harvester->getResourceList(); hRList->clear(); HarvesterHopperItem hopperTemp; for(uint64 i=0; i <count; i++) { result->getNextRow(binding,&hopperTemp); hRList->push_back(std::make_pair(hopperTemp.ResourceID,hopperTemp.Quantity)); } //now send the update to the client gMessageLib->SendHarvesterHopperUpdate(harvester,player); gWorldManager->getKernel()->GetDatabase()->destroyDataBinding(binding); } break; case Structure_HopperDiscard: { //PlayerStructure* structure = dynamic_cast<PlayerStructure*>(gWorldManager->getObjectById(asynContainer->mStructureId)); PlayerObject* player = dynamic_cast<PlayerObject*>(gWorldManager->getObjectById(asynContainer->mPlayerId)); if(!player) { return; } StructureManagerAsyncContainer* asyncContainer = new StructureManagerAsyncContainer(Structure_HopperUpdate,player->getClient()); asyncContainer->mStructureId = this->getId(); asyncContainer->mPlayerId = player->getId(); int8 sql[250]; sprintf(sql,"SELECT hr.resourceID, hr.quantity FROM %s.harvester_resources hr WHERE hr.ID = '%"PRIu64"' ",gWorldManager->getKernel()->GetDatabase()->galaxy(),this->getId()); gWorldManager->getKernel()->GetDatabase()->executeSqlAsync(this,asyncContainer,sql); } break; default: break; } SAFE_DELETE(asynContainer); }