<?php
// animal_mineral_vegetable.php
// August, 2023
// RJM Programming
// Using experimental_drag_and_drop.htm for Animal/Mineral/Vegetable game

$cols=['yellow','cyan','pink'];
$vclasses=['peach', 'eggplant', 'tangerine', 'strawberry', 'pineapple', 'watermelon', 'grapes', 'banana', 'cherries', 'red_apple', 'lemon', 'green_apple', 'pear', 'hot_pepper', 'tomato', 'melon'];
$vcontents=['127825', '127814', '127818', '127827', '127821', '127817', '127815', '127820', '127826', '127822', '127819', '127823', '127824', '127798', '127813', '127816'];
// Thanks to https://www.webnots.com/alt-code-shortcuts-for-animals-symbols/
$animaltbody='>1F435 Alt + X<>Alt + 128018<>Alt + 129421<>Alt + 129447<>Alt + 128054<>Alt + 128021<>Alt + 129454<>Alt + 128041<>Alt + 128058<
>Alt + 129418<>Alt + 129437<>Alt + 128049<>Alt + 128008<>Alt + 129409<>Alt + 128047<>Alt + 128005<
>Alt + 128006<>Alt + 128052<>Alt + 128014<>Alt + 129412<>Alt + 129427<>Alt + 129743<>Alt + 129742<>Alt + 129420<
>Alt + 129452<>Alt + 128046<>Alt + 128002<>Alt + 128003<>Alt + 128004<>Alt + 128055<>Alt + 128022<>Alt + 128023<
>Alt + 128061<>Alt + 128015<>Alt + 128017<>Alt + 128016<>Alt + 128042<>Alt + 128043<>Alt + 129433<>Alt + 129426<
>Alt + 128024<>Alt + 129443<>Alt + 129423<>Alt + 129435<>Alt + 128045>Alt + 128001<
>Alt + 128000<>Alt + 128057<>Alt + 128048<>Alt + 128007<>Alt + 128063<>Alt + 129451<>Alt + 129428<>Alt + 129415<>Alt + 128059<>Alt + 128040<
>Alt + 128060<>Alt + 129445<>Alt + 129446<>Alt + 129448<>Alt + 129432<>Alt + 129441<>Alt + 128062<>Alt + 129411<>Alt + 128020<>Alt + 128019<
>Alt + 128035<>Alt + 128036<>Alt + 128037<>Alt + 128038<>Alt + 128039<>Alt + 128330<>Alt + 129413<>Alt + 129414<>Alt + 129442<>Alt + 129417<
>Alt + 129444<>Alt + 129718<>Alt + 129449<>Alt + 129434<>Alt + 129436<>Alt + 129725<>Alt + 129727<>Alt + 128056<>Alt + 128010<>Alt + 128034<
>Alt + 129422<>Alt + 128013<>Alt + 128050<>Alt + 128009<>Alt + 129429<>Alt + 129430<>Alt + 128051<>Alt + 128011<>Alt + 128044<
>Alt + 129453<>Alt + 128031<>Alt + 128032<>Alt + 128033<>Alt + 129416<>Alt + 128025<>Alt + 128026<>Alt + 129720<
>Alt + 129724<>Alt + 128012<>Alt + 129419<>Alt + 128027<>Alt + 128028<>Alt + 128029<>Alt + 129714<>Alt + 128030<>Alt + 129431<
>Alt + 129715<>Alt + 128375<>Alt + 128376<>Alt + 129410<>Alt + 129439<>Alt + 129712<>Alt + 129713<>Alt + 129440<';

function mapit($inidea) {
  global $vclasses, $vcontents, $animaltbody;
  $outidea=$inidea;
  if (strtolower($inidea) == 'vegetable') {
    $ifv=rand(1, sizeof($vclasses));
    return '<font size=10>&#' . $vcontents[-1 + $ifv] . ';</font>';
  } else {
    $ifv=rand(128000, 129742);
    if (strtolower($inidea) == 'animal') {
    if (strpos($animaltbody, ' + ' . $ifv . '<') !== false) {
    return '<font size=10>&#' . $ifv . ';</font>';
    } else {
    while (strpos($animaltbody, ' + ' . $ifv . '<') === false) {
    $ifv=rand(128000, 129742);
    }
    return '<font size=10>&#' . $ifv . ';</font>';
    }
    } else if (strtolower($inidea) != 'animal') {
    return '<font size=10>&#' . rand(128640, 128676) . ';</font>';
    }
  }
  return $outidea; 
}

function mapittwo($inidea) {
  global $vclasses, $vcontents, $animaltbody;
  $outidea=$inidea;
  if (strtolower($inidea) == strtolower('Vegetable')) {
    $ifv=rand(1, sizeof($vclasses));
    return '<font size=10>&#' . $vcontents[-1 + $ifv] . ';</font>';
  } else {
    $ifv=rand(128000, 129742);
    if (strtolower($inidea) == strtolower('Animal')) {
    if (strpos($animaltbody, ' + ' . $ifv . '<') !== false) {
    return '<font size=10>&#' . $ifv . ';</font>';
    } else {
    while (strpos($animaltbody, ' + ' . $ifv . '<') === false) {
    $ifv=rand(128000, 129742);
    }
    return '<font size=10>&#' . $ifv . ';</font>';
    }
    } else if (strtolower($inidea) != strtolower('Animal')) {
    return '<font size=10>&#' . rand(128640, 128676) . ';</font>';
    }
  }
  return $outidea; 
}

 $scorebit='';
 if (isset($_GET['score']) && isset($_GET['secs'])) {
   $scorebit="Score " . $_GET['score'] . '/' . $_GET['secs'];
 }

 $wikistuff="animal Animal
mineral Mineral
vegetable Vegetable";

  $lastotherde='';
  $wikistuffs=explode("\n", $wikistuff);
  $wikidesignations=[];
  $wikidescriptions=[];
  $crandlist='';
  $randlist=[];
  for ($i=0; $i<sizeof($wikistuffs); $i++) {
    $thisde=explode(' ', $wikistuffs[$i])[0];
    $otherde=substr($wikistuffs[$i], (strlen($thisde) + 1));
    if (strpos($otherde, ') ') !== false) {
      $otherde=explode(') ', $otherde)[1];
    }
    if (trim($thisde) != '') {
    array_push($wikidesignations, $thisde);
    array_push($wikidescriptions, trim($otherde));
    $lastotherde=$otherde;
    }
  }
  
  for ($i=0; $i<3; $i++) {
    $j=rand(0, (-1 + sizeof($wikidesignations)));
    if ($crandlist == '') {
      $crandlist='' . $j;
    } else if (strpos((',' . $crandlist . ','), (',' . $j . ',')) !== false) {
      while (strpos((',' . $crandlist . ','), (',' . $j . ',')) !== false) {
        $j=rand(0, (-1 + sizeof($wikidesignations)));
      }
      $crandlist.=',' . $j;
    } else {
      $crandlist.=',' . $j;
    }
  }
  
  $randlist=explode(',', $crandlist);
  $randmode=rand(0,1);
  $correctans=rand(0,2);
  
  $callbackurl='./animal_mineral_vegetable.php';
  $dru='http://' . $_SERVER['SERVER_NAME'] . "" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "" . explode("#", explode("?", str_replace('/animal_mineral_vegetable.php','/experimental_drag_and_drop.htm',$_SERVER['REQUEST_URI']))[0])[0];
  //$templategame=file_get_contents($dru . '?tdnum=3');
  $templategame=str_replace("('' + ev.target.getAttribute('data-answer')) == ('' + document.getElementById('source').getAttribute('data-answer'))", "('' + ev.target.getAttribute('data-answer')).toLowerCase() == ('' + document.getElementById('source').getAttribute('data-answer')).toLowerCase() || ('' + ev.target.getAttribute('data-answer')) == 'null'", str_replace('var tdnum=', 'var tdnum=3; /' . '/ ',str_replace('<tr id=tr3><td id="td31">7</td><td id="td32">8</td><td id="td33">9</td></tr>','',str_replace('<tr id=tr2><td id="td21">4</td><td id="td22">5</td><td id="td23">6</td></tr>','',file_get_contents('./experimental_drag_and_drop.htm')))));
  $ione=rand(0,2);
  $itwo=$ione;
  $ithree=$ione;
  while ($itwo == $ione) {
  $itwo=rand(0,2);
  }
  while ($ithree == $ione || $ithree == $itwo) {
  $ithree=rand(0,2);
  }
  $templategame=str_replace('</body>', '<style> #td11 { background-color:' . $cols[$ione] . '; }  #td12 { background-color:' . $cols[$itwo] . '; }  #td13 { background-color:' . $cols[$ithree] . '; } </style></body>', $templategame);
  
  $templategame=str_replace('</h4>', '<input type=hidden id="callback" value="' . $callbackurl . '"></input></h4>', $templategame);
  $templategame=str_replace('>Game<', '>Animal Mineral Vegetable 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://en.wikipedia.org/wiki/Street_suffix>https://en.wikipedia.org/wiki/Street_suffix</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;
  
  if ($randmode == 0) {  // designation up top and descriptions in drop zone
    $templategame=str_replace('>' . (1 + $correctans) . '<', ' style=text-align:center; data-answer="' . $wikidescriptions[$randlist[$correctans]] . '">' . $wikidescriptions[$randlist[$correctans]] . '<', $templategame);
    $templategame=str_replace('">' . $thepsource . '<', 'text-align:center;" data-answer="' . $wikidescriptions[$randlist[$correctans]] . '">' . mapit($wikidesignations[$randlist[$correctans]]) . '<', $templategame);
    for ($i=1; $i<=3; $i++) {
       if ((-1 + $i) != $correctans) {
    $templategame=str_replace('>' . $i . '</td', ' style=text-align:center; data-answer="">' . $wikidescriptions[$randlist[-1 + $i]] . '</td', $templategame);
       }
    }
    $templategame=str_replace(' contenteditable=', ' data-contenteditable=', $templategame);
    echo $templategame;
  } else { // description up top and designations in drop zone
    $templategame=str_replace('>' . (1 + $correctans) . '<', ' style=text-align:center; data-answer="' . $wikidesignations[$randlist[$correctans]] . '">' . mapittwo($wikidesignations[$randlist[$correctans]]) . '<', $templategame);
    $templategame=str_replace('">' . $thepsource . '<', 'text-align:center;" data-answer="' . $wikidesignations[$randlist[$correctans]] . '">' . $wikidescriptions[$randlist[$correctans]] . '<', $templategame);
    for ($i=1; $i<=3; $i++) {
       if ((-1 + $i) != $correctans) {
    $templategame=str_replace('>' . $i . '</td', ' style=text-align:center; data-answer="">' . mapittwo($wikidesignations[$randlist[-1 + $i]]) . '</td', $templategame);
       }
    }
    $templategame=str_replace(' contenteditable=', ' data-contenteditable=', $templategame);
    echo $templategame;
  }
  

?>
