Changing syntax and union support

This commit is contained in:
ookami125 2025-03-25 16:07:29 -04:00
parent 2331a29358
commit acadefcaa5
4 changed files with 408 additions and 303 deletions

View file

@ -6,69 +6,64 @@ pub export fn spacetime_includes() void {
_ = &spacetime.__call_reducer__;
}
pub const moduleTablesDef = .{
.person = spacetime.Table(.{.name = "person", .layout = Person}),
.person2 = spacetime.Table(.{.name = "person2", .layout = Person}),
pub const DbVector2 = struct {
x: f32,
y: f32,
};
pub const moduleReducersDef = .{
.Init = spacetime.Reducer(Init){ .lifecycle = .Init },
.OnConnect = spacetime.Reducer(OnConnect){ .lifecycle = .OnConnect },
.OnDisconnect = spacetime.Reducer(OnDisconnect){ .lifecycle = .OnDisconnect },
.add = spacetime.Reducer(add){ .param_names = &[_][:0]const u8{ "name" }},
.say_hello = spacetime.Reducer(say_hello){},
pub const person: spacetime.Table = .{ .schema = Person, };
pub const Person = struct{
name: []const u8,
pos: DbVector2,
schedule: spacetime.ScheduleAt,
};
pub const DbVector2 = spacetime.Struct(.{
.name = "DbVector2",
.fields = &[_]spacetime.StructFieldDecl{
.{ .name = "x", .type = f32, },
.{ .name = "y", .type = f32 },
},
});
pub const Init: spacetime.Reducer = .{
.func_type = @TypeOf(InitReducer),
.func = @ptrCast(&InitReducer),
.lifecycle = .Init,
};
pub const Person = spacetime.Struct(.{
.name = "person",
.fields = &[_]spacetime.StructFieldDecl{
.{ .name = "name", .type = []const u8, },
.{ .name = "pos", .type = DbVector2, },
},
});
pub fn Init(ctx: *spacetime.ReducerContext) void {
pub fn InitReducer(_: *spacetime.ReducerContext) void {
// Called when the module is initially published
_ = ctx;
spacetime.print("[Init]");
}
pub fn OnConnect(ctx: *spacetime.ReducerContext) void {
pub const OnConnect = spacetime.Reducer{ .func_type = @TypeOf(OnConnectReducer), .func = @ptrCast(&OnConnectReducer), .lifecycle = .OnConnect, };
pub fn OnConnectReducer(_: *spacetime.ReducerContext) void {
// Called everytime a new client connects
_ = ctx;
spacetime.print("[OnConnect]");
}
pub fn OnDisconnect(ctx: *spacetime.ReducerContext) void {
pub const OnDisconnect = spacetime.Reducer{ .func_type = @TypeOf(OnDisconnectReducer), .func = @ptrCast(&OnDisconnectReducer), .lifecycle = .OnDisconnect, };
pub fn OnDisconnectReducer(_: *spacetime.ReducerContext) void {
// Called everytime a client disconnects
_ = ctx;
spacetime.print("[OnDisconnect]");
}
pub fn add(ctx: *spacetime.ReducerContext, name: []const u8) void {
const personTable = ctx.*.db.get(moduleTablesDef.person);
personTable.insert(Person{ .name = name, .pos = DbVector2{ .x = 10.4, .y = 20.6 } });
pub const add = spacetime.Reducer{ .func_type = @TypeOf(addReducer), .func = @ptrCast(&addReducer), .params = &.{ "name", "time" }};
pub fn addReducer(ctx: *spacetime.ReducerContext, name: []const u8, time: i64) void {
const personTable = ctx.*.db.get("person");
personTable.insert(Person{
.name = name,
.pos = DbVector2{ .x = 10.4, .y = 20.6 },
.schedule = .{ .Interval = .{ .__time_duration_micros__ = time, }, },
});
var buf: [128]u8 = undefined;
spacetime.print(std.fmt.bufPrint(&buf, "[add] {{{s}}}!", .{ name }) catch "[add] Error: name to long");
}
pub fn say_hello(ctx: *spacetime.ReducerContext) void {
var personIter = ctx.*.db.get(moduleTablesDef.person).iter();
while(personIter.next() catch {
@panic("person Iter errored!");
}) |person| {
var buffer: [512]u8 = undefined;
const msg = std.fmt.bufPrint(&buffer, "Hello, {s} (pos: {{{d}, {d}}})!", .{ person.name, person.pos.x, person.pos.y }) catch "<Unknown>";
spacetime.print(msg);
}
spacetime.print("Hello, World!");
pub const say_hello = spacetime.Reducer{ .func_type = @TypeOf(say_helloReducer), .func = @ptrCast(&say_helloReducer), };
pub fn say_helloReducer(ctx: *spacetime.ReducerContext) void {
//_ = ctx;
var personIter = ctx.*.db.get("person").iter();
while(personIter.next() catch {
@panic("person Iter errored!");
}) |item| {
var buffer: [512]u8 = undefined;
const msg = std.fmt.bufPrint(&buffer, "Hello, {s} (pos: {{{d}, {d}}}) (time: {})!", .{ item.name, item.pos.x, item.pos.y, item.schedule.Interval.__time_duration_micros__ }) catch "<Unknown>";
spacetime.print(msg);
}
spacetime.print("Hello, World!");
}