<?php

#################################################################################
##              -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =-                 ##
## --------------------------------------------------------------------------- ##
##  Project:       TravianZ                                                    ##
##  Version:       28.10.2025                    			                   ## 
##  Filename       wdata.tpl                                                   ##
##  Developed by:  Mr.php , Advocaite , brainiacX , yi12345 , Shadow , ronix   ## 
##  Fixed by:      Shadow - STARVATION , HERO FIXED COMPL.  		           ##
##  Fixed by:      InCube - double troops				                       ##
##  Fixed by:      lietuvis10 - crop finder				                       ##
##  License:       TravianZ Project                                            ##
##  Copyright:     TravianZ (c) 2010-2015. All rights reserved.                ##
##  URLs:          http://travian.shadowss.ro                		 	       ##
##  Source code:   https://github.com/Shadowss/TravianZ		       	           ## 
##                                                                             ##
#################################################################################

?>

<?php
// install/wdata.tpl

include_once('../GameEngine/config.php');

if (isset($_GET['c']) && $_GET['c'] == '1') {
    echo '<br /><hr /><br /><div class="headline"><span class="f10 c5">Error creating wdata. Check configuration or file.</span></div><br><br>';
}
if (isset($_GET['err']) && $_GET['err'] == '1') {
    echo '<br /><hr /><br /><div class="headline"><span class="f10 c5">Existing World Data found in the database! Please empty tables <i>'
        . TB_PREFIX . 'odata, ' . TB_PREFIX . 'units, ' . TB_PREFIX . 'vdata, ' . TB_PREFIX . 'wdata</i> before continuing.</span></div><br /><br />';
}

$autoStartCroppers = isset($_GET['startCroppers']) && $_GET['startCroppers'] === '1';
?>

<form action="process.php" method="post" id="dataform">
    <input type="hidden" name="subwdata" value="1" />

    <p>
        <span class="f10 c">Create World Data</span>

        <table>
            <tr>
                <td>
                    <b>Warning</b>: This can take some time. Please wait until the next page has been loaded.
                    Click Create to proceed...
                    <br /><br />

                    <!-- Submit block (hidden when autoStartCroppers=1) -->
                    <div id="submitWrap" style="display:<?php echo $autoStartCroppers ? 'none' : 'block'; ?>;">
                        <center>
                            <input type="submit" name="Submit" id="Submit" value="Create..." onClick="return proceed()" />
                            <br /><br />
                        </center>
                    </div>

                    <!-- Progress UI (shown when startCroppers=1) -->
                    <div id="progressBox" style="display:<?php echo $autoStartCroppers ? 'block' : 'none'; ?>; margin-top:20px;">
                        <div style="font-weight:bold;margin-bottom:6px;">Building croppers…</div>

                        <div style="background:#ddd;border-radius:8px;overflow:hidden;height:20px;max-width:500px;">
                            <!-- Orange bar to match Travian vibes -->
                            <div id="pbar" style="background:#f6a21a;height:100%;width:0%;transition:width .2s;"></div>
                        </div>

                        <div id="pinfo" style="margin-top:6px;font-size:13px;color:#333;">Starting…</div>

                        <pre id="plog" style="margin-top:10px;background:#f9f9f9;border:1px solid #ddd;border-radius:8px;padding:8px;font-size:12px;max-height:200px;overflow:auto;"></pre>

                        <!-- Continue button appears on completion -->
						<div id="autoNext" style="display:none;margin-top:10px;">
						  Proceeding to next step in <b id="cd">3</b>…
						</div>
                    </div>

<script>
(function () {
  var NEXT_URL = 'index.php?s=4'; // your next step
  var COUNTDOWN_SECS = 3;
  var finished = false;

  function startCountdown() {
    var box = document.getElementById('autoNext');
    var cdEl = document.getElementById('cd');
    var left = COUNTDOWN_SECS;
    box.style.display = 'block';
    cdEl.textContent = left;
    var t = setInterval(function () {
      left--;
      cdEl.textContent = left;
      if (left <= 0) {
        clearInterval(t);
        window.location.href = NEXT_URL;
      }
    }, 1000);
  }

  function startCroppersBuild() {
    var box  = document.getElementById('progressBox');
    var pbar = document.getElementById('pbar');
    var pinfo= document.getElementById('pinfo');
    var plog = document.getElementById('plog');

    var submitWrap = document.getElementById('submitWrap');
    if (submitWrap) submitWrap.style.display = 'none';
    box.style.display = 'block';

    if (!('EventSource' in window)) {
      plog.textContent += "Your browser does not support live progress.\n";
      return;
    }

    var MAX_RETRIES = 3;
    var retries = 0;

    var es = new EventSource('ajax_croppers.php');

    es.onopen = function () {
      // When a connection (re)opens and we had errors before, log a small note
      if (!finished && retries > 0) {
        plog.textContent += "Reconnected to server.\n";
        plog.scrollTop = plog.scrollHeight;
      }
    };

    es.onmessage = function (e) {
      // Ignore non-JSON messages (pings / blanks)
      if (!e.data || e.data.charCodeAt(0) !== 123 /* '{' */) return;

      try {
        var d = JSON.parse(e.data);
        var pct   = (d.pct  || 0) | 0;
        var done  = (d.done || 0) | 0;
        var total = (d.total|| 0) | 0;

        // If we've already finished, ignore further events
        if (finished) return;

        // Valid data received -> reset retry counter
        retries = 0;

        pbar.style.width = pct + '%';
        pinfo.textContent = done + ' / ' + total + ' (' + pct + '%)';

        if (d.msg) {
          plog.textContent += d.msg + "\n";
          plog.scrollTop = plog.scrollHeight;
        }

        if (pct >= 100) {
          finished = true;
          plog.textContent += "✅ Completed!\n";
          plog.scrollTop = plog.scrollHeight;
          es.close();
          startCountdown();
        }

        // Optional: handle explicit error flag from server if you ever send it
        if (d.error) {
          finished = true;
          plog.textContent += "❌ " + (d.msg || "Server reported an error.") + "\n";
          plog.scrollTop = plog.scrollHeight;
          es.close();
          startCountdown();
        }
      } catch (err) {
        // Silently ignore parsing problems now that we guard by '{'
        // plog.textContent += "Parse error.\n";
      }
    };

    es.onerror = function () {
      // Don’t spam after we’re done
      if (finished) return;

      retries++;
      plog.textContent += "⚠ Connection hiccup (" + retries + "/" + MAX_RETRIES + "), retrying…\n";
      plog.scrollTop = plog.scrollHeight;

      // EventSource will auto-reconnect by itself; we just decide when to give up
      if (retries >= MAX_RETRIES) {
        finished = true;
        plog.textContent += "❌ Too many connection failures — skipping croppers build.\n";
        plog.scrollTop = plog.scrollHeight;
        es.close();
        // Reuse the same countdown UI to move on
        startCountdown();
      }
    };
  }

  document.addEventListener('DOMContentLoaded', function () {
    <?php if ($autoStartCroppers) { echo 'startCroppersBuild();'; } ?>
  });
})();
</script>


                </td>
            </tr>
        </table>
    </p>
</form>
</div>
