<?php
// planet_moon_game.php
// July, 2023
// RJM Programming
// Using experimental_drag_and_drop.htm for Planet Moon#//upload.wikimedia.org/wikipedia/commons/thumb/f/f7/Far_side_of_the_Moon.png/220px-Far_side_of_the_Moon.pngs


 $scorebit='';
 if (isset($_GET['score']) && isset($_GET['secs'])) {
   $scorebit="Score " . $_GET['score'] . '/' . $_GET['secs'];
 }
 
 // Thanks to https://www.enneagraminstitute.com/type-descriptions and https://www.psychologyjunkie.com/enneagram-famous-people/
 $typedescstuff="1 MERCURY

2 VENUS

3 EARTH
Moon#//upload.wikimedia.org/wikipedia/commons/thumb/f/f7/Far_side_of_the_Moon.png/220px-Far_side_of_the_Moon.png

4 MARS
Phobos#//upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Phobos_colour_2008.jpg/220px-Phobos_colour_2008.jpg,Deimos#//upload.wikimedia.org/wikipedia/commons/thumb/8/86/NASA-Deimos-MarsMoon-20090221.jpg/220px-NASA-Deimos-MarsMoon-20090221.jpg

5 JUPITER
Io#//upload.wikimedia.org/wikipedia/commons/thumb/c/cc/Io%2C_Earth_%26_Moon_size_comparison.jpg/220px-Io%2C_Earth_%26_Moon_size_comparison.jpg,Europa#//upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Europa_in_natural_color.png/275px-Europa_in_natural_color.png,Ganymede#//upload.wikimedia.org/wikipedia/commons/thumb/7/76/Ganymede%2C_Earth_%26_Moon_size_comparison.jpg/250px-Ganymede%2C_Earth_%26_Moon_size_comparison.jpg,Callisto#//upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Callisto_-_July_8_1979_%2838926064465%29.jpg/220px-Callisto_-_July_8_1979_%2838926064465%29.jpg

6 SATURN
Titan#//upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Titan_in_true_color_by_Kevin_M._Gill.jpg/220px-Titan_in_true_color_by_Kevin_M._Gill.jpg,Iapetus#//upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Iapetus_as_seen_by_the_Cassini_probe_-_20071008.jpg/220px-Iapetus_as_seen_by_the_Cassini_probe_-_20071008.jpg,Rhea#//upload.wikimedia.org/wikipedia/commons/thumb/a/ab/PIA07763_Rhea_full_globe5.jpg/220px-PIA07763_Rhea_full_globe5.jpg,Dione#//upload.wikimedia.org/wikipedia/commons/thumb/d/d0/Dione_in_natural_light_%28cropped%29.jpg/220px-Dione_in_natural_light_%28cropped%29.jpg,Tethys#//upload.wikimedia.org/wikipedia/commons/thumb/b/bc/PIA18317-SaturnMoon-Tethys-Cassini-20150411.jpg/220px-PIA18317-SaturnMoon-Tethys-Cassini-20150411.jpg,Mimas#//upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Mimas_Cassini.jpg/220px-Mimas_Cassini.jpg,Enceladus#//upload.wikimedia.org/wikipedia/commons/thumb/8/83/PIA17202_-_Approaching_Enceladus.jpg/280px-PIA17202_-_Approaching_Enceladus.jpg
 
7 URANUS
Titania#//upload.wikimedia.org/wikipedia/commons/thumb/4/44/Titania_Earth_Moon_Comparison.png/250px-Titania_Earth_Moon_Comparison.png,Oberon#//upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Oberon_in_true_color_by_Kevin_M._Gill.jpg/220px-Oberon_in_true_color_by_Kevin_M._Gill.jpg,Miranda#//upload.wikimedia.org/wikipedia/commons/thumb/e/e1/PIA18185_Miranda%27s_Icy_Face.jpg/220px-PIA18185_Miranda%27s_Icy_Face.jpg,Cressida#//upload.wikimedia.org/wikipedia/commons/2/2d/Cressida_enhanced_Southern_skies-2.png,Umbriel#//upload.wikimedia.org/wikipedia/commons/thumb/7/72/Umbriel_Earth_Moon_Comparison.png/250px-Umbriel_Earth_Moon_Comparison.png,Ariel#//upload.wikimedia.org/wikipedia/commons/thumb/9/92/Ariel_Earth_Moon_Comparison.png/250px-Ariel_Earth_Moon_Comparison.png

8 NEPTUNE
Naiad#//upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Naiad_Voyager.png/220px-Naiad_Voyager.png,Thalassa#//upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Neptune_Trio.jpg/220px-Neptune_Trio.jpg,Despina#//upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Despina.jpg/220px-Despina.jpg,Galatea#//upload.wikimedia.org/wikipedia/commons/thumb/d/d8/Galatea_moon.jpg/220px-Galatea_moon.jpg,Larissa#//upload.wikimedia.org/wikipedia/commons/thumb/5/5b/Larissa_1.jpg/220px-Larissa_1.jpg,Proteus#//upload.wikimedia.org/wikipedia/commons/thumb/8/83/Proteus_%28Voyager_2%29.jpg/220px-Proteus_%28Voyager_2%29.jpg,Triton#//upload.wikimedia.org/wikipedia/commons/thumb/a/a6/Triton_moon_mosaic_Voyager_2_%28large%29.jpg/300px-Triton_moon_mosaic_Voyager_2_%28large%29.jpg,Nereid#//upload.wikimedia.org/wikipedia/commons/b/b0/Nereid-Voyager2.jpg,Halimede#//upload.wikimedia.org/wikipedia/commons/thumb/0/02/Halimede.jpg/250px-Halimede.jpg,Sao#//upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Sao_VLT-FORS1_2002-09-03_annotated.gif/250px-Sao_VLT-FORS1_2002-09-03_annotated.gif,Laomedeia#//upload.wikimedia.org/wikipedia/commons/thumb/0/05/Laomedeia_VLT-FORS1_2002-09-03_annotated.gif/250px-Laomedeia_VLT-FORS1_2002-09-03_annotated.gif,Psamathe#//upload.wikimedia.org/wikipedia/commons/thumb/6/65/Psamathe_feat.jpg/250px-Psamathe_feat.jpg,Neso#//upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Neso_VLT-FORS1_2002-09-03_annotated.gif/250px-Neso_VLT-FORS1_2002-09-03_annotated.gif

9 PLUTO
Charon#//upload.wikimedia.org/wikipedia/commons/thumb/2/2e/Charon_in_True_Color_-_High-Res.jpg/220px-Charon_in_True_Color_-_High-Res.jpg,Styx#//upload.wikimedia.org/wikipedia/commons/thumb/3/38/Styx_%28moon%29.jpg/220px-Styx_%28moon%29.jpg,Nix#//upload.wikimedia.org/wikipedia/commons/thumb/5/51/Nix_best_view.jpg/220px-Nix_best_view.jpg,Kerberos#//upload.wikimedia.org/wikipedia/commons/thumb/2/23/Kerberos_%28moon%29.jpg/220px-Kerberos_%28moon%29.jpg,Hydra#//upload.wikimedia.org/wikipedia/commons/thumb/9/94/Hydra_reprocessed.png/220px-Hydra_reprocessed.png";

  $thisis=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'planet_moon_game.php');
  $lastotherde='';
  $categorizations=[];
  $wikistuffs=explode("\n", $typedescstuff);
  $wikidesignations=[];
  $wikidescriptions=[];
  $crandlist='';
  $randlist=[];
  $thisde='';
  $otherde='';
  for ($i=0; $i<sizeof($wikistuffs); $i++) {
    if (trim($wikistuffs[$i]) == '') {
    if ($thisde != '') {
    array_push($categorizations, $thisde);
    //array_push($wikidesignations, $thisde);
    //array_push($wikidescriptions, '');
    }
    $thisde='';
    } else if (trim($wikistuffs[$i]) != '') {
    if (trim($wikistuffs[$i]) != '' && trim($wikistuffs[$i]) == strtoupper(trim($wikistuffs[$i]))) {
    $thisde=substr($wikistuffs[$i],2);
    } else if ($thisde != '') {
    $otherde=$thisde; // . ' ... ' . $wikistuffs[$i];
    array_push($categorizations, $otherde);
    $thisde='';
    //} else {    
    if (trim($otherde) != '') {
    $cwikistuffs=explode(',', trim($wikistuffs[$i]));
    for ($ii=0; $ii<sizeof($cwikistuffs); $ii++) {
    if (strpos(($cwikistuffs[$ii] . '@'), '#@') !== false) {
       $bio=file_get_contents('http://en.wikipedia.org/wiki/' . str_replace(' ','_',explode('#',$cwikistuffs[$ii])[0]) . '_(moon)');
       $biois=explode('<img src="', $bio);
       if (sizeof($biois) > 1) {
       //file_put_contents('x.x', file_get_contents('x.x') . "\n" . 'http://en.wikipedia.org/wiki/' . str_replace(' ','_',explode('#',$cwikistuffs[$ii])[0]) . '_(moon)');
         $thisis=str_replace(($cwikistuffs[$ii] . ''), $cwikistuffs[$ii] . '' . explode('"', $biois[1])[0], $thisis);
         file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'planet_moon_game.php', $thisis); 
         $cwikistuffs[$ii].='' . explode('"', $biois[1])[0];
       }
    } else if (strpos($cwikistuffs[$ii], '#') === false) {
       $bio=file_get_contents('http://en.wikipedia.org/wiki/' . str_replace(' ','_',$cwikistuffs[$ii]) . '_(moon)');
       $biois=explode('<img src="', $bio);
       if (sizeof($biois) > 1) {
         $thisis=str_replace($cwikistuffs[$ii], $cwikistuffs[$ii] . '#' . explode('"', $biois[1])[0], $thisis);
         file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'planet_moon_game.php', $thisis); 
         $cwikistuffs[$ii].='#' . explode('"', $biois[1])[0];
       }
    }
    array_push($wikidesignations, $cwikistuffs[$ii]);
    array_push($wikidescriptions, $otherde);
    }
    $otherde='';
    }
    }
    }
  }
  
  //echo "1 " . sizeof($wikidesignations) . ' ' . sizeof($categorizations) . ' ' . sizeof($wikidescriptions);
  //exit;
  
  $randmode=rand(0,1);
  $badlist=',Mercury,Venus,';
  
  $correctans=rand(0,8);
  $sofar=';';
  for ($i=0; $i<9; $i++) {
    $j=rand(0, (-1 + sizeof($wikidesignations)));
    if ($crandlist == '') {
      $crandlist='' . $j;
      $sofar.=$wikidescriptions[$j] . ';';
    } else if (strpos((',' . $crandlist . ','), (',' . $j . ',')) !== false) { // || strpos($sofar, ';' . $wikidescriptions[$j] . ';') !== false) {
      while (strpos((',' . $crandlist . ','), (',' . $j . ',')) !== false) { // || strpos($sofar, ';' . $wikidescriptions[$j] . ';') !== false) {
        $j=rand(0, (-1 + sizeof($wikidesignations)));
      }
      $crandlist.=',' . $j;
      $sofar.=$wikidescriptions[$j] . ';';
    } else {
      if (strpos($sofar, ';' . $wikidescriptions[$j] . ';') !== false) { $badlist.=$wikidescriptions[$j] . ','; }
      $crandlist.=',' . $j;
      $sofar.=$wikidescriptions[$j] . ';';
    }
  }
  
  
  $sofar=';';
  $ccrandlist='';
  for ($i=1; $i<=9; $i++) {
    $j=rand(1, 9);
    if ($ccrandlist == '') {
      $ccrandlist='' . $j;
      $sofar.='' . $j . ';';
    } else if (strpos((',' . $ccrandlist . ','), (',' . $j . ',')) !== false) {
      while (strpos((',' . $ccrandlist . ','), (',' . $j . ',')) !== false) {
        $j=rand(1, 9);
      }
      $ccrandlist.=',' . $j;
      $sofar.='' . $j . ';';
    } else {
      $ccrandlist.=',' . $j;
      $sofar.='' . $j . ';';
    }
  }
  
  $randlist=explode(',', "1,2,3,4,5,6,7,8,9"); //$crandlist);
  $randlist=explode(',', $crandlist);
  $rrandlist=explode(',', $ccrandlist);
  
  $callbackurl='./planet_moon_game.php';
  $templategame=file_get_contents('./experimental_drag_and_drop.htm');

  for ($i=1; $i<=9; $i++) {
    $templategame=str_replace('>' . $i . '<', '>##' . $rrandlist[-1 + $i] . '<', $templategame);
  }
  
  $templategame=str_replace('>##', '>', $templategame);
  $templategame=str_replace('<table ', '<table cellspacing=8 cellpadding=8 ', $templategame);

  $templategame=str_replace('</h4>', '<input type=hidden id="callback" value="' . $callbackurl . '"></input></h4>', $templategame);
  $templategame=str_replace('>Game<', '>Planet Mo' . 'on Game <font size=1>... drag to matching correct answer to score</font><', $templategame);
  $templategame=str_replace('></h3>', '>' . $scorebit . '</h3>', $templategame);
  $thepsource=explode('f0;">', explode("</p>", $templategame)[0])[-1 + sizeof(explode('f0;">', explode("</p>", $templategame)[0]))];

  $templategame=str_replace(', 2023<', ', 2023 ... thanks to <a target=_blank href=https://wikipedia.org>Wikipedia</a><', $templategame);
  $templategame=str_replace('>Reset example<', ' style="display:none;">Reset example<', $templategame);
  
  //echo $crandlist . "\n" . $thepsource . "\n" . $correctans . "\n";
  //exit;
  //echo $crandlist . "\n";
  //echo "" . $randlist[$correctans] . "\n" . $wikidescriptions[$randlist[$correctans]] . "\n" . $wikidesignations[$randlist[$correctans]];
  //exit;
  
  //file_put_contents('x.x', '' . $randmode . "\n" . $crandlist . "\n" . $thepsource . "\n" . $correctans . "\n" . $crandlist . "\n" . $randlist[$correctans] . "\n" . $wikidescriptions[$randlist[$correctans]] . "\n" . $wikidesignations[$randlist[$correctans]]);
  
  if ($randmode == 0) {  // designation up top and descriptions in drop zone
    while (strpos($badlist, ',' . explode(' ... ', $wikidescriptions[$randlist[$correctans]])[0] . ',') !== false) {
      $correctans=rand(0,8);
    }
    $templategame=str_replace('>' . (1 + $correctans) . '<', ' data-answer="' . explode(' ... ', $wikidescriptions[$randlist[$correctans]])[0] . '">' . $categorizations[$correctans] . '<', $templategame);
    if (strpos($wikidesignations[$randlist[$correctans]], '#') !== false) {
    $templategame=str_replace(' style="background-color:#f0f0f0;"', ' ', $templategame);
    $templategame=str_replace('>' . $thepsource . '<', ' data-answer="' . explode(' ... ', $wikidescriptions[$randlist[$correctans]])[0] . '" style="background-color:#f0f0f0;background:URL(' . str_replace('http:','',explode('#', $wikidesignations[$randlist[$correctans]])[1]) . '); background-repeat: no-repeat; background-size: contain;background-position: center right;border:5px dashed pink;" alt="f0;">&nbsp;<br>' . explode('#', $wikidesignations[$randlist[$correctans]])[0] . '<br>&nbsp;<', $templategame);
    } else {
    $templategame=str_replace('>' . $thepsource . '<', ' data-answer="' . explode(' ... ', $wikidescriptions[$randlist[$correctans]])[0] . '">' . explode('#', $wikidesignations[$randlist[$correctans]])[0] . '<', $templategame);
    }
    for ($i=1; $i<=9; $i++) {
       if ((-1 + $i) != $correctans) {
    $templategame=str_replace('>' . $i . '</td', ' data-answer="' . explode(' ... ', $categorizations[-1 + $i])[0] . '">' . $categorizations[-1 + $i] . '</td', $templategame);
       }
    }
    $templategame=str_replace(' contenteditable=', ' data-contenteditable=', $templategame);
    echo $templategame;
  } else { // description up top and designations in drop zone
    while (strpos($badlist, ',' . explode(' ... ', $wikidescriptions[$randlist[$correctans]])[0] . ',') !== false) {
      $correctans=rand(0,8);
    }
    if (strpos($wikidesignations[$randlist[$correctans]], '#') !== false) {
    $templategame=str_replace(' style="background-color:#f0f0f0;"', ' ', $templategame);
    $templategame=str_replace('>' . (1 + $correctans) . '<', ' data-answer="' . explode('#', $wikidesignations[$randlist[$correctans]])[0] . '" style="background-color:#f0f0f0;background:URL(' . str_replace('http:','',explode('#', $wikidesignations[$randlist[$correctans]])[1]) . '); background-repeat: no-repeat; background-size: contain;background-position: center right;" alt="f0;">' . explode('#', $wikidesignations[$randlist[$correctans]])[0] . '<', $templategame);
    } else {
    $templategame=str_replace('>' . (1 + $correctans) . '<', ' data-answer="' . explode('#', $wikidesignations[$randlist[$correctans]])[0] . '">' . explode('#', $wikidesignations[$randlist[$correctans]])[0] . '<', $templategame);
    }
    if (strpos($wikidesignations[$randlist[$correctans]], '#') !== false && 1 == 7) {
    $templategame=str_replace('>' . $thepsource . '<', ' data-answer="' . explode('#',$wikidesignations[$randlist[$correctans]])[0] . '" style="background:URL(' . str_replace('http:','',explode('#', $wikidesignations[$randlist[$correctans]])[1]) . '); background-repeat: no-repeat; background-size: contain;background-position: center right;border:5px dashed pink;" alt="f0;">&nbsp;<br>' . $wikidescriptions[$randlist[$correctans]] . '<br>&nbsp;<', $templategame);
    } else {
    $templategame=str_replace(' style="border:5px dashed pink;background-color:#f0f0f0;"', ' ', $templategame);
    $templategame=str_replace('>' . $thepsource . '<', ' style="border:5px dashed pink;background-color:#f0f0f0;" data-answer="' . explode('#', $wikidesignations[$randlist[$correctans]])[0] . '">' . $wikidescriptions[$randlist[$correctans]] . '<', $templategame);
    }
    for ($i=1; $i<=9; $i++) {
       if ((-1 + $i) != $correctans) {
    if (strpos($wikidesignations[$randlist[-1 + $i]], '#') !== false) {
    $templategame=str_replace('>' . $i . '</td', ' data-answer="" style="background:URL(' . str_replace('http:','',explode('#', $wikidesignations[$randlist[-1 + $i]])[1]) . '); background-repeat: no-repeat; background-size: contain;background-position: center right;" alt="f0;">' . explode('#', $wikidesignations[$randlist[-1 + $i]])[0] . '</td', $templategame);
    } else {
    $templategame=str_replace('>' . $i . '</td', ' data-answer="">' . explode('#', $wikidesignations[$randlist[-1 + $i]])[0] . '</td', $templategame);
    }
       }
    }
    $templategame=str_replace(' contenteditable=', ' data-contenteditable=', $templategame);
    echo $templategame;
  }
  

?>