简体   繁体   中英

Replace these if/elseif statements with something cleaner

Okay I have been working on script for about a week now. In a game(runescape) there is content known as the citadel. What you do is on weekly basis you gather resources towards upkeep and upgrades(if any). Upkeep is the weekly cost to keep the plots of the citadel up and running or they will degrade. Each tier or level that upkeep cost will change. Upgrade is any thing you are upgrading or building(new plots). You must meet upkeep first before any resources are applied to any upgrades. Every week each player can only cap a certain amount of resources. Okay now that I got that outta of the way now to explain what I am trying to accomplish. There will be a form that passes the level of each plot to the script. So what I am doing is:

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";
}

Times that by 10 plots that is alot of code(over 500 lines). I calculated and it would come out to over 2000 lines of code just to spit out a number of "cappers" needed to achieve x upgrade/upkeep. What these if/elseif statements are doing is simply just assigning the cost values based on what tier(lvl) x plot is. Then I use the cost values and add em all up and divide by the weekly cap limit and that will give me the number of cappers need to achieve x upgrade + upkeep. I am pretty sure there is a simpler way of doing this, just not aware of how or what.

use 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;


}

There are primary three solutions I've got on my mind right now:

  1. Use switch (citadel) + case.

  2. Prepare associative array with those values like:

    $citadelParams[1] = Array("wood_upkeep" => "3600", "stone_upkeep = "0", ...);

You can access those values simply by:

$citadelParams[$citadel_id]["wood_upkeep"];
  1. This is my favourite. Just use xml or json file for storing those values and read them in your code. There is no need to hardcode values in your code, and also you can easily find and change values in this specific file even if you are not a programmer :)

Some helpful things to use with #3:

  • file_get_contents to read file to string
  • json_decode to parse string read before with file_get_contents (I suggest to use second parameter as TRUE, so you can get simply associative array in return and use it like in #2)

Personally, ( if this is for a game or such ). I would store each of these values in their own file as an array. almost like a config. So you would have files like

file citadel1.php

<?php
return [
   'wood_upkeep' => "1500",
   'stone_upkeep' => "0",
   'bar_upkeep' => "0",
   'pbar_upkeep' => "0",
   'cloth_upkeep' => "0",
   'rations_upkeep' => "0",
];

Then you just load the file depending on the value of $citadel

$values = include "{$pathtofile}/citadel{$citadel}.php";

That way you can organize them way better. For other things like maybe you have a folder for citadel with the configs in there, then you have a folder for blacksmith etc. Be way easier then digging through code to find them.

home/
   buildings/
        citadel/
            citadel1.php
            citadel2.php
            citadel3.php
            citadel4.php
            citadel5.php
       blacksmith/
            ... etc.

Also say you want to make a upgrade to $citadel = '6' you would just copy the file for citadel5.php , change a few things, save it as citadel6.php and then set $citadel to = 6 no code change required.

It's beautiful, no if required.

Note - assigning a var from the file like so, only works with include , if I recall correctly.

You can even save them using php.

    file_put_contents('citadel5.php', "<?php\nreturn ".var_export($values, true ).";");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM