implement client side
This commit is contained in:
parent
d828eeb1f7
commit
edb2cb6ef9
17 changed files with 415 additions and 0 deletions
4
client/.gitignore
vendored
Normal file
4
client/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Godot 4+ specific ignores
|
||||
.godot/
|
||||
/android/
|
||||
module_bindings/
|
||||
22
client/Game.cs
Normal file
22
client/Game.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using Godot;
|
||||
|
||||
public partial class Game : Node
|
||||
{
|
||||
private Spacetime spacetime;
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
spacetime = Spacetime.Instance;
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
spacetime.Connection.FrameTick();
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
spacetime.Connection.Disconnect();
|
||||
}
|
||||
}
|
||||
1
client/Game.cs.uid
Normal file
1
client/Game.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bg6n0cm6dds7j
|
||||
20
client/chat/ChatLog.cs
Normal file
20
client/chat/ChatLog.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using Godot;
|
||||
|
||||
public partial class ChatLog : RichTextLabel
|
||||
{
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
}
|
||||
|
||||
public void PushMessage(string name, string message, string color)
|
||||
{
|
||||
string entry = $"[color={color}]{name}:[/color] {message}";
|
||||
this.Text += $"{entry}\n";
|
||||
}
|
||||
}
|
||||
1
client/chat/ChatLog.cs.uid
Normal file
1
client/chat/ChatLog.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cgn6wa7td0ekp
|
||||
136
client/chat/ChatWindow.cs
Normal file
136
client/chat/ChatWindow.cs
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
using Godot;
|
||||
using SpacetimeDB;
|
||||
using SpacetimeDB.Types;
|
||||
|
||||
public partial class ChatWindow : VBoxContainer
|
||||
{
|
||||
const string SystemColor = "#747474";
|
||||
|
||||
private LineEdit _userNameInput;
|
||||
private ChatLog _log;
|
||||
private LineEdit _input;
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _EnterTree()
|
||||
{
|
||||
_userNameInput = GetNode<LineEdit>("Options/Username");
|
||||
_log = GetNode<ChatLog>("ChatLog");
|
||||
_input = GetNode<LineEdit>("ChatInput");
|
||||
|
||||
_log.Text = "";
|
||||
}
|
||||
|
||||
string UserNameOrIdentity(User user)
|
||||
{
|
||||
return user != null ? user.Name ?? user.Identity.ToString()[..8] : "unknown";
|
||||
}
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
DbConnection conn = Spacetime.Instance.Connection;
|
||||
|
||||
_userNameInput.TextSubmitted += OnUsernameInput;
|
||||
_userNameInput.FocusExited += ResetUsername;
|
||||
_input.TextSubmitted += OnMessageInput;
|
||||
|
||||
conn.Db.User.OnInsert += User_OnInsert;
|
||||
conn.Db.User.OnUpdate += User_OnUpdate;
|
||||
|
||||
conn.Db.Message.OnInsert += Message_OnInsert;
|
||||
|
||||
conn.Reducers.OnSetName += Reducer_OnSetNameEvent;
|
||||
conn.Reducers.OnSendMessage += Reducer_OnSendMessageEvent;
|
||||
}
|
||||
|
||||
void ResetUsername()
|
||||
{
|
||||
_userNameInput.Text = UserNameOrIdentity(Spacetime.Instance.Me);
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
}
|
||||
|
||||
private void OnUsernameInput(string text)
|
||||
{
|
||||
Spacetime.Instance.Connection.Reducers.SetName(text);
|
||||
}
|
||||
|
||||
private void OnMessageInput(string text)
|
||||
{
|
||||
Spacetime.Instance.Connection.Reducers.SendMessage(text);
|
||||
_input.Text = "";
|
||||
}
|
||||
|
||||
void User_OnInsert(EventContext ctx, User insertedValue)
|
||||
{
|
||||
if (ctx.Event is Event<Reducer>.SubscribeApplied)
|
||||
{
|
||||
if (insertedValue.Identity == Spacetime.Instance.Identity)
|
||||
{
|
||||
ResetUsername();
|
||||
}
|
||||
return;
|
||||
}
|
||||
_log.PushMessage("System", $"{UserNameOrIdentity(insertedValue)} connected", SystemColor);
|
||||
}
|
||||
|
||||
void User_OnUpdate(EventContext ctx, User oldValue, User newValue)
|
||||
{
|
||||
if (oldValue.Name != newValue.Name)
|
||||
{
|
||||
_log.PushMessage("System", $"{UserNameOrIdentity(oldValue)} renamed to {UserNameOrIdentity(newValue)}", SystemColor);
|
||||
}
|
||||
if (oldValue.Online != newValue.Online)
|
||||
{
|
||||
if (newValue.Online)
|
||||
{
|
||||
_log.PushMessage("System", $"{UserNameOrIdentity(newValue)} connected", SystemColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
_log.PushMessage("System", $"{UserNameOrIdentity(newValue)} disconnected", SystemColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Message_OnInsert(EventContext ctx, Message insertedValue)
|
||||
{
|
||||
User sender = ctx.Db.User.Identity.Find(insertedValue.Sender);
|
||||
string color = sender.Identity.Equals(Spacetime.Instance.Identity) ? "blue" : "red";
|
||||
if (ctx.Event is Event<Reducer>.SubscribeApplied)
|
||||
{
|
||||
_log.PushMessage(UserNameOrIdentity(sender), insertedValue.Text, color);
|
||||
return;
|
||||
}
|
||||
_log.PushMessage(UserNameOrIdentity(sender), insertedValue.Text, color);
|
||||
}
|
||||
|
||||
/// Our `OnSetNameEvent` callback: print a warning if the reducer failed.
|
||||
void Reducer_OnSetNameEvent(ReducerEventContext ctx, string name)
|
||||
{
|
||||
var e = ctx.Event;
|
||||
if (e.CallerIdentity != Spacetime.Instance.Identity)
|
||||
{
|
||||
// Not me
|
||||
return;
|
||||
}
|
||||
if (e.Status is Status.Failed(var error))
|
||||
{
|
||||
GD.PrintErr($"Failed to change name to {name}: {error}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// Our `OnSendMessageEvent` callback: print a warning if the reducer failed.
|
||||
void Reducer_OnSendMessageEvent(ReducerEventContext ctx, string text)
|
||||
{
|
||||
var e = ctx.Event;
|
||||
if (e.CallerIdentity == Spacetime.Instance.Identity && e.Status is Status.Failed(var error))
|
||||
{
|
||||
GD.PrintErr($"Failed to send message {text}: {error}");
|
||||
}
|
||||
}
|
||||
}
|
||||
1
client/chat/ChatWindow.cs.uid
Normal file
1
client/chat/ChatWindow.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://c3s41dv7hv4md
|
||||
40
client/chat/ChatWindow.tscn
Normal file
40
client/chat/ChatWindow.tscn
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://cqmy41vtnqd6f"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://c3s41dv7hv4md" path="res://chat/ChatWindow.cs" id="1_d8jvm"]
|
||||
[ext_resource type="Script" uid="uid://cgn6wa7td0ekp" path="res://chat/ChatLog.cs" id="2_fkxbv"]
|
||||
|
||||
[node name="Chat Window" type="VBoxContainer"]
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_d8jvm")
|
||||
|
||||
[node name="Options" type="HBoxContainer" parent="."]
|
||||
layout_mode = 2
|
||||
alignment = 2
|
||||
|
||||
[node name="Label" type="Label" parent="Options"]
|
||||
layout_mode = 2
|
||||
text = "Username:"
|
||||
|
||||
[node name="Username" type="LineEdit" parent="Options"]
|
||||
custom_minimum_size = Vector2(120, 0)
|
||||
layout_mode = 2
|
||||
|
||||
[node name="ColorPickerButton" type="ColorPickerButton" parent="Options"]
|
||||
layout_mode = 2
|
||||
text = "Color"
|
||||
|
||||
[node name="ChatLog" type="RichTextLabel" parent="."]
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 3
|
||||
bbcode_enabled = true
|
||||
text = "BBCode [color=green]test[/color]"
|
||||
scroll_following = true
|
||||
vertical_alignment = 2
|
||||
script = ExtResource("2_fkxbv")
|
||||
|
||||
[node name="ChatInput" type="LineEdit" parent="."]
|
||||
layout_mode = 2
|
||||
9
client/client.csproj
Normal file
9
client/client.csproj
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<Project Sdk="Godot.NET.Sdk/4.4.1">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<EnableDynamicLoading>true</EnableDynamicLoading>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SpacetimeDB.ClientSDK" Version="1.1.1" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
19
client/client.sln
Normal file
19
client/client.sln
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2012
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "client", "client.csproj", "{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
ExportDebug|Any CPU = ExportDebug|Any CPU
|
||||
ExportRelease|Any CPU = ExportRelease|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU
|
||||
{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU
|
||||
{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU
|
||||
{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
1
client/icon.svg
Normal file
1
client/icon.svg
Normal file
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128"><rect width="124" height="124" x="2" y="2" fill="#363d52" stroke="#212532" stroke-width="4" rx="14"/><g fill="#fff" transform="translate(12.322 12.322)scale(.101)"><path d="M105 673v33q407 354 814 0v-33z"/><path fill="#478cbf" d="m105 673 152 14q12 1 15 14l4 67 132 10 8-61q2-11 15-15h162q13 4 15 15l8 61 132-10 4-67q3-13 15-14l152-14V427q30-39 56-81-35-59-83-108-43 20-82 47-40-37-88-64 7-51 8-102-59-28-123-42-26 43-46 89-49-7-98 0-20-46-46-89-64 14-123 42 1 51 8 102-48 27-88 64-39-27-82-47-48 49-83 108 26 42 56 81zm0 33v39c0 276 813 276 814 0v-39l-134 12-5 69q-2 10-14 13l-162 11q-12 0-16-11l-10-65H446l-10 65q-4 11-16 11l-162-11q-12-3-14-13l-5-69z"/><path d="M483 600c0 34 58 34 58 0v-86c0-34-58-34-58 0z"/><circle cx="725" cy="526" r="90"/><circle cx="299" cy="526" r="90"/></g><g fill="#414042" transform="translate(12.322 12.322)scale(.101)"><circle cx="307" cy="532" r="60"/><circle cx="717" cy="532" r="60"/></g></svg>
|
||||
|
After Width: | Height: | Size: 994 B |
37
client/icon.svg.import
Normal file
37
client/icon.svg.import
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://bqjxu2injm0kq"
|
||||
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"
|
||||
metadata={
|
||||
"vram_texture": false
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://icon.svg"
|
||||
dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"]
|
||||
|
||||
[params]
|
||||
|
||||
compress/mode=0
|
||||
compress/high_quality=false
|
||||
compress/lossy_quality=0.7
|
||||
compress/hdr_compression=1
|
||||
compress/normal_map=0
|
||||
compress/channel_pack=0
|
||||
mipmaps/generate=false
|
||||
mipmaps/limit=-1
|
||||
roughness/mode=0
|
||||
roughness/src_normal=""
|
||||
process/fix_alpha_border=true
|
||||
process/premult_alpha=false
|
||||
process/normal_map_invert_y=false
|
||||
process/hdr_as_srgb=false
|
||||
process/hdr_clamp_exposure=false
|
||||
process/size_limit=0
|
||||
detect_3d/compress_to=1
|
||||
svg/scale=1.0
|
||||
editor/scale_with_editor_scale=false
|
||||
editor/convert_colors_with_editor_theme=false
|
||||
26
client/project.godot
Normal file
26
client/project.godot
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
; Engine configuration file.
|
||||
; It's best edited using the editor UI and not directly,
|
||||
; since the parameters that go here are not all obvious.
|
||||
;
|
||||
; Format:
|
||||
; [section] ; section goes between []
|
||||
; param=value ; assign values to parameters
|
||||
|
||||
config_version=5
|
||||
|
||||
[application]
|
||||
|
||||
config/name="Massive"
|
||||
run/main_scene="uid://bukwg8y6gdt3g"
|
||||
config/features=PackedStringArray("4.4", "C#", "Forward Plus")
|
||||
config/icon="res://icon.svg"
|
||||
|
||||
[dotnet]
|
||||
|
||||
project/assembly_name="client"
|
||||
|
||||
[file_customization]
|
||||
|
||||
folder_colors={
|
||||
"res://module_bindings/": "gray"
|
||||
}
|
||||
9
client/root.tscn
Normal file
9
client/root.tscn
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://bukwg8y6gdt3g"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://cqmy41vtnqd6f" path="res://chat/ChatWindow.tscn" id="1_pq8q7"]
|
||||
[ext_resource type="Script" uid="uid://bg6n0cm6dds7j" path="res://Game.cs" id="1_pyidc"]
|
||||
|
||||
[node name="Root" type="Node"]
|
||||
script = ExtResource("1_pyidc")
|
||||
|
||||
[node name="Chat Window" parent="." instance=ExtResource("1_pq8q7")]
|
||||
86
client/spacetime/Spacetime.cs
Normal file
86
client/spacetime/Spacetime.cs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
using System;
|
||||
using Godot;
|
||||
using SpacetimeDB;
|
||||
using SpacetimeDB.Types;
|
||||
|
||||
public class Spacetime
|
||||
{
|
||||
/// 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";
|
||||
|
||||
public static Spacetime Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
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
|
||||
{
|
||||
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)
|
||||
{
|
||||
GD.PrintErr($"Disconnected abnormally: {e}");
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.Print($"Disconnected normally.");
|
||||
}
|
||||
}
|
||||
}
|
||||
1
client/spacetime/Spacetime.cs.uid
Normal file
1
client/spacetime/Spacetime.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dug30f321xvvt
|
||||
|
|
@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.0.31903.59
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "client", "client\client.csproj", "{BDFCBF69-C44E-4188-A9FC-2CFED6EC2246}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StdbModule", "server\StdbModule.csproj", "{25B51793-082D-44ED-A8D2-4A87A11F1882}"
|
||||
EndProject
|
||||
Global
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue