Class NonobaGame<Player>
Namespace: Nonoba.GameLibrary
Language: C# / (.NET)
The NonobaGame<Player> is the main serverside class of a Multiplayer Game. It contains methods that are called when messages are received, when players joins or leaves the game, and methods to interact with the players that are in the game.
It's an abstract class, so you must create your game by inheriting from it.
NonobaGame<Player> is also 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.
Here is a simple bare-bones game and player implementation that simply broadcasts all received messages to all connected users. A bounce server if you will.
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) {
//Send message to all connected clients.
Broadcast(m);
}
// When a player enters this game instance
public override void UserJoined(MyPlayer player) {
}
// When a player leaves this game instance
public override void UserLeft(MyPlayer player) {
}
}
}
Public Abstract Methods
Methods that are required to be implemented in your game
abstract void GameStarted()
Called once when a game is created. Useful for setting up initial state.
abstract void UserJoined(Player player)
Called whenever a player joins the game instance.
abstract void UserLeft(Player player)
Called whenever a player leaves the game instance.
abstract void GotMessage(Player player, Message m)
Called when a player sends a message to the server.
Public Virtual Methods
Methods that you can override if you want.
virtual void GameClosed()
Called once when a game is closed after all players have left
virtual Image GenerateDebugImage()
Called by the development server when RefreshDebugView() is called.
public virtual bool HandleChatMessage(U user, string message)
Allows you to filter and handle chat messages yourself.
HandleChatMessage method
public virtual bool HandleChatMessage(U user, string message)
Allows you to have custom handling and filtering of chat messages. This is useful for building your own command system, or receiving guesses in text-based games without broadcasting them to the others in the same game.
Example
public override bool HandleChatMessage(Player user, string message) {
string[] parts = message.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
if( parts[0].ToLower()=="/guess"){
Console.WriteLine("The users guess was: " + parts[1]);
return true; // indicate that we've handled the chat message;
}
return false; // false means the default handler gets to handle the message
}
Public Properties
Player[] Users
Array of players currently connected to the game instance
int UserCount
Amount of users currently connected to the game instance
int MaxUsers
The maximum amount of users you want to have in a single instance.
ParsedSetup Setup
Access to the setup of the current game, as given by the player when creating the game in the lobby.
Public Methods
void Broadcast(params Message[] messages)
Send one or more messages to all players connected to this game instance.
void Broadcast(string type, params object[] parameters)
Send one message to all players connected to this game instance.
void DisconnectUser(Player player, string message)
Disconnect the user with the given message.
Timer AddTimer(TimerCallback callback, int interval)
Sets up a timer to call the callback at regular intervals of interval as milliseconds, i.e, 1000 = 1sec.
void ScheduleCallback(TimerCallback callback, int dueTime)
Schedules a one-time callback for dueTime in milliseconds in the future.
void RefreshDebugView()
Tells the development server to update its debug view with the image returned from GenerateDebugImage().
void LogError(string message)
Logs an error message.
void LogError(Exception e, string message)
Logs an error message with exception.
void SetState(NonobaGameState newState)
Changes the state of the game. Useful for controlling when players can and cannot join the game.
Broadcast method
void Broadcast(params Message[] messages)
Sends one or more messages to all users connected to the game instance. The params keyword indicates that you can send any number of messages in one call.
Example// send one message to everybody Broadcast(new Message("hi",1,2,3)) // send two messages to everybody Broadcast(new Message("hello"), new Message("world"))
Broadcast method
void Broadcast(string type, params object[] parameters)
Sends one message to all users connected to the game instance with an arbitrary number of arguments.
Example
// send one message to everybody
Broadcast("hi",1,"strings are okay also",true)
DisconnectUser method
void DisconnectUser(Player player, string reason)
Disconnects a user from the current game instance with the given message.
Example
// this would be the GotMessage() method of a very boring "quiet game"
public override void GotMessage(Player player, Message m) {
DisconnectUser(player, "couldn't stay quiet")
}
AddTimer method
Timer AddTimer(TimerCallback callback, int interval)
Sets up a timer to call the callback at regular intervals of interval as milliseconds.
Example// setting up some timers in the GameStarted method public override void GameStarted() { // using inline delegate AddTimer(delegate{ // called once every 0.5 second },500); // using a method defined on the class (tick) AddTimer(new TimerCallback(tick), 1000); } // called once every second private void tick() { //... }
ScheduleCallback method
Timer ScheduleCallback(TimerCallback callback, int dueTime)
Schedules a one-time callback for dueTime in milliseconds in the future
Example
public override void GameStarted() {
// using inline delegate
ScheduleCallback(delegate{
// called once, 0.5 seconds after the game has started
},500);
// using a method defined on the class (sayhi)
ScheduleCallback(new TimerCallback(sayhi), 1000);
}
private void sayhi() {
//...
}
RefreshDebugView method
void RefreshDebugView()
Tells the development server to update its debug view with the image returned from GenerateDebugImage().
Example
public override void GameStarted() {
// Setup a timer to update the game state 10 times a second
AddTimer(delegate{
// ... code to update game state here ...
RefreshDebugView()
},100);
}
public override Image GenerateDebugImage(){
// example code creating a 100x100 image and drawing the string "hello world" on it.
// normally, the image would be a graphical representation of the state of the game
// useful for debugging by the developer
Bitmap image = new Bitmap(100, 100);
using (Graphics g = Graphics.FromImage(image)) {
g.FillRectangle(Brushes.DarkGray, 0, 0, 100, 100);
g.DrawString("Hello World",new Font("verdana",10F), Brushes.Black, 0,0);
}
return image;
}
LogError method
void LogError(string message)
Logs an error message. In the development server, these messages go to the output window of the game instance.
Example
LogError("I couldn't do it")
LogError method
void LogError(Exception e, string message)
Logs an error message along with corresponding exception. In the development server, these messages go to the output window of the game instance.
Example
try{
//do something that might fail
}catch(Exception e){
LogError(e, "I couldn't do it")
}
SetState method
void SetState(NonobaGameState newState)
Changes the NonobaGameState of the current game, which is used to control whether or not the game is marked as open, closed or waiting for other players.
The SetState() method is useful for controlling when users are allowed to enter into a game. For instance, a game might have a build-up phase where players are allowed to join, and then have a long playing phase where only the players that where there from the beginning are allowed to stay, and no new users can join.
Important: If you wish to control the NonobaGameState yourself, you should set the MaxUsers property to a suitable high value, since it works by setting the state to ClosedGameInProgress when MaxUsers is reached, and thus might conflict with your logic
Example// This will make a multiplayer game where only one // player can be in any instance at a time. public override void UserJoined(Player player) { SetState(NonobaGameState.ClosedGameInProgress) } public override void UserLeft(Player player) { SetState(NonobaGameState.WaitingForPlayers) }
Enum NonobaGameState
This enum defines the different states a game can be in.
public enum NonobaGameState {
// This game is waiting for players
WaitingForPlayers = 0,
// The game is running and players are playing,
// but more people could join for more fun
OpenGameInProgress = 1,
// The game is running, but nobody can join.
// This could be when the game is in a state
// that new plays shouldn't join, like when the
// maximum amount of players are in a game.
ClosedGameInProgress = 2
}