开发者

Multiplayer through database and updatepanels - How to handle everything?

开发者 https://www.devze.com 2023-02-08 19:21 出处:网络
Description of what I am trying to do I have a situation where I have created a game in ASP.NET. Right now I am working on adding multiplayer functionality so two (or more) players can fight at the s

Description of what I am trying to do

I have a situation where I have created a game in ASP.NET. Right now I am working on adding multiplayer functionality so two (or more) players can fight at the same time in "real time".

In short, I have:

  • A server (ASP.NET website)
  • One or several visitors which are on side A
  • One or several visitors which are on side B

The main functionality: One player uses an attack on an another player, this attack has a fire time (both players can see the time before fired) and then after some seconds, the attack is fired.

Right now I handle this in every player. Every player has a list of activities. Activities could be:

  • Firing an attack
  • A load of an attack is 0 and the attack is fired
  • Some stat modifiers: Drinking a potion, being hit, doing some magic which modifies stats

Now, the problem is I want the multiplayer functionality added, and I want this added through a database.

- Please, NO Comet suggestions -

My work so far...

I have a database with some tables:

  • One for each fight started
  • One with fight participants
  • One with all activities added

Every 750ms-1000ms I run the two methods:

  • GetNewActivities() which reads all new activities from the database (timestamped)
  • FireCurrentTraces() which looks at all current traces and react upon them

But, of course, t开发者_开发技巧his gives problems. For instance: Player A fires an attack on player B. While this is counting down the fire timer, both people has this "attack" in their pending attacks list. When timer is down, player A or B will fire it, but in this case, both will end up firing it.

This is just one of MANY problems which will lead to HORRIBLE design.

Which leads to my final question...

What would be a doable way to solve this issue? Please, no Comet or solutions alike. I need something which has great support in ASP.NET and should be simple to work with.

I guess some global list of activities could do wonders, but how in the world would one manage that? Also, this could give some race conditions with several users?

Thanks! :-)


This sounds like you're attempting an MMO using three technologies not well tailored for gaming:

  • TCP (rather than UDP)
  • Polling (rather than always connected)
  • Database rather than in memory for the state

That aside, I would ditch the database idea for storing the state as it will cause so much latency. What MMO servers do is dump their state to disk periodically instead of the whole time.

First of all you'll need some kind of token that all the players are associated with, a game id. Store this in the Application object as the key. You can then store a state object as the value which contains all data necessary. It won't be a huge amount of data as it'll mostly be integers.

The alternative of using databases tables introduces lots of issues with locking rows, and figuring out what to do in deadlock situations as you mentioned. With the in-memory option you can simply use first-in wins.

So as an example:

  1. I fire a spell
  2. My friend fires his spell 0.5 seconds later which sends the command the server
  3. He receives the damage from my spell back

Try doing all of that with the latency of a database server:

  • Opening a new connection to the database server
  • Querying
  • Returning back to IIS
  • Turning back into .NET objects
  • Returning to the client.

You remove the first 4 steps by keeping it in memory. You can then serialize your game state object periodically to the database if you're worried about people disconnecting and re-connecting. You could try an object database for that task, for example db4o.

HTH

0

精彩评论

暂无评论...
验证码 换一张
取 消