8 Replies - 2919 Views - Last Post: 16 November 2011 - 03:13 AM

#1 Cyclopses  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 38
  • Joined: 20-January 11

Where to put the server timers

Posted 15 November 2011 - 07:35 AM

Hello all,

I am still a novice in MVC ASP.Net development, but just to give you an idea of what I'm trying to build for learning purposes and more is a text-based browser game.
The problem I have is that I'm trying to make more sense out of the background architecture, and where should go what.

Quick example:
- Player A starts production of Barracks.
- Javascript shows Player A production is in order.
- C# server code handles the requests and creates a timer object
- When the timer hits 0 (on server? or console?) the function would be called to set Barracks boolean to true in the database.

This is a rough scetch and I would appreciate if people can tell me if what I think is right.
However, I also thought I had to write a console application too which, will accept a timer request and handle it in a console rather than the actual website, or am I really confused now?

Any help is much appreciated! I have been strugling with understanding websites under the hood.
John.

This post has been edited by Cyclopses: 15 November 2011 - 07:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Where to put the server timers

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Where to put the server timers

Posted 15 November 2011 - 08:01 AM

You could use a timer of some sort; but I wouldn't. Rather, each resource has a construction start and end time. If the time is past the end time, it's done.

When the browser asks the server about the resource, it just gives the current state based on current time.

So:
- Player A starts production of Barracks.
-- A record for the production of a barracks is created. Done.
- Javascript shows Player A production is in order.
-- A request on the status of barracks from the server. The status of objects is based on server time.
- C# server code handles the requests and creates a timer object
-- Server code handles requests, but there are no timers
- When the timer hits 0 (on server? or console?) the function would be called to set Barracks boolean to true in the database.
-- Leave the database alone. It already has a perfectly good barracks entry

What you might do is have a list of user notifications. If the user was never told that the barracks was finished, and it is, let them know about it and record that you did so you don't keep telling them the same thing.
Was This Post Helpful? 0
  • +
  • -

#3 Cyclopses  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 38
  • Joined: 20-January 11

Re: Where to put the server timers

Posted 15 November 2011 - 08:24 AM

I can see how your approach would work on offline gameplay, but if let's say.. you have to execute a command at end-time, to calculate a new rank within the multiplayer world, or save your Barrack's level to the DB.
(The boolean was a bad example of me sorry, Rank would fit better, wouldn't this be stored?)

And if I were to apply your method, where would all those requests be saved?
Could they actually be saved on 1 server even if there were hundred to thousands of players creating requests?
Was This Post Helpful? 0
  • +
  • -

#4 Cyclopses  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 38
  • Joined: 20-January 11

Re: Where to put the server timers

Posted 15 November 2011 - 09:00 AM

I meant Level would fit better than Boolean, not Rank. Confusismsmn.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Where to put the server timers

Posted 15 November 2011 - 09:04 AM

View PostCyclopses, on 15 November 2011 - 11:24 AM, said:

but if let's say.. you have to execute a command at end-time, to calculate a new rank within the multiplayer world

You don't have to, you can determine that from the state.

View PostCyclopses, on 15 November 2011 - 11:24 AM, said:

, or save your Barrack's level to the DB.

Also known based on stored information.

View PostCyclopses, on 15 November 2011 - 11:24 AM, said:

And if I were to apply your method, where would all those requests be saved?

You need a database. The more people, the more you need it. A single database can handle thousands of simultaneous requests. A good architecture can scale it infinitely, limited only by your resources.

As to the freshness of data, keep in mind that you're dealing with a "tree falls in woods" scenario. Until some asks for a particular piece of information, it doesn't matter what state it's in.

You can use the quantity of requests as you timer, if you like. Upon each request, you might preform update tasks that relate to that request. Again, if no one is asking, it doesn't matter. When you do regular backups, you can run all updates together.
Was This Post Helpful? 1
  • +
  • -

#6 Cyclopses  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 38
  • Joined: 20-January 11

Re: Where to put the server timers

Posted 15 November 2011 - 10:58 AM

View Postbaavgai, on 15 November 2011 - 05:04 PM, said:

Also known based on stored information.


So all procedures will be stored, and you could track a Barrack level by say, (stored in a DB)
Pseudo
SQL
WHERE ProcedureDone = True AND Type = Baracks (And give back highest Level result)


And use that to show their Barack level, without storing it individually?
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Where to put the server timers

Posted 15 November 2011 - 11:31 AM

Perhaps:
insert into UserResource (UserId, ResourceId, BuildStart, Qty)
	values (42, 12, GetDate(), 1) -- always add with Qty of 1

-- view VwUserResource
select a.UserId, a.ResourceId, 
		sum(case when a.BuildStart + b.BuildTime <= GetDate() when a.Qty else 0 end) as Completed,
		sum(case when a.BuildStart + b.BuildTime <= GetDate() when 0 else a.Qty end) as Building
	from UserResource a
		inner join Resource b
			on a.ResourceId=b.ResourceId
	group by a.UserId, a.ResourceId

-- for Baracks
select a.Completed, a.Building
	from VwUserResource a
		inner join Resource b
			on a.ResourceId=b.ResourceId
				and b.Name='Baracks'
	where a.UserId=42



Batch processing could easily roll this up later. Adding the Qty of completed resources together.

The trick is that any moment you look at a view of the data, it should be valid. Using a timed process, you really can't do that. Designing the data points with an awareness of time, you can.
Was This Post Helpful? 1
  • +
  • -

#8 Cyclopses  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 38
  • Joined: 20-January 11

Re: Where to put the server timers

Posted 15 November 2011 - 02:17 PM

I think I get it now :D
Thanks for taking the time to explain, I'll surely be trying to implement this exact system or what you wanna call it ghehe.

Atleast I don't have to worry about creating a background console that keeps track :o Saves a ton of work!
Thanks again.
Was This Post Helpful? 0
  • +
  • -

#9 Cyclopses  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 38
  • Joined: 20-January 11

Re: Where to put the server timers

Posted 16 November 2011 - 03:13 AM

And I presume you wouldnt save a Player's said Iron ores count on the database either and apply the auto-increment every second?
Instead the same tactic must be applied and use said Tree falls in woods scenario where you would calculate a player's current ores by doing some math on date-end till now times the number of increment per sec. minus the amount of used and sold ores?
In theory I see this work, but Im afraid of the stress it would shred on the server and retrieving the information highly indirrect.

Do you (or anyone else) use the same approach or would this be any different.
P.s. Should I move this question to the games forum section or would it do fine here?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1