From c10a9f5e0287fab19630faa62a99e3c96c196832 Mon Sep 17 00:00:00 2001 From: Egil Hansen Date: Sat, 18 Jan 2025 16:18:37 +0000 Subject: [PATCH 1/3] Upgrade sample to .NET 9 --- orleans/Adventure/Directory.Build.props | 2 +- orleans/Adventure/Directory.Packages.props | 12 ++++++------ orleans/Adventure/README.md | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/orleans/Adventure/Directory.Build.props b/orleans/Adventure/Directory.Build.props index bb8dce99261..365ae5d9f9f 100644 --- a/orleans/Adventure/Directory.Build.props +++ b/orleans/Adventure/Directory.Build.props @@ -1,6 +1,6 @@  - net8.0 + net9.0 enable enable true diff --git a/orleans/Adventure/Directory.Packages.props b/orleans/Adventure/Directory.Packages.props index 090fa293adb..f668184b9e6 100644 --- a/orleans/Adventure/Directory.Packages.props +++ b/orleans/Adventure/Directory.Packages.props @@ -4,11 +4,11 @@ true - - - - - - + + + + + + diff --git a/orleans/Adventure/README.md b/orleans/Adventure/README.md index c6af404b6d9..755d15a2913 100644 --- a/orleans/Adventure/README.md +++ b/orleans/Adventure/README.md @@ -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 From e8ec057d54f0294158bd338676c1643e351333d6 Mon Sep 17 00:00:00 2001 From: Egil Hansen Date: Sat, 18 Jan 2025 16:19:10 +0000 Subject: [PATCH 2/3] Normalize whitespace usage --- .../AdventureGrainInterfaces/IMonsterGrain.cs | 4 +++- .../AdventureGrainInterfaces/IPlayerGrain.cs | 4 +++- .../AdventureGrainInterfaces/IRoomGrain.cs | 8 +++++++- .../Adventure/AdventureGrains/MonsterGrain.cs | 1 - .../Adventure/AdventureGrains/PlayerGrain.cs | 8 +++----- orleans/Adventure/AdventureGrains/RoomGrain.cs | 17 +++++++++-------- orleans/Adventure/AdventureServer/Program.cs | 2 +- 7 files changed, 26 insertions(+), 18 deletions(-) diff --git a/orleans/Adventure/AdventureGrainInterfaces/IMonsterGrain.cs b/orleans/Adventure/AdventureGrainInterfaces/IMonsterGrain.cs index 061e48fa40a..f7960d36142 100644 --- a/orleans/Adventure/AdventureGrainInterfaces/IMonsterGrain.cs +++ b/orleans/Adventure/AdventureGrainInterfaces/IMonsterGrain.cs @@ -1,13 +1,15 @@ -namespace AdventureGrainInterfaces; +namespace AdventureGrainInterfaces; public interface IMonsterGrain : IGrainWithIntegerKey { // Even monsters have a name Task Name(); + Task SetInfo(MonsterInfo info); // Monsters are located in exactly one room Task SetRoomGrain(IRoomGrain room); + Task RoomGrain(); Task Kill(IRoomGrain room); diff --git a/orleans/Adventure/AdventureGrainInterfaces/IPlayerGrain.cs b/orleans/Adventure/AdventureGrainInterfaces/IPlayerGrain.cs index 204cb89ea18..d9a1b65bb9a 100644 --- a/orleans/Adventure/AdventureGrainInterfaces/IPlayerGrain.cs +++ b/orleans/Adventure/AdventureGrainInterfaces/IPlayerGrain.cs @@ -1,4 +1,4 @@ -namespace AdventureGrainInterfaces; +namespace AdventureGrainInterfaces; /// /// A player is, well, there's really no other good name... @@ -7,10 +7,12 @@ public interface IPlayerGrain : IGrainWithGuidKey { // Players have names Task Name(); + Task SetName(string name); // Each player is located in exactly one room Task SetRoomGrain(IRoomGrain room); + Task RoomGrain(); // Until Death comes knocking diff --git a/orleans/Adventure/AdventureGrainInterfaces/IRoomGrain.cs b/orleans/Adventure/AdventureGrainInterfaces/IRoomGrain.cs index 6f359dff9a9..b4d1cfcb9f6 100644 --- a/orleans/Adventure/AdventureGrainInterfaces/IRoomGrain.cs +++ b/orleans/Adventure/AdventureGrainInterfaces/IRoomGrain.cs @@ -1,4 +1,4 @@ -namespace AdventureGrainInterfaces; +namespace AdventureGrainInterfaces; /// /// A room is any location in a game, including outdoor locations and @@ -8,24 +8,30 @@ public interface IRoomGrain : IGrainWithIntegerKey { // Rooms have a textual description Task Description(PlayerInfo whoisAsking); + Task SetInfo(RoomInfo info); Task 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 FindThing(string name); // Players and monsters can be killed, if you have the right weapon. Task FindPlayer(string name); + Task FindMonster(string name); } diff --git a/orleans/Adventure/AdventureGrains/MonsterGrain.cs b/orleans/Adventure/AdventureGrains/MonsterGrain.cs index edb3c1b7463..713994e422a 100644 --- a/orleans/Adventure/AdventureGrains/MonsterGrain.cs +++ b/orleans/Adventure/AdventureGrains/MonsterGrain.cs @@ -61,7 +61,6 @@ private async Task Move() } } - Task IMonsterGrain.Kill(IRoomGrain room) { if (_roomGrain is not null) diff --git a/orleans/Adventure/AdventureGrains/PlayerGrain.cs b/orleans/Adventure/AdventureGrains/PlayerGrain.cs index 2968b011bbd..ec483448acc 100644 --- a/orleans/Adventure/AdventureGrains/PlayerGrain.cs +++ b/orleans/Adventure/AdventureGrains/PlayerGrain.cs @@ -1,4 +1,4 @@ -using System.Text; +using System.Text; using System.Threading; using AdventureGrainInterfaces; @@ -6,9 +6,8 @@ namespace AdventureGrains; public class PlayerGrain : Grain, IPlayerGrain { + private readonly List _things = []; // Things that the player is carrying private IRoomGrain? _roomGrain; // Current room - private readonly List _things = new(); // Things that the player is carrying - private bool _killed = false; private PlayerInfo _myInfo = null!; @@ -22,11 +21,10 @@ public override Task OnActivateAsync(CancellationToken cancellationToken) Task IPlayerGrain.RoomGrain() => Task.FromResult(_roomGrain!); - async Task IPlayerGrain.Die() { // Drop everything - var dropTasks = new List>(); + var dropTasks = new List>(); foreach (var thing in _things.ToArray() /* New collection */) { dropTasks.Add(Drop(thing)); diff --git a/orleans/Adventure/AdventureGrains/RoomGrain.cs b/orleans/Adventure/AdventureGrains/RoomGrain.cs index f4c7bc3e91c..9a1d9cf6b21 100644 --- a/orleans/Adventure/AdventureGrains/RoomGrain.cs +++ b/orleans/Adventure/AdventureGrains/RoomGrain.cs @@ -1,4 +1,4 @@ -using System.Text; +using System.Text; using AdventureGrainInterfaces; namespace AdventureGrains; @@ -8,14 +8,11 @@ namespace AdventureGrains; /// public class RoomGrain : Grain, IRoomGrain { - // TODO: replace placeholder grain interface with actual grain - // communication interface(s). - + private readonly List _players = []; + private readonly List _monsters = []; + private readonly List _things = []; + private readonly Dictionary _exits = []; private string? _description; - private readonly List _players = new(); - private readonly List _monsters = new(); - private readonly List _things = new(); - private readonly Dictionary _exits = new(); Task IRoomGrain.Enter(PlayerInfo player) { @@ -106,15 +103,19 @@ Task 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()); diff --git a/orleans/Adventure/AdventureServer/Program.cs b/orleans/Adventure/AdventureServer/Program.cs index 63bd9443226..384a496eb19 100644 --- a/orleans/Adventure/AdventureServer/Program.cs +++ b/orleans/Adventure/AdventureServer/Program.cs @@ -1,4 +1,4 @@ -using System.Reflection; +using System.Reflection; using AdventureSetup; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; From 227447e5ba3a71a5383a734345cee59f337ebad2 Mon Sep 17 00:00:00 2001 From: Egil Hansen Date: Sat, 18 Jan 2025 16:20:39 +0000 Subject: [PATCH 3/3] Use RegisterGrainTimer instead of obsolete RegisterTimer --- orleans/Adventure/AdventureGrains/MonsterGrain.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/orleans/Adventure/AdventureGrains/MonsterGrain.cs b/orleans/Adventure/AdventureGrains/MonsterGrain.cs index 713994e422a..7119561198c 100644 --- a/orleans/Adventure/AdventureGrains/MonsterGrain.cs +++ b/orleans/Adventure/AdventureGrains/MonsterGrain.cs @@ -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;