Nonoba Multiplayer API: Serverside
All multiplayer games made with the Nonoba Multiplayer API has code running either on the local development server, or on Nonoba's cluster of game servers, managing the shared state of the game and sending and receiving messages to and from connected players.
For any given multiplayer game, all its serverside code is contained inside a single .NET 2.0 .dll file. The dll must reference Nonoba.GameLibrary.dll and must contain a class inheriting from Nonoba.GameLibrary.NonobaGame<Player>. These .dll files can be created with any .NET 2.0 language, but we recommend that you use C# 2.0, since it's quite close to ActionScript, and all our examples are written in it. :-)
When a users asks for a game instance to be created via a game's lobby, the development server, or the Nonoba cluster of game servers, looks inside the dll file to find the first class inheriting from Nonoba.GameLibrary.NonobaGame<Player>, and then creates an instance of it charged with managing that game instance.
The class Nonoba.GameLibrary.NonobaGame<Player> is generic which means that it's parameterized and must be told what class is used to represent a player connected to a game. The player class must inherit from Nonoba.GameLibrary.NonobaGameUser.
For example, here is a simple, bare bones game that doesn't do anything:
using System;
using Nonoba.GameLibrary;
namespace MyGame {
// This class represents a user connected to our game
public class MyPlayer : NonobaGameUser {
}
public class Game : NonobaGame<MyPlayer> {
// This method is called when a game is created. Useful for setting up initial data.
public override void GameStarted() {
}
// This method is called whenever a player sends a message into the game.
public override void GotMessage(MyPlayer player, Message m) {
}
// When a player enters this game instance
public override void UserJoined(MyPlayer player) {
}
// When a player leaves the game instance
public override void UserLeft(MyPlayer player) {
}
}
}
If that game was contained in a file "MyGame.cs" located in a folder that also contained the "Nonoba.GameLibrary.dll" file, it could be compiled into a complete game dll with the C# compiler command (csc)
C:\>csc /out:MyGame.dll /t:library /reference:Nonoba.GameLibrary.dll MyGame.cs
We would however recommend using Visual Studio Express when creating games, since it provides a much easier development and debugging cycle.
Game and Player classes
The nice thing about having a specific class for both a game and a player, is that you can put properties on both and maintain your state that way. For example, if the objective of our game was simply to click a button most times, the serverside code might look like this.
using System;
using Nonoba.GameLibrary;
namespace ClickathonGame {
public class ClickingPlayer : NonobaGameUser {
public int ClickCounter = 0; // we'll add one every time
}
public class Clickathon : NonobaGame<ClickingPlayer> {
public int TotalClickCounter = 0;
public override void GotMessage(ClickingPlayer player, Message m) {
// player clicked!
if (m.Type == "Click") {
player.ClickCounter++; // update the player
TotalClickCounter++; // keep track of total clicks in this game instance.
}
// player wants highscore
if (m.Type == "GetScores") {
// loop over all connected players
for (int i = 0; i < Users.Length; i++) {
// send the score to the asking player
player.Send("score", Users[i].Username, Users[i].ClickCounter);
}
}
}
// We're not using these methods in this example
public override void GameStarted() {}
public override void UserJoined(ClikingPlayer player) {}
public override void UserLeft(ClikingPlayer player) {}
}
}