Before you can use it, you need to:
This section describes how to manually create new Game Function base on .NET 6.0. Alternatively, you can download and use the Sample Game Function.
SampleProject.csproj
with a content:<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Fireball.Game.Server" Version="1.5.1" />
<PackageReference Include="Google.Cloud.Functions.Hosting" Version="2.1.0" />
<PackageReference Include="Google.Cloud.PubSub.V1" Version="3.8.0" />
<PackageReference Include="Google.Events.Protobuf" Version="1.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.13" />
</ItemGroup>
</Project>
Function.cs
with a content:using System.Threading;
using System.Threading.Tasks;
using CloudNative.CloudEvents;
using Fireball.Game.Server;
using Google.Cloud.Functions.Framework;
using Google.Cloud.Functions.Hosting;
using Google.Events.Protobuf.Cloud.PubSub.V1;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace SampleProject
{
public class Startup : FunctionsStartup
{
public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) =>
services.AddFireballDependencies();
}
[FunctionsStartup(typeof(Startup))]
public class Function : ICloudEventFunction<MessagePublishedData>
{
private readonly IFireballLogger _logger;
private readonly IFireball _fireball;
public Function(ILogger<Function> logger, IFireball fireball)
{
_logger = new FireballLogger(nameof(Function), logger); ;
_fireball = fireball;
}
public async Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken)
{
string messageJson = data.Message?.TextData;
ParseResult result = await _fireball.ParseMessage(messageJson);
_logger.Log($"Message: {result.MessageName}");
}
}
}
Game.cs
script with a content:using System.Threading.Tasks;
using Fireball.Game.Server;
using Fireball.Game.Server.Models;
using Microsoft.Extensions.Logging;
namespace SampleProject
{
public interface IGame
{
Task<MessageResult> HandleMessage(ParseResult parseResult);
}
public class Game : IGame
{
private readonly IFireballLogger _logger;
private readonly IFireball _fireball;
public Game(ILogger<Game> logger, IFireball fireball)
{
_logger = new FireballLogger(nameof(Game), logger);
_fireball = fireball;
}
public async Task<MessageResult> HandleMessage(ParseResult parseResult)
{
if (parseResult.IsSuccess)
{
// convert message object to correct message class
var message = parseResult.ToMessage<BaseMessage>();
// here can do appropriate actions with income message
// return result of actions
return MessageResult.SuccessResult(message.Name, message.ActionId);
}
return MessageResult.ErrorResult("Error Parse message...");
}
}
}
public class Startup : FunctionsStartup
{
public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services)
{
services.AddFireballDependencies();
services.AddSingleton<IGame, Game>();
base.ConfigureServices(context, services);
}
}
[FunctionsStartup(typeof(Startup))]
public class Function : ICloudEventFunction<MessagePublishedData>
{
private readonly IFireballLogger _logger;
private readonly IFireball _fireball;
private readonly IGame _game;
public Function(ILogger<Function> logger, IFireball fireball, IGame game)
{
_logger = new FireballLogger(nameof(Function), logger); ;
_fireball = fireball;
_game = game;
}
public async Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken)
{
string messageJson = data.Message?.TextData;
ParseResult result = await _fireball.ParseMessage(messageJson);
_logger.Log($"Message: {result.MessageName}");
MessageResult messageResult = await _game.HandleMessage(result);
_logger.Log($"Result: {messageResult?.ToJson()}");
}
}