adding a game to my manialink with PHP problem

Talk about ManiaLink development in this forum

Moderator: NADEO

Post Reply
jonasredant
Posts: 39
Joined: 10 Aug 2014, 17:26

adding a game to my manialink with PHP problem

Post by jonasredant »

Hi,

I know this forum is for Trackmania 2, but for the older games there aren't many people
creating manialinks anymore or visiting that section of the forum...
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: Select all

<?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
User avatar
steeffeen
Translator
Translator
Posts: 2463
Joined: 14 Oct 2012, 16:22
Location: Germany

Re: adding a game to my manialink with PHP problem

Post by steeffeen »

you could use php sessions and save the game information in the $_SESSION variable
    Game Mode and Title Pack Creator, Developer, ShootMania-Player & more

    ManiaControl, FancyManiaLinks
    jonasredant
    Posts: 39
    Joined: 10 Aug 2014, 17:26

    Re: adding a game to my manialink with PHP problem

    Post by jonasredant »

    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... If php sessions is the way to go, could you tell me something about the syntax to get me started?
    User avatar
    steeffeen
    Translator
    Translator
    Posts: 2463
    Joined: 14 Oct 2012, 16:22
    Location: Germany

    Re: adding a game to my manialink with PHP problem

    Post by steeffeen »

    the session lifetime is renewed with every page impression but it expires after some time of inactivity, yes

    i thought you are only looking for saving information about the currently running game
    if you want to save long-term information you definitely need a database to build it properly
    i advise you against building an own database-like information saving in files

    you can find session examples online
    it's all about session_start(); and using the superglobal array $_SESSION
    Manual
      Game Mode and Title Pack Creator, Developer, ShootMania-Player & more

      ManiaControl, FancyManiaLinks
      jonasredant
      Posts: 39
      Joined: 10 Aug 2014, 17:26

      Re: adding a game to my manialink with PHP problem

      Post by jonasredant »

      Ok, thx. What's a page impression?

      btw, are you [TNT] Techno!.GC?? I downloaded replays like Steffen_BayB4.Replay.gbx
      If you are, you sure know how to drive...
      User avatar
      steeffeen
      Translator
      Translator
      Posts: 2463
      Joined: 14 Oct 2012, 16:22
      Location: Germany

      Re: adding a game to my manialink with PHP problem

      Post by steeffeen »

      a page impression (PI) is a single loading of a website

      nah i'm not that guy, if you would see me playing trackmania you would rather say "you sure know how to hit a wall"
        Game Mode and Title Pack Creator, Developer, ShootMania-Player & more

        ManiaControl, FancyManiaLinks
        jonasredant
        Posts: 39
        Joined: 10 Aug 2014, 17:26

        Re: adding a game to my manialink with PHP problem

        Post by jonasredant »

        Ah ok, lol

        while you're here: could you look at tmtp:///:mania-games
        My game will be something like that. Maybe you can tell me what I need (database, sessions,...)
        Post Reply

        Return to “ManiaLink”

        Who is online

        Users browsing this forum: No registered users and 1 guest