Jump to content
  • Advertisement
Sign in to follow this  
andy1984

php code

This topic is 3634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to make an online roguelike. I want to use php + javascript + ajax + mySQL to do it, but I'm not sure if thats a good idea. Also I can't find a lot of material about roguelike game development. All I have done so far is re-learn how to do php and mysql, made a bit of a login thingee and setup a timing thingee with ajax so it refreshes info on the page without reloading. Hmmm. *edit* I'm not sure if my code is ok or dumb. Can you look at it for me? I want to make like a singleton but I don't really know how. This is message_object.php. I was having issues with session variables and useing ajax, hence all the sessionXXX(). These classes hold error and other messages and I'll use it to print errors for the user as well as for me fixing bugs. I'll go look up php singletons now, lol.
<?php
require_once('session.php');

define("MSG_ALL", 1);
define("MSG_UNKNOWN", 2);
define("MSG_ERROR", 3);
define("MSG_SUCCESS", 4);
define("MSG_WARNING", 5);
define("MSG_INFO", 6);
define("MSG_INFO_GOOD", 7);
define("MSG_INFO_BAD", 8);
define("MSG_NONE", 9);
define("MSG_NAME", "messages");

// Make the messages object.
sessionBegin();
if (!sessionIsVarSet(MSG_NAME))
{
    $_SESSION[MSG_NAME] = new messages();
}
sessionEnd();

function report($msg, $class, $code = MSG_NONE)
{
    sessionBegin();
    $_SESSION[MSG_NAME]->report($msg, $class, $code);
    sessionEnd();
}

function writeMessages()
{
    sessionBegin();
    $_SESSION[MSG_NAME]->writeAll();
    sessionEnd();
}

function resetMessages()
{
    sessionBegin();
    unset($_SESSION[MSG_NAME]);
    $_SESSION[MSG_NAME] = new messages();
    sessionEnd();
}

function killMessages()
{
    sessionBegin();
    $_SESSION[MSG_NAME]->clear();
    sessionEnd();
}

function showMessages($show)
{
    sessionBegin();
    $_SESSION['messages']->show($show);
    sessionEnd();
}

class message
{
    private $code;
    private $msg;
    private $class;
    
    public function message($msg, $class, $code)
    {
        $this->code = $code;
        $this->msg = $msg;
        $this->class = $class;
    }
    
    public function __construct($msg, $class, $code)
    {
        $this->code = $code;
        $this->msg = $msg;
        $this->class = $class;
    }
    
    public function write($show)
    {
        if ($this->code >= $show)
        {
            switch ($this->code)
            { 
              case MSG_ERROR:
              {
                  print('<span style="color:red">Error ['.$this->class.']: '.$this->msg.'</span><br />');
                  break;
              }
              
              case MSG_SUCCESS:
              {
                  print('<span style="color:green">Success ['.$this->class.']: '.$this->msg.'</span><br />');
                  break;
              }
              
              case MSG_WARNING:
              {
                  print('<span style="color:orange">Warning ['.$this->class.']: '.$this->msg.'</span><br />');
                  break;
              }
              
              case MSG_INFO:
              {
                  print('<span style="color:orange">'.$this->msg.'</span><br />');
                  break;
              }
              
              case MSG_INFO_GOOD:
              {
                  print('<span style="color:green">'.$this->msg.'</span><br />');
                  break;
              }
              
              case MSG_INFO_BAD:
              {
                  print('<span style="color:red">'.$this->msg.'</span><br />');
                  break;
              }
              
              default:
              {
                  print('<span style="color:pink">Default ['.$this->class.']: '.$this->msg.'</span><br />');
              }
            }
        }
    }
}

class messages
{
    private $messages;
    private $show;
    
    public function messages()
    {
        // Init the session vars.
        $this->show = MSG_ALL;
        $this->messages = new ArrayObject();
        
        $this->report("Initialized messages", 'messages', MSG_SUCCESS);
    }
    
    public function __construct()
    {
        // Init the session vars.
        $this->show = MSG_ALL;
        $this->messages = new ArrayObject();
    }
    
    // Send a report.
    public function report($msg, $class, $code)
    {
      $this->messages->append(new message($msg, $class, $code));
      return $code;
    }
    
    public function show($show)
    {
        if (isMessageCode($show))
        {
            $this->show = $show;
            $this->report("Showing messages ".$_POST['show'], 'messages', MSG_SUCCESS);
        }
    }
    
    // Write all the messages.
    public function writeAll()
    {
        print('<div id="messages" name="messages">');
        
        if (isset($this->messages))
        {
            $it = $this->messages->getIterator();
            for($i = $this->messages->count() - 1;$i >= 0;$i--)
            {   
                $it->seek($i);
                if ($it->valid())
                {
                    $value = $it->current();
                    $value->write($this->show);
                }
            }
        }
        print('
'); } public function clear() { $this->messages = new ArrayObject(); $this->report("Cleared messages", 'messages', MSG_SUCCESS); } } // Chekcs if the var is a message code. function isMessageCode($value) { return ($value >= MSG_ALL && $value <= MSG_NONE); } ?> If its bad, please tell me! [Edited by - andy1984 on October 11, 2008 4:32:53 PM]

Share this post


Link to post
Share on other sites
Advertisement
Yes it's definately possible.
Do you want a multi-player game, or just a single-player game hosted on the web?

The system components you have chosen are fine too.
The browser forms the end-user display, powered by javascript. The Web Host controls the actual game state using PHP, and stores the game state in a SQL database.
AJAX allows the javascript to send requests (e.g. move south) to the Web Host, and retrieve the new game state (e.g. your character has moved south).

Browser Web Host
| | |
javascript -- AJAX -- PHP -- MySQL

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!