<commit_before>import fallback from '../../system/fallback';
import getForage from '../../ajax/getForage';
import getValue from '../../system/getValue';
import setForage from '../../ajax/setForage';
import setValue from '../../system/setValue';

export var showMonsterLog;
var monsterLog;

export function toggleShowMonsterLog() {
  showMonsterLog = !showMonsterLog;
  setValue('showMonsterLog', showMonsterLog);
}

function updateMinMax(_logStat, creatureStat) {
  var logStat = fallback(_logStat, {});
  if (logStat.min) {
    logStat.min = Math.min(logStat.min, creatureStat);
  } else {
    logStat.min = creatureStat;
  }
  if (logStat.max) {
    logStat.max = Math.max(logStat.max, creatureStat);
  } else {
    logStat.max = creatureStat;
  }
  return logStat;
}

function creatureHazEnhancements(creature) {
  return creature.enhancements && creature.enhancements.length > 0;
}

function doMonsterLog(creature) {
  if (!monsterLog) {monsterLog = {};}
  monsterLog[creature.name] = fallback(monsterLog[creature.name], {});
  var logCreature = monsterLog[creature.name];
  logCreature.creature_class = fallback(logCreature.creature_class,
    creature.creature_class);
  logCreature.image_id = fallback(logCreature.image_id,
    creature.image_id);
  logCreature.level = fallback(logCreature.level,
    Number(creature.level));
  logCreature.type = fallback(logCreature.type, creature.type);
  logCreature.armor = updateMinMax(logCreature.armor,
    Number(creature.armor));
  logCreature.attack = updateMinMax(logCreature.attack,
    Number(creature.attack));
  logCreature.damage = updateMinMax(logCreature.damage,
    Number(creature.damage));
  logCreature.defense = updateMinMax(logCreature.defense,
    Number(creature.defense));
  logCreature.hp = updateMinMax(logCreature.hp,
    Number(creature.hp));
  if (creatureHazEnhancements(creature)) {
    logCreature.enhancements = fallback(logCreature.enhancements, {});
    var logEnh = logCreature.enhancements;
    creature.enhancements.forEach(function(e) {
      logEnh[e.name] = updateMinMax(logEnh[e.name], Number(e.value));
    });
  }
  setForage('fsh_monsterLog', monsterLog);
}

export function processMonsterLog(creature) {
  if (showMonsterLog) {doMonsterLog(creature);}
}

export function getMonsterPrefs() {
  showMonsterLog = getValue('showMonsterLog');
  getForage('fsh_monsterLog').done(function(data) {
    monsterLog = data || {};
  });<commit_msg>break up doMonsterLog<commit_after>import fallback from '../../system/fallback';
import getForage from '../../ajax/getForage';
import getValue from '../../system/getValue';
import setForage from '../../ajax/setForage';
import setValue from '../../system/setValue';

export var showMonsterLog;
var monsterLog;

export function toggleShowMonsterLog() {
  showMonsterLog = !showMonsterLog;
  setValue('showMonsterLog', showMonsterLog);
}

function storeDescription(creature, logCreature) {
  logCreature.creature_class = fallback(logCreature.creature_class,
    creature.creature_class);
  logCreature.image_id = fallback(logCreature.image_id, creature.image_id);
  logCreature.level = fallback(logCreature.level, Number(creature.level));
  logCreature.type = fallback(logCreature.type, creature.type);
}

function updateMinMax(_logStat, creatureStat) {
  var logStat = fallback(_logStat, {});
  if (logStat.min) {
    logStat.min = Math.min(logStat.min, creatureStat);
  } else {
    logStat.min = creatureStat;
  }
  if (logStat.max) {
    logStat.max = Math.max(logStat.max, creatureStat);
  } else {
    logStat.max = creatureStat;
  }
  return logStat;
}

var stats = ['attack', 'armor', 'damage', 'defense', 'hp'];

function storeStats(creature, logCreature) {
  stats.forEach(function(stat) {
    logCreature[stat] = updateMinMax(logCreature[stat], Number(creature[stat]));
  });
}

function creatureHazEnhancements(creature) {
  return creature.enhancements && creature.enhancements.length > 0;
}

function storeEnhancements(creature, logCreature) {
  if (creatureHazEnhancements(creature)) {
    logCreature.enhancements = fallback(logCreature.enhancements, {});
    var logEnh = logCreature.enhancements;
    creature.enhancements.forEach(function(e) {
      logEnh[e.name] = updateMinMax(logEnh[e.name], Number(e.value));
    });
  }
}

function doMonsterLog(creature) {
  if (!monsterLog) {monsterLog = {};}
  monsterLog[creature.name] = fallback(monsterLog[creature.name], {});
  var logCreature = monsterLog[creature.name];
  storeDescription(creature, logCreature);
  storeStats(creature, logCreature);
  storeEnhancements(creature, logCreature);
  setForage('fsh_monsterLog', monsterLog);
}

export function processMonsterLog(creature) {
  if (showMonsterLog) {doMonsterLog(creature);}
}

export function getMonsterPrefs() {
  showMonsterLog = getValue('showMonsterLog');
  getForage('fsh_monsterLog').done(function(data) {
    monsterLog = data || {};
  });
{'fraction_non_alphanumeric': 0.13719885667619436, 'fraction_numerical': 0.0004083299305839118, 'mean_word_length': 5.100871731008717, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3012605', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1663, 'n_words': 265}