Sign in to follow this  
mercutio604

how to do a building timer?

Recommended Posts

So I was in the middle of building a text based browser strategy game but fear that there is no one interested in those anymore...So I am looking into making something like game of war without pay to win ideally but I am at a loss how the building timer works.

Basically I need a timer that runs even when you are offline and notifies the client when its done if they're online and shows it ticking down on the client. I assume I need to save it to a database at some point the completed building as well. I feel like I am missing something simple because frankly I couldn't find any questions regarding this topic.

Also do I need an instance of unity running on the server for this? I am not super new to programming but I am new to online multiplayer unfortunately.

Can someone enligthen me please? Thanks in advance.

Also are these type of games easy to make?

Share this post


Link to post
Share on other sites

Timers are trivial, you compute and store the end-time, and upon retrieval from the DB, you compare that time with the current time.

The big nightmare with timers in network play is keeping time in sync between different computers. Time on your server runs at the different speed than time on every client, there is also an offset between them (if we could compare local time on our computers, you could have several minutes difference), and there is network lag. Last but not least, there is non-zero query time. When a client asks for a result, the server needs time to get it from the DB, and/or compute it. A simple request is thus 2x network delay + 1x query delay of unknown quantities.

When a timer runs out, the client and the server should agree on when that happens. I play a commercial game where they make a mess of it, timers of 24 hours shift 10-15 seconds a day, even though I restart the timer within a second.

 

I don't know unity, but in theory, there is no need to run the same program at the server. You talk between client and server using a network that only transfers messages (as in, sequences of bytes), and nothing else. As long as the server runs a program that understands those messages, and returns useful answering messages, all should be fine. You can write such a program in pretty much anything.

Obviously, without unity, you won't get its functionality, so you have to implement that in another way. Not sure how much that is, for a network server.

 

Given the kind of questions you ask, I think you'll have a hard time grasping how to do these things. Network code is quite non-trivial, and distributed computing is complicated as well.

On the other hand, nothing works better in learning than trying it, and see what happens!

Share this post


Link to post
Share on other sites

I wouldn't necessarily do a _timer_ at all.

A timer implies some kind of eventual state change. It implies an eventual computation and an eventual event. If you start building, you must send an event to all players, and then upon completion, send a second. And know when the completion happens, efficiently. That's a lot of stuff.

Instead, just store the building as state with its initial build time and its expected completion time. Send both to the clients in a single event. Now all clients know when the building will be completed and can update themselves accordingly with no further involvement from the server.

For big Web-scale MMO things, you want to avoid every possible computation you can. Work in the minimal state changes possible. Create the building with all state it needs to know its current and eventual building state. If something changes the expected state (additional resources put into construction, for example) then update the state and expected completion and send that event to all clients (they'll need an event anyway to make the proper UI changes and the like).

Minimal effort/data/events.

Share this post


Link to post
Share on other sites
Thanks for the replies. Ok let me get this straight, so when a user orders a building, I insert into the database the userid, building, the time on the server and how long it will take. The server then spits back to me the difference between the two and the client is left to calculate from its end only. I can just leave the "complete" state marking as a comparison in the database if server time > timestamp + construction time for whoever needs to know instead of the client sending a "im done" message... correct?

Share this post


Link to post
Share on other sites

The server then spits back to me the difference between the two and the client is left to calculate from its end only.
That assumes the time to send the command "build" from client to server, server checking if allowed etc, server inserting entry in db, server sending client return message, and client computing end-time is all timeless? What if server needs 5 seconds to do its work (DB very busy, for example), or network is slow?

 

instead of the client sending a "im done" message...
I'd like this, I hack the client or the network connection, and send "done" as soon as server responds.

Share this post


Link to post
Share on other sites
That assumes the time to send the command "build" from client to server, server checking if allowed etc, server inserting entry in db, server sending client return message, and client computing end-time is all timeless? What if server needs 5 seconds to do its work (DB very busy, for example), or network is slow?
 

Well I guess I can have the server send back with the elapsed time as part of the formula, a few seconds isnt a deal breaker though...As long as the DB has the correct state and the client is close enough. Unless maybe the client finishes early in which case users will be wondering why something isnt working if it relies on said building. I guess I can also send a "r u done" check after the client is done so they can agree on it. Either way if the client finishes early, the user is left with a "uh whats happening" scenario :(

 

I'd like this, I hack the client or the network connection, and send "done" as soon as server responds.

thats why I said instead of...no? or did I misunderstand your point?

 

Thanks for your patience with me :D

Also I just reread your post I just realized I don\t really need the start time, just the end time.

Share this post


Link to post
Share on other sites

thats why I said instead of...no? or did I misunderstand your point?
Since you mentioned it as alternative solution, I thought I'd mention a possible security problem with this approach :)

 

Apparently, you know already, but I wasn't sure that was the case.

Share this post


Link to post
Share on other sites

I read your post last night.  Not sure if I can be of any help, but I'm making a game that requires a lot of the same functionality you need.  I'm using Javascript though.  Are you pretty comfortable with Unity which is why you're wanting to use it?  

Anyways, if you're interested look up what THREE.js can do along with node.js and redis.  I'm currently using all three for a browser game I'm working on, great thing is I only need to use Javascript for all of it.

Share this post


Link to post
Share on other sites
Well my original plan as was mentioned before was just a text based browser strategy game - I was only planning to use mysql php html css and javascript. I didn't actually give much thought on what else I should use should I go with the game of war like route yet I am still looking into it if I can pull it off or not. Unity was the first of the list because yes I have used it before though not much online multiplayer stuff. I figure it would get me finished the fastest regardless of what new things I need to learn for it. But I am pretty open to other things - gotta learn something somewhere no matter what anyway. Thanks for letting me know of these~

Share this post


Link to post
Share on other sites

Hey guys, I just realized a solution to a game design issue that made me drop this project but found a solution that might make me pick it up again but I realized the timer solution doesn't work in my head.

I mean yes I can save the due time of things and check by query if something is done but I realized I needed to check for it?

The issue : Not exactly a building timer at the moment but theoretically the same. So there a game called Utopia that I am loosely basing the genre from and it has armies go on attack then have a "due back" time. This is how I had it planned out, I have a database table of cities, and a table of "out armies" which has the army, time due, and city owner.

Now the issue I have is not letting every client know the army is back so much as letting the server know its back when its due.

The way I see it I have 2 options,

1. run a script every min to check for every out army - sounds computation heavy and inefficient

OR

2. Query the "out" armies every time someone checks the "at home" armies or similar action - which sounds a weird solution to me. Basically the army doesn't "officially get home" until someone checks for it from a client. Not to mention I have to repeatedly query the "out" armies. Although i think this is a better solution than 1.

But then what if I want the attack part to happen on a timer instead of instant? By that I mean theres a march time towards the actual attack then a march to come back. Then I don't even know what will trigger the attack part.

 

Hope I made sense ... T.T

 

TLDR

 

How does the database/server update itself without the client's cue it its time to do so?

Share this post


Link to post
Share on other sites

Let me make the question simpler.

Since I don't seem adequately familiar with developing online multiplayer stuff does anyone know a beginners tutorial for networking? Possibly for smartfoxserver(?) + unity + mysql(?).

I tried checking the networking forums but the sticky just has a broken link...

Share this post


Link to post
Share on other sites

What many people do is a variation on 1. They check every X time units (could even be once a second), but they don't really check. To understand, let's say we have 2 armies, one is due in 4 units of time, and one in 7 units of time.

So we check now, and notice that one army is due in 4 units of time, and one in 7 units of time. Nothing to do, let's wait one time unit.

We check again after 1 time unit, and notice that one army is due in 3 units of time, and one in 6 units of time. Nothing to do, let's wait one time unit.

We check again after 1 time unit, and notice that one army is due in 2 units of time, and one in 5 units of time. Nothing to do.

 

What you see that after each time we wait, all armies arrive 1 time unit sooner. This is of course obvious, while we waited, 1 time unit passed! Since for each army, the amount of passed time is the same, it means that the second army (with 5 units to go) will not arrive before the first army (with 2 remaining time units). In other words, we don't need to check all armies, just the one with the smallest remaining time is sufficient.

A second thing to note is that for the first army, 2 units of time remain. We don't need to check it each time, we can also find that number once, and then remember it. Instead of checking the army, we just decrement the remembered number. If it's not 0, the army hasn't arrived, and we wait one more time unit.

Alternatively, instead of repeatedly waiting a single time unit, we can wait also wait "number" time units, doing nothing (since we know nothing is going to happen until the army arrives). After that amount of time, one army arrived (or more, if several armies arrive at the same time), and there is interesting stuff to do. In our example, we'd wait 2 time units, and then we know an army has arrived. We scan the list armies to find them again, and deal with it. (If you keep them sorted on arrival time, it's even simpler.)

After handling the arrived armies, you again scan all armies that still have not arrived, and find the next soonest arriving army, you wait for it to arrive, and  ..., etc etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this