is it possible doing this without cron?

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

is it possible doing this without cron?

Post by Gunner »

hey guys, sorry to interrupt, I need to ask some question.
Is it possible for us to use the resources system on our game without cron? (Production per hour) I found it possible coz on travian private server , the resources sytem works perfectly when hosted on localhost.. which as far as I concerned, you can'tuse cronjobs on localhost since its a windows running.

Second case.
What do you call it the timer for troops arrival?
I found the way to do it with javascript, but when the page is reloaded the timer got reset. What I want is a timer system that runs on the server that even the client logs its acc out, the timer is still going on.

Just answer with a yes/no answer and some additional info :)

Thanks!
Skype: mrvictordiaz
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: is it possible doing this without cron?

Post by Jackolantern »

Without server push (the ability for the server to reach out to a client without the client making a request) crons are not needed because they can be simulated. You could just add a script on every page load to check a field in the database to see when the last resource update was. If it has been over x time, perform it now. This system will work provided the site does not get huge, since PHP module is multi-threaded which means two page loads could check the x time at the same time, and both perform the update. But that is unlikely with less than 100 or 200 players since the execution in the Zend Engine on today's hardware would be in the range of nanoseconds, meaning the site would have to be hit many times a second for this event to likely occur.

Of course, the Travian private server software may very well be written on a platform that allows much easier real-time logic than PHP. Perhaps Java, .NET, Python or C++ are all common for server emulators. However, that road would be much more difficult. Node.js strikes a good balance between the PHP (easy) and Java (complex but flexible and allows real-time). In node, it would be as easy as setting up a setInterval() to go off to run your resource update code.
The indelible lord of tl;dr
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: is it possible doing this without cron?

Post by Callan S. »

Jackolantern wrote:This system will work provided the site does not get huge, since PHP module is multi-threaded which means two page loads could check the x time at the same time, and both perform the update.
Really? I would have thought not - that would so easily lead to discrepancies. Someone goes to charge someones account at the same time someone buys something. Bang, both transactions go through.
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

Re: is it possible doing this without cron?

Post by Gunner »

Thanks Jack, at least that fixed me up.
So what is your suggestion about my first step I should be taking to straightly apply these systems in my game? Should I jump in a new language and master it right away? Or just use some slight tips around PHP and Javascript I could found in the internet? Or maybe just simply mastertout JS to get the concept?
Skype: mrvictordiaz
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: is it possible doing this without cron?

Post by Jackolantern »

Callan S. wrote:
Jackolantern wrote:This system will work provided the site does not get huge, since PHP module is multi-threaded which means two page loads could check the x time at the same time, and both perform the update.
Really? I would have thought not - that would so easily lead to discrepancies. Someone goes to charge someones account at the same time someone buys something. Bang, both transactions go through.
You would not want to use it for something as critical as item or gold transactions. But for, say, regenerating player's health, I think it would be fine. In that incredible off chance that there is a race condition and 2 players both trigger the update, everyone will just get 2 ticks of health back instead of 1. Not exactly world-ending.

But if it was me, I would just use node. But of course that isn't for everyone ;)
The indelible lord of tl;dr
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: is it possible doing this without cron?

Post by Callan S. »

I just don't see why it would be allowed to be non sequential - it's just another page refresh. It could make ANY transaction screw with any transaction, just because you have a bit of traffic on the site. I'm skeptical.
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: is it possible doing this without cron?

Post by MikuzA »

Whoa!

This can be done by just doing PHP and MySQL eventhou PHP operates in multiple threads.
You just have to control the flow so that discrepancies/duplicate touches does NOT happen.

My first idea on how to control is just to simulate everything without saving, dependant on the timestamps stores in the database from that users last action.
So it actually calculates 'everything' an user sees and for some parts you can have a database storing "datacaches" on the data just to speed up things.

So,
Player 1 has created it's town on 'UNIXTIMESTAMP' and this is timestamp is also stored in the database as a 'ultimate tick start' time for this player.
From this point forward, this player is receiving every 10 minutes x-amount of Virtual Resources.

Everytime this player uses some of the resources, a timestamp is added to database to alter the flow.
When player goes to sleep no activity is happening.

But another player from another part of the world, sends a SPY visiting towards this sleeping players camp with a task to see how much resources this player has.
The result of this,
(NOW()-Player_Creation_start_UNIXTIMESTAMP) = 52002 seconds
Divide 52002 with 10 and round it = 5200 * x-amount of resources. Let's define the x as 2. So 10400 Resources would this user have as a base.
Now we check all this users actions.
User spents 500*4 on farms, 2000 on a barracks, 5000 on training units.
User got robbed 500 resources by another player.
User obtained 1000 resources from a random chest surprise

Summing this up,
10400 - ((500*4)+2000+5000+500) + 1000 = 1900.

the SPY sent by another player gets this in his report, this user has 1900 resources!

You see what I mean?

And when this happens, the result can be stored into a database as cache usage.
If someone sends a SPY towards this player again, cache is checked and if there is an entry, the calculation is done from that point forward regarding resources.

********************************************************

Another thing is playing with database UNIQUE ID's, so that when an 'transaction' starts, ID's are defined into an table where there cannot be duplicates.
These ID's reserve and action what cannot be executed, sort of an invisible queue.

And by playing with this, same crucial server operations can be split up into several instances, some may require correct order to maintain the flow.

One nice thing is using the simple function, if using mysql-databases, mysqli_insert_id or mysqli->insert_id().
This gives you in return the ID that was last inserted to a database by the connection session used by the code itself.

So how you would use this is that,
1. You do an select of an MAX(ID) table of where the update-actions are reserved.
2. Store the ID in variable.
3. Do an insert saying 'This client will perform this data update'.
4. Get the last insert ID.
5. Match that the variable storing MAX(ID) + 1 is the same as the newly created ID.
6. If it is a match, perform action.
7. If it is not a match, do not perform anything and perhaps refresh page.

This way, you could have several different tasks updating with only one person online.
And of course if there are 10000 users online.

Then your task would be to split up the data refreshes... But seriously if you would have over 50 active people, you might reconsider moving over to a service that gives you crontab possibilty!


Hope this helps!
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: is it possible doing this without cron?

Post by Jackolantern »

Nice solutions!

Of course, I would only have this setup while in development on Windows anyway. You could setup proper crons when you move to a Linux host. Or again...just use node :twisted:
The indelible lord of tl;dr
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: is it possible doing this without cron?

Post by MikuzA »

Or on windows, use the Windows Scheduler and just execute >php.exe 'the_file.php'
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: is it possible doing this without cron?

Post by Jackolantern »

Oh yeah, I forgot about that! It has been a long time since I worked with PHP. I think I am forgetting a lot of it haha.
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”