208 lines
5.1 KiB
Zig
208 lines
5.1 KiB
Zig
|
|
const std = @import("std");
|
||
|
|
const App = @import("../../app.zig");
|
||
|
|
|
||
|
|
const event = @import("event.zig");
|
||
|
|
const resource = @import("resource.zig");
|
||
|
|
const component = @import("component.zig");
|
||
|
|
|
||
|
|
/// ----------------------------------------------------
|
||
|
|
/// ----------------------------------------------------
|
||
|
|
pub fn test_resources(
|
||
|
|
ctx: App.scheduler.Context,
|
||
|
|
) !void {
|
||
|
|
// --- SET NEW ---
|
||
|
|
try ctx.app.resources.set(resource.Server{});
|
||
|
|
try ctx.app.resources.set(resource.Client{});
|
||
|
|
|
||
|
|
// --- CONTAINS DEBUG ---
|
||
|
|
std.debug.print(
|
||
|
|
"\x1b[34mRESOURCES EXIST\x1b[0m\n" ++
|
||
|
|
\\| Server: {}
|
||
|
|
\\| Client: {}
|
||
|
|
\\
|
||
|
|
,
|
||
|
|
.{
|
||
|
|
ctx.app.resources.contains(resource.Server),
|
||
|
|
ctx.app.resources.contains(resource.Client),
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
// --- GET READONLY RESOURCES ---
|
||
|
|
_ = try ctx.app.resources.getMany(struct {
|
||
|
|
server: ?*const resource.Server,
|
||
|
|
client: ?*const resource.Client,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const User = struct {
|
||
|
|
id: u32 = 0,
|
||
|
|
username: []const u8,
|
||
|
|
password: []const u8,
|
||
|
|
};
|
||
|
|
|
||
|
|
/// ----------------------------------------------------
|
||
|
|
/// ----------------------------------------------------
|
||
|
|
pub fn test_events(
|
||
|
|
ctx: App.scheduler.Context,
|
||
|
|
) !void {
|
||
|
|
// --- DEBUG ---
|
||
|
|
std.debug.print(
|
||
|
|
"\x1b[35mEVENTS\x1b[0m\n",
|
||
|
|
.{},
|
||
|
|
);
|
||
|
|
|
||
|
|
// --- CLEAR EVENTS ---
|
||
|
|
defer ctx.app.events.clear();
|
||
|
|
|
||
|
|
// --- USER LIST ---
|
||
|
|
var users: std.ArrayList(User) = .empty;
|
||
|
|
defer users.deinit(ctx.app.alloc);
|
||
|
|
|
||
|
|
// --- ID`S ---
|
||
|
|
var next_register_id: u32 = 0;
|
||
|
|
var next_connected_id: u32 = 0;
|
||
|
|
|
||
|
|
// --- ADD ADMIN USER ---
|
||
|
|
try users.append(ctx.app.alloc, .{
|
||
|
|
.id = next_register_id,
|
||
|
|
.username = "admin",
|
||
|
|
.password = "1234",
|
||
|
|
});
|
||
|
|
|
||
|
|
{ // NEW USER
|
||
|
|
try ctx.app.events.sendMany(.{
|
||
|
|
event.Register{
|
||
|
|
.username = "abux",
|
||
|
|
.password = "qwerty",
|
||
|
|
},
|
||
|
|
event.Login{
|
||
|
|
.username = "abux",
|
||
|
|
.password = "qwerty",
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- REGISTER LOOP ---
|
||
|
|
if (ctx.app.events.get(event.Register)) |queue| {
|
||
|
|
for (queue.items()) |reg| {
|
||
|
|
next_register_id += 1;
|
||
|
|
try users.append(ctx.app.alloc, .{
|
||
|
|
.id = next_register_id,
|
||
|
|
.username = reg.username,
|
||
|
|
.password = reg.password,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
queue.clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- LOGIN LOOP ---
|
||
|
|
if (ctx.app.events.get(event.Login)) |queue| {
|
||
|
|
for (queue.items()) |login| {
|
||
|
|
for (users.items) |user| {
|
||
|
|
// --- CHECK CREDS ---
|
||
|
|
if (!std.mem.eql(u8, user.username, login.username)) continue;
|
||
|
|
if (!std.mem.eql(u8, user.password, login.password)) continue;
|
||
|
|
|
||
|
|
// --- SEND ACCEPTED CONNECTION ---
|
||
|
|
next_connected_id += 1;
|
||
|
|
try ctx.app.events.send(event.ConnectionAccepted{
|
||
|
|
.id = next_connected_id,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
queue.clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- CONNECTED ---
|
||
|
|
if (ctx.app.events.consume(event.ConnectionAccepted)) |ca| {
|
||
|
|
std.debug.print(
|
||
|
|
\\ | Connected:
|
||
|
|
\\ | ID: {}
|
||
|
|
\\
|
||
|
|
,
|
||
|
|
.{
|
||
|
|
ca.id,
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- DEBUG ---
|
||
|
|
std.debug.print(
|
||
|
|
"\x1b[31mUSERS\x1b[0m\n",
|
||
|
|
.{},
|
||
|
|
);
|
||
|
|
for (users.items) |user| {
|
||
|
|
std.debug.print(
|
||
|
|
\\ | ID: {}
|
||
|
|
\\ | Username: {s}
|
||
|
|
\\ | Password: {s}
|
||
|
|
\\
|
||
|
|
,
|
||
|
|
.{
|
||
|
|
user.id,
|
||
|
|
user.username,
|
||
|
|
user.password,
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// ----------------------------------------------------
|
||
|
|
/// ----------------------------------------------------
|
||
|
|
pub fn test_ecs(
|
||
|
|
ctx: App.scheduler.Context,
|
||
|
|
) !void {
|
||
|
|
// --- SPAWN ENTITY ---
|
||
|
|
try ctx.app.world.spawn(.{
|
||
|
|
component.Player{ .name = "abux" },
|
||
|
|
component.Material{},
|
||
|
|
component.Transform{},
|
||
|
|
});
|
||
|
|
try ctx.app.world.spawn(.{
|
||
|
|
component.Material{},
|
||
|
|
component.Transform{},
|
||
|
|
});
|
||
|
|
|
||
|
|
// --- DEBUG ---
|
||
|
|
std.debug.print(
|
||
|
|
"\x1b[35mENTITIES\x1b[0m\n",
|
||
|
|
.{},
|
||
|
|
);
|
||
|
|
|
||
|
|
// --- QUERY ---
|
||
|
|
var query = ctx.app.world.query(struct {
|
||
|
|
player: ?*component.Player,
|
||
|
|
material: *component.Material,
|
||
|
|
transform: *component.Transform,
|
||
|
|
}, .{});
|
||
|
|
while (query.next()) |e| {
|
||
|
|
// --- UPDATE POS ---
|
||
|
|
e.transform.pos[0] += 1;
|
||
|
|
e.transform.pos[1] += 1;
|
||
|
|
|
||
|
|
// --- CHANGE PLAYER'S COLOR ---
|
||
|
|
if (e.player) |_| {
|
||
|
|
e.material.albedo = .{ 20, 200, 20, 255 };
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- ENTITIES DEBUG ---
|
||
|
|
std.debug.print("\x1b[36m", .{});
|
||
|
|
std.debug.print(
|
||
|
|
\\| ID: {}
|
||
|
|
\\| Components:
|
||
|
|
\\ | Player: {?f}
|
||
|
|
\\ | Material: {f}
|
||
|
|
\\ | Transform: {f}
|
||
|
|
\\
|
||
|
|
,
|
||
|
|
.{
|
||
|
|
query.entity(),
|
||
|
|
e.player,
|
||
|
|
e.material,
|
||
|
|
e.transform,
|
||
|
|
},
|
||
|
|
);
|
||
|
|
std.debug.print("\x1b[0m", .{});
|
||
|
|
}
|
||
|
|
}
|