Jump to content

Using functions within another function


muskelmann098

Recommended Posts

Hello,

 

I'm trying to make a sort of browser-based multiplayer game. It might be way over my head, but my plan is to learn from it as I go along. Right now, I'm trying to get a system down for the "payment" of resources whenever a field is upgraded. Here is what I have at the moment:

 

 

<?php
include("database.php");
include("createcamp.php");
include("login.php");
include("resformula.php");
$name = $_SESSION['username'];

/*This function levels up the field when the user asks for it. 
So far it's working perfectly! */
function resUpdt($resnum){
      global $conn, $name; // I know you told me, but I haven't got to fixing this one yet. Will do soon though.
      $result = mysql_query("SELECT camp FROM `Villages` WHERE username='$name'");
      $cnamearr = mysql_fetch_array($result);
      $cname = $cnamearr['camp'];
      $level_result = mysql_query("SELECT * FROM Villages 
      WHERE camp = '$cname'");
      $levelarr = mysql_fetch_array($level_result);
      $level = $levelarr["$resnum"];
      $newlevel = ($level + 1);
      $result = mysql_query("UPDATE Villages SET $resnum = '$newlevel' WHERE camp = '$cname'");
      }

/*This function uses another one of my functions (included at the bottom to
subtract the resource cost from the total amount of resources. Unfortunately,
it's not working. I'm not very surprised because I had no clue how to do it
from the start, so this is just some trial and error attempt. I hope someone 
here can tell me how to do it properly.*/
      
function cost($res){
switch($res){
	case oil:
		$oil = totRes(Oil) - $oil_org = 70;
		$iron = totRes(Iron) - $iron_org = 100;
		$wood = totRes(Wood) - $wood_org = 100;
		$crop = totRes(Crop) - $crop_org = 40;
	case iron:
		$oil_org = 110;
		$iron_org = 60;
		$wood_org = 100;
		$crop_org = 40;
	case wood:
		$oil_org = 100;
		$iron_org = 110;
		$wood_org = 60;
		$crop_org = 40;
	case crop:
		$oil = totRes(oil) - $oil_org = 90;
		$wood = totRes(iron) - $wood_org = 85;
		$iron = totRes(iron) - $iron_org = 85;
		$crop = totRes(iron) - $crop_org = 20;
		echo "$oil, $iron, $wood, $crop";
	}
}

switch($_GET['action']) {
case 'crop_1': 
	resUpdt(crop1);
	cost (Crop);
	break;
/* This switch statement continues, but it's very large and it
does the same for ever case so I'm not going to post all of it. 
So there you have it! That's what I want to do, and I have to admit, it seems like a huge undertaking, but hopefully with a push in the right direction, I will find a way.

 

Here is the "totRes" function. It takes the current number of resources in store in the player's village.

function totRes($resource){
global $conn, $name;
	$result = mysql_query("SELECT camp FROM `Villages` WHERE username = '$name'");
	$cnamearr = mysql_fetch_array($result);
	$cname = $cnamearr['camp'];
	$totres_que = mysql_query("SELECT * FROM `Villages` WHERE camp = '$cname'");
	$totres_arr = mysql_fetch_array($totres_que);
	$totres = $totres_arr["$resource"];
	echo $totres;

 

I hope someone here is able to understand what I'm trying to do and see where I'm going wrong.

 

Thanks in advance :)

Link to comment
Share on other sites

Well... that really helps...

 

So what would you say my problem is? I said in my previous post I'm not even close to sure about how to do this.

 

I guess you could start by trying to explain clearly what you are trying to achieve and what the actual problems are you encounter...

Link to comment
Share on other sites

I'm sorry if I wasn't being clear enough. I'll give it another shot.

 

I'm trying to make a game where a player has a village with a certain amount of resources. The total number of resources for each village is stored in a MySQL database. When a player wants to upgrade his resource field, this should cost him a certain amount of resources as described here:

<?php
function cost($res){
   switch($res){
      case oil:
         $oil = totRes(Oil) - $oil_org = 70;
         $iron = totRes(Iron) - $iron_org = 100;
         $wood = totRes(Wood) - $wood_org = 100;
         $crop = totRes(Crop) - $crop_org = 40;
      }
}
?>

The "_org" variables are the costs, and the $oil, $iron, $wood, and $crop variables are the total amount of resources left. The totRes() function, described below, gets the total number of resources in that village, so that if this was working, it would take the total number of resources in a village, and subtract the costs. Then I would go on with updating the database.

 

The "totRes()" function gets the total number of resources for each village. These numbers are stored in a database.

<?php
include("login.php");
include("database.php");
include("createcamp.php");
include("resupdt.php");
$name = $_SESSION['username'];


function totRes($resource){
global $conn, $name;
	$result = mysql_query("SELECT camp FROM `Villages` WHERE username = '$name'");
	$cnamearr = mysql_fetch_array($result);
	$cname = $cnamearr['camp'];
	$totres_que = mysql_query("SELECT * FROM `Villages` WHERE camp = '$cname'");
	$totres_arr = mysql_fetch_array($totres_que);
	$totres = $totres_arr["$resource"];
	echo $totres;
}
?>

The problem is basically that it doesn't work... No matter how "newbish" that sounds, it's a fact, and since I'm not sure if I am allowed to do what I'm trying to do, I'm not really sure how to go by troubleshooting either. I think this is the first time in this project where I've simply not had a clue.

 

I hope that makes it clearer. If you need any more information, or anything is unclear, please ask and I will do my best to answer.

 

Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.