Skip to content

Commit

Permalink
Upgraded 'Adventure' samples
Browse files Browse the repository at this point in the history
  • Loading branch information
IEvangelist committed Nov 10, 2022
1 parent f549f81 commit e59f454
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 83 deletions.
7 changes: 4 additions & 3 deletions samples/Adventure/AdventureClient/AdventureClient.csproj
@@ -1,16 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ServerGarbageCollection>true</ServerGarbageCollection>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Orleans.Client" Version="3.6.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Client" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
29 changes: 17 additions & 12 deletions samples/Adventure/AdventureClient/Program.cs
@@ -1,24 +1,29 @@
using AdventureGrainInterfaces;
using Orleans;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using var client = new ClientBuilder()
.UseLocalhostClustering()
using var host = Host.CreateDefaultBuilder(args)
.UseOrleansClient(clientBuilder =>
clientBuilder.UseLocalhostClustering())
.Build();

await client.Connect();
await host.StartAsync();

Console.WriteLine(@"
___ _ _
/ _ \ | | | |
/ /_\ \ __| |_ _____ _ __ | |_ _ _ _ __ ___
| _ |/ _` \ \ / / _ \ '_ \| __| | | | '__/ _ \
| | | | (_| |\ V / __/ | | | |_| |_| | | | __/
\_| |_/\__,_| \_/ \___|_| |_|\__|\__,_|_| \___|");
Console.WriteLine("""
______ __ __
/\ _ \ /\ \ /\ \__
\ \ \L\ \ \_\ \ __ __ __ ___\ \ ,_\ __ __ _ __ __
\ \ __ \ /'_` \/\ \/\ \ /'__`\/' _ `\ \ \/ /\ \/\ \/\`'__\/'__`\
\ \ \/\ \/\ \L\ \ \ \_/ |/\ __//\ \/\ \ \ \_\ \ \_\ \ \ \//\ __/
\ \_\ \_\ \___,_\ \___/ \ \____\ \_\ \_\ \__\\ \____/\ \_\\ \____\
\/_/\/_/\/__,_ /\/__/ \/____/\/_/\/_/\/__/ \/___/ \/_/ \/____/
""");

Console.WriteLine();
Console.WriteLine("What's your name?");
var name = Console.ReadLine()!;

var client = host.Services.GetRequiredService<IClusterClient>();
var player = client.GetGrain<IPlayerGrain>(Guid.NewGuid());
await player.SetName(name);

Expand All @@ -42,5 +47,5 @@
{
await player.Die();
Console.WriteLine("Game over!");
await client.Close();
await host.StopAsync();
}
@@ -1,17 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.6.0" />
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0" />
</ItemGroup>

</Project>
2 changes: 0 additions & 2 deletions samples/Adventure/AdventureGrainInterfaces/IMonsterGrain.cs
@@ -1,5 +1,3 @@
using Orleans;

namespace AdventureGrainInterfaces;

public interface IMonsterGrain : IGrainWithIntegerKey
Expand Down
2 changes: 0 additions & 2 deletions samples/Adventure/AdventureGrainInterfaces/IPlayerGrain.cs
@@ -1,5 +1,3 @@
using Orleans;

namespace AdventureGrainInterfaces;

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions samples/Adventure/AdventureGrainInterfaces/IRoomGrain.cs
@@ -1,5 +1,3 @@
using Orleans;

namespace AdventureGrainInterfaces;

/// <summary>
Expand Down
10 changes: 4 additions & 6 deletions samples/Adventure/AdventureGrainInterfaces/MonsterInfo.cs
@@ -1,9 +1,7 @@
using Orleans.Concurrency;

namespace AdventureGrainInterfaces;

[Immutable]
[GenerateSerializer, Immutable]
public record class MonsterInfo(
long Id = 0,
string? Name = null,
List<long>? KilledBy = null);
[property: Id(0)] long Id = 0,
[property: Id(1)] string? Name = null,
[property: Id(2)] List<long>? KilledBy = null);
8 changes: 3 additions & 5 deletions samples/Adventure/AdventureGrainInterfaces/PlayerInfo.cs
@@ -1,8 +1,6 @@
using Orleans.Concurrency;

namespace AdventureGrainInterfaces;

[Immutable]
[GenerateSerializer, Immutable]
public record class PlayerInfo(
Guid Key,
string? Name);
[property: Id(0)] Guid Key,
[property: Id(1)] string? Name);
44 changes: 21 additions & 23 deletions samples/Adventure/AdventureGrainInterfaces/Thing.cs
@@ -1,32 +1,30 @@
using Orleans.Concurrency;

namespace AdventureGrainInterfaces;

[Immutable]
[GenerateSerializer, Immutable]
public record class Thing(
long Id,
string Name,
string Category,
long FoundIn,
List<string> Commands);
[property: Id(0)] long Id,
[property: Id(1)] string Name,
[property: Id(2)] string Category,
[property: Id(3)] long FoundIn,
[property: Id(4)] List<string> Commands);

[Immutable]
[GenerateSerializer, Immutable]
public record class MapInfo(
string Name,
List<RoomInfo> Rooms,
List<CategoryInfo> Categories,
List<Thing> Things,
List<MonsterInfo> Monsters);
[property: Id(0)] string Name,
[property: Id(1)] List<RoomInfo> Rooms,
[property: Id(2)] List<CategoryInfo> Categories,
[property: Id(3)] List<Thing> Things,
[property: Id(4)] List<MonsterInfo> Monsters);

[Immutable]
[GenerateSerializer, Immutable]
public record class RoomInfo(
long Id,
string Name,
string Description,
Dictionary<string, long> Directions);
[property: Id(0)] long Id,
[property: Id(1)] string Name,
[property: Id(2)] string Description,
[property: Id(3)] Dictionary<string, long> Directions);

[Immutable]
[GenerateSerializer, Immutable]
public record class CategoryInfo(
long Id,
string Name,
List<string> Commands);
[property: Id(0)] long Id,
[property: Id(1)] string Name,
[property: Id(2)] List<string> Commands);
8 changes: 2 additions & 6 deletions samples/Adventure/AdventureGrains/AdventureGrains.csproj
@@ -1,17 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.6.0" />
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 2 additions & 3 deletions samples/Adventure/AdventureGrains/MonsterGrain.cs
@@ -1,5 +1,4 @@
using AdventureGrainInterfaces;
using Orleans;

namespace AdventureGrains;

Expand All @@ -8,7 +7,7 @@ public class MonsterGrain : Grain, IMonsterGrain
private MonsterInfo _monsterInfo = new();
private IRoomGrain? _roomGrain; // Current room

public override Task OnActivateAsync()
public override Task OnActivateAsync(CancellationToken cancellationToken)
{
_monsterInfo = _monsterInfo with { Id = this.GetPrimaryKeyLong() };

Expand All @@ -18,7 +17,7 @@ public override Task OnActivateAsync()
TimeSpan.FromSeconds(150),
TimeSpan.FromMinutes(150));

return base.OnActivateAsync();
return base.OnActivateAsync(cancellationToken);
}

Task IMonsterGrain.SetInfo(MonsterInfo info)
Expand Down
6 changes: 3 additions & 3 deletions samples/Adventure/AdventureGrains/PlayerGrain.cs
@@ -1,6 +1,6 @@
using System.Text;
using System.Threading;
using AdventureGrainInterfaces;
using Orleans;

namespace AdventureGrains;

Expand All @@ -12,10 +12,10 @@ public class PlayerGrain : Grain, IPlayerGrain
private bool _killed = false;
private PlayerInfo _myInfo = null!;

public override Task OnActivateAsync()
public override Task OnActivateAsync(CancellationToken cancellationToken)
{
_myInfo = new(this.GetPrimaryKey(), "nobody");
return base.OnActivateAsync();
return base.OnActivateAsync(cancellationToken);
}

Task<string?> IPlayerGrain.Name() => Task.FromResult(_myInfo?.Name);
Expand Down
1 change: 0 additions & 1 deletion samples/Adventure/AdventureGrains/RoomGrain.cs
@@ -1,6 +1,5 @@
using System.Text;
using AdventureGrainInterfaces;
using Orleans;

namespace AdventureGrains;

Expand Down
3 changes: 1 addition & 2 deletions samples/Adventure/AdventureServer/AdventureGame.cs
@@ -1,10 +1,9 @@
using AdventureGrainInterfaces;
using Newtonsoft.Json;
using Orleans;

namespace AdventureSetup;

public class AdventureGame
public sealed class AdventureGame
{
private readonly IGrainFactory _client;

Expand Down
8 changes: 4 additions & 4 deletions samples/Adventure/AdventureServer/AdventureServer.csproj
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ServerGarbageCollection>true</ServerGarbageCollection>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Orleans.Server" Version="3.6.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.Orleans.Server" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 1 addition & 3 deletions samples/Adventure/AdventureServer/Program.cs
Expand Up @@ -2,8 +2,6 @@
using AdventureSetup;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Orleans;
using Orleans.Hosting;

var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
var mapFileName = Path.Combine(path, "AdventureMap.json");
Expand All @@ -27,7 +25,7 @@
}

// Configure the host
using var host = Host.CreateDefaultBuilder()
using var host = Host.CreateDefaultBuilder(args)
.UseOrleans(siloBuilder =>
{
siloBuilder.UseLocalhostClustering();
Expand Down

0 comments on commit e59f454

Please sign in to comment.