Skip to content

Commit

Permalink
🔒️ calculate intents cost outside the sandbox
Browse files Browse the repository at this point in the history
DEV-389
  • Loading branch information
o4kapuk committed Dec 5, 2023
1 parent a1cd7de commit 0a59e26
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
32 changes: 26 additions & 6 deletions lib/runtime/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -172,19 +189,22 @@ 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;
}
if (runResult.defaultPublicSegment !== undefined) {
$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;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0a59e26

Please sign in to comment.