An update tool requires 4 things to work properly at a basic level:
1: A Keyed list of all the regions in the game, along with all the data required for the tool
- The first nation that updates in it
- How many regions come before it (It's index)
- How many nations are in it
2: An index list of all regions in the game.
3: A keyed list of all the nations in the game along with al lthe data required for the tool
- The nation's index
4: An index list of all nations in the game
A basic tool works by calculating the time that a Nation updates by using math to get the time it takes for a nation to update. This is done with the following equation.
TimePerNation = UpdateLengthInSeconds / NumberOfNations
This can be used to calculate the rough time a region will update in the game. You do this using how many nations update before it.
EstimatedUpdateTime = NationsBeforeRegion * TimePerNation
However, this time will be wildly inaccurate because there is an arbitary amount of time added to each nation called Variance. Variance was added because initially raider tools were 100% accurate. Calculating variance can only be done during the update by comparing the time that a nation actually updates to the tool-estimated time using this equation.
Variance = ActualUpdateTime - EstimatedUpdateTime
You can then calculate a more accurate time for your target region using that variance.
AccurateTime = EstimatedUpdateTime + Variance
As of the time of writing, the only way to get the Actual Update time of a nation is to watch the happenings API for influence changes.
http://www.nationstates.net/cgi-bin/api ... ge;limit=5
With this poll, you are looking for either world census rankings, or influence changes. You can safely poll that API call every 0.75 seconds, although you won't gain much accuracy over polling it once per second. Although the delay is 28 seconds, the variance calculation will still put your time within about 2 or 3 seconds of accuracy.
Pseudocode wise, you're looking at this:
- Code: Select all
RegionDict = //Dictionary of regions. RegionName:{FirstNation, Index, NumNations}
RegionList = //List of regions Index:RegionName
NationDict = //Dictionary of nations. {Nation:Index}
NationList = //List of Nations Index:Nation
UpdateStartTime = //This is important for later. For the major update, it is 12:00am EST, or 4:00am GMT
SecondsInUpdate = //For our purposes, we will assume the Major Update at 12:00am EST lasts 5600 seconds
NumberOfNations = //NationList.length
SecondsPerNation = SecondsInUpdate/NumberOfNations
PollData = PollAPI() //Language dependent. We will assume it returns a pythonic tuple (NationName, Timestamp)
Target = //Doesn't particularly matter for this example
TargetNationIndex = NationDict[RegionDict[Target][FirstNation]] //This will give us the index of the first nation in the region
TargetEstimate = TargetNationIndex * SecondsPerNation // This is Approximately how many seconds into the update the target will update
VarianceNationIndex = NationDict[PollData[0]] //The index of the nation that updated in our API poll
VarianceNationApproximate = VarianceNationIndex * SecondsPerNation //This will give us how many seconds in we thought the nation would update
VarianceNationActual = PollData[0] - UpdateStartTime //This is how many seconds it took for the nation to actully update
Variance = VarianceNationActual - VarianceNationApproximate //This will give us the variance
AccurateUpdateTime = TargetEstimate + Variance //Tada We have our accurate time!
With this implementation, HTML scraping is not really necessary, although if you're desperate for the one or two second difference it makes, you can scrape the world happenings pages a maximum of 10 times per minutes. I wouldn't advise it.
Happy 20xx everyone, and remember to set your user agents!