KOC Power Tools

By George Jetson Last update Mar 23, 2011 — Installed 628,272 times.

food alert

in
Subscribe to food alert 9 posts, 5 voices



uglierThanThou Scriptwright
FirefoxWindows

Small enhancement to send a msg to alliance chat when food is low.
ie. If the overview tab shows food in RED, than msg is sent every 10mins to chat.

The idea is that alliance can send food before troops desert...

(not claiming to understand how it works - just cut&paste from george's work...
maybe could be incorporated and supported in his next update.. ?
BTW George - most awesome script - KUDOS and thanks!)

3 mods listed below...

[paste this in ptStartup() function, just above "TowerAlerts.init();"]

FoodAlerts.init();

[paste this just below addCommas() ...]

function addCommasWhole(nStr){
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1;
}

[paste this just above Tower Alerts section...]

/************************ Food Alerts *************************/
var FoodAlerts = {

  init : function (){
   var f = FoodAlerts;
   f.e_eachMinute();
  },

  minuteTimer : null,

  e_eachMinute : function (){   
    var f = FoodAlerts;
    var now = unixTime();
      row = [];

      for(i=0; i < Cities.numCities; i++) {
        var rp = getResourceProduction (Cities.cities[i].id);
        var foodleft = parseInt(Seed.resources["city" + Cities.cities[i].id]['rec1'][0])/3600;
        var usage = rp[1] - parseInt(Seed.resources["city" + Cities.cities[i].id]['rec1'][3]);
        row[i] = rp[1] - usage;
          var timeLeft = parseInt(Seed.resources["city" + Cities.cities[i].id]['rec1'][0]) / 3600 / (0-usage) * 3600;
          var msg = '';
          if (timeLeft<(Options.foodWarnHours*3600)) {
                msg += 'My city ' + Cities.cities[i].name.substring(0,10) + ' (' +
                       Cities.cities[i].x +','+ Cities.cities[i].y + ')';
                msg += ' is low on food. Remaining: '+addCommasWhole(foodleft)+' ('+timestrShort(timeLeft)+') Upkeep: '+addCommas(usage);
                sendChat ("/a " + msg);
          }
      }  
  f.minuteTimer = setTimeout (f.e_eachMinute, 600000);
  },  
}

 
WrzWaldo User
ieWindows

More location detail on the second part?? I do not see an "addCommas()"

 
uglierThanThou Scriptwright
FirefoxWindows

Actually, not sure it matters where the second part goes... but suppose it is good to keep the related functions grouped together...
Below is complete function, so paste it after that.

function addCommas(nStr){
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
}

 
WrzWaldo User
ieWindows

Yeah had it there this morning, only thing it does is kill the script. I'll fiddle with it when I get home this evening.

 
Sammers User
FirefoxWindows

Tried this and it just kills power tools on me. Great idea if it works

 
uglierThanThou Scriptwright
FirefoxWindows

Sorry guys - there was a formatting error.
Problem was with the last section.
Not sure why, but had to add some spaces around the '<' in the first for loop to get the formatting to work properly.

Have updated the original post...

 
George Jetson Script's Author
FirefoxWindows

This is a good idea, and has been requested by others. I will see about adding this to Power BOT with additional options to control when/if a message is posted to alliance chat.

BTW, nice job on the implementation :)

 
crates User
ChromeX11

I'm not seeing anywhere to update Options.foodWarnHours in any of the options dialogs I'm familiar with.

How would I go about adding this to the Power Bot options box?

 
crates User
ChromeX11

Regarding the option, I just hard-coded it to 48 hours. It works flawlessly.

@Sammers: You did something wrong. Hopefully if you're doing script editing you know enough to check the error console to figure out where you goofed up. Even an accidental carriage return in the wrong place can bork up your script.