好的,我已经在脚本上工作了大约一个星期了。在游戏(runescape)中,有一些内容称为城堡。你所做的是每周你收集资源进行维护和升级(如果有的话)。保养是每周保持城堡土地上升和运行的成本,或者它们会降低。维持成本的每个级别或级别都将发生变化。升级是您升级或构建的任何内容(新图)。在将任何资源应用于任何升级之前,您必须先满足维护。每个星期每个玩家只能获得一定数量的资源。好了,现在我已经解决了我想要解决的问题。将有一个表单将每个绘图的级别传递给脚本。所以我正在做的是:
if($citadel = '1'){
$wood_upkeep = "1500";
$stone_upkeep = "0";
$bar_upkeep = "0";
$pbar_upkeep = "0";
$cloth_upkeep = "0";
$rations_upkeep = "0";
}elseif($citadel = '2'){
$wood_upkeep = "3600";
$stone_upkeep = "0";
$bar_upkeep = "0";
$pbar_upkeep = "0";
$cloth_upkeep = "0";
$rations_upkeep = "0";
}elseif($citadel = '3'){
$wood_upkeep = "2975";
$stone_upkeep = "2550";
$bar_upkeep = "0";
$pbar_upkeep = "0";
$cloth_upkeep = "0";
$rations_upkeep = "0";
}elseif($citadel = '4'){
$wood_upkeep = "1690";
$stone_upkeep = "1500";
$bar_upkeep = "1500";
$pbar_upkeep = "0";
$cloth_upkeep = "0";
$rations_upkeep = "0";
}elseif($citadel = '5'){
$wood_upkeep = "2250";
$stone_upkeep = "2300";
$bar_upkeep = "2200";
$pbar_upkeep = "100";
$cloth_upkeep = "0";
$rations_upkeep = "0";
$cap_limit = "2000";
}elseif($citadel = '6'){
$wood_upkeep = "2470";
$stone_upkeep = "1495";
$bar_upkeep = "1690";
$pbar_upkeep = "1365";
$cloth_upkeep = "2210";
$rations_upkeep = "0";
$cap_limit = "2350";
}elseif($citadel = '7'){
$wood_upkeep = "2800";
$stone_upkeep = "400";
$bar_upkeep = "1600";
$pbar_upkeep = "2000";
$cloth_upkeep = "3000";
$rations_upkeep = "3000";
$cap_limit = "2700";
}
通过10个图表来表示很多代码(超过500行)。我计算过,它会出现超过2000行代码,只是为了吐出一些" cappers"需要实现x升级/保养。这些if / elseif语句正在做的只是根据层(lvl)x plot分配成本值。然后我使用成本值并将em全部添加并除以每周上限限制,这将为我提供需要实现x升级+维持的封盖器数量。我很确定有一种更简单的方法可以做到这一点,只是不知道如何或者是什么。
答案 0 :(得分:8)
使用switch-case
/* zeroing the values as suggested by @paul-t */
$wood_upkeep = "0";
$stone_upkeep = "0";
$bar_upkeep = "0";
$pbar_upkeep = "0";
$cloth_upkeep = "0";
$rations_upkeep = "0";
switch($citadel) {
case '1':
$wood_upkeep = "1500";
break;
case '2':
$wood_upkeep = "3600";
break;
case '3':
$wood_upkeep = "2975";
$stone_upkeep = "2550";
break;
.
.
.
case 'n':
//statements here
break;
}
答案 1 :(得分:3)
我现在已经想到了三个主要的解决方案:
使用switch(citadel)+ case。
使用以下值准备关联数组:
$ citadelParams [1] =数组(“wood_upkeep”=>“3600”,“stone_upkeep =”0“,...);
您只需通过以下方式访问这些值:
$citadelParams[$citadel_id]["wood_upkeep"];
与#3一起使用的一些有用的东西:
答案 2 :(得分:1)
就个人而言(如果是游戏等)。我会将这些值中的每一个作为数组存储在自己的文件中。几乎像一个配置。所以你会有像
这样的文件文件citadel1.php
<?php
return [
'wood_upkeep' => "1500",
'stone_upkeep' => "0",
'bar_upkeep' => "0",
'pbar_upkeep' => "0",
'cloth_upkeep' => "0",
'rations_upkeep' => "0",
];
然后根据$citadel
$values = include "{$pathtofile}/citadel{$citadel}.php";
这样你可以更好地组织它们。对于其他一些事情,例如你可能有一个citadel
的文件夹,其中有一个配置文件,那么你有一个blacksmith
的文件夹等。然后更容易挖掘代码来找到它们。
home/
buildings/
citadel/
citadel1.php
citadel2.php
citadel3.php
citadel4.php
citadel5.php
blacksmith/
... etc.
还要说你要升级到$citadel = '6'
,你只需复制citadel5.php
的文件,更改一些内容,将其保存为citadel6.php
,然后设置$citadel to = 6
无需更改代码。
它很漂亮,如果需要则没有。
注意 - 如果我没记错的话,从文件中分配var就像这样,只适用于include
。
您甚至可以使用php保存它们。
file_put_contents('citadel5.php', "<?php\nreturn ".var_export($values, true ).";");