<?php
// street_type_game.php
// July, 2023
// RJM Programming
// Using experimental_drag_and_drop.htm for Australian street types
// Thanks to https://en.wikipedia.org/wiki/Street_suffix 

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

 $wikistuff="Alley (Ally) Usually narrow roadway in cities or towns, often through city block or squares.
Approach (App) Roadway leading to an area of community interest, such as a public open space, commercial area, beach etc.
Arcade (Arc) Passage having an arched roof, or any covered passageway, especially one with shops along the sides.
Avenue (Av) Broad roadway, usually planted on each side with trees.
Boardwalk (Bwlk) Promenade or path, especially of wooden planks, for pedestrians and sometimes vehicles, along or overlooking a beach or waterfront.
Boulevard (Bvd) Wide roadway, well paved, usually ornamented with trees and grass plots.
Break (Brk) Vehicular access on a formed or unformed surface, which was originally prepared as a firebreak.
Bypass (Bypa) Alternative roadway constructed to enable through traffic to avoid congested areas or other obstructions to movement.
Chase (Ch) Roadway leading down to a valley.
Circuit (Cct) Roadway enclosing an area.
Close (Cl) Short, enclosed roadway.
Concourse (Con) Roadway that runs around a central area, such as a public open space or commercial area.
Court (Ct) Short, enclosed roadway.
Crescent (Cr) Crescent-shaped thoroughfare, especially where both ends join the same thoroughfare.
Crest (Crst) Roadway running along the top or summit of a hill.
Drive (Dr) Winding thoroughfare allowing a steady flow of traffic without many cross-streets.
Entrance (Ent) Roadway connecting other roads.
Esplanade (Esp) Level roadway, often along the seaside, lake or a river.
Firetrail (Ftrl) Vehicular access on a formed or unformed surface, which was originally prepared as a firebreak.
Freeway (Fwy) Express, multi-lane highway, with limited or controlled access.
Glade (Glde) Roadway usually in a valley of trees.
Grange (Gra) Roadway leading to a country estate, or focal point, public open space, shopping area etc.
Grove (Gr) Roadway that features a group of trees standing together.
Highway (Hwy) Main road or thoroughfare, a main route. Specifically reserved for roads associated with state arterial road networks, restricted to roads of strategic importance constructed to a high standard.
Lane Narrow way between walls, buildings or a narrow country or city roadway.
Loop Roadway that diverges from and re-joins the main thoroughfare.
Mall Sheltered walk, promenade or shopping precinct.
Mews Roadway in a group of houses.
Parade (Pde) Public promenade or roadway that has good pedestrian facilities along the side.
Parkway (Pwy) Roadway through parklands or an open grassland area.
Passage (Psge) Narrow street for pedestrians.
Path Roadway used only for pedestrian traffic.
Place (Pl) Short, sometimes narrow, enclosed roadway.
Plaza (Plza) Roadway enclosing the four sides of an area forming a market place or open space.
Promenade (Prom) Roadway like an avenue with plenty of facilities for the public to take a leisurely walk, a public place for walking.
Quays (Qys) Roadway leading to a landing place alongside or projecting into water.
Ramp Access road to and from highways and freeways.
Retreat (Rtt) Roadway forming a place of seclusion.
Ridge (Rdge) Roadway along the top of a hill.
Rise Roadway going to a higher place or position.
Road (Rd) Open way or public passage primarily for vehicles.
Square (Sq) Roadway bounding the four sides of an area to be used as an open space or a group of buildings.
Steps (Stps) Route consisting mainly of steps.
Street (St) Public roadway in a town, city or urban area, especially a paved thoroughfare with footpaths and buildings along one or both sides.
Subway (Sbwy) Underground passage or tunnel that pedestrians use for crossing under a road, railway, river etc.
Terrace (Tce) Roadway usually with houses on either side raised above the road level.
Track (Trk) Roadway with a single carriageway. A roadway through a natural bushland region. The interpretation for both Track and Trail is limited to roadways, whereas in many areas, like Tasmania, these are often associated with walking rather than vehicular movement.
Trail (Trl) See ‘Track’.
View Roadway commanding a wide panoramic view across surrounding areas.
Vista (Vsta) Roadway with a view or outlook.
Walk Thoroughfare with restricted access used mainly by pedestrians.
Way Roadway affording passage from one place to another. Usually not as straight as an avenue or street.
Wharf (Whrf) A roadway on a wharf or pier.";

  $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, str_replace("See ‘Track’.", $lastotherde,trim($otherde)));
    $lastotherde=$otherde;
    }
  }
  
  for ($i=0; $i<9; $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,8);
  
  $callbackurl='./street_type_game.php';
  $templategame=file_get_contents('./experimental_drag_and_drop.htm');
  
  $templategame=str_replace('</h4>', '<input type=hidden id="callback" value="' . $callbackurl . '"></input></h4>', $templategame);
  $templategame=str_replace('>Game<', '>Australian Street Types 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) . '<', ' data-answer="' . $wikidescriptions[$randlist[$correctans]] . '">' . $wikidescriptions[$randlist[$correctans]] . '<', $templategame);
    $templategame=str_replace('>' . $thepsource . '<', ' data-answer="' . $wikidescriptions[$randlist[$correctans]] . '">' . $wikidesignations[$randlist[$correctans]] . '<', $templategame);
    for ($i=1; $i<=9; $i++) {
       if ((-1 + $i) != $correctans) {
    $templategame=str_replace('>' . $i . '</td', ' 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) . '<', ' data-answer="' . $wikidesignations[$randlist[$correctans]] . '">' . $wikidesignations[$randlist[$correctans]] . '<', $templategame);
    $templategame=str_replace('>' . $thepsource . '<', ' data-answer="' . $wikidesignations[$randlist[$correctans]] . '">' . $wikidescriptions[$randlist[$correctans]] . '<', $templategame);
    for ($i=1; $i<=9; $i++) {
       if ((-1 + $i) != $correctans) {
    $templategame=str_replace('>' . $i . '</td', ' data-answer="">' . $wikidesignations[$randlist[-1 + $i]] . '</td', $templategame);
       }
    }
    $templategame=str_replace(' contenteditable=', ' data-contenteditable=', $templategame);
    echo $templategame;
  }
  

?>
