Jump to content
MakeWebGames

Getting rid of the "page-lag"


Inveteratus

Recommended Posts

While playing a number of mccodes games over the years, a lot of them exhibit a page "lag", where you perhaps commit a crime, our brave (nerve) bar goes down by one, your money goes up by $100, but you don't see these changes until the next page load. It's a very common problem and easily explained as the code as it stands outputs the user data with your bars and money *before* the page itself runs and outputs itself. So the simple solution, at the cost of a single extra query is to tweak the header.php script from:

<?php

class headers
{
    function startheaders()
    {
        // ...
    }

    function userdata($ir, $lv, $fm, $cm, $dosessh = 1)
    {
        // ...
    }

    function menuarea()
    {
        // ...
    }

    function smenuarea()
    {
        // ...
    }

    function endpage()
    {
        // ...
    }
}

to:

<?php

class headers
{
    private $_menu = '_nomenuarea';

    // new constructor to save the body data
    public function __construct()
    {
        ob_start();
    }

    // new destructor to display everything correctly
    public function __destruct()
    {
        global $db, $userid, $ir, $atkpage;

        // Get the body html
        $body = ob_get_clean();

        // Refresh the global user information array
        $sql = 'SELECT u.*, us.* FROM users u LEFT JOIN userstats us USING (userid) WHERE userid = ' . $userid;
        $ir = $db->fetch_row($db->query($sql));

        // Prep some basic variables for the header
        $fm = money_formatter($ir['money']);
        $cm = money_formatter($ir['crystals'], '');
        $lv = date('F j, Y, g:i a', $ir['laston']);

        // Output the headers and userdata
        $this->_startheaders();
        $this->_userdata($ir, $lv, $fm, $cm, $atkpage);

        // Call the correct menu
        call_user_func([$this, $this->_menu]);

        // Output the body
        echo $body;

        // And tidy up
        $this->_endpage();
    }

    // These functions are called by globals.php/sglobals.php and intentionally do nothing
    public function startheaders() { }
    public function userdata($ir, $lv, $fm, $cm, $dosessh = 1) { }
    public function menuarea() { $this->menu = "_menuarea"; }
    public function smenuarea() { $this->menu = "_smenuarea"; }
    public function endpage() { }

    // These functions are the original functions called by globals.php/sglobals.php
    // Note, they have all been prefixed with _
    private function _startheaders()
    {
        // ...
    }

    private function _userdata($ir, $lv, $fm, $cm, $dosessh = 1)
    {
        // ...
    }

    private function _menuarea()
    {
        // ...
    }

    private function _smenuarea()
    {
        // ...
    }

    private function _endpage()
    {
        // ...
    }

    // Finally add a "no-menu" for when no menu function has been called
    private function _nomenuarea()
    {
        echo '</td>';
        echo '<td width="2" class="linegrad" bgcolor="#ffffff">&nbsp;</td>';
        echo '<td width="80%" bgcolor="#ffffff" valign="top">';
        echo '<center>';
    }
}

It has the added advantage of meaning that that no longer have to call $h->endpage() anywhere in your pages, as it is automatically called.

Obviously, if you have made changes to your header.php, then you have to fiddle with it quite a bit to shoehorn this concept in, and of course you may need to adjust the new _nomenuarea() method to suit.

 

Edited by Lacuna
Code tweak
Link to comment
Share on other sites

It should be noted that I don't consider this a definitive solution - rather it's a workaround.

The reason being that you are adding another query, and it would probably be better to use a template manager such as Twig or maybe Smarty.

Your workflow then becomes:

  • requires/includes, and bootstrap your session
  • check to see if the user is permitted to access this page
  • handle any POST requests and perform redirects
  • handle the GET request
  • push all the up-to-date information out to your templating system
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...