Old Functions

```
/\* Functions from original, rewritten RailWAM that have been incorporated into the RailWAM library.
 Some may be uncompleted. \*/

 /\*\*
 \* Executes code when all necessary WAM data has been retrieved.
 \*
 \* @param {string} type - 'rail' or 'database'; how data will be used
 \* @param {string} displayDate - Date to display ('database' only)
 \* @param {number} dateSec - Date of data in seconds
 \*
 \* @returns {void}
 \*/
 function dataComplete(type,dateSec,displayDate) {

 // Check if all module data has been retrieved
 if (type === 'rail' && (wamData[dateSec] && wamData[dateSec - 86400]) !== undefined && typeof (wamData[dateSec] && wamData[dateSec - 86400]) !== 'undefined') {

 // Add RailWAM module to right rail
 addModule(dateSec);

 // Check if all database data has been retrieved
 } else if (type === 'database' && (wamData[dateSec] && wamData[dateSec + 86400]) !== undefined && typeof (wamData[dateSec] && wamData[dateSec + 86400]) !== 'undefined' && $('#railwam-entrytable-' + dateSec).length === 0) {

 // Execute based on search type
 switch ($('input[name="railwam-database"]:checked').val()) {

 // Data always meets search; no need to check
 newEntry(displayDate,dateSec);

 break;

 // Data may or may not meet search; must check
 if (local.database.checkEntry(dateSec) === true) {

 newEntry(displayDate,dateSec);

 }

 break;
 }

 } else {

 // Not all data has been retrieved
 return;

 }

 }

 /\*\*
 \* Logs WAM data on log page in user's settings.
 \*
 \* @param {number} dateSec - Date of data in seconds
 \*
 \* @returns {void}
 \*/
 function logWAMData(dateSec) {

 // Send AJAX request to edit API and add data in HTML to log page
 $.post(rw.api.config.wgServer + '/api.php?action=edit&title=' + encodeURIComponent(railWAM.logPage) + '&recreate=true&appendtext='+ encodeURIComponent('<span id="railwam-' + dateSec + '" class="railwam-data-element"><span class="railwam-score">' + wamData[dateSec].wam + '</span><span class="railwam-rank">' + wamData[dateSec].wam\_rank + '</span><span class="railwam-vert-rank">' + wamData[dateSec].vertical\_wam\_rank + '</span></span>') + '&summary=[[w:c:dev:RailWAM|RailWAM]]: ' + i18n.logSummary + '&format=json&bot=' + botParam + '&token=' + encodeURIComponent(token));

 }

 /\*\*
 \* Retrieves WAM data within last ## days.
 \*
 \* @param {number} days - Number of days to retrieve WAM data for
 \*
 \* @returns {void}
 \*/
 function getNewData(days,dateSec) {

 // Initialize request counter
 var requests = 0,

 // Initialize data object
 data = {};

 // Add extra day to calculate difference
 days++;

 (function getData(num) {

 // Within "days" days starting yesterday
 if (num > days) {

 // Create entry date
 date.setDate(date.getDate() - num);
 var extraZeroDay = date.getUTCDate().toString().length === 1 ? '0' : '',
 extraZeroMonth = (date.getUTCMonth() + 1).toString().length === 1 ? '0' : '',
 entryDate = extraZeroDay + date.getUTCDate() + '/' + extraZeroMonth + (date.getUTCMonth() + 1) + '/' + date.getUTCFullYear();
 date.setDate(date.getDate() + num);

 if (data[dateSec - (86400 \* num)] === undefined && typeof data[dateSec - (86400 \* num)] === 'undefined') {
 secLeft += 10;

 $.ajax({
 url:'https://query.yahooapis.com/v1/public/yql?q=select%20\*%20from%20json%20where%20url%3D%22http%3A%2F%2Fwww.wikia.com%2Fapi%2Fv1%2FWAM%2FWAMIndex%3Fwam\_day%3D' + (dateSec - (86400 \* num)).toString() +'%26wiki\_id%3D' + mwConfig.wgCityId + '%26sort\_column%3Dwam%26sort\_direction%3DASC%26limit%3D1%26fetch\_admins%3Dfalse%26fetch\_wiki\_images%3Dfalse%22&format=json&diagnostics=false&callback=',
 success:function(data) {
 if (data.query.results.json.wam\_results\_total >= 1) {
 data[dateSec - (86400 \* num)] = data.query.results.json.wam\_index[dataId];
 } else {
 data[dateSec - (86400 \* num)] = {
 wam:'N/A',
 wam\_rank:'N/A',
 vertical\_wam\_rank:'N/A'
 };
 }
 dataComplete('database',dateSec - (86400 \* num),entryDate);

 // Increment counter
 requests++;

 checkIfLoading(requests,days);
 },
 error:function() {

 // Increment counter
 requests++;

 checkIfLoading(requests,days);

 data[dateSec - (86400 \* num)] = {
 wam:'N/A',
 wam\_rank:'N/A',
 vertical\_wam\_rank:'N/A'
 };

 //dataComplete('database',dateSec - (86400 \* num),entryDate);

 },
 cache:false
 });

 } else {

 //dataComplete('database',dateSec - (86400 \* num),entryDate);

 // Increment counter
 requests++;

 checkIfLoading(requests,days);
 }

 setTimeout(
 function() {
 getData(num + 1);
 },
 10000);

 } else {

 // Return data object
 return data;

 }

 })(0);
 }

 /\*\*
 \* Loads data stored on the WAM log to the wamData object.
 \*
 \* @param {function} callback - Function to be executed after data is loaded.
 \*
 \* @returns {void}
 \*/
 function loadLoggedData(callback) {

 // Send AJAX request to WAM log page
 $.ajax({
 url: rw.api.config.wgServer + '/wiki/' + encodeURIComponent(railWAM.logPage) + '?action=raw',
 success:function(wamLog) {

 // Convert string to HTML
 var wamLogHtml = $(wamLog),

 // Get elements with WAM data
 dataElms = wamLogHtml.filter('.railwam-data-element');

 $.each(dataElms,function(i,thisElm) {

 // Get date of data in seconds as stored in element's ID
 var dateSec = $(thisElm).attr('id').replace('railwam-','');

 // Create object inside wamData object and store day's data
 wamData[dateSec] = {};
 wamData[dateSec].wam = $('.railwam-score',thisElm).text();
 wamData[dateSec].wam\_rank = $('.railwam-rank',thisElm).text();
 wamData[dateSec].vertical\_wam\_rank = $('.railwam-vert-rank',thisElm).text();

 });

 // Execute a function after data has been retrieved
 callback();

 },
 error:function() {

 // Execute function even if WAM log does not exist.
 callback();

 // Show log alert because WAM log does not exist
 shouldAlertShow = true;

 },
 cache:false
 });

 }

 /\*\*
 \* Returns difference between two values and appropriate image.
 \*
 \* @param {number} cur - Current value
 \* @param {number} prev - Previous value
 \*
 \* @returns {string} HTML of SVG and difference
 \*/
 function calcDiff(cur,prev) {

 // Calculate numerical change
 var diff = cur - prev;

 // Round to nearest ten-thousandth
 diff = Math.round(diff \* 10000) / 10000;

 // Return images based on change
 if (diff > 0) {

 // Positive change - green up arrow
 $('#railwam-not-logged').css('display','none');
 return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 11 13" class="railwam-arrow"><use xlink:href="#railwam-arrow-up"/></svg> ' + diff;

 } else if (diff < 0) {

 // Negative change - orange down arrow
 $('#railwam-not-logged').css('display','none');
 return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 11 13" class="railwam-arrow"><use xlink:href="#railwam-arrow-down"/></svg> ' + Math.abs(diff);

 } else if (diff === 0) {

 // No change - neutral blue circle
 $('#railwam-not-logged').css('display','none');
 return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 11 13" class="railwam-arrow"><use xlink:href="#railwam-arrow-zero"/></svg> 0';

 } else if (isNaN(diff) === true) {

 // Change can not be calculated as one value is not numerical
 $('#railwam-not-logged').css('display','block');
 return '+/- ' + i18n.unknown;

 }
 }

 /\*\*
 \* Converts seconds to days, hours, minutes, and seconds.
 \*
 \* @param {number} sec - # of seconds
 \*
 \* @returns {Object} Days, hours, minutes, seconds as object
 \*/
 function convertTime(sec) {

 // Convert to days, hours, min, sec
 var days = Math.floor(sec / 86400),
 hours = Math.floor((sec - (days \* 86400)) / 3600),
 min = Math.floor((sec - (days \* 86400) - (hours \* 3600)) / 60),
 remainder = Math.floor(sec - (days \* 86400) - (hours \* 60) - (min \* 60));

 // Return object with time values
 return {
 d:days,
 h:hours,
 m:min,
 s:remainder,
 };

 }

/\*\*
 \* Gets score and rank data of last two days from WAM Index API and adds it to the wamData object.
 \*
 \* @returns {void}
 \*/
 function getTop5000Data() {

 // Load logged data before performing any requests.
 rw.api.data.loadLogged(function(){
 var maxWamDate,dateSec;

 // Get the maximum WAM date in seconds.
 $.ajax({
 url:'https://query.yahooapis.com/v1/public/yql?q=select%20\*%20from%20json%20where%20url%3D%22http%3A%2F%2Fwww.wikia.com%2Fapi%2Fv1%2FWAM%2FMinMaxWamIndexDate%22&format=json&callback=',
 success:function(data) {

 // Set the maximum WAM date from returned data.
 maxWamDate = data.query.results.min\_max\_dates.max\_date;
 dateSec = maxWamDate;

 // Check if all data has been retrieved.
 dataComplete('rail',dateSec);

 if (wamData[dateSec] === undefined && typeof wamData[dateSec] === 'undefined') {

 // Get the latest WAM data.
 $.ajax({
 url:'https://query.yahooapis.com/v1/public/yql?q=select%20\*%20from%20json%20where%20url%3D%22http%3A%2F%2Fwww.wikia.com%2Fapi%2Fv1%2FWAM%2FWAMIndex%3Fwam\_day%3D' + dateSec +'%26wiki\_id%3D' + mwConfig.wgCityId + '%26sort\_column%3Dwam%26sort\_direction%3DASC%26limit%3D1%26fetch\_admins%3Dfalse%26fetch\_wiki\_images%3Dfalse%22&format=json&diagnostics=false&callback=',
 success:function(data) {

 // Show log alert because the latest data is not logged.
 shouldAlertShow = true;

 // Use retrieved data, if any.
 if (data.query.results.json.wam\_results\_total >= 1) {

 // Create object to hold day's WAM data.
 wamData[dateSec] = data.query.results.json.wam\_index[wamId];

 // Check if all data has been retrieved.
 dataComplete('rail',dateSec);

 } else {

 /\* Retrieve data from wiki data API if
 there is no data in the WAM Index API. \*/
 getRegularData();
 }
 },
 cache:false,
 error:function(){

 // Retrieve data from wiki data API if WAM Index is unavailable.
 getRegularData();

 }
 });
 }

 if (wamData[dateSec - 86400] === undefined && typeof wamData[dateSec - 86400] === 'undefined') {

 // Get WAM data from 2 days ago.
 $.ajax({
 url:'https://query.yahooapis.com/v1/public/yql?q=select%20\*%20from%20json%20where%20url%3D%22http%3A%2F%2Fwww.wikia.com%2Fapi%2Fv1%2FWAM%2FWAMIndex%3Fwam\_day%3D' + (dateSec - 86400) + '%26wiki\_id%3D' + mwConfig.wgCityId + '%26sort\_column%3Dwam%26sort\_direction%3DASC%26limit%3D1%26fetch\_admins%3Dfalse%26fetch\_wiki\_images%3Dfalse%22&format=json&diagnostics=false&callback=',
 success:function(data) {

 /\* logAlert() should not be executed here.
 Only yesterday's data should be logged. \*/

 // Use retrieved data, if any.
 if (data.query.results.json.wam\_results\_total >= 1) {

 // Create object to hold day's WAM data.
 wamData[dateSec - 86400] = data.query.results.json.wam\_index[wamId];

 } else {

 // Create object with N/A for all data if no data is available.
 wamData[dateSec - 86400] = {
 wam:'N/A',
 wam\_rank:'N/A',
 vertical\_wam\_rank:'N/A'
 };

 }

 // Check if all data has been retrieved.
 dataComplete('rail',dateSec);
 },
 cache:false,
 error:function(){

 // Create object with N/A for all data if WAM Index API is unavailable.
 wamData[dateSec - 86400] = {
 wam:'N/A',
 wam\_rank:'N/A',
 vertical\_wam\_rank:'N/A'
 };

 // Check if all data has been retrieved.
 dataComplete('rail',dateSec);

 }
 });

 }

 },
 error:function(){

 // Retrieve data from wiki data API if WAM Max/Min Date API is unavailable.
 getRegularData();

 },
 cache:false
 });

 });

 }

 /\*\*
 \* Gets score data only for one day from Wiki Data API and adds it to the wamData object.
 \*/
 function getRegularData() {

 // Date of data in seconds
 var dateSec = (Math.floor(date.getTime() / 1000 / 86400) \* 86400) - 86400;

 // Load logged data before performing any requests.
 loadLoggedData(function(){
 if (wamData[dateSec - 86400] === undefined && typeof wamData[dateSec - 86400] === 'undefined') {

 // Data from two days ago can not be retrieved with this API call.
 wamData[dateSec - 86400] = {
 wam:'N/A',
 wam\_rank:'N/A',
 vertical\_wam\_rank:'N/A'
 };
 }

 // Check if all data has been retrieved.
 dataComplete('rail',dateSec);

 // Get yesterday's WAM data
 if (wamData[dateSec] === undefined && typeof wamData[dateSec] === 'undefined') {
 $.ajax({
 url:'https://query.yahooapis.com/v1/public/yql?q=select%20\*%20from%20json%20where%20url%3D%22http%3A%2F%2Fwww.wikia.com%2Fapi%2Fv1%2FWikis%2FDetails%3Fids%3D' + mwConfig.wgCityId + '%22&format=json&callback=',
 success:function(data) {

 // Show log alert because the latest data is not logged.
 shouldAlertShow = true;

 // Rank and vertical rank can not be retrieved with this API call
 wamData[dateSec] = {
 wam:data.query.results.items[wamId].wam\_score,
 wam\_rank:'N/A',
 vertical\_wam\_rank:'N/A'
 };

 // Check if all data has been retrieved.
 dataComplete('rail',dateSec);

 },
 cache:false,
 error:function(){

 // Retrieve data from wiki data API if WAM Index is unavailable.
 errorModule();

 }
 });
 }

 });

 }

 /\*\*
 \* Searches for and displays WAM data for a single date.
 \*
 \* @param {Object[]} dateSplit - [year,month,day]
 \*
 \* @returns {void}
 \*/
 function searchByDate(dateSplit) {
 var requests = 0,
 dateSec = Date.UTC(dateSplit[2],(dateSplit[1] - 1),dateSplit[0]) / 1000;

 dateSplit[0] = dateSplit[0].length === 1 ? '0' + dateSplit[0] : dateSplit[0],
 dateSplit[1] = dateSplit[1].length === 1 ? '0' + dateSplit[1] : dateSplit[1];

 var entryDate = dateSplit[0] + '/' + dateSplit[1] + '/' + dateSplit[2];
 // Latest
 if (typeof wamData[dateSec] === 'undefined') {
 $.ajax({
 url:'https://query.yahooapis.com/v1/public/yql?q=select%20\*%20from%20json%20where%20url%3D%22http%3A%2F%2Fwww.wikia.com%2Fapi%2Fv1%2FWAM%2FWAMIndex%3Fwam\_day%3D' + (dateSec).toString() +'%26wiki\_id%3D' + mwConfig.wgCityId + '%26sort\_column%3Dwam%26sort\_direction%3DASC%26limit%3D1%26fetch\_admins%3Dfalse%26fetch\_wiki\_images%3Dfalse%22&format=json&diagnostics=false&callback=',
 success:function(data) {
 if (data.query.results.json.wam\_results\_total >= 1) {
 wamData[dateSec] = data.query.results.json.wam\_index[wamId];
 } else {
 wamData[dateSec] = {
 wam:'N/A',
 wam\_rank:'N/A',
 vertical\_wam\_rank:'N/A'
 };
 }
 dataComplete('database',dateSec,entryDate);
 requests++;
 checkIfLoading(requests,2);
 },
 error:function() {
 requests++;
 checkIfLoading(requests,2);
 },
 cache:false
 });
 }

 // Prev day
 if (typeof wamData[dateSec - 86400] === 'undefined') {
 $.ajax({
 url:'https://query.yahooapis.com/v1/public/yql?q=select%20\*%20from%20json%20where%20url%3D%22http%3A%2F%2Fwww.wikia.com%2Fapi%2Fv1%2FWAM%2FWAMIndex%3Fwam\_day%3D' + (dateSec - 86400).toString() +'%26wiki\_id%3D' + mwConfig.wgCityId + '%26sort\_column%3Dwam%26sort\_direction%3DASC%26limit%3D1%26fetch\_admins%3Dfalse%26fetch\_wiki\_images%3Dfalse%22&format=json&diagnostics=false&callback=',
 success:function(data) {
 if (data.query.results.json.wam\_results\_total >= 1) {
 wamData[dateSec - 86400] = data.query.results.json.wam\_index[wamId];
 } else {
 wamData[dateSec - 86400] = {
 wam:'N/A',
 wam\_rank:'N/A',
 vertical\_wam\_rank:'N/A'
 };
 }
 dataComplete('database',dateSec,$('#railwam-database-field').val());
 requests++;
 checkIfLoading(requests,2);
 },
 error:function() {
 requests++;
 checkIfLoading(requests,2);
 },
 cache:false
 });
 // If data from both days already exists
 } else if (typeof (wamData[dateSec] && wamData[dateSec - 86400]) !== 'undefined') {
 dataComplete('database',dateSec,$('#railwam-database-field').val());
 checkIfLoading(2,2);
 }
 }

```