FunTrackers Easter Special 2016 - Finished! » View News


Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
adding a game to my manialink with PHP problem
Author Message
jonasredant Offline
Homely Mapper
*

Posts: 17
Joined: Aug 2014
Reputation: 0
Post: #1
adding a game to my manialink with PHP problem
Hi,

I'm adding a simple poker game to my manialink (for Trackmania United Forever)
using PHP 5.2. To see what I mean, just paste tmtp:///:jonas in an internet browser
or type jonas in the ingame browser in Trackmania.

After the cards are dealt, the user will be able to replace one or more cards
by clicking on those cards and deal again. Before stating my problem, I will
submit the code:

Code:
<?php

//declare and initialize variables
$delen = $_GET['dln'];

//maak array met de 52 kaarten
$kaartenLijst = array (
    0 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_2.png",
    1 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_3.png",
    2 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_4.png",
    3 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_5.png",
    4 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_6.png",
    5 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_7.png",
    6 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_8.png",
    7 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_9.png",
    8 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_10.png",
    9 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_J.png",
    10 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_Q.png",
    11 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_K.png",
    12 => "http://jonasmanialink.freeiz.com/speelkaarten/harten/harten_A.png",
    13 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_2.png",
    14 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_3.png",
    15 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_4.png",
    16 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_5.png",
    17 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_6.png",
    18 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_7.png",
    19 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_8.png",
    20 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_9.png",
    21 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_10.png",
    22 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_J.png",
    23 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_Q.png",
    24 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_K.png",
    25 => "http://jonasmanialink.freeiz.com/speelkaarten/ruiten/ruiten_A.png",
    26 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_2.png",
    27 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_3.png",
    28 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_4.png",
    29 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_5.png",
    30 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_6.png",
    31 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_7.png",
    32 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_8.png",
    33 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_9.png",
    34 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_10.png",
    35 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_J.png",
    36 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_Q.png",
    37 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_K.png",
    38 => "http://jonasmanialink.freeiz.com/speelkaarten/klaveren/klaver_A.png",
    39 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_2.png",
    40 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_3.png",
    41 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_4.png",
    42 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_5.png",
    43 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_6.png",
    44 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_7.png",
    45 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_8.png",
    46 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_9.png",
    47 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_10.png",
    48 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_J.png",
    49 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_Q.png",
    50 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_K.png",
    51 => "http://jonasmanialink.freeiz.com/speelkaarten/schoppen/schoppen_A.png"  
);

// Output the manialink
header('Content-Type: text/xml');
if (2 == 2) {

$gedeeldeKaarten = array (
0 => mt_rand(0,51),
1 => -1,
2 => -1,
3 => -1,
4 => -1
);

for ($i = 1; $i < 5; $i++) {
   $getal = 0;
   do {
         $getal = mt_rand(0,51);
      } while ($getal == $gedeeldeKaarten[0] || $getal == $gedeeldeKaarten[1] || $getal == $gedeeldeKaarten[2] || $getal == $gedeeldeKaarten[3] || $getal == $gedeeldeKaarten[4]);

   $gedeeldeKaarten[$i] = $getal;
}

echo <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<manialink>
<timeout>0</timeout>
<type>default</type>
<quad posn="7 15 -32" sizen="19 7" style="Bgs1" substyle="BgList" manialink="jonas?dln=1"></quad>
<label posn="11 13 -31" sizen="20 4" textsize="3" textcolor="FFFF" text="Deal cards"></label><!--FRM=kaarten-->
        <frame posn="-17 0 0">
            <quad posn="-24 -1 -31" sizen="13.7 24.3" image="{$kaartenLijst[$gedeeldeKaarten[0]]}"></quad>
            <quad posn="-7 -1 -31" sizen="13.7 24.3" image="{$kaartenLijst[$gedeeldeKaarten[1]]}"></quad>
            <quad posn="10 -1 -31" sizen="13.7 24.3" image="{$kaartenLijst[$gedeeldeKaarten[2]]}"></quad>
            <quad posn="27 -1 -31" sizen="13.7 24.3" image="{$kaartenLijst[$gedeeldeKaarten[3]]}"></quad>
            <quad posn="44 -1 -31" sizen="13.7 24.3" image="{$kaartenLijst[$gedeeldeKaarten[4]]}"></quad>
            <quad posn="-27 3 -32" sizen="88 33" style="Bgs1" substyle="BgList"></quad>          
            
<label posn="0 10 0" sizen="10 10" text="$gedeeldeKaarten[0]"></label>
<label posn="5 10 0" sizen="10 10" text="$gedeeldeKaarten[1]"></label>
<label posn="10 10 0" sizen="10 10" text="$gedeeldeKaarten[2]"></label>
<label posn="15 10 0" sizen="10 10" text="$gedeeldeKaarten[3]"></label>
<label posn="20 10 0" sizen="10 10" text="$gedeeldeKaarten[4]"></label>            
        </frame>
</manialink>
EOT;
} else {
echo <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<manialink>
<timeout>0</timeout>
<type>default</type>
<!--FRM=kaarten-->
<frame posn="-17 0 0">
<quad posn="7 15 -32" sizen="19 7" style="Bgs1" substyle="BgList" manialink="jonas?dln=1"></quad>
<label posn="11 13 -31" sizen="20 4" textsize="3" textcolor="FFFF" text="Deal cards"></label>
</frame>    
</manialink>
EOT;
}
exit;
?>
translation: $kaartenLijst = list of cards, harten = harts, ruiten = diamonds, klaveren = clubs, schoppen = spades,
$gedeeldeKaarten = cards that have been dealt so far

The problem is that the content of the array $gedeeldeKaarten has to be stored temporarilly until the script
executes again (to deal the cards the user wants to replace).
Otherwise I can't determine what cards have been dealt already. Also it will be necessary to store
which cards the user wants to replace before the remaining cards are dealt. How can i temporarilly store
variables or other data with PHP? My idea is to write the necessary variables to a text file, but I don't know
if I can write to a file on a server that isn't mine. Plz some help. (keep in mind that I'm not an experienced
programmer, I occasionally write something when I need to)

Greetings,

Jonas
02.09.2014 16:50
Find all posts by this user Quote this message in a reply
Marcel Offline
ここにはコードがありません。
********
ClanLeader

Posts: 695
Joined: Aug 2007
Reputation: 22
Post: #2
RE: adding a game to my manialink with PHP problem
I think what you want to sue is Sessions: These are data saved for each user (so each user has his own data), and stored on the server to "survive" to the next script calls. If you google for e.g. "PHP Sessions", you find lots of tutorials which may help you Wink2

[Image: 54e39adf682a7e.png][Image: 54e81d421182e1.png]
[Image: message.png]
02.09.2014 17:31
Visit this user's website Find all posts by this user Quote this message in a reply
jonasredant Offline
Homely Mapper
*

Posts: 17
Joined: Aug 2014
Reputation: 0
Post: #3
RE: adding a game to my manialink with PHP problem
Is that a safe way to store information? I want to make sure that the data is stored properly. I don't know much about php sessions, but I did a quick google search and red that a session only lasts 24 minutes. Is that information correct?
Also, I will have to store other data (like scores and account information) for my game. So is it possible to write to a file on a server that isn't mine? Maybe I need to worry about multiple users accessing that file at the same time?
Maybe you will suggest using a database, but I don't know anything about such functionallity...
02.09.2014 17:37
Find all posts by this user Quote this message in a reply
Marcel Offline
ここにはコードがありません。
********
ClanLeader

Posts: 695
Joined: Aug 2007
Reputation: 22
Post: #4
RE: adding a game to my manialink with PHP problem
Yes, sessions will time out after some time (depends on the server settings how long they will live).

If you want to save more persistent data like scores, I suggest a database. The first steps with a DB may be slow, but once you have basic knowledge of how a database works (tables, queries), it will become very handy as finding datasets with any criteria becomes very easy (i.e. SELECT commands). Text files may be possible, but may also become very slow the more data you are storing in them. And as you said: Multiple user accessing a text file becomes a problem. Multiple users on a database is what they are designed for Biggrin

If you already have a server (or webspace), you most likely have a MySQL database. And again Google will help you finding some tutorials to get used to it Wink2

[Image: 54e39adf682a7e.png][Image: 54e81d421182e1.png]
[Image: message.png]
02.09.2014 20:29
Visit this user's website Find all posts by this user Quote this message in a reply
jonasredant Offline
Homely Mapper
*

Posts: 17
Joined: Aug 2014
Reputation: 0
Post: #5
RE: adding a game to my manialink with PHP problem
Thx, really helped me a lot :-)
02.09.2014 20:31
Find all posts by this user Quote this message in a reply
Post Reply 



Forum Jump:

User(s) browsing this thread: 1 Guest(s)

Contact Us | FunTrackers | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication | Twitter