Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,005 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,364 people online right now. Registration is fast and FREE... Join Now!




Text based game, need some advices here.

 
Reply to this topicStart new topic

Text based game, need some advices here.

Barmoen
10 Jan, 2008 - 03:54 PM
Post #1

New D.I.C Head
*

Joined: 10 Jan, 2008
Posts: 8

Hello.

I read through your forums looking for an answer to my problems, though there was some answers, they mostly didn't help me fully out.
I'm trying to create a textbased game, using PHP and HTML. I made the MySQL database with 3 rows, ID, Username and Password.

Though the only thing I've accomplished so far is the login and registration, I've read through several forums and tutorials to learn but I'm stuck, and I need assist by some experienced programmers.

Is it
CODE
Sessions start()
I have to write ontop of each .php file to send them back to the login if they aren't logged in?

And do I have to create a new row in the database to manage the money via ID's on my game?

Any help and advices given to me is appreciated, and if you got some time to explain me directly, you can add my MSN, Aleksandr@catchgamer.no or send me an email on Aleks.barmoen@gmail.com or just answer here!

Love, Barmoen.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Text Based Game, Need Some Advices Here.
10 Jan, 2008 - 04:16 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,184



Thanked: 210 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
We usually ask and answer questions directly on here... just for future reference. smile.gif

The session_start is used at the top of files to create a user session. This is one way and the most common way of keeping track of a user being logged in. When you create a login script, after verifying the user and their account, you would set a session variable like $_SESSION['authorized'] = true;. The on each page, to keep them in the session, you would use the session_start() you see as well as checking the session variable to see if it exists. If it exists it means they went through the login screen and got validated. Here is the general idea...

CODE


<?php
     // Needed to start the session on every page.
     session_start();

     // If the session variable is not set or does not equal true, send them back to the login page.
     if (!isset($_SESSION['authorized']) || (!$_SESSION['authorized'])) {
          header("location: login.php");
     }
?>



Usually what happens is that if you want them to be able to logout, you destroy the session using session_destroy(). Look at the php.net website under sessions for more information on how sessions work and how you cause use them to keep a user session going and valid.

As for the money portion of the game, where you store that field is up to how you want to design the game. I have designed games where the money was part of the user's table, and there have been games where I put it in a "resources table" along with things like wood, stone, gems etc. That resources table would then have a key field that links back to the user's table.

Hope this answered your question. smile.gif

"At DIC we be session hugging code ninjas!" decap.gif

User is online!Profile CardPM
+Quote Post

Barmoen
RE: Text Based Game, Need Some Advices Here.
10 Jan, 2008 - 04:42 PM
Post #3

New D.I.C Head
*

Joined: 10 Jan, 2008
Posts: 8

I understood the session part, and thank you very much for that!

Though, to the money part, since that is what struggles me the most at the moment, is there a chance that you can give me some examples on who I can build that up? I am going to use it as money in a mafia game, some simple money is what's needed for me.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Text Based Game, Need Some Advices Here.
10 Jan, 2008 - 04:52 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,184



Thanked: 210 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Sorry for the somewhat incoherent post earlier. I went back through it and realized I made several typos. I guess I got a little ahead of myself.

As for showing you how to setup the money system for your game, that is a little beyond what we do here. It is also dependent on your game setup and what you want to do in the mafia game. If your game is going to do things like keep track of the player's turf, money, thugs, weaponry or anything else that may be considered an "asset" you may want to put it into a separate table like so...

CODE

User Account Table
-----------------------
ID
name
Age
Location
email
interests
...


Resources Table
-----------------------
ID
UserID <---- Key back to the user account table
money
weapons
thugs
cars
girls
houses
....



Notice that there is a field key (known as a foreign key) back to the user's account. This allows you to keep all the user's resources together in one table, one row representing one user's assets and then link back to users. So if I wanted to know what user 1 had for money I could run a query that links the tables like so...

CODE

Select money from resources, users where user.id = resources.userid and users.id = 1


This would return one row and one field, money for the user with the id of 1.

Hopefully that makes sense. smile.gif
User is online!Profile CardPM
+Quote Post

Barmoen
RE: Text Based Game, Need Some Advices Here.
10 Jan, 2008 - 05:02 PM
Post #5

New D.I.C Head
*

Joined: 10 Jan, 2008
Posts: 8

Okey, so basically I need to create a new table in the same database and call it whatever I like, and include ID, UserID and whatever each person can get?

Do these rows need to be VARCHAR ?
User is offlineProfile CardPM
+Quote Post

Barmoen
RE: Text Based Game, Need Some Advices Here.
11 Jan, 2008 - 04:17 PM
Post #6

New D.I.C Head
*

Joined: 10 Jan, 2008
Posts: 8

Anyone that can assist me how to build the resources table up? Are the resources VARCHAR in the database?
User is offlineProfile CardPM
+Quote Post

Barmoen
RE: Text Based Game, Need Some Advices Here.
14 Jan, 2008 - 12:36 PM
Post #7

New D.I.C Head
*

Joined: 10 Jan, 2008
Posts: 8

Hello again.

I have now succesfully made sessions and uhh, not really the money.

Martyr2, can you please explain it in another way how to build up the resources table?
I didn't quite get it in the way you explained me.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 12:36PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month