Jump to content

Is there a cleaner way of doing this without all the if/elseif statements


natturefrk

Recommended Posts

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. That is to many lines to achieve one thing. 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. The cost values are static and do not change. What I am asking is if there is a simpler way to achieve the same thing with less lines of code. I thought of associative arrays, but I think I will still need all those if statements. Switch statements would generate just as much code if not more. 

Link to comment
Share on other sites

what you doing is mapping input values to a set of output values. rather than use conditional logic to do this, just store the data somewhere, addressed/indexed by the input value. a database would be the best choice, or for a smaller set of data, you can use an array.

 

once you have the data stored, you can perform any calculation when you 'query' for the matching data, either in an sql query, if the data is stored in a database, or using php array functions, if the data is stored in an array.

 

some additional advantages of storing the data in a database, you would be able to create an administrative interface that allows the data to be created/edited by someone who doesn't have any programming knowledge and you can display the various levels to the users simply by querying for the data and displaying it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.