Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP returning array with requirements done

Tags:

arrays

php

I have a few functions here and im struggeling to get the right output.

the main idea is to use function called buildinginfo and call function avaliableBuildingBuildings to loop trough buildinginfo `s arrays and loop trough requirements to find out if the requirements is complete or not.

in my database, my main_building level is 1.

every other table, is empty.

when calling avaliableBuildingBuildings i get the output under this line, that would list buildings i can create. you can see that the requirements for main_building are 3, then the barracks shouldnt be created.

q: how can i fix my code so it would acually just give me the array of buildings i can build?

current output:

array(5) {
  ["barracks"]=>
  array(2) {
    ["max"]=>
    int(1)
    ["requirements"]=>
    array(2) {
      ["main_building"]=>
      int(3)
      ["rally_point"]=>
      int(1)
    }
  }
  ["blacksmith"]=>
  array(2) {
    ["max"]=>
    int(1)
    ["requirements"]=>
    array(3) {
      ["main_building"]=>
      int(3)
      ["academy"]=>
      int(3)
      ["barracks"]=>
      int(3)
    }
  }
  ["embassy"]=>
  array(2) {
    ["max"]=>
    int(1)
    ["requirements"]=>
    array(1) {
      ["main_building"]=>
      int(1)
    }
  }
  ["marketplace"]=>
  array(2) {
    ["max"]=>
    int(1)
    ["requirements"]=>
    array(3) {
      ["warehouse"]=>
      int(1)
      ["granary"]=>
      int(1)
      ["main_building"]=>
      int(3)
    }
  }
  ["palace"]=>
  array(2) {
    ["max"]=>
    int(1)
    ["requirements"]=>
    array(3) {
      ["main_building"]=>
      int(5)
      ["embassy"]=>
      int(1)
      ["residence"]=>
      int(0)
    }
  }
}

full code:

 function isBuilt($villageid,$name) {
        $buildinginfo = $this->buildinginfo();
        $thisBuilding = $buildinginfo[$name];

        $built = $this->CI->db->query("SELECT * from `$name` where `villageid`='$villageid'")->row();
        return ($built ? $built : false);
    }

    function requirementsDone($villageid,$name) {
        $buildinginfo = $this->buildinginfo()[$name];
        $canbebuilt = true;
        if (isset($buildinginfo['requirements'])) {
            $requirements = $buildinginfo['requirements'];


            foreach ($requirements as $reqname => $level) {
                $building = $this->isBuilt($villageid, $reqname);
                if ($building) {
                    echo "needs that $reqname (".$building->level.")is higher than $level to build $name <br><br>";
                    if ($building->level >= $level) {
                        echo "<br> $reqname is acually higher ! <br><br>";

                    } else {
                        echo " i cant acually build this.. <br><br>";
                        $canbebuilt = false;
                        break;
                    }
                } else {
                    $canbebuilt = false;
                    break;
                }
            }
        }

        if ($canbebuilt) {
            echo "result to build $name is true<br>";

        } else {
            echo "result to build $name is false<br>";

        }
        return $canbebuilt;
    }

    function existingBuildings($villageid) {

        $buildinginfo = $this->buildinginfo();
        $buildings = array();
        foreach($buildinginfo as $name => $array) {
            $built = $this->isBuilt($villageid,$name);
            if ($built) {
                $buildings[$name] =   $built;
            }

        }

        return $buildings;

    }

    function avaliableBuildingBuildings($villageid) {
        $avaliable = $this->avaliableBuildings($villageid);

        $tobuild = array();
        foreach($avaliable as $name => $built) {

            if (isset($built['requirements'])) {
                $req = $built['requirements'];

                foreach ($req as $reqname => $level) {
                    $canbuild = $this->requirementsDone($villageid, $reqname);

                    if ($canbuild) {
                        echo "verify to build $reqname is true <br><br>";
                                                   $tobuild[$name] = $avaliable[$name];

                    } else {
                        echo "verify to build $reqname is false <br><br>";

                        break;
                    }
                }
            } else {

            }
        }
        return $tobuild;
    }

    function avaliableBuildings($villageid) {
        $buildinginfo = $this->buildinginfo();

        foreach($this->existingBuildings($villageid) as $name => $built) {
            if (array_key_exists($name,$buildinginfo)) {
                unset($buildinginfo[$name]);
                echo "removing $name <br>";
            }
        }

        return $buildinginfo;



    }

    function buildinginfo() {

        $info = array(
                'academy' => array('max' => 1),

                'barracks' => array('max' => 1,
                    'requirements' => array(
                        'main_building' => 3,
                        'rally_point' => 1,
                    )
                ),
                'main_building' => array('max' => 1,
                    'requirements' => array(
                        'main_building' => 1,
                    )
                ),
                'greatbarracks' => array('max' => 1),
                'greatstable' => array('max' => 1),
                'heromansion' => array('max' => 1),
                'rally_point' => array('max' => 1),
                'stable' => array('max' => 1,'requirements' => array(
                    'blacksmith' => 3,
                    'academy' => 5,
                )
                ),
                'blacksmith' => array('max' => 1,'requirements' => array(
                    'main_building' => 3,
                    'academy' => 3,
                    'barracks' => 3,
                )
                ),
                'forge' => array('max' => 1),
                'tournament_square' => array('max' => 1),
                'trapper' => array('max' => 1),
                'cranny' => array('max' => 1),
                'embassy' => array('max' => 1,
                    'requirements' => array(
                        'main_building' => 1
                    )
                ),
                'marketplace' => array('max' => 1,'requirements' => array(
                    'warehouse' => 1,
                    'granary' => 1,
                    'main_building' => 3
                )
                ),

                'palace' => array('max' => 1,
                    'requirements' => array(
                        'main_building' => 5,
                        'embassy' => 1,
                        'residence' => 0
                    )
                ),
                'residence' => array('max' => 1),
                'town_hall' => array('max' => 1),
                'trade_office' => array('max' => 1),
                 'treasury'  => array('max' => 1),
                'granary' => array('max' => 99, 'new' => 20),
                'warehouse' => array('max' => 99, 'new' => 20)

        );

        return $info;

}

like image 829
maria Avatar asked Jun 09 '16 06:06

maria


1 Answers

It looks like you're requirements check loop is doubled. That resulted in checking if any of the required buildings can be build (and it always can since there's no nested requirements). Just get rid of inner loop and it should be fine.

function avaliableBuildingBuildings($villageid) {
    $avaliable = $this->avaliableBuildings($villageid);

    $tobuild = array();
    foreach ($avaliable as $name => $built) {
        if (isset($built['requirements'])) {
            if ($this->requirementsDone($villageid, $name)) {
                echo "verify to build $name is true <br><br>";
                $tobuild[$name] = $avaliable[$name];
            } else {
                echo "verify to build $name is false <br><br>";
            }
        } else {

        }
    }
    return $tobuild;
}

Ps. Those "echoes" don't help, do they?:) Think about TDD - this process resolves more issues than just logic errors (it would force you to introduce fake data layer for example -> decoupled code)

like image 62
shudder Avatar answered Sep 29 '22 03:09

shudder