Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Orleans Adventure sample to .net 9 #7013

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
namespace AdventureGrainInterfaces;
namespace AdventureGrainInterfaces;

public interface IMonsterGrain : IGrainWithIntegerKey
{
// Even monsters have a name
Task<string?> Name();

Task SetInfo(MonsterInfo info);

// Monsters are located in exactly one room
Task SetRoomGrain(IRoomGrain room);

Task<IRoomGrain> RoomGrain();

Task<string> Kill(IRoomGrain room);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AdventureGrainInterfaces;
namespace AdventureGrainInterfaces;

/// <summary>
/// A player is, well, there's really no other good name...
Expand All @@ -7,10 +7,12 @@ public interface IPlayerGrain : IGrainWithGuidKey
{
// Players have names
Task<string?> Name();

Task SetName(string name);

// Each player is located in exactly one room
Task SetRoomGrain(IRoomGrain room);

Task<IRoomGrain> RoomGrain();

// Until Death comes knocking
Expand Down
8 changes: 7 additions & 1 deletion orleans/Adventure/AdventureGrainInterfaces/IRoomGrain.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AdventureGrainInterfaces;
namespace AdventureGrainInterfaces;

/// <summary>
/// A room is any location in a game, including outdoor locations and
Expand All @@ -8,24 +8,30 @@ public interface IRoomGrain : IGrainWithIntegerKey
{
// Rooms have a textual description
Task<string> Description(PlayerInfo whoisAsking);

Task SetInfo(RoomInfo info);

Task<IRoomGrain?> ExitTo(string direction);

// Players can enter or exit a room
Task Enter(PlayerInfo player);

Task Exit(PlayerInfo player);

// Monsters can enter or exit a room
Task Enter(MonsterInfo monster);

Task Exit(MonsterInfo monster);

// Things can be dropped or taken from a room
Task Drop(Thing thing);

Task Take(Thing thing);

Task<Thing?> FindThing(string name);

// Players and monsters can be killed, if you have the right weapon.
Task<PlayerInfo?> FindPlayer(string name);

Task<MonsterInfo?> FindMonster(string name);
}
11 changes: 6 additions & 5 deletions orleans/Adventure/AdventureGrains/MonsterGrain.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
using AdventureGrainInterfaces;
using AdventureGrainInterfaces;

namespace AdventureGrains;

public class MonsterGrain : Grain, IMonsterGrain
public class MonsterGrain : Grain, IMonsterGrain, IDisposable
{
private MonsterInfo _monsterInfo = new();
private IGrainTimer? _timer;
private IRoomGrain? _roomGrain; // Current room

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

RegisterTimer(
_timer = this.RegisterGrainTimer(
_ => Move(),
null!,
TimeSpan.FromSeconds(150),
TimeSpan.FromMinutes(150));

return base.OnActivateAsync(cancellationToken);
}

public void Dispose() => _timer?.Dispose();

Task IMonsterGrain.SetInfo(MonsterInfo info)
{
_monsterInfo = info;
Expand Down Expand Up @@ -61,7 +63,6 @@ private async Task Move()
}
}


Task<string> IMonsterGrain.Kill(IRoomGrain room)
{
if (_roomGrain is not null)
Expand Down
8 changes: 3 additions & 5 deletions orleans/Adventure/AdventureGrains/PlayerGrain.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System.Text;
using System.Text;
using System.Threading;
using AdventureGrainInterfaces;

namespace AdventureGrains;

public class PlayerGrain : Grain, IPlayerGrain
{
private readonly List<Thing> _things = []; // Things that the player is carrying
private IRoomGrain? _roomGrain; // Current room
private readonly List<Thing> _things = new(); // Things that the player is carrying

private bool _killed = false;
private PlayerInfo _myInfo = null!;

Expand All @@ -22,11 +21,10 @@ public override Task OnActivateAsync(CancellationToken cancellationToken)

Task<IRoomGrain> IPlayerGrain.RoomGrain() => Task.FromResult(_roomGrain!);


async Task IPlayerGrain.Die()
{
// Drop everything
var dropTasks = new List<Task<string?>>();
var dropTasks = new List<Task<string?>>();
foreach (var thing in _things.ToArray() /* New collection */)
{
dropTasks.Add(Drop(thing));
Expand Down
17 changes: 9 additions & 8 deletions orleans/Adventure/AdventureGrains/RoomGrain.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text;
using System.Text;
using AdventureGrainInterfaces;

namespace AdventureGrains;
Expand All @@ -8,14 +8,11 @@ namespace AdventureGrains;
/// </summary>
public class RoomGrain : Grain, IRoomGrain
{
// TODO: replace placeholder grain interface with actual grain
// communication interface(s).

private readonly List<PlayerInfo> _players = [];
private readonly List<MonsterInfo> _monsters = [];
private readonly List<Thing> _things = [];
private readonly Dictionary<string, IRoomGrain> _exits = [];
private string? _description;
private readonly List<PlayerInfo> _players = new();
private readonly List<MonsterInfo> _monsters = new();
private readonly List<Thing> _things = new();
private readonly Dictionary<string, IRoomGrain> _exits = new();

Task IRoomGrain.Enter(PlayerInfo player)
{
Expand Down Expand Up @@ -106,15 +103,19 @@ Task<string> IRoomGrain.Description(PlayerInfo whoisAsking)
{
builder.AppendLine("Beware! These guys are in the room with you:");
if (others.Length > 0)
{
foreach (var player in others)
{
builder.Append(" ").AppendLine(player.Name);
}
}
if (_monsters.Count > 0)
{
foreach (var monster in _monsters)
{
builder.Append(" ").AppendLine(monster.Name);
}
}
}

return Task.FromResult(builder.ToString());
Expand Down
2 changes: 1 addition & 1 deletion orleans/Adventure/AdventureServer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Reflection;
using System.Reflection;
using AdventureSetup;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down
2 changes: 1 addition & 1 deletion orleans/Adventure/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ServerGarbageCollection>true</ServerGarbageCollection>
Expand Down
12 changes: 6 additions & 6 deletions orleans/Adventure/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageVersion Include="Microsoft.Orleans.Client" Version="8.0.0" />
<PackageVersion Include="Microsoft.Orleans.Sdk" Version="8.0.0" />
<PackageVersion Include="Microsoft.Orleans.Server" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.1" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.1" />
<PackageVersion Include="Microsoft.Orleans.Client" Version="9.0.1" />
<PackageVersion Include="Microsoft.Orleans.Sdk" Version="9.0.1" />
<PackageVersion Include="Microsoft.Orleans.Server" Version="9.0.1" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion orleans/Adventure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This is a simple game and there are only a few verbs which the game understands:

## Sample prerequisites

This sample is written in C# and targets .NET 7.0. It requires the [.NET 7.0 SDK](https://dotnet.microsoft.com/download/dotnet/7.0) or later.
This sample is written in C# and targets .NET 9.0. It requires the [.NET 9.0 SDK](https://dotnet.microsoft.com/download/dotnet/9.0) or later.

## Building the sample

Expand Down
Loading