Skip to content

Commit

Permalink
Update for Timestamp stuff & EventContext stuff & ConnectionId stuff (#…
Browse files Browse the repository at this point in the history
…19)

* Fix C# server bug with overlapping circles

Update for Timestamp stuff

* Update for EventContext changes (TODO: update package .jsons once c# SDK PR is merged

* Regenerate

* Fix package .jsons

* Update for new AuthToken logic

* Update for no onUnhandledReducerError + fix package jsons

* Revert server-rust/Cargo.toml change
  • Loading branch information
kazimuth authored Feb 11, 2025
1 parent ab13921 commit 080df88
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 66 deletions.
10 changes: 2 additions & 8 deletions client-unity/Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,14 @@ private void Start()
// Then, you can run the executable multiple times. Since the executable will not check for
// a saved auth token, each run of will receive a different Identifier,
// and their circles will be able to eat each other.
if (PlayerPrefs.HasKey(AuthToken.GetTokenKey()))
if (AuthToken.Token != "")
{
builder = builder.WithToken(AuthToken.Token);
}

// Building the connection will establish a connection to the SpacetimeDB
// server.
Conn = builder.Build();

/* BEGIN: not in tutorial */
#pragma warning disable CS0612 // Type or member is obsolete
Conn.onUnhandledReducerError += InstanceOnUnhandledReducerError;
#pragma warning restore CS0612 // Type or member is obsolete
/* END: not in tutorial */
}

// Called when we connect to SpacetimeDB and receive our client identity
Expand Down Expand Up @@ -100,7 +94,7 @@ void HandleDisconnect(DbConnection _conn, Exception ex)
}
}

private void HandleSubscriptionApplied(EventContext ctx)
private void HandleSubscriptionApplied(SubscriptionEventContext ctx)
{
Debug.Log("Subscription applied!");
OnSubscriptionApplied?.Invoke();
Expand Down
4 changes: 2 additions & 2 deletions client-unity/Assets/Scripts/autogen/Reducers/CircleDecay.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void CircleDecayHandler(EventContext ctx, CircleDecayTimer timer);
public delegate void CircleDecayHandler(ReducerEventContext ctx, CircleDecayTimer timer);
public event CircleDecayHandler? OnCircleDecay;

public void CircleDecay(CircleDecayTimer timer)
{
conn.InternalCallReducer(new Reducer.CircleDecay(timer), this.SetCallReducerFlags.CircleDecayFlags);
}

public bool InvokeCircleDecay(EventContext ctx, Reducer.CircleDecay args)
public bool InvokeCircleDecay(ReducerEventContext ctx, Reducer.CircleDecay args)
{
if (OnCircleDecay == null) return false;
OnCircleDecay(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void CircleRecombineHandler(EventContext ctx, CircleRecombineTimer timer);
public delegate void CircleRecombineHandler(ReducerEventContext ctx, CircleRecombineTimer timer);
public event CircleRecombineHandler? OnCircleRecombine;

public void CircleRecombine(CircleRecombineTimer timer)
{
conn.InternalCallReducer(new Reducer.CircleRecombine(timer), this.SetCallReducerFlags.CircleRecombineFlags);
}

public bool InvokeCircleRecombine(EventContext ctx, Reducer.CircleRecombine args)
public bool InvokeCircleRecombine(ReducerEventContext ctx, Reducer.CircleRecombine args)
{
if (OnCircleRecombine == null) return false;
OnCircleRecombine(
Expand Down
4 changes: 2 additions & 2 deletions client-unity/Assets/Scripts/autogen/Reducers/Connect.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void ConnectHandler(EventContext ctx);
public delegate void ConnectHandler(ReducerEventContext ctx);
public event ConnectHandler? OnConnect;

public bool InvokeConnect(EventContext ctx, Reducer.Connect args)
public bool InvokeConnect(ReducerEventContext ctx, Reducer.Connect args)
{
if (OnConnect == null) return false;
OnConnect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void ConsumeEntityHandler(EventContext ctx, ConsumeEntityTimer request);
public delegate void ConsumeEntityHandler(ReducerEventContext ctx, ConsumeEntityTimer request);
public event ConsumeEntityHandler? OnConsumeEntity;

public void ConsumeEntity(ConsumeEntityTimer request)
{
conn.InternalCallReducer(new Reducer.ConsumeEntity(request), this.SetCallReducerFlags.ConsumeEntityFlags);
}

public bool InvokeConsumeEntity(EventContext ctx, Reducer.ConsumeEntity args)
public bool InvokeConsumeEntity(ReducerEventContext ctx, Reducer.ConsumeEntity args)
{
if (OnConsumeEntity == null) return false;
OnConsumeEntity(
Expand Down
4 changes: 2 additions & 2 deletions client-unity/Assets/Scripts/autogen/Reducers/Disconnect.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void DisconnectHandler(EventContext ctx);
public delegate void DisconnectHandler(ReducerEventContext ctx);
public event DisconnectHandler? OnDisconnect;

public bool InvokeDisconnect(EventContext ctx, Reducer.Disconnect args)
public bool InvokeDisconnect(ReducerEventContext ctx, Reducer.Disconnect args)
{
if (OnDisconnect == null) return false;
OnDisconnect(
Expand Down
4 changes: 2 additions & 2 deletions client-unity/Assets/Scripts/autogen/Reducers/EnterGame.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void EnterGameHandler(EventContext ctx, string name);
public delegate void EnterGameHandler(ReducerEventContext ctx, string name);
public event EnterGameHandler? OnEnterGame;

public void EnterGame(string name)
{
conn.InternalCallReducer(new Reducer.EnterGame(name), this.SetCallReducerFlags.EnterGameFlags);
}

public bool InvokeEnterGame(EventContext ctx, Reducer.EnterGame args)
public bool InvokeEnterGame(ReducerEventContext ctx, Reducer.EnterGame args)
{
if (OnEnterGame == null) return false;
OnEnterGame(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void MoveAllPlayersHandler(EventContext ctx, MoveAllPlayersTimer timer);
public delegate void MoveAllPlayersHandler(ReducerEventContext ctx, MoveAllPlayersTimer timer);
public event MoveAllPlayersHandler? OnMoveAllPlayers;

public void MoveAllPlayers(MoveAllPlayersTimer timer)
{
conn.InternalCallReducer(new Reducer.MoveAllPlayers(timer), this.SetCallReducerFlags.MoveAllPlayersFlags);
}

public bool InvokeMoveAllPlayers(EventContext ctx, Reducer.MoveAllPlayers args)
public bool InvokeMoveAllPlayers(ReducerEventContext ctx, Reducer.MoveAllPlayers args)
{
if (OnMoveAllPlayers == null) return false;
OnMoveAllPlayers(
Expand Down
4 changes: 2 additions & 2 deletions client-unity/Assets/Scripts/autogen/Reducers/PlayerSplit.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void PlayerSplitHandler(EventContext ctx);
public delegate void PlayerSplitHandler(ReducerEventContext ctx);
public event PlayerSplitHandler? OnPlayerSplit;

public void PlayerSplit()
{
conn.InternalCallReducer(new Reducer.PlayerSplit(), this.SetCallReducerFlags.PlayerSplitFlags);
}

public bool InvokePlayerSplit(EventContext ctx, Reducer.PlayerSplit args)
public bool InvokePlayerSplit(ReducerEventContext ctx, Reducer.PlayerSplit args)
{
if (OnPlayerSplit == null) return false;
OnPlayerSplit(
Expand Down
4 changes: 2 additions & 2 deletions client-unity/Assets/Scripts/autogen/Reducers/Respawn.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void RespawnHandler(EventContext ctx);
public delegate void RespawnHandler(ReducerEventContext ctx);
public event RespawnHandler? OnRespawn;

public void Respawn()
{
conn.InternalCallReducer(new Reducer.Respawn(), this.SetCallReducerFlags.RespawnFlags);
}

public bool InvokeRespawn(EventContext ctx, Reducer.Respawn args)
public bool InvokeRespawn(ReducerEventContext ctx, Reducer.Respawn args)
{
if (OnRespawn == null) return false;
OnRespawn(
Expand Down
4 changes: 2 additions & 2 deletions client-unity/Assets/Scripts/autogen/Reducers/SpawnFood.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void SpawnFoodHandler(EventContext ctx, SpawnFoodTimer timer);
public delegate void SpawnFoodHandler(ReducerEventContext ctx, SpawnFoodTimer timer);
public event SpawnFoodHandler? OnSpawnFood;

public void SpawnFood(SpawnFoodTimer timer)
{
conn.InternalCallReducer(new Reducer.SpawnFood(timer), this.SetCallReducerFlags.SpawnFoodFlags);
}

public bool InvokeSpawnFood(EventContext ctx, Reducer.SpawnFood args)
public bool InvokeSpawnFood(ReducerEventContext ctx, Reducer.SpawnFood args)
{
if (OnSpawnFood == null) return false;
OnSpawnFood(
Expand Down
4 changes: 2 additions & 2 deletions client-unity/Assets/Scripts/autogen/Reducers/Suicide.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void SuicideHandler(EventContext ctx);
public delegate void SuicideHandler(ReducerEventContext ctx);
public event SuicideHandler? OnSuicide;

public void Suicide()
{
conn.InternalCallReducer(new Reducer.Suicide(), this.SetCallReducerFlags.SuicideFlags);
}

public bool InvokeSuicide(EventContext ctx, Reducer.Suicide args)
public bool InvokeSuicide(ReducerEventContext ctx, Reducer.Suicide args)
{
if (OnSuicide == null) return false;
OnSuicide(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void UpdatePlayerInputHandler(EventContext ctx, DbVector2 direction);
public delegate void UpdatePlayerInputHandler(ReducerEventContext ctx, DbVector2 direction);
public event UpdatePlayerInputHandler? OnUpdatePlayerInput;

public void UpdatePlayerInput(DbVector2 direction)
{
conn.InternalCallReducer(new Reducer.UpdatePlayerInput(direction), this.SetCallReducerFlags.UpdatePlayerInputFlags);
}

public bool InvokeUpdatePlayerInput(EventContext ctx, Reducer.UpdatePlayerInput args)
public bool InvokeUpdatePlayerInput(ReducerEventContext ctx, Reducer.UpdatePlayerInput args)
{
if (OnUpdatePlayerInput == null) return false;
OnUpdatePlayerInput(
Expand Down
80 changes: 70 additions & 10 deletions client-unity/Assets/Scripts/autogen/SpacetimeDBClient.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public RemoteTables(DbConnection conn)
}

public sealed partial class SetReducerFlags { }

public sealed class EventContext : IEventContext
{
private readonly DbConnection conn;
Expand All @@ -44,18 +45,70 @@ public sealed class EventContext : IEventContext
public RemoteReducers Reducers => conn.Reducers;
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;

internal EventContext(DbConnection conn, Event<Reducer> reducerEvent)
internal EventContext(DbConnection conn, Event<Reducer> Event)
{
this.conn = conn;
this.Event = Event;
}
}

public sealed class ReducerEventContext : IReducerEventContext
{
private readonly DbConnection conn;
public readonly ReducerEvent<Reducer> Event;

public RemoteTables Db => conn.Db;
public RemoteReducers Reducers => conn.Reducers;
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;

internal ReducerEventContext(DbConnection conn, ReducerEvent<Reducer> reducerEvent)
{
this.conn = conn;
Event = reducerEvent;
}
}

public sealed class ErrorContext : IErrorContext
{
private readonly DbConnection conn;
public readonly Exception Event;
Exception IErrorContext.Event
{
get
{
return Event;
}
}

public RemoteTables Db => conn.Db;
public RemoteReducers Reducers => conn.Reducers;
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;
public Exception Error => Event;

internal ErrorContext(DbConnection conn, Exception error)
{
this.conn = conn;
Event = error;
}
}

public sealed class SubscriptionEventContext : ISubscriptionEventContext
{
private readonly DbConnection conn;

public RemoteTables Db => conn.Db;
public RemoteReducers Reducers => conn.Reducers;
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;

internal SubscriptionEventContext(DbConnection conn)
{
this.conn = conn;
}
}

public abstract partial class Reducer
{
private Reducer() { }

public sealed class StdbNone : Reducer { }
}

public sealed class DbConnection : DbConnectionBase<DbConnection, RemoteTables, Reducer>
Expand Down Expand Up @@ -87,17 +140,25 @@ protected override Reducer ToReducer(TransactionUpdate update)
"spawn_food" => BSATNHelpers.Decode<Reducer.SpawnFood>(encodedArgs),
"suicide" => BSATNHelpers.Decode<Reducer.Suicide>(encodedArgs),
"update_player_input" => BSATNHelpers.Decode<Reducer.UpdatePlayerInput>(encodedArgs),
"<none>" or "" => new Reducer.StdbNone(),
var reducer => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {reducer}")
};
}

protected override IEventContext ToEventContext(Event<Reducer> reducerEvent) =>
new EventContext(this, reducerEvent);
protected override IEventContext ToEventContext(Event<Reducer> Event) =>
new EventContext(this, Event);

protected override IReducerEventContext ToReducerEventContext(ReducerEvent<Reducer> reducerEvent) =>
new ReducerEventContext(this, reducerEvent);

protected override ISubscriptionEventContext MakeSubscriptionEventContext() =>
new SubscriptionEventContext(this);

protected override IErrorContext ToErrorContext(Exception exception) =>
new ErrorContext(this, exception);

protected override bool Dispatch(IEventContext context, Reducer reducer)
protected override bool Dispatch(IReducerEventContext context, Reducer reducer)
{
var eventContext = (EventContext)context;
var eventContext = (ReducerEventContext)context;
return reducer switch
{
Reducer.CircleDecay args => Reducers.InvokeCircleDecay(eventContext, args),
Expand All @@ -112,11 +173,10 @@ protected override bool Dispatch(IEventContext context, Reducer reducer)
Reducer.SpawnFood args => Reducers.InvokeSpawnFood(eventContext, args),
Reducer.Suicide args => Reducers.InvokeSuicide(eventContext, args),
Reducer.UpdatePlayerInput args => Reducers.InvokeUpdatePlayerInput(eventContext, args),
Reducer.StdbNone => true,
_ => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {reducer}")
};
}

public SubscriptionBuilder<EventContext> SubscriptionBuilder() => new(this);
public SubscriptionBuilder<SubscriptionEventContext, ErrorContext> SubscriptionBuilder() => new(this);
}
}
4 changes: 2 additions & 2 deletions client-unity/Assets/Scripts/autogen/Types/Circle.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public sealed partial class Circle
[DataMember(Name = "speed")]
public float Speed;
[DataMember(Name = "last_split_time")]
public ulong LastSplitTime;
public SpacetimeDB.Timestamp LastSplitTime;

public Circle(
uint EntityId,
uint PlayerId,
DbVector2 Direction,
float Speed,
ulong LastSplitTime
SpacetimeDB.Timestamp LastSplitTime
)
{
this.EntityId = EntityId;
Expand Down
4 changes: 2 additions & 2 deletions client-unity/Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"com.clockworklabs.spacetimedbsdk": "[email protected]:clockworklabs/com.clockworklabs.spacetimedbsdk.git#ingvar/csharp-codegen-rewrite",
"com.clockworklabs.spacetimedbsdk": "[email protected]:clockworklabs/com.clockworklabs.spacetimedbsdk.git#staging",
"com.unity.collab-proxy": "2.3.1",
"com.unity.feature.2d": "2.0.1",
"com.unity.ide.rider": "3.0.28",
Expand Down Expand Up @@ -48,4 +48,4 @@
"com.unity.modules.wind": "1.0.0",
"com.unity.modules.xr": "1.0.0"
}
}
}
Loading

0 comments on commit 080df88

Please sign in to comment.