diff --git a/lib/runtime/make.js b/lib/runtime/make.js index 1cd1cd0..5a5903e 100644 --- a/lib/runtime/make.js +++ b/lib/runtime/make.js @@ -8,8 +8,10 @@ var common = require('@screeps/common'), q = require('q'), _ = require('lodash'), utils = require('@screeps/engine/dist/utils'), + systemFunctions = require('./system-functions'), driver = require('../index'), pathfinderFactory = require('../path-finder'), + index = require('../index'), runtimeData = require('./data'), runtimeUserVm = require('./user-vm'); @@ -93,6 +95,21 @@ function getUserData(userId) { }); } +const intentsCount = function(intentsList) { + const freeMethods = {say: true, pull: true}; + + let count = 0; + for(const entity in intentsList) { + for(const intentName in intentsList[entity]) { + if(!freeMethods[intentName]) { + count += Array.isArray(intentsList[entity][intentName]) ? intentsList[entity][intentName].length : 1; + } + } + } + + return count; +} + async function make (scope, userId) { let userData; @@ -172,11 +189,14 @@ async function make (scope, userId) { run.release(); dataRef.release(); - runResult.intents = utils.storeIntents(userId, runResult.intentsList, data, index.config.customIntentTypes) - var $set = { - lastUsedCpu: runResult.usedTime, - lastUsedDirtyTime: runResult.usedDirtyTime - }; + runResult.intents = utils.storeIntents(userId, runResult.intentsList, data, index.config.customIntentTypes); + const intentsCpu = 0.2*intentsCount(runResult.intentsList); + const cpuUsed = Math.ceil(runResult.usedCleanTime+intentsCpu); + + const $set = systemFunctions.Object.create(null); + $set.lastUsedCpu = runResult.usedTime; + $set.lastUsedDirtyTime = runResult.usedDirtyTime; + if (runResult.activeSegments) { $set.activeSegments = runResult.activeSegments; } @@ -184,7 +204,7 @@ async function make (scope, userId) { $set.defaultPublicSegment = runResult.defaultPublicSegment; } if (userData.cpu < Infinity) { - var newCpuAvailable = userData.user.cpuAvailable + userData.user.cpu - runResult.usedTime; + var newCpuAvailable = userData.user.cpuAvailable + userData.user.cpu - cpuUsed; if(newCpuAvailable > config.engine.cpuBucketSize) { newCpuAvailable = config.engine.cpuBucketSize; } diff --git a/lib/runtime/runtime.js b/lib/runtime/runtime.js index 2e93ef3..c804b00 100644 --- a/lib/runtime/runtime.js +++ b/lib/runtime/runtime.js @@ -207,7 +207,7 @@ global._init = (function() { data.staticTerrainData = staticTerrainData; data.customObjectPrototypes = customObjectPrototypes; - var outMessage = systemFunctions.Object.create(null); + const outMessage = systemFunctions.Object.create(null); scope = game.init( global, @@ -265,6 +265,7 @@ global._init = (function() { } outMessage.usedTime = systemFunctions.Math.ceil(nowCpuTime() - startTime + intents.cpu); + outMessage.usedCleanTime = nowCpuTime() - startTime; outMessage.usedDirtyTime = systemFunctions.Math.ceil(nowCpuTime() - startDirtyTime); outMessage.intentsList = intents.list; outMessage.intentsCpu = intents.cpu;