Jump to content

laZuRe

Members
  • Posts

    13
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

laZuRe's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thanks a lot for the explanation. I understand that logic and i'll get rid of the global. I have another question - I've seen how classes are coded, they use something like var then give it a variable and set it to something. but its quite limiting, like you cant do maths on that variable set or you can't call a function on that variable. And currently my Game.php (the same one with the battle function) sets the $player stats and $enemy stats tables every time it is run. so I'm just not sure if i should be doing that. ie. // ## PLAYER STATS ## $player = array(); $player['name'] = "Default Player"; $player['level'] = 1; $player['statsAccuracy'] = 5; $player['statsEvasion'] = 5; $player['statsRepair'] = 5; $player['statsIntelligence'] = 5; $player['statsSpeed'] = 5; $player['statsLuck'] = 5; // ## CALC PLAYER BASE STATS ## $player['rawHealth'] = $player['statsRepair'] * 5 + 100; $player['currentHealth'] = $player['rawHealth']; $player['rawSkillPoint'] = 100; $player['rawActionPoint'] = $player['statsIntelligence']/2 + 100; $player['rawDamage'] = 10; $player['rawSpeed'] = 5 + $player['statsSpeed']; $player['rawEvade'] = 10; $player['rawAccuracy'] = $player['statsAccuracy']/2 + 60 - $enemy[$enemyID][4]; Currently the battle function is coded so that its entirely CPU orientated.. Maybe if its player vs player the whole code needs a new function altogether. Oh and you have to mouseover to see the triangles in this editor. It does it so that it keeps the interface minimal, so I guess it's fine?
  2. I dont like having multiple rows for a single user in one table so as far as the enemyID goes, it will just be dynamically changed. So far the battle system is 1 on 1... so therefore I just need to store one instance of the ID and the next battle will update the ID to reference the variables from the global array. Is there a good reason why global keyword is bad? Also I'm not using classes, just functions. Maybe I should be looking into that when the project gets larger As far as syntax goes I'm not sure on what matching up means, here's a ss of my editor
  3. well I tried putting the update and sync variables across to the mysql as functions but it doesnt work so I guess they will just have to be duplicate code.
  4. Well actually I have the enemy database stored globally before the function. The gameplay is suppose to be a single player one where you VS AI monsters of different levels. But could be in future when I learn more about multiplayer it could be user vs user. I never knew anything about databases before I started this project. I think in terms of the username fetching it's fairly optimized.. i dont see where I did something wrong. But I think for the updating /inserting into SQL table it could be written as functions instead. Thanks // Enemy Database Tables $enemyID = 0; $enemy = array( array( name => "Trejon Scout", health => 100, skill => 100, level => 1, damage => 10, evade => 10, accuracy => 60, speed => 5, luck => 5 ), array( name => "Trejon Civilian APC", health => 125, skill => 100, level => 2, damage => 6, evade => 10, accuracy => 74, speed => 5, luck => 5 ) );
  5. Thanks heaps man that makes a lot of sense. It all works now, but I just had to make a new variable altogether to store the database version of the variable as opposed to the one storing it to the pre-defined variable $enemy. Not sure if this is the best way to go about it but I just had problems since it overwrote the predefined $enemy[$enemyID]['name'] for instance because the database version doesn't have a Name row. Here's the function in its entirety, maybe there are better ways of optimizing the code since im new to php. For eg I should be using a function call for the database updates because a lot of it is duplicate code - right? function battle($username = '') { global $player; global $enemy; global $enemyID; global $battleAction; // Set Variables if (!isset($enemy[$enemyID]['current_health'])) { $enemy[$enemyID]['current_health'] = $enemy[$enemyID]['health']; } if (!isset($enemy[$enemyID]['current_skill'])) { $enemy[$enemyID]['current_skill'] = $enemy[$enemyID]['skill']; } $sql = "SELECT * FROM sa_enemystats WHERE username='$username'"; $res = mysql_query($sql); if (mysql_num_rows($res) > 0) : $sql = "SELECT * FROM sa_enemystats WHERE username='$username'"; $res = mysql_query($sql); $db_enemy[$enemyID] = mysql_fetch_assoc($res); echo '<pre>' . print_r($db_enemy[$enemyID], true) . '</pre>'; echo 'debug: variables synced with db table'; if ($db_enemy[$enemyID]['current_health'] < 0 ) : $db_enemy[$enemyID]['current_health'] = $enemy[$enemyID]['health']; $sql = "UPDATE `sa_enemystats` SET enemy_id = '$enemyID', current_health = '" . $db_enemy[$enemyID]['current_health'] . "', current_skill = '" . $db_enemy[$enemyID]['current_skill'] . "', level = '" .$db_enemy[$enemyID]['level'] . "', damage = '" . $db_enemy[$enemyID]['damage'] . "', evade= '" . $db_enemy[$enemyID]['evade'] . "', accuracy = '" . $db_enemy[$enemyID]['accuracy'] . "', speed = '" . $db_enemy[$enemyID]['speed'] . "', luck = '" . $db_enemy[$enemyID]['luck'] . "' WHERE username = '$username'"; $res = mysql_query($sql) or die(mysql_error()); echo 'debug: new enemy stats table updated'; $sql = "SELECT * FROM sa_enemystats WHERE username='$username'"; $res = mysql_query($sql); $db_enemy[$enemyID] = mysql_fetch_assoc($res); endif; else : $sql = "INSERT INTO `sa_enemystats` VALUES (null, '$enemyID','" . $_SESSION['username'] . "', '" . $enemy[$enemyID]['current_health'] . "', '" . $enemy[$enemyID]['current_skill'] . "', '" . $enemy[$enemyID]['level'] . "', '" . $enemy[$enemyID]['damage'] . "', '" . $enemy[$enemyID]['evade'] . "', '" . $enemy[$enemyID]['accuracy'] . "', '" . $enemy[$enemyID]['speed'] . "', '" . $enemy[$enemyID]['luck'] . "')"; $res = mysql_query($sql) or die(mysql_error()); echo 'debug: new enemy stats table created'; $sql = "SELECT * FROM sa_enemystats WHERE username='$username'"; $res = mysql_query($sql); $db_enemy[$enemyID] = mysql_fetch_assoc($res); endif; echo '<div id="wrap-battle">'; //turn box echo '<div id="UI-turnBox">'; if ( $player['rawSpeed'] >= $enemy[$enemyID][6] ) : echo $player['name'].'\'s Turn'; else : echo $enemy[$enemyID][0].'\'s Turn'; endif; echo '</div>'; //actions echo '<p style="text-align: center;"> <a href="?battleAction=attack">Attack</a> | <a href="?battleAction=defend">Defensive Maneuvers</a> | <a href="?battleAction=evade">Evasive Maneuvers</a> | <a href="?battleAction=skill">Skill</a> | <a href="?battleAction=run">Run</a> </p>'; if ( $battleAction == "attack" ): if ($db_enemy[$enemyID]['current_health'] >= 0) : // Factor in the player level when calculating min and max damage. $randDamageMin = $player['rawDamage'] - $player['level'] * 2; $randDamageMax = $player['rawDamage'] + $player['level'] * 2; $randDamage = rand($randDamageMin, $randDamageMax); $db_enemy[$enemyID]['current_health'] = $db_enemy[$enemyID]['current_health'] - $randDamage; echo '<p style="text-align: center;"> You deal '.$randDamage.' damage!'. '</p>'; echo $db_enemy[$enemyID]['current_health']; $sql = "UPDATE `sa_enemystats` SET enemy_id = '$enemyID', current_health = '" . $db_enemy[$enemyID]['current_health'] . "', current_skill = '" . $db_enemy[$enemyID]['current_skill'] . "', level = '" .$db_enemy[$enemyID]['level'] . "', damage = '" . $db_enemy[$enemyID]['damage'] . "', evade= '" . $db_enemy[$enemyID]['evade'] . "', accuracy = '" . $db_enemy[$enemyID]['accuracy'] . "', speed = '" . $db_enemy[$enemyID]['speed'] . "', luck = '" . $db_enemy[$enemyID]['luck'] . "' WHERE username = '$username'"; $res = mysql_query($sql) or die(mysql_error()); echo 'debug: new enemy stats table updated'; endif; endif; // decide who is first echo '<div id="UI-statsEnemy">'. '<h2>'.$enemy[$enemyID]['name'].'</h2>'. '<br/>HP: '.$db_enemy[$enemyID]['current_health'].'/'.$enemy[$enemyID]['health']. '<br/>SP: '.$db_enemy[$enemyID]['current_skill'].'/'.$enemy[$enemyID]['skill']. '<br/>DMG: '.$db_enemy[$enemyID]['damage']. '<br/>EVD: '.$db_enemy[$enemyID]['evade']. '<br/>ACC: '.$db_enemy[$enemyID]['accuracy']. '<br/>SPD: '.$db_enemy[$enemyID]['speed']. '</div>'; // Print Player Stats echo '<div id="UI-statsPlayer">'. '<h2>'.$player['name'].'</h2>'. '<br/>HP: '.$player['rawHealth']. '<br/>SP: '.$player['rawSkillPoint']. '<br/>DMG: '.$player['rawDamage']. '<br/>EVD: '.$player['rawEvade']. '<br/>ACC: '.$player['rawAccuracy']. '<br/>SPD: '.$player['rawSpeed']. '</div>'; echo '</div>'; }
  6. I know its basic but im having problems with it.. At the moment the initial problem is fixed but just to optimize code better, I can't insert a 2D variable into a mySQL table: $enemy[$enemyID][current_health] ... does this mean i MUST make it $current_enemy = $enemy[$enemyID]; then insert into table? i.e. this doesnt work... $sql = "INSERT INTO `sa_enemystats` VALUES (null, '$enemyID','" . $_SESSION['username'] . "', '$enemy[$enemyID][current_health]')";
  7. Thanks guys that actually all worked. But is there a way to set a 2D array into a mysql database? The reason for the stuff ups was to do with that. I have $sql = "UPDATE `sa_enemystats` SET enemy_id = '$enemyID', current_health = '$enemy[current_health]', current_skill = '$enemy[current_skill]', level = '$enemy[level]', damage = '$enemy[damage]', evade= '$enemy[evade]', accuracy = '$enemy[accuracy]', speed = '$enemy[speed]', luck = '$enemy[luck]' WHERE username = '$username'"; Where the original variable was a 2D array and i had to make it a normal one. Like I want to put $enemy[$enemyID][current_health] into the database returned: Array ( [id] => 18 [enemy_id] => 0 [username] => asd [current_health] => -8 [current_skill] => 100 [level] => 1 [damage] => 10 [evade] => 10 [accuracy] => 60 [speed] => 5 [luck] => 5 ) debug: variables synced with db table
  8. I think it might be to do with the fact that the string arrays in the $row are actually MYSQL database entries while the ones in the $enemy are just PHP array variables... could that be it?? It did strange things when I have it with $enemy = $row. The health updated but all the other variables disappeared. I have tested it on a php variable to php variable basis and it worked so it must be something with mysql i guess <?php // test if variables are successfully copied. $enemyID = 0; $enemy = array( array( name => "Trejon Scout", health => 100, skill => 100, level => 1, damage => 10, evade => 10, accuracy => 60, speed => 5, luck => 5 ), array( name => "Trejon Civilian APC", health => 125, skill => 100, level => 2, damage => 6, evade => 10, accuracy => 74, speed => 5, luck => 5 ) ); $enemy['level'] = $enemy[$enemyID]['level']; $row = $enemy; echo $row['level']; echo 'enemy = row <br/>'; $row[] = $enemy; echo $row['level']; echo 'enemy[] = row <br/>'; echo $enemy['level']; echo 'enemy level <br/>'; ?> Returned: 1enemy = row 1enemy[] = row 1enemy level
  9. Yeah I did. The while loop only runs once though because the query gets it only from the one username. When i run it the variables are not synced and the enemy's HP stays at about ~90 when I attack him. Whereas before with the messy code, his hp drops with every attack. btw here's the attack action if ( $battleAction == "attack" ): if ($enemy['current_health'] >= 0) : // Factor in the player level when calculating min and max damage. $randDamageMin = $player['rawDamage'] - $player['level'] * 2; $randDamageMax = $player['rawDamage'] + $player['level'] * 2; $randDamage = rand($randDamageMin, $randDamageMax); $enemy['current_health'] = $enemy['current_health'] - $randDamage; echo '<p style="text-align: center;"> You deal '.$randDamage.' damage!'. '</p>'; echo $enemy['current_health']; $sql = "UPDATE `sa_enemystats` SET enemy_id = '$enemyID', current_health = '$enemy[current_health]', current_skill = '$enemy[current_skill]', level = '$enemy[level]', damage = '$enemy[damage]', evade= '$enemy[evade]', accuracy = '$enemy[accuracy]', speed = '$enemy[speed]', luck = '$enemy[luck]' WHERE username = '$username'"; $res = mysql_query($sql) or die(mysql_error()); echo 'debug: new enemy stats table updated'; endif; endif;
  10. Hi guys im in the middle of optimizing code.. $sql = "SELECT * FROM sa_enemystats WHERE username='$username'"; $res = mysql_query($sql); while ($row = mysql_fetch_assoc($res)) { $enemy['current_health'] = $row['current_health']; $enemy['current_skill'] = $row['current_skill']; $enemy['level'] = $row['level']; $enemy['damage'] = $row['damage']; $enemy['evade'] = $row['evade']; $enemy['accuracy'] = $row['accuracy']; $enemy['speed'] = $row['speed']; $enemy['luck'] = $row['luck']; echo 'debug: variables synced with db table'; } I know that the setting of these variables are messy and can be done in a better way... but how? I have tried foreach and copying other's code for an array copy but it lead no where. Then again my syntax could be wrong... I used something like foreach ( $enemy[$value] as $row => $value) { $enemy[$value] = $row[$value]; }
  11. Hi all, this problem has me going nuts now. in http://staging.tahititravel.com.au/package/ if you choose a category in the category filter box, the parent categories generate an ID number instead of the category slug in the URL. It's not SEO im worried about, it's that the WP_Query doesn't work if it doesnt generate a proper URL. So the box for sorting next to the category will not work. The categories within the hierarchy works fine. <form action="<?php bloginfo('url'); ?>/" method="get"> <?php $select = wp_dropdown_categories('show_option_all=Filter by Category...&exclude=1,31,21&show_count=1&hide_empty=1&depth=3&hierarchical=false&orderby=name&echo=0&selected=6&taxonomy=category'); $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select); echo $select; ?> <noscript><input type="submit" value="View" /></noscript> </form> thanks
×
×
  • 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.