diff --git a/client/spacetime/Spacetime.cs b/client/spacetime/Spacetime.cs index 6e7449a..1fc9034 100644 --- a/client/spacetime/Spacetime.cs +++ b/client/spacetime/Spacetime.cs @@ -1,86 +1,88 @@ -using System; using Godot; using SpacetimeDB; using SpacetimeDB.Types; +using System; +using System.Linq; public class Spacetime { - /// The URI of the SpacetimeDB instance hosting our chat database and module. - const string HOST = "http://localhost:3000"; + /// The URI of the SpacetimeDB instance hosting our chat database and module. + const string HOST = "http://localhost:3000"; - /// The database name we chose when we published our module. - const string DB_NAME = "massive"; + /// The database name we chose when we published our module. + const string DB_NAME = "massive"; - public static Spacetime Instance + public static Spacetime Instance + { + get { - get - { - if (_instance == null) - { - _instance = new Spacetime(HOST, DB_NAME); - } - return _instance; - } + if (_instance == null) + { + _instance = new Spacetime(HOST, DB_NAME); + } + return _instance; } + } - // our local client SpacetimeDB identity - public Identity Identity { get; private set; } - public DbConnection Connection { get; private set; } - public User Me + // our local client SpacetimeDB identity + public Identity Identity { get; private set; } + public DbConnection Connection { get; private set; } + public User Me + { + get => Connection.Db.User.Identity.Find(Identity); + } + + private static Spacetime _instance; + + public Spacetime(string host, string dbName) + { + // Initialize the `AuthToken` module + AuthToken.Init(".spacetime_csharp_quickstart"); + + Connection = DbConnection.Builder() + .WithUri(host) + .WithModuleName(dbName) + .WithToken(AuthToken.Token) + .OnConnect(OnConnected) + .OnConnectError(OnConnectError) + .OnDisconnect(OnDisconnected) + .Build(); + } + + /// Our `OnConnected` callback: save our credentials to a file. + void OnConnected(DbConnection conn, Identity identity, string authToken) + { + Identity = identity; + AuthToken.SaveToken(authToken); + + conn.SubscriptionBuilder() + .OnApplied(OnSubscriptionApplied) + .SubscribeToAllTables(); + } + + /// Our `OnSubscriptionApplied` callback: + /// sort all past messages and print them in timestamp order. + void OnSubscriptionApplied(SubscriptionEventContext ctx) + { + GD.Print("Connected"); + } + + /// Our `OnConnectError` callback: print the error, then exit the process. + void OnConnectError(Exception e) + { + GD.PrintErr($"Error while connecting: {e}"); + } + + /// Our `OnDisconnect` callback: print a note, then exit the process. + void OnDisconnected(DbConnection conn, Exception? e) + { + if (e != null) { - get => Connection.Db.User.Identity.Find(Identity); + GD.PrintErr($"Disconnected abnormally: {e}"); } - - private static Spacetime _instance; - - public Spacetime(string host, string dbName) + else { - // Initialize the `AuthToken` module - AuthToken.Init(".spacetime_csharp_quickstart"); - - Connection = DbConnection - .Builder() - .WithUri(host) - .WithModuleName(dbName) - .WithToken(AuthToken.Token) - .OnConnect(OnConnected) - .OnConnectError(OnConnectError) - .OnDisconnect(OnDisconnected) - .Build(); - } - - /// Our `OnConnected` callback: save our credentials to a file. - void OnConnected(DbConnection conn, Identity identity, string authToken) - { - Identity = identity; - AuthToken.SaveToken(authToken); - - conn.SubscriptionBuilder().OnApplied(OnSubscriptionApplied).SubscribeToAllTables(); - } - - /// Our `OnSubscriptionApplied` callback: - /// sort all past messages and print them in timestamp order. - void OnSubscriptionApplied(SubscriptionEventContext ctx) - { - GD.Print("Connected"); - } - - /// Our `OnConnectError` callback: print the error, then exit the process. - void OnConnectError(Exception e) - { - GD.PrintErr($"Error while connecting: {e}"); - } - - /// Our `OnDisconnect` callback: print a note, then exit the process. - void OnDisconnected(DbConnection conn, Exception? e) - { - if (e != null) - { - GD.PrintErr($"Disconnected abnormally: {e}"); - } - else - { - GD.Print($"Disconnected normally."); - } + GD.Print($"Disconnected normally."); } + } }