add color to server
This commit is contained in:
parent
2caf0d4d91
commit
b216e22d10
1 changed files with 27 additions and 1 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using SpacetimeDB;
|
using SpacetimeDB;
|
||||||
|
|
||||||
public static partial class Module
|
public static partial class Module
|
||||||
|
|
@ -8,6 +9,7 @@ public static partial class Module
|
||||||
[PrimaryKey]
|
[PrimaryKey]
|
||||||
public Identity Identity;
|
public Identity Identity;
|
||||||
public string? Name;
|
public string? Name;
|
||||||
|
public string? Color;
|
||||||
public bool Online;
|
public bool Online;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,6 +44,31 @@ public static partial class Module
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Reducer]
|
||||||
|
public static void SetColor(ReducerContext ctx, string color)
|
||||||
|
{
|
||||||
|
color = ValidateColor(color);
|
||||||
|
|
||||||
|
var user = ctx.Db.User.Identity.Find(ctx.Sender);
|
||||||
|
|
||||||
|
if (user is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
user.Color = color;
|
||||||
|
ctx.Db.User.Identity.Update(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string ValidateColor(string color)
|
||||||
|
{
|
||||||
|
var regex = new Regex("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
|
||||||
|
if (regex.IsMatch(color))
|
||||||
|
{
|
||||||
|
throw new Exception("Invalid color code");
|
||||||
|
}
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
[Reducer]
|
[Reducer]
|
||||||
public static void SendMessage(ReducerContext ctx, string text)
|
public static void SendMessage(ReducerContext ctx, string text)
|
||||||
{
|
{
|
||||||
|
|
@ -113,4 +140,3 @@ public static partial class Module
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue