/// <summary> /// обменивает ресурс текцщего владения на другой тип ресурса текущего владения, сохраняет изменения в базу /// </summary> /// <param name="clientData">client name energyConverterChangeOutDataModel</param> /// <returns></returns> public async Task <bool> BuildEnergyConverterExchangeResource(EnergyConverterChangeOut clientData) { return(await _contextAction(connection => { var cr = _getCurrentUser(connection); var crPremium = cr.GetPremiumWorkModel(connection, _storeService); clientData.UserId = cr.UserId; if (clientData.OwnId == 0) { var mother = _mothershipService.GetMother(connection, clientData.UserId); mother = _synchronizer.UserMothership(connection, mother, crPremium, _mothershipService, _motherJumpService); return _energyConverter.CallculateNewResources(connection, _mothershipService, mother, clientData); } var planet = _gDetailPlanetService.GetUserPlanet(connection, clientData.OwnId, cr.UserId); planet = _synchronizer.UserPlanet(connection, planet, crPremium, _gDetailPlanetService); return _energyConverter.CallculateNewResources(connection, _gDetailPlanetService, planet, clientData); })); }
private static StorageResources _callculateNewResources(StorageResources dbRes, EnergyConverterChangeOut clientData, int level) { var toConvert = clientData.ToConvert; var resources = dbRes.ToDictionary(); var current = StorageResources.GetCurrent(resources); var max = StorageResources.GetMax(resources); var srcName = clientData.From; var targetName = clientData.To; var srcCount = current[srcName]; var targetCount = current[targetName]; var targetMax = max[targetName]; if (srcCount < 1) { throw new Exception(Error.NotEnoughResources); } if (targetMax - targetCount < 1) { throw new Exception(Error.TargetResourceIsFull); } var bp = ExtractionModule.BaseProportion.ToDictionary(); double realCount = toConvert; if ((srcCount - toConvert) < 1) { realCount = srcCount; } var loses = 1 - GetEnergyConverterMod(level); var rate = (bp[srcName] / bp[targetName]) * loses; if (rate <= 0) { rate = 1; } var freeTargetRes = targetMax - targetCount; var potentialSourceMax = freeTargetRes / rate; if (potentialSourceMax - realCount < 1) { realCount = potentialSourceMax; } var raealAdedResource = realCount * rate; if (raealAdedResource < 1) { throw new Exception(Error.NotEnoughResources); } var targetSum = targetCount + raealAdedResource; if (targetMax < targetSum) { throw new Exception(Error.MathError); } resources[StorageResources.CurrentKey][srcName] = srcCount - realCount; resources[StorageResources.CurrentKey][targetName] = targetCount + raealAdedResource; return(StorageResources.DictionaryToStorageResources(resources)); }
public bool CallculateNewResources(IDbConnection connection, IGDetailPlanetService service, GDetailPlanetDataModel planet, EnergyConverterChangeOut clientData) { var dbRes = planet.Resources; var level = planet.BuildEnergyConverter.GetLevel(1); var calculatedRes = _callculateNewResources(dbRes, clientData, level); if (dbRes.Equals(calculatedRes)) { return(false); } planet.Resources = calculatedRes; service.AddOrUpdate(connection, planet); return(true); }
public bool CallculateNewResources(IDbConnection connection, IMothershipService motherService, UserMothershipDataModel mother, EnergyConverterChangeOut clientData) { var dbRes = mother.Resources; var calculatedRes = _callculateNewResources(dbRes, clientData, 1); if (dbRes.Equals(calculatedRes)) { return(false); } mother.Resources = calculatedRes; mother = motherService.AddOrUpdate(connection, mother); return(true); }