views:

320

answers:

1

Hey everyone,

Update: I'm sorry if maybe my question isn't clear enough. I've read about the command pattern, but unfortunately have not used it myself. I'm trying to figure out how I could use it (or some other pattern) to make game events abstract enough that the server can process them using a single Process() method. My main hang up here is making sure the game events receive enough information to actually DO what they need to do (e.g., log in a user and add them to the active user list, send map data, move a player, etc.). A relevant example would be very much appreciated.

I'm pretty new to game development but have decided to start working on a (relatively) simple, 2D MMORPG in my spare time. I would consider myself to be a very capable programmer and I have a good foundation of skills, but I'm still grappling with some of the design related to a client-server game. Specifically, I'm having a hard time thinking of an extensible way to process commands. Let me provide a functional example:

Log In Request

  1. Start the game
  2. Click "Continue"
  3. Type a user name and password
  4. Click "Log In"
  5. See the character wherever you were when you logged out

From a client-server architecture perspective, here's what I'm doing right now:

[Client]

  1. Send a SimpleTextNetworkMessage to the server - {LogInRequest, UN:[UserName]|PW:[Password]}
  2. Darken the UI and wait for a response (timeout: 10 seconds)
  3. Receive a SimpleTextNetworkMessage from the server - {LogInSuccessResponse, [Player ID]}
  4. Send a SimpleTextNetworkMessage to the server - {GetPlayerInfoRequest, [Player ID]}
  5. Receive a SimpleDataNetworkMessage from the server - {GetPlayerInfoResponse, [Player Info]}
  6. Send a SimpleTextNetworkMessage to the server - {GetMapInfoRequest, [Player ID]}
  7. Receive a SimpleDataNetworkMessage from the server - {GetMapInfoResponse, [MapData]}
  8. Draw the screen

My example identifies three key events that occur:

Process Log In

Validate the information the user provided, download the player information from the database (HP, MP, last location, etc.), and associate the player with a map and a connection.

Get Player Info

Send back information about the player's stats, equipment, experience, current map ID, and anything else that needs to be displayed on the UI.

Get Map Info

Send information to the player about all the tiles within a 50 tile radius...this should include tile information for a three-layer map and the locations and names of NPCs/monsters/players; when the player moves, more map information will be requested/updated.

You can see that each of these processes is different and requires different information. On the server-side, how can I do something like:

while (ServerIsRunning)
{
   foreach (Client c in clients)
   {
      eventQueue.AddList(c.ReceiveAll());
   }

   foreach(GameEvent event in eventQueue)
   {
      event.Process();
   }

   int[] keys = messageQueue.Keys;

   foreach (int key in keys)
   {
      Client c = clients.Get(key);

      foreach(NetworkMessage message in messageQueue[key])
      {
         c.Send(message);
      }
   }
}
+1  A: 

When I read what you're asking, you seem to be asking for "an extensible way of implementing command processing".

From the way you phrase it, your request obviously points to the Command Pattern.

I'm not a C# person, so sadly I can't do much of a job evaluating the plethora of suggestions Google provides. Here's one to get you started. http://www.c-sharpcorner.com/UploadFile/cupadhyay/CommandPatternsInCS11142005021734AM/CommandPatternsInCS.aspx

CPerkins
Thanks! I've read about this pattern already in relation to games, but I'm having a hard time figuring out how the commands will have access to the things they need.For example, how can a log in command attach the user token to an active user list once the command is successful and put a success message on the send queue? How can a character move request process the move on a map and then send an acknowledgement back to the client? Etc.Thanks again!
Ed Altorfer
Again, I'm not a C# person. I can't build your pony. I can only offer suggestions from a distance.If I were trying to figure this out, I'd start with this question: how would I handle it if I were doing hard-coded, compile-time server-side command processing? How would you get your user token, your user list, your send queue?
CPerkins