Jump to content

assigning weapons to soldiers


MasterACE14

Recommended Posts

Good Day Everyone!

 

I'm having trouble figuring out how to assign weapons from a MySQL table to soldiers.

Say I have the following...

$soldiers = 100;
$damage = 0;
$q = mysql_query("SELECT `item_quantity`, `item_power` FROM `items` WHERE `owner_id`='".$userid."' ORDER BY `item_power` DESC");
while($row = mysql_fetch_assoc($q)) 
{
   // assign weapons to soldiers and add `item_power` * `item_quantity`to $damage
}

I want to assign all the weapons until there's not enough soldiers to assign them to, and I want to assign them in order of `item_power` descending.

 

I'm sure this is really simple, I just can't for the life of me get my head around it.

 

Thanks in advance!

 

Kind Regards,

Ace

Link to comment
Share on other sites

<?php
$i = 1;
$damage = 0;

// don't surrond integers with single quotes. single quotes are for strings.
$q = mysql_query("SELECT `item_quantity`, `item_power` FROM `items` WHERE `owner_id` = " . intval($userid). " ORDER BY `item_power` DESC");
while($row = mysql_fetch_assoc($q) AND $i <= 100 # 100 = soldier count) 
{
// $solider[] now contains array('item_quantity', 'item_power');
$solider[] = $row;
   
   ++$i;
}

Link to comment
Share on other sites

Well how else are you going to assign the soldiers a weapon? That loop will either pass through every soldier or through every weapon, whichever case is smaller.

 

So if you want to assign weapons to soldiers but don't want to loop through every weapon you're out of options.

Link to comment
Share on other sites

assigning weapons to soldiers is an explanation of what I'm theoretically trying to achieve, I'm not literally trying to assign an array of values to each individual soldier. As I commented within my example

// assign weapons to soldiers and add `item_power` * `item_quantity`to $damage

 

thinking something like...

if($soldiers > $row['item_quantity']) {
  $damage += $row['item_power'];
} else // ...

Link to comment
Share on other sites

Maybe if you explained how soldiers are represented in your code you'd get a better answer.  $soldier = 100 doesn't convey any meaning.

My apologies, $soldier = 100; is the total amount of soldiers the user has(usually comes from the database, 100 for the purpose of this thread).

 

I'll try to explain my problem differently.

 

If I have 100 soldiers, and say 3 records for weapons(items) this particular user has in the database containing...

 

weapon -------- item_quantity ---------- item_power       

1                      30                                25

2                      50                                10

3                      50                                5

 

 

I want to determine the total power(damage) that this player has by essentially adding `item_power` * `soldiers`(from strongest item_power to lowest) until there is no more 'weaponless' soldiers or, there is not enough soldiers to virtually 'equip' the items.

 

So in this scenario the total power of this user would be:

 

first weapon: 30 * 25  (30 soldiers have the first weapon equipped)

second weapon: 50 * 25 (50 soldiers have the second weapon equipped, 80 soldiers have weapons equipped in total now)

third weapon: 20 * 5 (20 soldiers have the third weapon equipped, 100 soldiers have weapons equipped in total, 30 are weaponless)

 

total power = (30 * 25) + (50 * 25) + (20 * 5)

 

If in this scenario there was less weapons than soldiers, it would not equip anymore weapons once all the soldiers have weapons.

Link to comment
Share on other sites

Your comment says "assign weapons to soliders" which is what my code does. I'm not sure what you're asking now, because your comments are conflicting.

Sorry I just noticed the mistake I made in the comment in my first example, should of been $soldiers * item_power rather than item_power * item_quantity.

Link to comment
Share on other sites

Okay, now you're making sense:

 

<?php
$weapons = array();
$total_weapons = 0;
$soldiers = 100;

$q = mysql_query("SELECT `item_quantity`, `item_power` FROM `items` WHERE `owner_id` = " . intval($userid) . " ORDER BY `item_power` DESC");
while($row = mysql_fetch_assoc($q)) 
{
    $row['item_quantity'] = intval($row['item_quantity']);
    $weapon[] = array('quantity' => $row['item_quantity'], 'damage' => $row['item_quantity'] * intval($row['item_power']));
   
   $total_weapons += $row['item_quantity'];

    if ($total_weapons >= $soldiers)
    {
        break;
    }
}

Link to comment
Share on other sites

Or if you just want total damage and don't care about individual weapon stats:

 

<?php

$total_power 	= 0;
$total_weapons 	= 0;
$soldiers 		= 100;

$q = mysql_query("SELECT `item_quantity`, `item_power` FROM `items` WHERE `owner_id` = " . intval($userid) . " ORDER BY `item_power` DESC");
while($row = mysql_fetch_assoc($q)) 
{
$row['item_quantity'] = intval($row['item_quantity']);

$total_weapons += $row['item_quantity'];
    if ($total_weapons > $soldiers)
    {
	$extra_weapons = $total_weapons - $soldiers;

	// trim the item_quantity so that it doesn't surpass how many soliders you have
	if ($row['item_quantity'] = ($row['item_quantity'] - $extra_weapons) != 0)
	{
		$total_power += $row['item_quantity'] * intval($row['item_power']));
	}
        break;
    }
$total_power += $row['item_quantity'] * intval($row['item_power']));
}

Link to comment
Share on other sites

Or if you just want total damage and don't care about individual weapon stats:

 

<?php

$total_power 	= 0;
$total_weapons 	= 0;
$soldiers 		= 100;

$q = mysql_query("SELECT `item_quantity`, `item_power` FROM `items` WHERE `owner_id` = " . intval($userid) . " ORDER BY `item_power` DESC");
while($row = mysql_fetch_assoc($q)) 
{
$row['item_quantity'] = intval($row['item_quantity']);

$total_weapons += $row['item_quantity'];
    if ($total_weapons > $soldiers)
    {
	$extra_weapons = $total_weapons - $soldiers;

	// trim the item_quantity so that it doesn't surpass how many soliders you have
	if ($row['item_quantity'] = ($row['item_quantity'] - $extra_weapons) != 0)
	{
		$total_power += $row['item_quantity'] * intval($row['item_power']));
	}
        break;
    }
$total_power += $row['item_quantity'] * intval($row['item_power']));
}

 

that's done the trick! many 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.