Saturday, October 25, 2014

Three Tier Architecture Sample in C#


The short version:

Uploaded a simple C# sample solution (Visual Studio 2012), featuring:


  • 3 tier architecture (web application, web server, database)
  • AJAX calls from a web application
  • XMLSerialization
  • Accessing a Database with SqlDataReader
  • layer architecture of the server tier (business connector, data connector and core)


The long version:

Lately I made some changes to the client server architecture. The original design consisted of a big client (javascript running in the browser), a very small web server and a lot of SQL.
I put most of the business logic into sql because I thought that transaction handling, concurrency and scaling would be easy to implement with sql. The use of spatial data types seemed also to simple some things which would otherwise be programmed by hand.
Additionally, I wanted to find out if it is possible to write all of the server sided code in sql.
And finally, I just like sql programming and wanted to write more code in it.

Until recently, the program flow was as follows: 
The web server remembers user id per user in a session cookie. The server receivers ajax requests, calls a sql stored procedures with the client parameters and the user id and returns the result of the stored procedure.

Mostly, this implementation worked just fine, but as reported in the last blog, I ran upon a performance problem regarding spatial indices. Subsequently, I added a programmed game server to the game architecture.
The web server now receives user requests, but does not call the stored procedure but a method of the game server.

The game server consists of the following namespaces:
BC (business connector)
Core
DataConnectors

The following requirements should be fulfilled by the server tier :
- The business connector should be the only one accessible from the outside. It reveals a bunch of methods.
- Classes in the core contain the game data and do all the processing.
- The data connector is called when the core needs data or to writes updates 
- The game server has to be designed in a way that it may grow to many thousands lines of code in 100+ classes, without getting a big unstructured mess (source code tends to rot if you are not careful).

The last point is the one for which I didn't find good advice or examples. That is the reason why I put a tidied up version of the game server on github. Keep in mind that I have until now programmed only few features in the game server, so I can't guarantee that its design is as good as hoped.

This is a typical workflow for a web server request: 

  1. calling the bc method in BusinessConectort.cs file 
  2. creating an instance of the corresponding bc class which3
  3. creates an instance of a core class doing the work
  4. the core class 
    1. fetches data into sql table corresponding objects
    2. does the calculations
    3. writes back to sql
  5. the bc class then generates the output
I do not momentarily need a sophisticated way of communication between the web server and the game server. Thus the current solution is a dll which lets the web server instantiate the business connector. A better solution in the long term would be a stand alone server. That server could then even communicate with multiple web servers.

That only the business connector is accessible from the outside is not yet fully implemented. Some classes have "internal" as access modifier, preventing outside access.Others had to be set as public, since I wanted to use them with the XMLSerializer.

The web application project is kept very simple. The ajax return is imply inserted into the dom. Either adapt the result of the business connector, or add some javascript logic to parse the result, if you are implementing a web application yourself. Or do both, returning a json string from the server and creating a javascript object from it would be a good beginning.

I would be happy to hear suggestions or get even improvements to the sample.

If you like it, and in case that you also like multiplayer 4X games, check out Empires in Space, the reason for writing the game server...










Monday, October 20, 2014

Performance Problem (SQL) and its (ongoing) Solution

During the end phase of the second alpha test, one of the players informed me that logging in to the game took about 10 seconds for him.

The reason for this lied in a sql query which performed bad.
If you are interested in this technical details, you can read the corresponding question on stackoverflow. You may even reproduce the problem with the source code posted there. 

There seems to be no way to get the query running fast enough for the game, so I decided to calculate the required data in the game server tier instead of the sql server tier.
That is what I am programming at the moment.

Next post will be about the architecture of the server tier. I will probably even put a sample project (C#) on the github account of Empires in Space.

Saturday, October 18, 2014

Empires in Space is now in Open Alpha Phase

A New Massive Multiplayer Online 4X Computer Game

Empires in Space is like all classic 4X strategy games, but in addition to the standard features of the genre it is designed be a massive multiplayer online game.

  • all the standard features of normally installed 4X games now online
  • play with friend without needing them to be online
  • compete with human beings: all other realms are player controlled (and not AI)
  • there is no pay to win and never will
  • strategic cooperation with friends – defend (or expand) the borders of your realms together
  • ally yourself with your friends instead of fighting them – there will be enough opponents against who it would be better to stand as a group


Here are two playable online demos (no login or registration required):
Demo Starting Phase
Demo Advanced Phase

This is what the Alpha Version looks like at the moment. The screenshot shows a solar system, where your colonizer space ship arrives at the beginning of the game.



This screenshot shows the star map later in the game, when you have discovered a large area and met multiple other nations:



The game does run continuously on a server, without the need for a player to log in.
The idea is that multiple turns are played per day but all benefits of the turns stack up, so that you don't have to get online after each turn. Depending on the game settings, there are up to six turns a day. Only the movement points will be capped after having added up for five turns.
The most important tasks may be done within five minutes per day, but of course a player may spend more time on scouting, diplomacy and trade.

These features are implemented up to now:
  • ship movement, combat and colonization of new worlds
  • colony management, small chains of productions
  • research
  • ship designer to create unit templates, and ship building
  • alliances (clans)
  • diplomacy
  • local communication networks (generic ones and new ones from players themselves)
  • trade between players
The game ends shortly after the research tree is fully explored and the players should be able to influence the ending scenario.
Since it is a massively multiplayer game, there are some problems with ending scenarios that affect all players. The typical scenario would be “Conquest”, which would mean that a lot of players actually lose the game.
The ending scenarios are not implemented yet. The first one to come will be “Research” which leads to a global “Ascension”: After having researched the technology, players may start an “Ascension Construct“ or help to build another players construct (and of course try to destroy rival constructs). The first such construct to be finished will end the game, and players will get points according to their contribution to the construct, their spatial distance to the construct, their population and possibly other factors.
As long as there is no ending scenario, players can simply enjoy themselves by exploring, colonizing, exterminating, bartering and negotiating for fun.

You can find the project's homepage here, feel free to join the current game. I will enable turn progress as soon as ten players are part of the game.
Game content and game mechanics may be discussed in the forum, e.g. how to prevent a blitzkrieg.


I am looking forward to hearing from you, learn about your ideas, your wishes about content or your recommendations on changes.