Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 169f147

Browse files
authored
Update Orleans Adventure sample to .net 9 (#7013)
* Upgrade sample to .NET 9 * Normalize whitespace usage * Use RegisterGrainTimer instead of obsolete RegisterTimer
1 parent 2adfa49 commit 169f147

File tree

10 files changed

+40
-30
lines changed

10 files changed

+40
-30
lines changed

orleans/Adventure/AdventureGrainInterfaces/IMonsterGrain.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
namespace AdventureGrainInterfaces;
1+
namespace AdventureGrainInterfaces;
22

33
public interface IMonsterGrain : IGrainWithIntegerKey
44
{
55
// Even monsters have a name
66
Task<string?> Name();
7+
78
Task SetInfo(MonsterInfo info);
89

910
// Monsters are located in exactly one room
1011
Task SetRoomGrain(IRoomGrain room);
12+
1113
Task<IRoomGrain> RoomGrain();
1214

1315
Task<string> Kill(IRoomGrain room);

orleans/Adventure/AdventureGrainInterfaces/IPlayerGrain.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace AdventureGrainInterfaces;
1+
namespace AdventureGrainInterfaces;
22

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

1213
// Each player is located in exactly one room
1314
Task SetRoomGrain(IRoomGrain room);
15+
1416
Task<IRoomGrain> RoomGrain();
1517

1618
// Until Death comes knocking

orleans/Adventure/AdventureGrainInterfaces/IRoomGrain.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace AdventureGrainInterfaces;
1+
namespace AdventureGrainInterfaces;
22

33
/// <summary>
44
/// A room is any location in a game, including outdoor locations and
@@ -8,24 +8,30 @@ public interface IRoomGrain : IGrainWithIntegerKey
88
{
99
// Rooms have a textual description
1010
Task<string> Description(PlayerInfo whoisAsking);
11+
1112
Task SetInfo(RoomInfo info);
1213

1314
Task<IRoomGrain?> ExitTo(string direction);
1415

1516
// Players can enter or exit a room
1617
Task Enter(PlayerInfo player);
18+
1719
Task Exit(PlayerInfo player);
1820

1921
// Monsters can enter or exit a room
2022
Task Enter(MonsterInfo monster);
23+
2124
Task Exit(MonsterInfo monster);
2225

2326
// Things can be dropped or taken from a room
2427
Task Drop(Thing thing);
28+
2529
Task Take(Thing thing);
30+
2631
Task<Thing?> FindThing(string name);
2732

2833
// Players and monsters can be killed, if you have the right weapon.
2934
Task<PlayerInfo?> FindPlayer(string name);
35+
3036
Task<MonsterInfo?> FindMonster(string name);
3137
}

orleans/Adventure/AdventureGrains/MonsterGrain.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
using AdventureGrainInterfaces;
1+
using AdventureGrainInterfaces;
22

33
namespace AdventureGrains;
44

5-
public class MonsterGrain : Grain, IMonsterGrain
5+
public class MonsterGrain : Grain, IMonsterGrain, IDisposable
66
{
77
private MonsterInfo _monsterInfo = new();
8+
private IGrainTimer? _timer;
89
private IRoomGrain? _roomGrain; // Current room
910

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

14-
RegisterTimer(
15+
_timer = this.RegisterGrainTimer(
1516
_ => Move(),
16-
null!,
1717
TimeSpan.FromSeconds(150),
1818
TimeSpan.FromMinutes(150));
1919

2020
return base.OnActivateAsync(cancellationToken);
2121
}
2222

23+
public void Dispose() => _timer?.Dispose();
24+
2325
Task IMonsterGrain.SetInfo(MonsterInfo info)
2426
{
2527
_monsterInfo = info;
@@ -61,7 +63,6 @@ private async Task Move()
6163
}
6264
}
6365

64-
6566
Task<string> IMonsterGrain.Kill(IRoomGrain room)
6667
{
6768
if (_roomGrain is not null)

orleans/Adventure/AdventureGrains/PlayerGrain.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
using System.Text;
1+
using System.Text;
22
using System.Threading;
33
using AdventureGrainInterfaces;
44

55
namespace AdventureGrains;
66

77
public class PlayerGrain : Grain, IPlayerGrain
88
{
9+
private readonly List<Thing> _things = []; // Things that the player is carrying
910
private IRoomGrain? _roomGrain; // Current room
10-
private readonly List<Thing> _things = new(); // Things that the player is carrying
11-
1211
private bool _killed = false;
1312
private PlayerInfo _myInfo = null!;
1413

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

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

25-
2624
async Task IPlayerGrain.Die()
2725
{
2826
// Drop everything
29-
var dropTasks = new List<Task<string?>>();
27+
var dropTasks = new List<Task<string?>>();
3028
foreach (var thing in _things.ToArray() /* New collection */)
3129
{
3230
dropTasks.Add(Drop(thing));

orleans/Adventure/AdventureGrains/RoomGrain.cs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Text;
1+
using System.Text;
22
using AdventureGrainInterfaces;
33

44
namespace AdventureGrains;
@@ -8,14 +8,11 @@ namespace AdventureGrains;
88
/// </summary>
99
public class RoomGrain : Grain, IRoomGrain
1010
{
11-
// TODO: replace placeholder grain interface with actual grain
12-
// communication interface(s).
13-
11+
private readonly List<PlayerInfo> _players = [];
12+
private readonly List<MonsterInfo> _monsters = [];
13+
private readonly List<Thing> _things = [];
14+
private readonly Dictionary<string, IRoomGrain> _exits = [];
1415
private string? _description;
15-
private readonly List<PlayerInfo> _players = new();
16-
private readonly List<MonsterInfo> _monsters = new();
17-
private readonly List<Thing> _things = new();
18-
private readonly Dictionary<string, IRoomGrain> _exits = new();
1916

2017
Task IRoomGrain.Enter(PlayerInfo player)
2118
{
@@ -106,15 +103,19 @@ Task<string> IRoomGrain.Description(PlayerInfo whoisAsking)
106103
{
107104
builder.AppendLine("Beware! These guys are in the room with you:");
108105
if (others.Length > 0)
106+
{
109107
foreach (var player in others)
110108
{
111109
builder.Append(" ").AppendLine(player.Name);
112110
}
111+
}
113112
if (_monsters.Count > 0)
113+
{
114114
foreach (var monster in _monsters)
115115
{
116116
builder.Append(" ").AppendLine(monster.Name);
117117
}
118+
}
118119
}
119120

120121
return Task.FromResult(builder.ToString());

orleans/Adventure/AdventureServer/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Reflection;
1+
using System.Reflection;
22
using AdventureSetup;
33
using Microsoft.Extensions.DependencyInjection;
44
using Microsoft.Extensions.Hosting;

orleans/Adventure/Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<TargetFramework>net8.0</TargetFramework>
3+
<TargetFramework>net9.0</TargetFramework>
44
<ImplicitUsings>enable</ImplicitUsings>
55
<Nullable>enable</Nullable>
66
<ServerGarbageCollection>true</ServerGarbageCollection>

orleans/Adventure/Directory.Packages.props

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
8-
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
9-
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
10-
<PackageVersion Include="Microsoft.Orleans.Client" Version="8.0.0" />
11-
<PackageVersion Include="Microsoft.Orleans.Sdk" Version="8.0.0" />
12-
<PackageVersion Include="Microsoft.Orleans.Server" Version="8.0.0" />
7+
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.1" />
8+
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.1" />
9+
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.1" />
10+
<PackageVersion Include="Microsoft.Orleans.Client" Version="9.0.1" />
11+
<PackageVersion Include="Microsoft.Orleans.Sdk" Version="9.0.1" />
12+
<PackageVersion Include="Microsoft.Orleans.Server" Version="9.0.1" />
1313
</ItemGroup>
1414
</Project>

orleans/Adventure/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ This is a simple game and there are only a few verbs which the game understands:
3434

3535
## Sample prerequisites
3636

37-
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.
37+
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.
3838

3939
## Building the sample
4040

0 commit comments

Comments
 (0)