Jump to content

Dillybob

New Members
  • Posts

    6
  • Joined

  • Last visited

Posts posted by Dillybob

  1. I'm actually going to add another process into the mix. With that in mind, here's what I was trying to describe:

     

    - There is more than one process [1] running, each one having its own dedicated purpose

    - Each client that connects gets its own process that is oriented towards supporting communication with the client

    - There is a "socket server" that monitors each client process; it is the process that has the original socket that people connect to, but when they connect it creates a child to handle it rather than do the work by itself

    - The socket server will often relay messages to and from a client process

    - There is a process for each thing ("handler") that needs to run on ticks, and it can communicate with its managing process

    - There is a "tick server" process that monitors each tick handler: it counts out the ticks and tells each child when a tick happens

    - Likewise, the tick server will often relay messages to and from a tick handler

    - (New) There is a "game server" process that deals with the game itself, and is what people would generally call "the server" when they're talking about this game

    - The three server processes can communicate with each other

     

    Here's an example of how it all works together. Let's take an MMO as an example:

    - Game server starts and does whatever it needs to do (eg, initialize state, set up environments, whatever)

    - Socket server starts and waits for connections

    - Tick server starts and launches processes for all the tick handlers it has

    - Tick handler #1: deals with the time of day, like it tracks when "day" and "night" happen:

    1. When it starts it queries the game server for state and is told it is daytime - or is told the in-game time, or whatever

    2. It runs, sleeps, runs, sleeps, and so on, waiting...

    - Tick handler #2: resources regenerate 1 unit per 120 ticks (60s)

    1. When it starts it sets a counter to 0

    2. Every tick the counter increments

    3. When the counter reaches 120 the process issues an update [2] and resets the counter to 0

    - Now, a user connects:

    1. They connect to the socket server

    2. The socket server accepts the connection, spawns a child process for the client, and goes back to waiting for connections

    3. The child process has the user authenticate and whatever, and informs the game server (via the socket server)

    4. The game server sends information back about the client, blah blah blah

    - As the user acts, the client processes sends information to the game server, which acts accordingly

    - Meanwhile the tick handler has been executing, however it's currently daytime (as it was told by the game server) and nighttime hasn't come yet

    - Eventually the tick handler detects a change to nighttime:

    1. It sends a message to the game server (via the tick server) regarding the change

    2. The game server updates itself

    3. It then sends a "broadcast" message to the socket server to inform all clients of the change (by basically just passing the same message along to everyone)

    - The user doesn't like playing during the dark and rage-quits:

    1. The client process discovers the connection died

    2. It passes a message to the game server (via the socket server) that the user disconnected

    3. Game server updates accordingly

     

    However as I explain this I realize I'm not a fan of ticks. Personally I would go for an event-based system since ticks can be simulated through sleeping:

    - Daytime tick handler sleeps for however long day-/nighttime is, wakes, sends an event to update, and goes back to sleep

    - Resources tick handler sleeps for a minute at a time

     

    So questions for you:

    1. Have more details about what this game (I presume) system is for and what it does?

    2. What do you need ticks for?

     

    And disclaimer: I know enough about socket programming and multi-threading/-processing to think I know what I'm talking about. You should also look for resources written by professionals.

     

    [1] Again, because forking is easier to explain. The threading model is similar enough.

    [2] Few possibilities. My first thought would be, essentially, executing an UPDATE query on a database, then informing the game server that resources have expired. This is slow. Faster would be having everything in memory, but I'm not knowledgeable enough to know how to pull that off well. (Shared memory, maybe?)

    That is quite a reply. I understand the 'tick process' more thoroughly now though. Thanks for that!

     

    My issue I'm trying to comprehend is, how in the hell do I install a tick server that runs along side with PHP? I'm currently re-writing my game socket server from php to nodejs (javascript), so I can literally, just use setInterval. I think it's a waste of time, but the setInterval is what will make the game dynamic and differentiate it between a regular pokemon combat game, or an action RPG where monsters can attack you. I'm aiming moreover for the dynamic attack system.

     

    It's for monster ticks. As of right now, the only way a monster can do any damage to the main characters in game, is if a user engages a request to PHP so I can do the calculations. I am not going to rely on javascript clientside coding to cast the monster to attack, that is unreliable and not secure. This is where setInterval comes in and where I cannot achieve it with PHP.

     

    When a player enters a world and walks in range to a monster the setInterval will start ticking. This will include the monsters attack speed, and other functions. This achieves a much more 'dynamic' and 'fluid' feel for combat and attack mechanisms. I can also incorporate HP Regeneration, MP Regeneration, fury regeneration and other fun stuff. All using simple setIntervals (or a simple ticker). That's pretty much the jist of it thus far.

  2. Please don't.

     

    usleep.

    $start = microtime(true);
    while(true) {
        // stuff
    
        $end = microtime(true);
        $remaining = 0.5 - ($end - $start));
        if ($remaining > 0) usleep($remaining * 1e6);
        $start = $end;
    }

     

    Thanks for the response.

    One issue though. usleep won't work for my project as other players will be accessing that code at the same time (It will bottleneck other users access to the script itself)

     

    Someone told me I need to use threads? Would that make the usleep per user specific? So it won't effect other people accessing the script?

  3. I have a very troubling problem at hand. I am using a web-socket server that runs in PHP. The issue is I need to be able to use a setInterval/setTimeout function similar to javascript, but within my php server.

     

    I do not have the time or resources to convert my entire project over to nodejs/javascript. It will take forever. I love php so much, that I do not want to make the switch. Everything else works fine and I feel like it's not worth it to re-write everything just because I cannot use a similar setInterval function inside php.

     

    Since the php socket server runs through the shell, I can use a setInterval type function using a loop:

     

    protected function on_server_tick($counter)
    {
        if($counter%5 == 0)
        {
            // 5 seconds interval
        }
    
        if($counter%10 == 0)
        {
            // 10 seconds interval
        }
    }
    
    $this->last_tick_time = microtime(true);
    $this->tick_interval = 1;
    $this->tick_counter = 0;
    
    while(true)
    {
        //loop code here...
    
        $t= microtime(true) - $this->last_tick_time;
    
        if($t>= $this->tick_interval)
        {
            $this->on_server_tick(++$this->tick_counter);
            $this->last_tick_time = microtime(true) - ($t- $this->tick_interval);
        }
    }
     This code does work as intended, but it seems a bit overboard for resources and I feel like that while loop will suck a lot resources.

     

    Is there anyway I can re-compile PHP from source and include a "while2" loop that only iterates every 500 miliseconds instead of instantly?

  4. You don't have to do anything yet server-side. Write your game in JavaScript and use events to have monsters spawn and auto-attack. Spawn monsters outside of the visible area, when the player moves close enough have the monster attack the player.

     

    Yeah but someone can just simply disable that monster from attacking via F12 (Dev tools on Chrome)? (Or by clientside JS) I need some type of server ticker on PHP's websocket, is it possible? Similiar to setTimeout or setInterval functions. I can achieve this effect by doing this:

     
    $echo_time = $users[$clientID]['last_session_request'];
    $interval = 3;
     
    while($echo_time + $interval >= time() ){
    echo 'Looping...';
      if ($echo_time + $interval <= time()){
        echo "$interval seconds have passed... \n ";
        $echo_time = time(); // set up timestamp for next interval
      }
      // other uninterrupted code goes here.
    }

     

    But, the 'echo Looping...' runs like mad and I think that sucks in a lot of CPU usage on my socket server, that isn't supposed to be used..  Hmm...
  5. I am creating a top-down rpg character movement w/ combat. You click on a part of the map (in this case, it's a image, and your character .png image transitions to that spot). It's with Javascript and PHP. I'm using a PHP socket server. I have it where players can only move up to 200 pixels per each click. Each click is essentially a packet being sent to my gameserver. Then the gameserver validates if it's a valid input (1235x1235) format, and spits it out to other players in that specific game. (So it's a consistent world/map). This works perfect.

     

     If I want to incorporate a monster that has AI and starts to attack the character... I can do that with javascript easily with setInterval or setTimeout functions (to make the mob move and then attack the character). But how do I do that serverside with PHP simultaneously? Because a user could just press F12 and edit the javascript to stop the mob from attacking. But, I need the mob to attack the player every xxx milliseconds serverside as well. How can this done with PHP? I feel like I need some type of heartbeat going on where it's a constant check between client and server every xxx seconds? I might be going off on the deep end and I'll stop talking I hope you get the jist of it. Any ideas are greatly appreciated.

     

    Another thought: Think of the popular aRPG games out there. Diablo 2, Diablo 3, Path of Exile, etc. Imagine using a hack that just stops mobs from moving towards your character and attacking it. Is that just an illusion? And the "auto attacking" is done serverside regardless if you've stopped that mob on the client, right? If so, how to accomplish this with a PHP socket server with some Javascript?  I'm not looking for code, just an application theory or design process would be awesome.

     

    P.S. It's either I go this route, or the game becomes a simple real time strategy Pokemon style combat. Which I really, really don't want...

     

    TLDR: How to have monsters auto attack your character (or in this case, update values in a temp variable, or a field name) server-side with some client-side JavaScript, and a php socket server. Securely on the server.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.