<commit_before>'use strict';


const {debugLog} = require('./logging');
const {findRoomsWithinReach} = require('./helper_findMyRooms');

/**
 * isClaimableRoom - Checks if a room is claimable
 * - not already claimed
 * - has a controller
 * - has at least two sources
 * - is not `Occupied`
 * - is not `Reserved`
 *
 * @param {string} roomName - The room to check
 * @return {boolean} - If the room is claimable
 **/
function isClaimableRoom(roomName) {
  const data = global.data.rooms[roomName];
  if (Memory.myRooms.indexOf(roomName) >= 0) {
    return false;
  }
  if (!data.controllerId) {
    return false;
  }
  if (data.sources < 2) {
    return false;
  }
  if (data.state === 'Occupied') {
    return false;
  }
  if (data.state === 'Controlled') {
    return false;
  }
  if (data.state === 'HostileReserved') {
    return false;
  }
  return true;
}

/**
 * getMinLinearDistanceToMyRooms
 *
 * @param {string} roomName
 * @return {number}
 */
function getMinLinearDistanceToMyRooms(roomName) {
  let minDistance = config.nextRoom.maxDistance;
  for (const myRoom of Memory.myRooms) {
    const distance = Game.map.getRoomLinearDistance(roomName, myRoom);
    minDistance = Math.min(distance, minDistance);
  }
  return minDistance;
}

/**
 * getNextRoomValuatedRoomMap - Evaluates rooms based on mineral and distance
 * and sort based on the value
 *
 * @param {array} rooms
 * @return {array}
 */
function getNextRoomValuatedRoomMap(rooms) {
  const mineralValues = JSON.parse(JSON.stringify(config.nextRoom.mineralValues));
  for (const roomName of Memory.myRooms) {
    mineralValues[global.data.rooms[roomName].mineral] /= 2;
  }
  const evaluatedRooms = rooms.map((roomName) => {
    return {
      value: getMinLinearDistanceToMyRooms(roomName) + mineralValues[global.data.rooms[roomName].mineral],
      roomName: roomName,
    };
  });
  evaluatedRooms.sort((a, b) => b.value - a.value);
  return evaluatedRooms;
}

/**
 * haveEnoughSystemResources
 *
 * @return {bool}
 */
function haveEnoughSystemResources() {
  if (config.nextRoom.resourceStats) {
    debugLog('nextroomer', `stats: ${JSON.stringify(global.stats)}`);
    const myRoomsLength = Memory.myRooms.length;
    const cpuPerRoom = global.stats.cpuUsed / myRoomsLength;
    if (cpuPerRoom > global.stats.cpuIdle) {
      debugLog('nextroomer', `not enough cpu: ${cpuPerRoom} > ${global.stats.cpuIdle}`);
      return false;
    }
    const heapPerRoom = global.stats.heapUsed / myRoomsLength;
    if (heapPerRoom > global.stats.heapFree) {
      debugLog('nextroomer', `not enough heap: ${heapPerRoom} > ${global.stats.heapFree}`);
      return false;
    }
    const memoryPerRoom = global.stats.memoryUsed / myRoomsLength;
    if (memoryPerRoom > global.stats.memoryFree) {
      debugLog('nextroomer', `not enough heap: ${memoryPerRoom} > ${global.stats.memoryFree}`);
      return false;
    }
  } else {
    if (Memory.myRooms.length >= 0) { // config.nextRoom.maxRooms) {
      return false;
    }

    if ((Memory.myRooms.length + 1) * config.nextRoom.cpuPerRoom >= Game.cpu.limit) {
      return false;
    }
  }
  return true;
}

brain.handleNextroomer = function() {
  if (!Memory.myRooms) {
    return;
  }
  if (Memory.myRooms.length >= Game.gcl.level) {
    return;
  }
  if (Game.time % config.nextRoom.intervalToCheck !== 0) {
    return;
  }<commit_msg>fix bug about misuse global.stats and global.data.stats (#663)<commit_after>'use strict';


const {debugLog} = require('./logging');
const {findRoomsWithinReach} = require('./helper_findMyRooms');

/**
 * isClaimableRoom - Checks if a room is claimable
 * - not already claimed
 * - has a controller
 * - has at least two sources
 * - is not `Occupied`
 * - is not `Reserved`
 *
 * @param {string} roomName - The room to check
 * @return {boolean} - If the room is claimable
 **/
function isClaimableRoom(roomName) {
  const data = global.data.rooms[roomName];
  if (Memory.myRooms.indexOf(roomName) >= 0) {
    return false;
  }
  if (!data.controllerId) {
    return false;
  }
  if (data.sources < 2) {
    return false;
  }
  if (data.state === 'Occupied') {
    return false;
  }
  if (data.state === 'Controlled') {
    return false;
  }
  if (data.state === 'HostileReserved') {
    return false;
  }
  return true;
}

/**
 * getMinLinearDistanceToMyRooms
 *
 * @param {string} roomName
 * @return {number}
 */
function getMinLinearDistanceToMyRooms(roomName) {
  let minDistance = config.nextRoom.maxDistance;
  for (const myRoom of Memory.myRooms) {
    const distance = Game.map.getRoomLinearDistance(roomName, myRoom);
    minDistance = Math.min(distance, minDistance);
  }
  return minDistance;
}

/**
 * getNextRoomValuatedRoomMap - Evaluates rooms based on mineral and distance
 * and sort based on the value
 *
 * @param {array} rooms
 * @return {array}
 */
function getNextRoomValuatedRoomMap(rooms) {
  const mineralValues = JSON.parse(JSON.stringify(config.nextRoom.mineralValues));
  for (const roomName of Memory.myRooms) {
    mineralValues[global.data.rooms[roomName].mineral] /= 2;
  }
  const evaluatedRooms = rooms.map((roomName) => {
    return {
      value: getMinLinearDistanceToMyRooms(roomName) + mineralValues[global.data.rooms[roomName].mineral],
      roomName: roomName,
    };
  });
  evaluatedRooms.sort((a, b) => b.value - a.value);
  return evaluatedRooms;
}

/**
 * haveEnoughSystemResources
 *
 * @return {bool}
 */
function haveEnoughSystemResources() {
  if (config.nextRoom.resourceStats) {
    debugLog('nextroomer', `stats: ${JSON.stringify(global.data.stats)}`);
    const myRoomsLength = Memory.myRooms.length;
    const cpuPerRoom = global.data.stats.cpuUsed / myRoomsLength;
    if (cpuPerRoom > global.data.stats.cpuIdle) {
      debugLog('nextroomer', `not enough cpu: ${cpuPerRoom} > ${global.data.stats.cpuIdle}`);
      return false;
    }
    const heapPerRoom = global.data.stats.heapUsed / myRoomsLength;
    if (heapPerRoom > global.data.stats.heapFree) {
      debugLog('nextroomer', `not enough heap: ${heapPerRoom} > ${global.data.stats.heapFree}`);
      return false;
    }
    const memoryPerRoom = global.data.stats.memoryUsed / myRoomsLength;
    if (memoryPerRoom > global.data.stats.memoryFree) {
      debugLog('nextroomer', `not enough heap: ${memoryPerRoom} > ${global.data.stats.memoryFree}`);
      return false;
    }
  } else {
    if (Memory.myRooms.length >= 0) { // config.nextRoom.maxRooms) {
      return false;
    }

    if ((Memory.myRooms.length + 1) * config.nextRoom.cpuPerRoom >= Game.cpu.limit) {
      return false;
    }
  }
  return true;
}

brain.handleNextroomer = function() {
  if (!Memory.myRooms) {
    return;
  }
  if (Memory.myRooms.length >= Game.gcl.level) {
    return;
  }
  if (Game.time % config.nextRoom.intervalToCheck !== 0) {
    return;
  }
{'fraction_non_alphanumeric': 0.1378398236590742, 'fraction_numerical': 0.002204261572373255, 'mean_word_length': 3.4864864864864864, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5964719', 'n_tokens_mistral': 2276, 'n_tokens_neox': 2259, 'n_words': 489}