PHP - Design Patterns For Games Theory
Hey guys,
I have been programming for a while and I want to know what sort of design patterns I should look into when making a game? For example what design pattern is best for: Database management User management Forum management Etc I can only think of singleton or factory (for user/forum) I am reasonably new to OOP, but would like to learn it properly, so I thought creating a simple program such as rock-paper-siccors would work to be able to expand it eventually, and hopefully it will teach me reusable code practice. Similar TutorialsHi, I own a website called www.gpstudios.com, a flash games site. I didn't design it myself, but since having it built a year ago, I understand pretty much all of the inne workings. I'm a game coder so php makes good sense to me. Anyway, i woudl like to change the way a game info (instructions, description) is fetched. Currently, we have 650 games in our database. The way all the info is gotten is through mySQL. So, what I would like to do is take these lengthy instructions/description strings and put them each into a text file, within the folder destination of the game. Currently though, we don't even have a folder destination for each game, which is also a problem. Some games have extra files, which our site-developers didn't account for. There are several games that we've had to manually put into folders because of this problem (two .xml files cannot be named the same in the same folder) Also, if you go onto my website and click on a game, you get sent to playgame.php?id=xxx The main thing I would like to change though (which I have attempted before, but not succeeded with) would be to change the url adress of the games to something more like: www.gpstudios.com/games/game_name/ This is how most game websites do it. When I tried to do this myself, what I did was to simply create an index.php within the folder of one of the aforementioned games, and within this folder I wrote something as simple as: include("../../../playgame.php?id=xxx"); But upon loading this page, I would receive a load of error messages regarding all the other includes and requires in the playgame.php. So by inserting playgame.php?id=xxx in this deep folder, it destroys all other directory links elsewhere. I investigated and found that inserting set_include_path($_SERVER['DOCUMENT_ROOT']) would fix the problem with all these links, but it didnt - while the page loads, it appears to ignore anything that isn't in the root directory, including images. I tried the include method with loads of different files, and they just wouldn't load correctly if they used anything outside the root directory. And my final wish would be to do all of this automatically. To simply write a php file that I would run once and it would generate all the directories for me. It would need to copy every single description and instructions from mySQL and put them within text files within directories which are named after the game. I would then need to automatically create a load of index.phps for each individual game with some simple code similar to: include("../../../playgame.php?id=xxx"); Obviously it would be silly to create a load of unique index files with a hard copy of playgame.php, incase I want to give my site a makeover at some stage. I'm asking alot aren't I? Well I know that if I was given the code from another games website, I would understand it and it would be easy to implement, but unfortunately, despite searching, downloading and installing over 10 Flash Game arcade scripts, none of them used this method... despite their demo page showing that they do. Mainly, all I need to know is how to is how to generate a php/text file, and then I need to know what method I would use for getting the playgame.php running correctly from within a different folder. I'm pretty sure I could do the rest on my own then. Thanks! Creating my own little MVC framework to understand it better. Question - When a view outputs a page with a form in it, how is the form handled? ie, what is the action of the form? Where does the IF statement that checks if the submit button was pressed, go? I was told inside a method in the Model...but I don't understand how a method can check if a form has been submitted or not. Any help greatly appreciated. Hey everyone! New member here still getting the hang of PHP. Any help is much appreciated. Suppose the array contains: array ('http://www.php.net/styles/site.css', 'http://www.example.com/styles/some.js', 'http://www.domain.net/styles/hey.php'); How do I get php to find the array item that contains let's say "css" and instead of returning just "css" it returns the entire string that has the "css" in it, instance "http://www.php.net/styles/site.css". I was thinking of using the following: preg_match('/[SOME_PCRE_PATTERN].css/', 'http://static.php.net/www.php.net/styles/site.css', $matches); Problem is since the links could be different and have all sorts of different characters in it, I have no idea what to put for the PCRE_PATTERN. In other words, this: $links = array ('http://www.php.net/styles/site.css', 'http://www.example.com/styles/some.js', 'http://www.domain.net/styles/hey.php'); foreach ($links as $key) { preg_match('/css/', $key, $matches); $results[] = $matches; } print_r ($results); Returns this: Array ( [0] => Array ( [0] => css ) [1] => Array ( ) [2] => Array ( ) ) I would like it to return: Array ( [0] => Array ( [0] => http://www.php.net/styles/site.css ) [1] => Array ( ) [2] => Array ( ) ) I hope that makes sense to you guys. Once again, any help is appreciated and thank you! <input type="text" maxlength="5" name="zip" value="Zip Code" pattern="(\d{5})?" />So, if the zip code is filled, it should be a five digit number. If it's not filled it's optional. The above RegEx is forcing everyone to enter a zip code. Can HTML do this, or am I going to have to script it? Although this is the PHP coding section i couldn't think of a better place to post this, it has more to do with theory or a way for me to achieve what I want rather than a piece of code which I cant get to work. I have been working on this project, sort of a note taking application. When a new note is added to the database, after the ajax, the page is refreshed and then the new note appears along with the note title in the navigation pane. My problem is when I put the site up on my web server it runs more slowly so when the page is refreshed after the ajax the data is not yet in the database so it needs a second manual refresh for the data to appear. I tried delaying the refresh a tenth of a second and it sort of worked but I ran into problems when actually trying to implement it. But after getting frustrated, I have decided that my refresh workaround isn't very elegant. Is there a technique or method I could use to have certain parts 'refresh' without actually refreshing? I'm sure it is possible and probably quite simple I can think of lots of times when this sort of thing would be needed. Even just making a comment on facebook the whole page isn't refreshed, it just appears. Or rather somehow knows there is new data in the database and updates its self. But this is my first real php project so I'm still learning how everything is done. Hey guys. I am building a games website, and I am using ID (example: games.php?id_) to view games with their Title, SWF file to play, and their Image Icon. I am basically fetching information per game using ID that is typed in the URL after my website address containing the '?id_' or '?id='. Can somebody tell me whats wrong with the code I have bellow, because it's not working. I always get parse error (T_STRING) at line 9. This code is inside my games.php page. It doesn't seem to work. ================================ <?php $conn = mysql_connect("host", "username", "password"); mysql_select_db("games"); $game_id = $_GET['id']; $sql = "SELECT * FROM games WHERE id='$game_id' LIMIT 1"; $result = mysql_query($sql, $conn) or die(mysql_error()); $number_of_results = mysql_num_rows($result); while ($result_array = mysql_fetch_array($result, MYSQL_ASSOC)) { $id = $result_array['fldID']; $title = $result_array['fldTitle']; $graphic = $result_array['fldIconSmall']; // Example: game1234.png $swf = $result_array['fldSwfSrc']; // Example: game1234.swf $graphic_file_location = 'http://www.blabla.com/games/images/' . $graphic; $swf_file_location = 'http://www.blabla.com/games/' .$swf; echo '<a href="http://www.blabla.com/games.php?id=' . $id . '"></a>'; echo '<h1>' . $title . '</h1>'; echo '<img src="' . $graphic_file_location . '" width="50" height="50" border="0" />'; echo '<object width="550" height="400">'; echo '<param name="movie" value="' . $swf_file_location . '">'; echo '<embed src="' . $swf_file_location . '" width="550" height="400">'; echo '</embed>'; echo '</object>'; } ?> ================================ Any help would be appreciated since so far this script has not executed at all. I keep getting an error when executing this entire script. I can't find any errors in it either..but it just doesn't work. Is there anyway to have users play flash games on my forum and if they win/etc, send them Forum Gold? Wouldn't it be easy to hack it though by just changing the data being sent from the flash app with tamper data? I want to put a poker flash game on my site, and if users play poker/etc and if they win, I want to run a mysql query to update there account with more "Forum gold" If you will, and I want the poker flash game to have correspond and work with the amount of Forum Gold that they have? Would this have to be custom coded by me If I learn actionscript? OR is there anything out like this that I can use? I have created a system that uses an API to drop utility data into MSQL Server 2008. Once the data is in the db, a web app can query the data to create electric bills. Currently we are working with only one customer but are entertaining expansion and want to approach the growth from the best possible angle.
Considering that all data is the same type of data but comes from different customers and sources, would it be best to maintain 1 database with all of the utility meter data streaming to it or would it be best to have a new data base with each customer?
Also, assuming we keep it all in the same database, would it be best (assuming we are secure) to have all the individual customers data go to the same tables or have separate tables for separate customers.
I'm pretty certain I know the answer but I'm a novice so that means nothing.
Thank you for any help with this issue.
Edited by Butterbean, 18 November 2014 - 11:28 AM. Hi all am haiving abit of a problem am trying to query 6 categories with 6 games each.With the code i done is only showing one cat with 6 games.
How can i fix this ?.
If you go to games-flash.co.uk you will see at the bottom of the home page that only one cat shows.
php code.
<div class="catgames"> This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=357178.0 Hello, I am fairly new at php (have been using HTML/CSS for a while but recently learned the php basics). I am building a website to allow clients to schedule through multiple massage therapists and I am struggling with the basic beginings of a scheduler scripts. If someone can point me towards a tutorial or script that would be great, or answer any of the following questions: 1. What would be the best way to store each therapists weekly schedule (based on monday-sunday with start and end times) and propagate that info. to every day that a client might schedule on? 2. How do i get the day of the week based on the date? 3. How do i display a calendar that lets them choose the date? I have an audio mix submitting form on my website. The first part of the form takes the details from the user and inserts a new row in the first table 'mix', auto incrementing to generate a mix_id primary key. The second part then uses a series of check boxes to select which genres the mix contains and a foreach for the resulting array to insert, into the 2nd table 'genres_for_mix', a new row for each genre id (foreign key for the genres table) that is selected and the mix_id that it concerns. Now I'm starting to think this is a bad way to do this because: 1. I don't know how to echo this out when a user edit's the mix to make all the relevant checkboxes selected. 2. When a user deselects a choice, it would be quite long winded to delete the nessesary row. So instead I'm thinking that it would be much wiser to user the serialise functions to put the array into the database However, I still don't know how to make the right checkboxes selected when I echo out the edit form. Do I use something like inarray()? If so how? Also, it's really important that I can query the database for mixes that are associated with a specific genre. Without unserialising the array and therefore echo'ing out all the results, I don't know how to query this. Would I just use a 'WHERE genre_field LIKE 'genre_id'? Can anyone help me with this please? Basically I am setting up my own cricket management game, but there are 3 areas that I am slightly stuck on and would appreciate help: 1) When a use registers, I need to create 11 players for their team. For this, I am going to have one text files with first names and one text file with surnames. How can I open each file and select a name and then match them together? 2) I need to create the list of games to be executed at particular times through a cron job. How can I go through all the teams in a table and match them together in games, ensuring everyone plays everyone? If a new player registers, he will be put into a random league and so the games for that league will have to be redone 3) How can I set up a cron job to execute the above games? Each team should play once a day and so if there are 24 teams in a table, there will be 12 games that occur through this cron job. I assume cron jobs are the best thing for this. Any help would be appreciated. Thanks. Hi Guys.
Doing an assignment for uni, and stuck on an error. Ill attach some files to show the problem and any help very much appreciated.
Error and the screen it comes on
Code
<?php $select = mysqli_query($con, "SELECT * FROM categories");
while ($row = mysqli_fetch_assoc($select)) { }
function dispsubcategories($parent_id) {
}
function getnumtopics($cat_id, $subcat_id) {
and the structure of the database
Edited March 28, 2020 by Ben555 Hello everyone, I'm in the process of creating a PHP script (will be open-source), I just need to know how to make it easy for people to add plugins? Like I don't really know much about the concept of plugins (how they should work), all I know is that there should be a function to activate / deactivate the plugin, but how can I make it possible for people to run their "code" wherever they want? I've heard about "hooks" but I don't really understand how this should work, anyone can explain please? Thank you very much in advance Hi, For the last few years on my website I have been using FluxBB/PunBB's login script which basically just stores a cookie with their user ID and their password hash. This is fine except if the database got leaked anyone can login as anyone else providing they have their hash. I'm trying to be more secure about this now. Can someone explain a better process to me please? I was thinking of storing sessions with a unique hash in an 'active_sessions' table and providing they have a cookie with that hash they are granted that session? If someone uses a different IP to what that session was created with it's deleted? I'm new to all of this so any advice would be great. I want this to be secure. Hi guys, I don't know if I'm over complicating things here and cannot see the wood for the trees, but I seem to have got myself confused. What I'm simply trying to do is get teamName and teamresults of a team from a database. The code will perhaps explain better than I can (note I haven't included the DB class which is fine).
class TeamCollection { protected $database; protected $teamid; protected $team_name; // pass db by dependency injection... public function __construct(Database $database) { $this->database = $database; } public function getTeam($teamid){ $this->team_id = $teamid; $this->database->query("SELECT team_name, nickname, founded FROM club WHERE team_id=:teamid"); $this->database->bind(':teamid', $this->team_id); // spawn a new Team object if query is valid, if not throw exception and end via try/catch... if ($result = $this->database->single()) { return new Team($result); // create new Team passing $result to Team constructor } else { throw new exception ('No Valid Team returned'); } } public function getResults($teamid){ $this->team_id = $teamid; $this->database->query("SELECT result FROM results WHERE team_id=:teamid"); $this->database->bind(':teamid', $this->team_id); // If a Team has valid results, return array of match scores.. if ($scores = $this->database->results()) { return $scores; } else { throw new exception ('No Valid Team Results to return'); } } } //end class // Team is abstracted from DB class Team { private $team_name; private $result; private $scores; public function __construct(array $result) { $this->team_name = $result['team_name']; } public function getName() { $this->team_name; } public function getResults(array $scores) { foreach ($scores as $row){ echo $row; } } } // implementation $team_id =9; // passed variable.. $database = new Database($server,$db_type); // create db.. $database->connect(); // connect to db.. $collection = new TeamCollection($database); // pass live DB link to TeamCollection Class try { $team = $collection->getTeam($team_id); // try and spawn a valid Team Object.. echo $name = $team->getName(); // If so lets return a Team name $scores = $team->getResults($team_id); // Now try and get Team Results.. $display_results = $scores->getResults($scores); // Display results by passing them to Team class to } catch (Exception $e) { Echo 'Exception Thrown: ' . $e->getMessage(); }My basic difficulty is where to put the getResults method and how to use it? I'm fine spawning a new Team as required, but is my logic for then moving on to getResults sound? I was trying to abstract my Team class from having any database association at all. Is this a wise approach or just not required? Am I overcomplicating the issue or missing something? Can anyone help? I guess my difficulty is between connecting classes and objects in the best manner. Thanks in advance. Edited by mich2004, 19 May 2014 - 06:48 AM. Hello I have a question in regards to design. No need for example code just of how to approach the problem. Issues and things that cannot change because they are part of the situation. 1. Cannot use any database like oracle, MySQL, or SQLite 2. The server does not run PHP5 3. The students do not have a unique field and cant have one because they have not my employers have not decided on one. Here is the problem: I have to make a form with the following fields first name last name comments date I have stored the fields in a text file. However, how can I relate date to the comment without having duplicates records? For example. Lets suppose my boss asked me can you look up the comments that the counselor made in july 10 2010. My answer: One must have duplicate records of each instance of a comment. Am I wrong? Hi All, I currently have a ticketqueue that show's all tickets assigned to a group of people, but split into personal queues, but the way that I wrote it, means that it needs manually updating if a specific person leaves/joins the department. For example, to get the queue details, I use the following query: Code: [Select] $username1 = mssql_query("select id,subject,body,priority from queue where assignedto = username1";) $username2 = mssql_query("select id,subject,body,priority from queue where assignedto = username2";) I have repeated this code for all of the users in our team. Which seams a waste, as I have all the information on our team stored in a DB called "sysadminusers". Is there an array I could use that would look at all the usernames in the table, and then repeat the query for me? I would also need this array to display the results on the page, currently I use the following: Code: [Select] while($username1_tickets = mssql_fetch_assoc($username1)){ echo $username1_tickets['id'],$username1_tickets['subject']$username1_tickets['body'],$username1_tickets['priority'];} while($username2_tickets = mssql_fetch_assoc($username2)){ echo $username2_tickets['id'],$username2_tickets['subject']$username2_tickets['body'],$username2_tickets['priority'];} I am just looking for some design advice and code examples that would help me tidy up my code for this page, it seams a lot of code for quite a simple page. Thanks Matt |