Before you can send a messages from Client Game to Server Game function, you need to:
Communication between any Fireball components is done by sending and receiving messages type of 'BaseMessage' class or their child. Same messages using to communicate between Game Client and Game Server Function. Any message required to have the unique Name
to register the current action. Some names are taken by reserved by Fireball and can't be used (as "authenticate", "session" e.t.c.)
GameMessages.cs
script with registring custom messages names as const variables: public class GameMessages
{
public const string SPIN_REQUEST = "spin-request";
public const string SPIN_RESULT = "spin-result";
....
}
public class CustomSpinRequest : BaseMessage
{
public string GameType;
public long BetAmount;
public CustomSpinRequest()
{
Name = GameMessages.SPIN_REQUEST;
}
}
public class CustomSpinResult : BaseMessage
{
public string GameType;
public long WinAmount;
public CustomSpinResult(string gameType, long winAmount, BaseMessage baseMessage)
{
CopyBaseParams(baseMessage);
Name = GameMessages.SPIN_RESULT;
GameType = gameType;
WinAmount = winAmount;
}
}
...
public async Task<MessageResult> HandleMessage(ParseResult parseResult)
{
if (parseResult.IsSuccess)
{
switch (parseResult.MessageName)
{
...
case GameMessages.SPIN_REQUEST:
return await MakeCustomSpin(parseResult.ToMessage<CustomSpinRequest>());
...
}
}
...
}
public async Task<MessageResult> MakeCustomSpin(CustomSpinRequest message)
{
// example of custom calculation
long winAmount = 0;
var random = await _fireball.Random.GetRandomDouble(0, 1);
if (random > 0.5f)
{
winAmount = message.BetAmount * 2;
}
// create response message
var response = new CustomSpinResult(message.GameType, winAmount, message);
// send response message to the game client
return await _fireball.SendMessageToClient(response);
}