Jump to content
MakeWebGames

LordDan

Members
  • Posts

    66
  • Joined

  • Last visited

Everything posted by LordDan

  1. Credit given where credit due, no worries :thumbup:
  2. Ah nice function Crim, mind if I add that to my Utilities directory? :rolleyes:
  3. Okay, I'm posting this here because it seems to be a common mistake. It seems the majority of Percentage functions I have ever seen in PHP are actually wrong because of a simple mistake... I've also seen this common mistake in Open Source projects dotted around SourceForge.. For my examples, I am going to get 12% of 144. This function below is common, but it's also incorrect.. function getPercentage( $perc, $val ) { return round( ( $perc*100 ) / $val ); }   This is incorrect because it returns 8 (Or 9 if you use ceil instead of number_format)... 12% of 144 is not 8, it's 17 (17.28 to be precise). So what went wrong? Simply put, the * and / are the wrong way round..   function getPercentage( $perc, $val ) { return round( ( $perc/100 ) * $val ); }   This will return the 17 we're looking for.. Math: 12% is 12/100 So: (12/100) x 144 = (0.12) x 144 = 17.28 Why wasn't this realized during testing? Simple, most poeple test an easy percentage by getting a percentage out of 100 (10% of 100) because the answer is obvious and in this case, both functions actually return the correct value of 10%. It's only when you want a percentage of something other than 100 you'll start to get incorrect results. But, because it was correct the first time, you assume it's correct and use it none the wiser.   Anyway, just posting this because it is a common mistake. Check your percentage functions for 12 of 144, if you get the correct value, well done for paying attention in school, if not, you can now correct it. I'm interested to see your results :P (PS: I'm no math genius, so if I'm wrong, show me the math :thumbup: ) ------------------ Test Case for those who care.. echo getPercentage( 12, 144 ).' '; echo getPercentage( 200, 1500 ).' '; echo getPercentage( 268, 1678 ).' '; echo getPercentage( 6789, 1000 ).' '; // 12% of 144 = 17 // 200% of 1500 = 3000 // 268% of 1678 = 4497 // 6789% of 1000 = 67890
  4. A PHP Class which will generate a HTML select list of Timezones (GMT). It'll then cache the generated list for all future usage. Wrote it some time ago, still use it ^^ timezones.class.php <?php class timezoneUtility { /* listZones( STR, STR ) @Param: Id of the <select> element @Param: Key of the timezone array to preselect (IE: "Europe/London") ------------------------- This function will generate a list of timezones and then cache them for further use. Eg: $timezone->listZones(); */ public function listZones( $elementId = 'timezones', $selected = null ) { if( file_exists( 'cache/timezones.cache.html' ) ) { return '<select name="'. $elementId .'" id="'. $elementId .'" style="font-size: 7pt;">'. file_get_contents( 'display/cache/timezones.cache.html' ) .'</select>'; } else { $zones = $this->getTimezones(); $selected = is_null( $selected ) ? date_default_timezone_get() : $selected; $select = ''; foreach( $zones as $key => $gmt ) { $select .= '<option value="' . $key . '"'; $select .= ( $key == $selected ) ? ' selected="selected"' : ''; $select .= '>' . $gmt . '</option>' . "\r\n"; } file_put_contents( 'cache/timezones.cache.html', $select ); return '<select name="'. $elementId .'" id="'. $elementId .'" style="font-size: 7pt;">'. $select .'</select>'; } } /* isValid( STR ) @Param: The users selected Timezone (Key of the array) ------------------------- This short function validates the users timezone selection. Eg: $timezone->isValid( ... ); */ public function isValid( $selected ) { if( !array_key_exists( $selected, $this->getTimezones() ) ){ return false; } else { return true; } } /* getTimezones(); Simply returns the array of Timezones for use in Generating the list and validating the users Timezone selection.. */ private function getTimezones() { $zones = array( "Pacific/Midway" => "(GMT-11:00) Midway Island, Samoa", "America/Adak" => "(GMT-10:00) Hawaii-Aleutian", "Etc/GMT+10" => "(GMT-10:00) Hawaii", "Pacific/Marquesas" => "(GMT-09:30) Marquesas Islands", "Pacific/Gambier" => "(GMT-09:00) Gambier Islands", "America/Anchorage" => "(GMT-09:00) Alaska", "America/Ensenada" => "(GMT-08:00) Tijuana, Baja California", "Etc/GMT+8" => "(GMT-08:00) Pitcairn Islands", "America/Los_Angeles" => "(GMT-08:00) Pacific Time (US & Canada)", "America/Denver" => "(GMT-07:00) Mountain Time (US & Canada)", "America/Chihuahua" => "(GMT-07:00) Chihuahua, La Paz, Mazatlan", "America/Dawson_Creek" => "(GMT-07:00) Arizona", "America/Belize" => "(GMT-06:00) Saskatchewan, Central America", "America/Cancun" => "(GMT-06:00) Guadalajara, Mexico City, Monterrey", "Chile/EasterIsland" => "(GMT-06:00) Easter Island", "America/Chicago" => "(GMT-06:00) Central Time (US & Canada)", "America/New_York" => "(GMT-05:00) Eastern Time (US & Canada)", "America/Havana" => "(GMT-05:00) Cuba", "America/Bogota" => "(GMT-05:00) Bogota, Lima, Quito, Rio Branco", "America/Caracas" => "(GMT-04:30) Caracas", "America/Santiago" => "(GMT-04:00) Santiago", "America/La_Paz" => "(GMT-04:00) La Paz", "Atlantic/Stanley" => "(GMT-04:00) Faukland Islands", "America/Campo_Grande" => "(GMT-04:00) Brazil", "America/Goose_Bay" => "(GMT-04:00) Atlantic Time (Goose Bay)", "America/Glace_Bay" => "(GMT-04:00) Atlantic Time (Canada)", "America/St_Johns" => "(GMT-03:30) Newfoundland", "America/Araguaina" => "(GMT-03:00) UTC-3", "America/Montevideo" => "(GMT-03:00) Montevideo", "America/Miquelon" => "(GMT-03:00) Miquelon, St. Pierre", "America/Godthab" => "(GMT-03:00) Greenland", "America/Argentina/Buenos_Aires" => "(GMT-03:00) Buenos Aires", "America/Sao_Paulo" => "(GMT-03:00) Brasilia", "America/Noronha" => "(GMT-02:00) Mid-Atlantic", "Atlantic/Cape_Verde" => "(GMT-01:00) Cape Verde Is", "Atlantic/Azores" => "(GMT-01:00) Azores", "Europe/Belfast" => "(GMT) Greenwich Mean Time : Belfast", "Europe/Dublin" => "(GMT) Greenwich Mean Time : Dublin", "Europe/Lisbon" => "(GMT) Greenwich Mean Time : Lisbon", "Europe/London" => "(GMT) Greenwich Mean Time : London", "Africa/Abidjan" => "(GMT) Monrovia, Reykjavik", "Europe/Amsterdam" => "(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna", "Europe/Belgrade" => "(GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague", "Europe/Brussels" => "(GMT+01:00) Brussels, Copenhagen, Madrid, Paris", "Africa/Algiers" => "(GMT+01:00) West Central Africa", "Africa/Windhoek" => "(GMT+01:00) Windhoek", "Asia/Beirut" => "(GMT+02:00) Beirut", "Africa/Cairo" => "(GMT+02:00) Cairo", "Asia/Gaza" => "(GMT+02:00) Gaza", "Africa/Blantyre" => "(GMT+02:00) Harare, Pretoria", "Asia/Jerusalem" => "(GMT+02:00) Jerusalem", "Europe/Minsk" => "(GMT+02:00) Minsk", "Asia/Damascus" => "(GMT+02:00) Syria", "Europe/Moscow" => "(GMT+03:00) Moscow, St. Petersburg, Volgograd", "Africa/Addis_Ababa" => "(GMT+03:00) Nairobi", "Asia/Tehran" => "(GMT+03:30) Tehran", "Asia/Dubai" => "(GMT+04:00) Abu Dhabi, Muscat", "Asia/Yerevan" => "(GMT+04:00) Yerevan", "Asia/Kabul" => "(GMT+04:30) Kabul", "Asia/Yekaterinburg" => "(GMT+05:00) Ekaterinburg", "Asia/Tashkent" => "(GMT+05:00) Tashkent", "Asia/Kolkata" => "(GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi", "Asia/Katmandu" => "(GMT+05:45) Kathmandu", "Asia/Dhaka" => "(GMT+06:00) Astana, Dhaka", "Asia/Novosibirsk" => "(GMT+06:00) Novosibirsk", "Asia/Rangoon" => "(GMT+06:30) Yangon (Rangoon)", "Asia/Bangkok" => "(GMT+07:00) Bangkok, Hanoi, Jakarta", "Asia/Krasnoyarsk" => "(GMT+07:00) Krasnoyarsk", "Asia/Hong_Kong" => "(GMT+08:00) Beijing, Chongqing, Hong Kong, Urumqi", "Asia/Irkutsk" => "(GMT+08:00) Irkutsk, Ulaan Bataar", "Australia/Perth" => "(GMT+08:00) Perth", "Australia/Eucla" => "(GMT+08:45) Eucla", "Asia/Tokyo" => "(GMT+09:00) Osaka, Sapporo, Tokyo", "Asia/Seoul" => "(GMT+09:00) Seoul", "Asia/Yakutsk" => "(GMT+09:00) Yakutsk", "Australia/Adelaide" => "(GMT+09:30) Adelaide", "Australia/Darwin" => "(GMT+09:30) Darwin", "Australia/Brisbane" => "(GMT+10:00) Brisbane", "Australia/Hobart" => "(GMT+10:00) Hobart", "Asia/Vladivostok" => "(GMT+10:00) Vladivostok", "Australia/Lord_Howe" => "(GMT+10:30) Lord Howe Island", "Etc/GMT-11" => "(GMT+11:00) Solomon Is, New Caledonia", "Asia/Magadan" => "(GMT+11:00) Magadan", "Pacific/Norfolk" => "(GMT+11:30) Norfolk Island", "Asia/Anadyr" => "(GMT+12:00) Anadyr, Kamchatka", "Pacific/Auckland" => "(GMT+12:00) Auckland, Wellington", "Etc/GMT-12" => "(GMT+12:00) Fiji, Kamchatka, Marshall Is", "Pacific/Chatham" => "(GMT+12:45) Chatham Islands", "Pacific/Tongatapu" => "(GMT+13:00) Nuku'alofa", "Pacific/Kiritimati" => "(GMT+14:00) Kiritimati" ); return $zones; } } ?>   Usage: <?php require_once('classes/timezone.class.php'); $timezones = new timezoneUtility; echo $timezones->listZones( 'tzone_select' ); ?>   Validating selection: <?php if( !$timezones->isValid( $_POST['tzone_select'] ){ die('Don\'t remember that being an option... 0_o'); } ?>
  5. Oh, you did sign up in the end then! Good to see you here Ace!
  6. ^^ LOL! If it wasn't for it being an April Fools, I'd totally get one! xD
  7. I just added Wildlife (Birds) to the map, it looks good, but the sprite "sometimes" doesn't change so it looks like the birds are flying backwards on occasion. So far I only have basic direction detection. Can anyone with JS/jQuery knowledge think of anything to make the directions better? You can see the Birds yourself on the link in my original post. curY and curX are the current tile position of the individual bird, they are simply grid ID's. (3x-5y, 6x-2y etc) newX and newY are then simply randomised. [Edit: Fixed it, forgot to update curX and curY after the wildlife had finished it's movement. Could still be improved though] [Edit: Completely fixed, the birds should fly around accurately now! :3] [Credit: Wildlife inspired by a_bertrands NEAB]
  8. I've had nothing but issues with Virgin Media.. As soon as Sky up their line where I live, i'm shifting to Sky.. Virgin Media are just awful. You pay stupid prices for unlimited package, yet they cut your speed to 28kb/s if you download more than 3.5GB?? Says on their site the limit is 7GB :huh: Also had problems with the junction box outside which was damaged causing intermittent connection issues randomly, some times for 5 minutes sometimes for 48 hours. All they had to to was replace it which took all of 5 minutes..... Took them a year :thumbdown: Also got sick and tired of the Indians telling me to turn my god damn modem off - over and over and over... "IT'S THE FREAKING JUNCTION BOX!" - Drove me so close to some racist remarks. You only have to search google for "Virgin Media sucks" to see the thousands of p*ssed ex VM users. Awful ISP from my experience. :thumbdown:
  9. Djkanna: Sound like I should look into some Preloading xD Cheers Spudinski: Thanks! I'll see if I can find it on Amazon :thumbup:
  10. Hey! I just spent around 7 hours learning jQuery for the first time and ended up with an RPG World. Quite surprised how easy jQuery is and how quickly I picked it up. My code is a bit messy but once I finish I should know enough to be able to clean it up and improve on it more. Anyway, I'm just interested to see what ya'll think of my noob start to jQuery. Also, if there is any specific jQuery books on Amazon or any decent jQuery articles you think I could learn something from please let me know. I want to continue to learn this stuff. Here is the result of my learning: RPG World Map Prototype It's built up in layers:   Ground Layer (Terrain Tiles) Objects Layer (Anything above terrain - Bushes/Signpost) Player Sprite (Me/You, obviously) Overhead Object Layer (Anything that needs to be above our sprite - walk around the lamp-post to see) Events Layer (Trigger something - Step in "front" of the signpost to see)   It seems to work IE8, Firefox 2+, Opera 9+, Chrome, Safari 3+ - Only IE6/7 have positioning issues. Movement: Arrow Keys (Will add WASD later) Feedback? Remarks? Noob Bashes and Insults? :S
  11. What? Switches are sexeh! :D Thanks for the welcome!
  12. That's true! I see your point :)
  13. Djkanna, whilst I agree what I have posted isn't in it's most simplistic of form, these functions I took out of one of my classes, which is why they're bigger. It's my way of keeping things simple by having my "reusable" code in one place. Take this for example, as you can see above, I actually forgot to Salt my hashing, which is a big mistake, luckily for me, I only have to update that one function to fix my error, whereas you would have to go over all your files where you have used hashing to add it in. The simplicity for me comes in here because my mistakes are quick and easy to fix ;) I guess this is more a conflict of preference. ^^ I also use $method and a switch to group outcome control, so to speak, like this..   public function formatDate( $stamp, $method ) { switch( $method ) { case 'full': return date( 'd F Y - l h:i A', $stamp ); break; case 'date': return date( 'd F Y', $stamp ); break; case 'time': return date( 'h:i A', $stamp ); break; default: return date( $this->customDateFormat, $stamp ); } } -------- echo $dateClass->formatDate( time(), 'full' ); echo $dateClass->formatDate( time(), 'date' ); echo $dateClass->formatDate( time(), 'time' ); # Or whenever I need something different... $dateClass->customDateFormat = 'd-m-y'; echo $dateClass->formatDate( time() );   Thanks all for the welcomes :thumbsup:
  14. Thanks for your input danny :thumbup:
  15. Hello MWG Community! I'm new to the community, so just introducing myself. So, I'm Dan, 21, UK, been developing games since I found the passion for it at 15, I use PHP/MySQL as my main language but also know some JS/Ajax/jQuery and mostly specialise in Virtual Pets/Adoptables and RPG/MMO games, both text and realtime. I prefer to use OO programming over procedural since finding the love of Objects, but I'm known to adapt my programming to fit both if needed. I have a few interested parties who want me to develop an adoptable game (product) so they can run their own, so that is my current project right now. I like to contribute when I join a new community, so I will post some of my own references or code below, if anyone finds them useful, let me know and I will move then to the correct forums for accessibility. Resource Contributions OpenGameArt.org - Found this a while ago, it has some good free game art resources. Code Contributions This small functions I use to clear out overflow of a large int, useful for games that has big game money. When an Int is too large, you may see something like E+13 on the end, this function turns it back into a full INT function intOverflow( $int ){ return number_format( $int, 0, '.', '' ); }   This function I use in ranking systems in my games, it'll take your rank and add st, nd, rd or th to it. (1st, 234th, 88th) function makeOrdinal( $rank ) { $last= substr( $rank, -1 ); $seclast = substr( $rank, -2, -1 ); if( $last > 3 || $last == 0 )$ext = 'th'; else if( $last == 3 ) $ext = 'rd'; else if( $last == 2 ) $ext = 'nd'; else $ext = 'st'; if( $last == 1 && $seclast == 1) $ext = 'th'; # Avoid 11st if( $last == 2 && $seclast == 1) $ext = 'th'; # Avoid 12nd if( $last == 3 && $seclast == 1) $ext = 'th'; # Avoid 13rd return $rank.$ext; }   And here is one I use for secure hashing (Due to md5 and sha1 being cracked) function hash( $value, $method = '' ) { switch( $method ) { case 'extreme': return hash( 'whirlpool', $value ); break; default: return hash( 'sha256', $value ); } }   I have lots more, but I'll wait and post them in the correct forums some other time ^^ Regards, Dan :thumbup:
×
×
  • Create New...