Skip to content

Commit

Permalink
Fix compass related bugs and wrong connections being created on grid …
Browse files Browse the repository at this point in the history
…maps
  • Loading branch information
louve committed Aug 19, 2021
1 parent c63bbef commit f4befc6
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 66 deletions.
52 changes: 32 additions & 20 deletions commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,47 +83,47 @@ function command_direction($db, $elements, $player, $direction){
if (is_player_dimension_grid_based($db, $player) && is_cardinal_directions($direction)){
// For stacked locations we make sure you can always go in all directions by only checking connections with root location
// Note: THIS IS NOT NECESSARY normally, as all necessary connections are created upon page creation...
$page_id = get_first_page_with_position($db, $player["position"]);
$page_id = get_first_page_with_position($db, $player["position"], $player["dimension"]);
}

$page = get_page($db, $page_id, $player);

if (isset($page["outputs"][$direction])){
$newId = $page["outputs"][$direction]["destination"];
$new_id = $page["outputs"][$direction]["destination"];

player_give_vision_on_page($db, $player["id"], $newId);
player_give_vision_on_page($db, $player["id"], $new_id);

$newPage = get_page($db, $newId, $player);
$new_page = get_page($db, $new_id, $player);


// HP Calculation
$change = 0;
foreach($newPage["hp_events"] as $hpChange){
foreach($new_page["hp_events"] as $hpChange){
$change += $hpChange;
}
$player["hp"] += $change;
$player["hp"] = min($player["hp"], MAX_HP);

if ($player["hp"] <= 0){
$firstPage = get_starting_page_id_for_player($db, $player);
$db->prepare("UPDATE player SET page_id=? WHERE id=?")->execute([$firstPage."", $player["id"]]);
$first_page = get_starting_page_id_for_player($db, $player);
$db->prepare("UPDATE player SET page_id=? WHERE id=?")->execute([$first_page."", $player["id"]]);
$db->prepare("UPDATE player SET hp=? WHERE id=?")->execute([BASE_HP, $player["id"]]);
$db->prepare("DELETE FROM player_prop WHERE player_id=?")->execute([$player["id"]]);
return_200("death", array_merge(get_page($db, $firstPage, $player), ["death_page"=>$newPage]));
return_200("death", array_merge(get_page($db, $first_page, $player), ["death_page"=>$new_page]));
}
else if ($change != 0){
$db->prepare("UPDATE player SET hp=? WHERE id=?")->execute([$player["hp"], $player["id"]]);
// $newPage["content"].= "<br>"."(You currently have ".$player["hp"]." health points)";
// $new_page["content"].= "<br>"."(You currently have ".$player["hp"]." health points)";
}

// Returning results
if ($newPage["is_dead_end"]){
unset($newPage["hourglass"]);
return_200("dead_end", $newPage);
if ($new_page["is_dead_end"]){
unset($new_page["hourglass"]);
return_200("dead_end", $new_page);
}

$db->prepare("UPDATE player SET page_id=? WHERE id=?")->execute([$newId, $player["id"]]);
return_200("page", $newPage);
$db->prepare("UPDATE player SET page_id=? WHERE id=?")->execute([$new_id, $player["id"]]);
return_200("page", $new_page);
}
else{
$state = $db->prepare("SELECT readonly FROM dimension WHERE id=?");
Expand Down Expand Up @@ -344,6 +344,8 @@ function command_warp($db, $elements, $player){

$page = get_page($db, $page_id, $player);
$page["dimension_name"] = $player["dimension_name"];
$page["dimension_starting_page_name"] = explode("\n", $page["content"])[0];

$page["pages_count"] = get_page_count_in_dimension($db, $player["dimension"]);

$db->prepare("UPDATE player SET page_id=? WHERE id=?")->execute([$page["id"], $player["id"]]);
Expand Down Expand Up @@ -427,9 +429,9 @@ function command_get_location($db, $elements, $player){

return_200("status",
"You're currently "
.($y != 0 ? abs($y)." leagues ".($y != 0 ? "north" : "south") : "")
.($y != 0 ? abs($y)." league".(abs($y)>1 ? "s" : "")." ".($y < 0 ? "south" : "north") : "")
.($y*$x != 0 ? " and " : "")
.($x != 0 ? abs($x)." leagues ".($x < 0 ? "east" : "west") : "")
.($x != 0 ? abs($x)." league".(abs($x)>1 ? "s" : "")." ".($x < 0 ? "west" : "east") : "")
." from this region's landmark.");
}

Expand Down Expand Up @@ -583,18 +585,28 @@ function command_goto($db, $elements, $player){
$state = $db->prepare("SELECT id FROM page WHERE content LIKE ?");
$state->execute([$name."\n%"]);
$data = $state->fetch();

$id = $name;

if ($data === false){
if (count($elements) > 0 && intval($elements[0])){
$data = array("id"=> $elements[0]);
$data = array("id"=> intval($elements[0]));
}
else{
// Maybe it's an ID
return_200("status", "No such page as ".$name);
}
}

$page = get_page($db, $data["id"], $player);
$db->prepare("UPDATE player SET page_id=? WHERE id=?")->execute([$data["id"], $player["id"]]);
return_200("page", $page);
try{
player_give_vision_on_page($db, $player["id"], $data["id"]);
$page = get_page($db, $data["id"], $player);
$db->prepare("UPDATE player SET page_id=? WHERE id=?")->execute([$data["id"], $player["id"]]);
return_200("page", $page);
}
catch(Exception $e){
return_200("status", ("Error during goto with elements ".json_encode($elements)." and data ".json_encode($data)." and ID ".$data["id"].": no such page as ".($e->getMessage())));
}
}

function command_ban_client($db, $elements, $player){
Expand Down
6 changes: 6 additions & 0 deletions communicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ function recover($db, $p, $player){

$content["dimension_type"] = is_player_dimension_grid_based($db, $player) ? "GRID" : "BRANCH";
$content["dimension_name"] = $player["dimension_name"];

// Starting page name
$starting_page_id = get_starting_page_id_for_player($db, $player);
$starting_page = get_page($db, $starting_page_id, $player);

$content["dimension_starting_page_name"] = explode("\n", $starting_page["content"])[0];
$content["pages_count"] = get_page_count_in_dimension($db, $player["dimension"]);

echo json_encode([
Expand Down
8 changes: 4 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function stringify_biome($biome, $contents){
<div id="mainContainer">
<div id="title">
<h1>
<span style='color:grey;'><a style="color:inherit; text-decoration:inherit;" href='https://louve.systems/'>LOUVESYSTEMS</a>'</span> <b>ADVNTURE</b> <span style='color:grey;'>V2.0.2<span style="float:right;" id="dimensionInfo"></span>
<span style='color:grey;'><a style="color:inherit; text-decoration:inherit;" href='https://louve.systems/'>LOUVESYSTEMS</a>'</span> <b>ADVNTURE</b> <span style='color:grey;'>V2.0.3<span style="float:right;" id="dimensionInfo"></span>
</h1>
</div>
<div class="terminal">
Expand All @@ -68,8 +68,8 @@ function stringify_biome($biome, $contents){
Join the <a style="color:white;" target="_blank" href="https://discord.gg/WQWZBN3">DISCORD</a>!
</div>
<div style="text-align:center;">
<!--Here's a <a style="color:white;" target="_blank" href="https://www.youtube.com/watch?v=QIxRDU_l8Bc&loop=1">fitting soundtrack</a> if you need one! -->
<a style="color:orange;font-weight:bold;font-size:1.5em;" target="_blank" href="https://portfolio.louve.systems/">HIRE ME!</a><br> I'm looking for a job as a Game Designer!
Here's a <a style="color:white;" target="_blank" href="https://www.youtube.com/watch?v=QIxRDU_l8Bc&loop=1">fitting soundtrack</a> if you need one!
<!-- <a style="color:orange;font-weight:bold;font-size:1.5em;" target="_blank" href="https://portfolio.louve.systems/">HIRE ME!</a><br> I'm looking for a job as a Game Designer!-->
</div>
<div style="text-align:right;">
Follow me on <a style="color:white;" target="_blank" href="https://twitter.com/Rackover">TWITTER</a>!
Expand Down Expand Up @@ -174,7 +174,7 @@ function stringify_biome($biome, $contents){
<span style='color:red;'>=============================<br><br></span>
<b style='color:red';>You fainted!</b><br><br>
Exhausted by your journey, you feel your limbs become numb and you decide to shut your eyes for a moment.
When you regain consciousness, you find yourself in the <span style='color:white;'>Forest outskirts</span>, stripped from all your belongings.<br><br>
When you regain consciousness, you find yourself in the <span style='color:white;'>%starting_page_name</span>, stripped from all your belongings.<br><br>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion map.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function get_html_characters_map($db, $player, $radius=3){

if (is_player_dimension_grid_based($db, $player)){

$page_id = get_first_page_with_position($db, $str_position);
$page_id = get_first_page_with_position($db, $str_position, $player["dimension"]);

if ($page_id){
$arr_position = explode(" ", $str_position);
Expand Down
73 changes: 42 additions & 31 deletions page.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@
define("ALLOW_BANNED_SUBMISSIONS", true);



function get_starting_page_id_for_player($db, $player){
$statement = $db->prepare(
"SELECT starting_page FROM dimension
WHERE id=?");
$statement->execute([$player["dimension"]]);
$id = $statement->fetch()["starting_page"];

return $id;
}

function get_starting_page_id_for_dimension($db, $dimension_id){
$statement = $db->prepare(
"SELECT starting_page FROM dimension
Expand All @@ -28,6 +17,10 @@ function get_starting_page_id_for_dimension($db, $dimension_id){
return $id;
}

function get_starting_page_id_for_player($db, $player){
return get_starting_page_id_for_dimension($db, $player["dimension"]);
}

function get_starting_page_id($db){
$statement = $db->prepare(
"SELECT starting_page FROM dimension
Expand All @@ -47,7 +40,7 @@ function is_cardinal_directions($dir){
return in_array($dir, $haystack);
}

function get_surrounding_pages_ids($db, $given_str_position){
function get_surrounding_pages_ids($db, $given_str_position, $dimension){

$directions = get_cardinal_directions();
$pages_ids = [];
Expand All @@ -59,7 +52,7 @@ function get_surrounding_pages_ids($db, $given_str_position){
$shift = get_cardinal_direction_position_shift($direction);
$str_position = ($numerical_position["x"]+$shift["x"])." ".($numerical_position["y"]+$shift["y"]);

$pages_at_position = get_all_pages_with_position($db, $str_position);
$pages_at_position = get_all_pages_with_position($db, $str_position, $dimension);
$pages_ids[$direction] = [];

foreach($pages_at_position as $page_id){
Expand All @@ -70,23 +63,25 @@ function get_surrounding_pages_ids($db, $given_str_position){
return $pages_ids;
}

function get_first_page_with_position($db, $str_position){
function get_first_page_with_position($db, $str_position, $dimension){
$statement = $db->prepare(
"SELECT id FROM page
WHERE position=?
AND dimension_id=?
ORDER BY id ASC");
$statement->execute([$str_position]);
$statement->execute([$str_position, $dimension]);
$data = $statement->fetch();

return $data === false ? false : $data["id"];
}

function get_all_pages_with_position($db, $str_position){
function get_all_pages_with_position($db, $str_position, $dimension){
$statement = $db->prepare(
"SELECT id FROM page
WHERE position=?
AND dimension_id=?
ORDER BY id ASC");
$statement->execute([$str_position]);
$statement->execute([$str_position, $dimension]);

$ids = [];

Expand All @@ -98,6 +93,7 @@ function get_all_pages_with_position($db, $str_position){
}

function get_page_hourglass_info($db, $page_id, $player){

$statement = $db->prepare(
"
SELECT
Expand Down Expand Up @@ -191,12 +187,13 @@ function get_page_position($db, $page_id){
return ["x"=>$x, "y"=>$y];
}

function is_position_occupied($db, $position){
function is_position_occupied($db, $position, $dimension){

$statement = $db->prepare(
"SELECT id FROM page
WHERE position=?");
$statement->execute([$position]);
WHERE position=?
AND dimension_id=?");
$statement->execute([$position, $dimension]);
$data = $statement->fetch();

return $data != false;
Expand Down Expand Up @@ -280,9 +277,8 @@ function get_page($db, $id, $player){
$statement->execute([ $id ]);
$result = $statement->fetchAll();

if ($result === false || $result[0]["content"] == null){
var_dump($id);
echo false;
if ($result === false || count($result) <= 0 || !isset($result[0]) || $result[0]["content"] == null){
throw new Exception("".$id);
// This should never happen
}

Expand Down Expand Up @@ -340,12 +336,12 @@ function get_page($db, $id, $player){
}

// HP Event
$hpEventsDone = [];
$hp_events_done = [];
foreach($result as $he){
if ($he["hp_change"] === null) continue;
if (in_array($he["id"], $hpEventsDone)) continue;
if (in_array($he["id"], $hp_events_done)) continue;
$page["hp_events"][] = $he["hp_change"];
$hpEventsDone[] = $he["id"];
$hp_events_done[] = $he["id"];
}

// Outputs
Expand All @@ -370,9 +366,24 @@ function get_page($db, $id, $player){
// Hourglass
$page["hourglass"]["here"] = get_page_hourglass_info($db, $page["id"], $player);
$cardinal_directions = get_cardinal_directions();
$root_page = $page;


if ($is_grid_based){
$root_page_id = get_first_page_with_position($db, $page["position"], $player["dimension"]);

// $root_page_id can be FALSE when using GOTO to traverse dimensions (not recommended!)
// or if the landing page for a dimension is a stacked page (not recommended either!)
// That is because player["dimension"] is different from the target page dimension, which does not arrive during normal gameplay
// It's okay. Let's just not crash everyone for that
if ($root_page_id != false && $root_page_id != $page["id"]){
$root_page = get_page($db, $root_page_id, $player);
}
}

foreach($cardinal_directions as $direction){
if (isset($page["outputs"][$direction])){
$page["hourglass"][$direction] = get_page_hourglass_info($db, $page["outputs"][$direction]["destination"], $player);
if (isset($root_page["outputs"][$direction])){
$page["hourglass"][$direction] = get_page_hourglass_info($db, $root_page["outputs"][$direction]["destination"], $player);
}
else{
$page["hourglass"][$direction] = get_page_hourglass_info($db, 0, $player); // Getting inexistant page
Expand Down Expand Up @@ -552,7 +563,7 @@ function receive_submission($db, $p, $player){

$position = [$origin_position["x"] + $randX * $offset * $shiftIncrement, $origin_position["y"] + $randY * $offset * $shiftIncrement];
$position = implode(" ", $position);
$ok = !is_position_occupied($db, $position);
$ok = !is_position_occupied($db, $position, $player["dimension"]);


if ($ok){
Expand All @@ -573,7 +584,7 @@ function receive_submission($db, $p, $player){
while(!$ok){
$position = [$origin_position["x"] + $shift["x"] * $shiftIncrement, $origin_position["y"] + $shift["y"]* $shiftIncrement];
$position = implode(" ", $position);
$ok = !is_position_occupied($db, $position);
$ok = !is_position_occupied($db, $position, $player["dimension"]);

if ($ok){
break;
Expand Down Expand Up @@ -646,7 +657,7 @@ function receive_submission($db, $p, $player){
->execute([$submission["origin"], $pageId, trim($submission["direction"])]);
}

$surroundings_by_direction = get_surrounding_pages_ids($db, $position);
$surroundings_by_direction = get_surrounding_pages_ids($db, $position, $player["dimension"]);
foreach($surroundings_by_direction as $direction=>$surrounding_page_ids){
foreach($surrounding_page_ids as $surrounding_page_id){
$db->prepare("INSERT IGNORE INTO page_succession (origin_id, target_id, command) VALUES (?,?,?)")
Expand Down
Loading

0 comments on commit f4befc6

Please sign in to comment.