PHP返回数组已完成要求

前端之家收集整理的这篇文章主要介绍了PHP返回数组已完成要求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在这里有一些功能,我正在努力获得正确的输出.

主要思想是使用名为buildinginfo的函数调用函数avaliableBuildingBuildings循环通过buildinginfo的数组并循环通过需求来查明需求是否完整.

在我的数据库中,我的main_building级别为1.

每隔一张桌子都是空的.

调用avaliableBuildingBuildings时,我得到这条线下的输出,这将列出我可以创建的建筑物.你可以看到main_building的要求是3,那么就不应该创建营房.

问:我怎样才能修复我的代码,以便它能真正地给我一些我可以构建的建筑物?

当前输出

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)
    }
  }
}

完整代码

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;

}

看起来你的需求检查循环加倍了.这导致检查是否可以构建任何所需的建筑物(并且它总是可以,因为没有嵌套要求).只是摆脱内循环,它应该没问题.
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.那些“回声”没有帮助,是吗?:)想想TDD – 这个过程解决了更多的问题而不仅仅是逻辑错误(它会迫使你引入假数据层 – 例如>解耦代码)

原文链接:https://www.f2er.com/php/136888.html

猜你在找的PHP相关文章