<?php

require_once dirname(__FILE__) . '/../classes/WarOfNations2.class.php';
require_once dirname(__FILE__) . '/../classes/data/DataLoad.class.php';
$won = new WarOfNations();
$won->setDataLoadId(DataLoadDAO::initNewLoad($won->db, 'TEST_AUTHENTICATE2', 0));
DataLoadDAO::startLoad($won->db, $won->data_load_id);
$won->Authenticate();
DataLoadDAO::loadComplete($won->db, $won->data_load_id);
     echo "Stop Signal Detected!\n";
     DataLoadLogDAO::logEvent2($won->db, $func_log_id, $log_seq++, 'INFO', 'Stop Signal Detected!');
     break;
 }
 // If enough time has passed, start out with a new session
 if (time() - $last_session_time > $hours_between_sessions * 60 * 60) {
     DataLoadLogDAO::logEvent2($won->db, $func_log_id, $log_seq++, 'INFO', "At least {$hours_between_sessions} hour(s) have passed. Starting new session.\n");
     // Save the data load for later, we don't want a new one
     $data_load_id = $won->data_load_id;
     // Re-initialize the WarOfNations object
     unset($won);
     $won = new WarOfNations();
     // Set the data load back
     $won->setDataLoadId($data_load_id);
     // Reauthenticate to start a new session
     $auth_result = $won->Authenticate(false, $device_id, true);
     $last_session_time = time();
 } else {
     // Otherwise, if this isn't the first run, call player sync to get updated information
     if (!$first_run) {
         $auth_result = $game->SyncPlayer();
     }
 }
 if (!$auth_result) {
     echo "Sync or Authentication Failed. Quitting.\n";
     DataLoadLogDAO::logEvent2($won->db, $func_log_id, $log_seq++, 'ERROR', 'Sync or Authentication Failed. Quitting.', null, 1);
     break;
 }
 // Setup/clear out our lists
 $bases = array();
 $base_distances = array();
echo "Connected!\r\n";

$player_id = '101013849913487';
$token = $uplink_info['token'];
$msg = '{"payload":{"hub":"war-of-nations-and","id":"'.$player_id.'","token":"'.$token.'"},"type":"subscribe"}';

echo "Sending: $msg\r\n";
$client->send($msg);
echo "Sent!\r\n\r\n";

while(true){
	try {
		echo 'Receiving: ['.$client->receive()."]\r\n\r\n";
	} 
	catch (Exception $ex) {
		echo "Error Reading From Connection\r\n";
	}

	// 30 Seconds between Heartbeat
	usleep(30000000);

	$msg = '{"payload":{},"type":"heartbeat"}';
	echo "Sending: $msg\r\n";
	$client->send($msg);
}
*/
$won->Authenticate(false, 5);
$uplink = $won->GetUplinkService();
$uplink->Subscribe();
$uplink->Run();
DataLoadDAO::loadComplete($won->db, $won->data_load_id);
$jeep_units_to_send = array('Jeep' => 1);
// Other operational/timing settings
$minutes_before_jeep = 6;
$seconds_before_second_cap = 10;
$seconds_pause_between_jeeps = 7;
$seconds_pause_before_recall = 10;
$seconds_pause_between_rounds = 5;
// State variables
$recall_fails_in_a_row = 0;
$total_recall_failures = 0;
// Initializing Operational Variables
$time_before_second_cap = 0;
$time_before_recall_hold = 0;
// Authenticate ourselves
//$auth_result = json_decode(file_get_contents('auth_result.json'), true);
$auth_result = $won->Authenticate(false, 5, true);
//print_r($auth_result['responses'][0]['return_value']['player_commanders']);
// Get an instance of our game operations class
$game = $won->GetGameOperations();
$func_log_id = DataLoadLogDAO::startFunction($won->db, $won->data_load_id, 'TrainCommanders', 'Main');
$quitting = false;
$log_seq = 0;
while (true) {
    // Sort commanders that are in the training base into groups
    $training_commanders = array();
    $jeeping_commanders = array();
    $extra_jeep_commanders = array();
    $holding_commanders = array();
    foreach ($auth_result['responses'][0]['return_value']['player_commanders'] as $comm) {
        // If the commander is in our designated training base and has energy
        if (array_key_exists('town_id', $comm) && $comm['town_id'] == $training_base_id && $comm['last_update_energy_value'] > 0) {
<?php

require_once dirname(__FILE__) . '/../classes/WarOfNations2.class.php';
require_once dirname(__FILE__) . '/../classes/data/DataLoad.class.php';
// Initialize Variables
$alliance_to_join = '101013232844480';
// 1st_HQ
$amount_needed = 58;
$amount_donated = 0;
// Login to Main
$won_main = new WarOfNations();
$won_main->setDataLoadId(DataLoadDAO::initNewLoad($won_main->db, 'OPERATION_CONCRETE', 0));
DataLoadDAO::startLoad($won_main->db, $won_main->data_load_id);
$won_main->Authenticate(false, 5);
$game_main = $won_main->GetGameOperations();
// Start Loop Here
while ($amount_donated < $amount_needed) {
    // Create Alt
    $won_alt = new WarOfNations();
    $won_alt->setDataLoadId($won_main->data_load_id);
    // Create a new account
    $won_alt->auth->CreateNewPlayer();
    //$won_alt->Authenticate(false, 17); // Use if something fails
    usleep(1000000);
    // Join World 13
    if ($won_alt->auth->world_id != 13) {
        $won_alt->auth->JoinNewWorld(13);
    }
    usleep(1000000);
    // Buy Reinforced Concrete (180000)
    $game_alt = $won_alt->GetGameOperations();