Timer Concept on Server Side - php

Here's the scenario:
I have a SmartFox server running on Amazon EC2 and using MySQL for the database. I'm using AS3 as the programming language.
I have a virtual world in which the user can click on some icon (like home design), and it shows the animation as the house is getting built. It takes eight hours to build it and it will show the percentage with a progress bar as it's getting build.
I could have used AS3 timers, but the problem is that the user might log out from the virtual world (or close the browser) and come back at a later (after maybe two to eight hours, for instance).
I haven't done anything like this before, so can anyone help me with the details, such as what general strategy needs to be used and how to implement, etc.
Please help.

You could do it like this - right after user clicks and starts the process notify the server about that and make server side script get current time and save it to the DB. If the user logs out right after that, just retrieve that saved timestamp each time your client app starts again and calculate how much time passed since the timestamp was taken (at server side), then pass calculated value to the client. This way you will always have the right time displayed and calculations will be client internal clock independent.

Related

Looking for MySQL performance advice for database-heavy app

I'm creating a messaging app using jQuery, PHP and MySQL. Every time a user enters a message, I store it in a MySQL table. On the receiving users end, I basically just added a Javascript timer to check the database every X number of seconds for new messages.
The system works well but is this going to be a performance problem? For example, let's say I have 1000 users and I'm hitting a MySQL table every 5 seconds for each user.
Can anyone suggest a better method?
With your actual architecture, your SGBD will get a heart attack :)
The solution reside on implementation of Web Socket
in back-end only 1 instance of PHP check if there is a new update on database, if there is, PHP can invoke a web service to your Web-Socket Server (like NodeJS), and Server send the message to the client

PHP - How would you make buildings or such things finish etc?

So as you know in some browser-games such as Travian, Tribalwars and etcetera, you can build up a building, it takes X amount of time and it finishes.
So I'm curious how that is done?
Is it a cron-job running every second or what? How are they then doing with troops, they can't have a cron-job running ever millisecond, that wouldn't be resource usage friendly, right?
So I'm really curious about this and I have no idea so I can't really say I have tried. I have however searched around but never found anything helpful.
Thanks.
The easiest way to implement something like this is simple timestamps. The request on the front end generates a timestamp based on the constraints given by the details of the request (what type of building you are building, what level you are, if you have bought the upgrade). Then a timestamp is inserted into the database for when the completion occurs. Then, if you want the browser to refresh when the job is up, you make a script on the js that makes a request for all timestamps in queue and reloads when they come up.
One way: You let the client handle the timer. So the timer will sit on the browser side counting down using javascript. When the time is up it will contact server to see if it's valid (never trust client side code). Server looks up the building and see if it would have been finished by then. Server side doesn't need to keep any timers it just answers requests. Timers are UI side.
Well, PHP is one of the worst things you could use to build a game... In games, everything is controlled by the main loop that controls the game. So basically, in a game, everything is running inside an infinite loop, albeit one that allows for user input without freezeing the computer, obviously. So that loop takes care of the timing as well, and the way to compute timing will depends on the language on which the game is developed. For web-based games, Java, JavaScript and Flash are usual choices.

Updating a PHP text-based game continuously

I'm building a multiplayer game in PHP. Each game is split into stages that last a set amount of time - currently 2 minutes. In each stage, players work together for a set amount of time until the game either advances to the next stage, or the stage (and hence the game) is lost, and a new game starts again.
I have a vision of playing the game on an HTML page, where users can see how much time is left in the current stage through a constantly updating countdown clock. When the countdown clock reaches zero, the game either advances or ends, and the result is returned through Ajax. What I'm having problems with is the (theoretical) thought of 100 players playing the game simultaneously.
How do I call the update script? If I call it by running PHP, which player's browser calls it? If a player's browser refreshes the content one second before the stage actually finishes, what happens?
Is PHP the right language for a game like this?
"Is PHP the right language for a game like this?"
Yes, if you do it right. Here's how you do it...
Using a method called Comet, you can have multiple clients pulling data from the server, but only when that data is updated. It works something like this:
Client A makes pull request to server.
Server waits, keeping connection to Client A open.
Client B makes pull request to server.
Server waits, keeping connection to Client B open.
Client A pushes new data to server.
Server pushes new data to Client A and Client B through still open connections.
Client A makes pull request...
etc...
In other words, it means that all clients receive up-to-date information immediately it is pushed to the server by any client. This means you don't have to implement a "refresh every x seconds" system, which is good for 2 reasons:
1) You don;t waste time and bandwidth make requests to the server when nothing has happened;
2) All the clients get data pushed to them at the same time, that's absolutely up-to-date and not x seconds old.
In practice, Comet is implemented via AJAX. Google for some examples of Comet- it's pretty simple, and extremely useful.
In client-server games, your server should be running a simulation in parallel to the client's simulation (the game).
Usually the server simulation is authoritative, so that when, say, a match is over it will inform all clients in the match that the simulation state changed to "match over" or whatever. The client will then lock the UI or present a "Game Over" message or announce winners-- whatever you want.
So for you, your server should run the count down timer, while the clients keeping polling (via AJAX or WebSockets) the server for state changes. When the server's timer hits zero, the next time clients poll the server, the server will say, "Hey, yo-- the game's over!".
Edit
I'd also like to say that PHP will probably work, but you have to understand that PHP was built for web services, not games. Simple games like tic tac toe or checkers or chess can become quite complex when you toss in multiplayer functionality. Simulations often require threading. PHP can do threading but it can get very ugly if you don't know what you're doing.
Well, if you are using the PHP session system, the script will recognize the Ajax request as being from the same user who (for example) logged in into your game.
One second before the stage finishes, well you could return in PHP the time left to the stage so the javascript counter stay synchronized? Both the server and the clients must be aware of the status of the game, else expect hackers somewhen.

PHP and Labview - grabing a value from labview and displaying the value on a webpage - two different computers on the same network

I am by no means considered a programmer. The only programming language I some what know is HTML - so be gentle :)
My boss came up to me the other day and asked me if I wanted to learn PHP, so i said sure. He threw me some text books and said here's what I want you to do:
There's a computer running labview that monitors various sensors. He wants me to take the data from the sensors and display it on a webserver on the same network. The format he wants it in: you punch in a command in a text entry box on the newly created website such as "chamber pressure" and then it takes the data that is displayed on the computer running labview and reads the value in another box that is under the first text box on the website.
Let me know where I should start. Any bit of help would be awesome.
I would start by using LabVIEW to store the data from the sensors to a database. A simple database with sensor name, value and possibly last reading time would do. MySQL databases are relatively easy to setup and work well with php I have found.
Next would be to use php on the webserver to fetch the data depending on the input and display it on the web page.
The first thing to try is the Remote Panel support built into LabView. This allows you to view or control a LabView application from a web browser on another machine. Remote Panels have many limitations (there is a restriction on max number of connected clients and the LabView runtime is required on the client) but it will only take 20 minutes to get them set up. See "Viewing an Application or Front Panel Remotely Using a Browser" in the LV help.
Remote panels are particularly good if you want to have e.g. a pull-down list of sensors and some plots that update in real time. Dynamically
updating plots are something that takes real work to implement in a web-standards way.
Use this prototype to figure out what your boss really wants to do. If his goal is to be able to peek at the chamber pressure down on the production line once in a while, you're done. If his goal is to share realtime updates on chamber pressure with 100,000 of his Facebook friends, half of whom want to view it with an iPhone or a Blackberry, then there's more work to do. Either of the approaches discussed above (using the LabView web server/web service platform, or writing everything to a database and pulling it out using php etc) will work; the latter is more scalable.
This may not be helpful, but what you've been asked to do is advanced. This should not be your first PHP project.
Start by creating a web page where you can type in a command code and a hard-coded value will be displayed based on what code was typed in.
If you know how to access a Webservice from within PHP, you could build your LabVIEW app as a webservice and access that from PHP. But without any proper training (LabVIEW and PHP) that will take you quite some time.
Ton

Turn based web game in PHP - communication issue

I am working on a turn based web game in PHP5.
This is pretty simple game, a kind of board game: two people join a "session" and they play until one of them wins.
My problem in nutshell:
User A and User B play a game.
User A finished his turn
Request will be sent to the server to perform necessary operations.
Now it is time for User B to move..
But how could I notify User B about this?
I mean, now the server has to communicate with the other user, the one that is inactive, not the one that initiated the request.
I know that this could be implemented using some kind of periodic AJAX call that checks whether the opponent finished his turn etc, but such a thing generates a huge number of requests.
Isn't there a better way to solve this?
I'm thinking of something like this:
User A's turn ends
Server saves his score
Server contacts User B
User B's turn gets started.
Is this possible using PHP and comet-style requests somehow? Or is there a better way to do this?
Any help would be appreciated!
Thanks in advance!
An in-memory database of currently running games/turns, and one check from user B every 1.5 secs or so, won't really generate a huge number of requests, or server load.
You can even have a polling scheme like 7s, 5s, 3s, 2s, 1s, 1s and so on, according to what fits your game.
You can even leave PHP out entirely, if you just touch a session file whenever a turn is done, and check the last-modified client side.
Php Sockets !
http://php.net/manual/en/book.sockets.php
server listen
client_a connect
client_b connect
client_a send start game to server
client_a send move to server
server send play client_b
[...]

Categories