introduce zod schemas
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
import { loginUser, getOpenToken } from '@/lib/api/server/auth';
|
||||
import { login, logout } from '@/lib/auth/server';
|
||||
import { loginSchema, loginResponseSchema } from '../schemas';
|
||||
|
||||
export type LoginResult = {
|
||||
success: boolean;
|
||||
@@ -14,18 +15,19 @@ export async function loginAction(prevState: LoginResult, formData: FormData) {
|
||||
const username = formData.get('username') as string;
|
||||
const password = formData.get('password') as string;
|
||||
|
||||
if (!username || !password) {
|
||||
const loginData = loginSchema.safeParse({ username, password });
|
||||
if (!loginData.success) {
|
||||
return {
|
||||
success: false,
|
||||
message: 'Username and password are required'
|
||||
message: loginData.error.message
|
||||
};
|
||||
}
|
||||
|
||||
const result = await loginUser(username, password);
|
||||
const result = loginResponseSchema.safeParse(await loginUser(loginData.data));
|
||||
|
||||
if (result.token && result.user) {
|
||||
const openToken = await getOpenToken(result.token);
|
||||
await login(result.token, result.user, openToken);
|
||||
if (result.success) {
|
||||
const openToken = await getOpenToken(result.data.token);
|
||||
await login(result.data.token, result.data.user, openToken);
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
|
||||
@@ -3,14 +3,20 @@
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { requireAuth } from '@/lib/auth/server';
|
||||
import { updateServerConfiguration } from '@/lib/api/server/configuration';
|
||||
import { ConfigFile } from '@/lib/types/config';
|
||||
import {
|
||||
assistRulesSchema,
|
||||
ConfigFile,
|
||||
eventConfigSchema,
|
||||
eventRulesSchema,
|
||||
serverSettingsSchema
|
||||
} from '@/lib/schemas/config';
|
||||
import type {
|
||||
Configuration,
|
||||
AssistRules,
|
||||
EventConfig,
|
||||
EventRules,
|
||||
ServerSettings
|
||||
} from '@/lib/types/config';
|
||||
} from '@/lib/schemas/config';
|
||||
|
||||
export async function updateConfigurationAction(serverId: string, formData: FormData) {
|
||||
try {
|
||||
@@ -49,7 +55,7 @@ export async function updateAssistRulesAction(serverId: string, formData: FormDa
|
||||
const session = await requireAuth();
|
||||
const restart = formData.get('restart') === 'on';
|
||||
|
||||
const config: AssistRules = {
|
||||
const rawConfig: AssistRules = {
|
||||
stabilityControlLevelMax: parseInt(formData.get('stabilityControlLevelMax') as string),
|
||||
disableAutosteer: parseInt(formData.get('disableAutosteer') as string),
|
||||
disableAutoLights: parseInt(formData.get('disableAutoLights') as string),
|
||||
@@ -61,11 +67,16 @@ export async function updateAssistRulesAction(serverId: string, formData: FormDa
|
||||
disableIdealLine: parseInt(formData.get('disableIdealLine') as string)
|
||||
};
|
||||
|
||||
const config = assistRulesSchema.safeParse(rawConfig);
|
||||
if (!config.success) {
|
||||
return { success: false, message: config.error.message };
|
||||
}
|
||||
|
||||
await updateServerConfiguration(
|
||||
session.token!,
|
||||
serverId,
|
||||
ConfigFile.assistRules,
|
||||
config,
|
||||
config.data,
|
||||
restart
|
||||
);
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
@@ -84,7 +95,7 @@ export async function updateServerSettingsAction(serverId: string, formData: For
|
||||
const session = await requireAuth();
|
||||
const restart = formData.get('restart') === 'on';
|
||||
|
||||
const config: ServerSettings = {
|
||||
const rawConfig: ServerSettings = {
|
||||
serverName: formData.get('serverName') as string,
|
||||
adminPassword: formData.get('adminPassword') as string,
|
||||
carGroup: formData.get('carGroup') as string,
|
||||
@@ -103,8 +114,18 @@ export async function updateServerSettingsAction(serverId: string, formData: For
|
||||
formationLapType: parseInt(formData.get('formationLapType') as string),
|
||||
ignorePrematureDisconnects: parseInt(formData.get('ignorePrematureDisconnects') as string)
|
||||
};
|
||||
const config = serverSettingsSchema.safeParse(rawConfig);
|
||||
if (!config.success) {
|
||||
return { success: false, message: config.error.message };
|
||||
}
|
||||
|
||||
await updateServerConfiguration(session.token!, serverId, ConfigFile.settings, config, restart);
|
||||
await updateServerConfiguration(
|
||||
session.token!,
|
||||
serverId,
|
||||
ConfigFile.settings,
|
||||
config.data,
|
||||
restart
|
||||
);
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
|
||||
return { success: true, message: 'Server settings updated successfully' };
|
||||
@@ -124,7 +145,7 @@ export async function updateEventConfigAction(serverId: string, formData: FormDa
|
||||
const sessionsData = formData.get('sessions') as string;
|
||||
const sessions = sessionsData ? JSON.parse(sessionsData) : [];
|
||||
|
||||
const config: EventConfig = {
|
||||
const rawConfig: EventConfig = {
|
||||
track: formData.get('track') as string,
|
||||
preRaceWaitingTimeSeconds: parseInt(formData.get('preRaceWaitingTimeSeconds') as string),
|
||||
sessionOverTimeSeconds: parseInt(formData.get('sessionOverTimeSeconds') as string),
|
||||
@@ -140,8 +161,18 @@ export async function updateEventConfigAction(serverId: string, formData: FormDa
|
||||
),
|
||||
sessions
|
||||
};
|
||||
const config = eventConfigSchema.safeParse(rawConfig);
|
||||
if (!config.success) {
|
||||
return { success: false, message: config.error.message };
|
||||
}
|
||||
|
||||
await updateServerConfiguration(session.token!, serverId, ConfigFile.event, config, restart);
|
||||
await updateServerConfiguration(
|
||||
session.token!,
|
||||
serverId,
|
||||
ConfigFile.event,
|
||||
config.data,
|
||||
restart
|
||||
);
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
|
||||
return {
|
||||
@@ -161,7 +192,7 @@ export async function updateEventRulesAction(serverId: string, formData: FormDat
|
||||
const session = await requireAuth();
|
||||
const restart = formData.get('restart') === 'on';
|
||||
|
||||
const config: EventRules = {
|
||||
const rawConfig: EventRules = {
|
||||
qualifyStandingType: parseInt(formData.get('qualifyStandingType') as string),
|
||||
pitWindowLengthSec: parseInt(formData.get('pitWindowLengthSec') as string),
|
||||
driverStintTimeSec: parseInt(formData.get('driverStintTimeSec') as string),
|
||||
@@ -178,11 +209,16 @@ export async function updateEventRulesAction(serverId: string, formData: FormDat
|
||||
tyreSetCount: parseInt(formData.get('tyreSetCount') as string)
|
||||
};
|
||||
|
||||
const config = eventRulesSchema.safeParse(rawConfig);
|
||||
if (!config.success) {
|
||||
return { success: false, message: config.error.message };
|
||||
}
|
||||
|
||||
await updateServerConfiguration(
|
||||
session.token!,
|
||||
serverId,
|
||||
ConfigFile.eventRules,
|
||||
config,
|
||||
config.data,
|
||||
restart
|
||||
);
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { requireAuth } from '@/lib/auth/server';
|
||||
import { createUser, deleteUser } from '@/lib/api/server/membership';
|
||||
import { userCreateSchema } from '../schemas';
|
||||
|
||||
export async function createUserAction(formData: FormData) {
|
||||
try {
|
||||
@@ -11,7 +12,13 @@ export async function createUserAction(formData: FormData) {
|
||||
const password = formData.get('password') as string;
|
||||
const role = formData.get('role') as string;
|
||||
|
||||
await createUser(session.token!, { username, password, role });
|
||||
const rawData = { username, password, role };
|
||||
const data = userCreateSchema.safeParse(rawData);
|
||||
if (!data.success) {
|
||||
return { success: false, message: data.error.message };
|
||||
}
|
||||
|
||||
await createUser(session.token!, data.data);
|
||||
revalidatePath('/dashboard/membership');
|
||||
|
||||
return { success: true, message: 'User created successfully' };
|
||||
|
||||
@@ -1,17 +1,26 @@
|
||||
import { User } from '@/lib/types';
|
||||
import {
|
||||
Login,
|
||||
LoginResponse,
|
||||
loginResponseSchema,
|
||||
loginSchema,
|
||||
loginTokenSchema,
|
||||
User,
|
||||
userSchema
|
||||
} from '@/lib/schemas';
|
||||
import { fetchServerAPI } from './base';
|
||||
|
||||
const BASE_URL = process.env.API_BASE_URL || 'http://localhost:8080';
|
||||
|
||||
const authRoute = '/auth';
|
||||
|
||||
export async function loginUser(username: string, password: string) {
|
||||
export async function loginUser(login: Login): Promise<LoginResponse> {
|
||||
const validatedLogin = loginSchema.parse(login);
|
||||
const response = await fetch(`${BASE_URL}${authRoute}/login`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ username, password })
|
||||
body: JSON.stringify(validatedLogin)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -22,16 +31,18 @@ export async function loginUser(username: string, password: string) {
|
||||
throw new Error(`Login failed: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const { token } = await response.json();
|
||||
const rawData = await response.json();
|
||||
|
||||
const { token } = loginTokenSchema.parse(rawData);
|
||||
|
||||
const userResponse = await getCurrentUser(token);
|
||||
|
||||
return { token, user: userResponse };
|
||||
return loginResponseSchema.parse({ token, user: userResponse });
|
||||
}
|
||||
|
||||
export async function getCurrentUser(token: string): Promise<User> {
|
||||
const response = await fetchServerAPI<User>(`${authRoute}/me`, token);
|
||||
return response.data!;
|
||||
return userSchema.parse(response.data);
|
||||
}
|
||||
|
||||
export async function getOpenToken(token: string): Promise<string> {
|
||||
@@ -40,5 +51,5 @@ export async function getOpenToken(token: string): Promise<string> {
|
||||
token,
|
||||
'POST'
|
||||
);
|
||||
return response.data!.token;
|
||||
return loginTokenSchema.parse(response.data).token;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
import { fetchServerAPI } from './base';
|
||||
import type { Configurations, ConfigFile, Config } from '@/lib/types/config';
|
||||
import {
|
||||
type Configurations,
|
||||
type Config,
|
||||
ConfigFile,
|
||||
configurationsSchema,
|
||||
configSchemaMap
|
||||
} from '@/lib/schemas/config';
|
||||
import * as z from 'zod';
|
||||
|
||||
const serverRoute = '/server';
|
||||
|
||||
@@ -8,7 +15,15 @@ export async function getServerConfigurations(
|
||||
serverId: string
|
||||
): Promise<Configurations> {
|
||||
const response = await fetchServerAPI<Configurations>(`${serverRoute}/${serverId}/config`, token);
|
||||
return response.data!;
|
||||
return configurationsSchema.parse(response.data);
|
||||
}
|
||||
|
||||
export function validateConfig(
|
||||
configType: ConfigFile,
|
||||
data: unknown
|
||||
): z.infer<(typeof configSchemaMap)[typeof configType]> {
|
||||
const schema = configSchemaMap[configType];
|
||||
return schema.parse(data);
|
||||
}
|
||||
|
||||
export async function getServerConfiguration(
|
||||
@@ -20,7 +35,7 @@ export async function getServerConfiguration(
|
||||
`${serverRoute}/${serverId}/config/${configType}`,
|
||||
token
|
||||
);
|
||||
return response.data!;
|
||||
return validateConfig(configType, response.data);
|
||||
}
|
||||
|
||||
export async function updateServerConfiguration(
|
||||
@@ -31,7 +46,7 @@ export async function updateServerConfiguration(
|
||||
restart = false
|
||||
): Promise<void> {
|
||||
await fetchServerAPI(`${serverRoute}/${serverId}/config/${configType}`, token, 'PUT', {
|
||||
...config,
|
||||
...validateConfig(configType, config),
|
||||
restart
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,21 +1,32 @@
|
||||
import { fetchServerAPI } from './base';
|
||||
import { Track, CarModel, CupCategory, DriverCategory, SessionType } from '@/lib/types';
|
||||
import {
|
||||
Track,
|
||||
CarModel,
|
||||
CupCategory,
|
||||
DriverCategory,
|
||||
SessionType,
|
||||
trackSchema,
|
||||
carModelSchema,
|
||||
cupCategorySchema,
|
||||
driverCategorySchema,
|
||||
sessionTypeSchema
|
||||
} from '@/lib/schemas';
|
||||
|
||||
const lookupRoute = '/lookup';
|
||||
|
||||
export async function getTracks(token: string): Promise<Track[]> {
|
||||
const response = await fetchServerAPI<Track[]>(`${lookupRoute}/tracks`, token);
|
||||
return response.data!;
|
||||
return trackSchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
export async function getCarModels(token: string): Promise<CarModel[]> {
|
||||
const response = await fetchServerAPI<CarModel[]>(`${lookupRoute}/car-models`, token);
|
||||
return response.data!;
|
||||
return carModelSchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
export async function getCupCategories(token: string): Promise<CupCategory[]> {
|
||||
const response = await fetchServerAPI<CupCategory[]>(`${lookupRoute}/cup-categories`, token);
|
||||
return response.data!;
|
||||
return cupCategorySchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
export async function getDriverCategories(token: string): Promise<DriverCategory[]> {
|
||||
@@ -23,10 +34,10 @@ export async function getDriverCategories(token: string): Promise<DriverCategory
|
||||
`${lookupRoute}/driver-categories`,
|
||||
token
|
||||
);
|
||||
return response.data!;
|
||||
return driverCategorySchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
export async function getSessionTypes(token: string): Promise<SessionType[]> {
|
||||
const response = await fetchServerAPI<SessionType[]>(`${lookupRoute}/session-types`, token);
|
||||
return response.data!;
|
||||
return sessionTypeSchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { use } from 'react';
|
||||
import { fetchServerAPI } from './base';
|
||||
import { User, Role } from '@/lib/types';
|
||||
import { User, Role, userSchema, UserCreate, userCreateSchema, roleSchema } from '@/lib/schemas';
|
||||
|
||||
export interface UserListParams {
|
||||
username?: string;
|
||||
@@ -25,27 +26,29 @@ export async function getUsers(token: string, params: UserListParams = {}): Prom
|
||||
const endpoint = `${membershipRoute}${queryString ? `?${queryString}` : ''}`;
|
||||
|
||||
const response = await fetchServerAPI<User[]>(endpoint, token);
|
||||
return response.data!;
|
||||
return userSchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
export async function createUser(
|
||||
token: string,
|
||||
userData: { username: string; password: string; role: string }
|
||||
): Promise<void> {
|
||||
await fetchServerAPI(membershipRoute, token, 'POST', userData);
|
||||
export async function createUser(token: string, userData: UserCreate): Promise<void> {
|
||||
await fetchServerAPI(membershipRoute, token, 'POST', userCreateSchema.parse(userData));
|
||||
}
|
||||
|
||||
export async function getUserById(token: string, userId: string): Promise<User> {
|
||||
const response = await fetchServerAPI<User>(`${membershipRoute}/${userId}`, token);
|
||||
return response.data!;
|
||||
return userSchema.parse(response.data);
|
||||
}
|
||||
|
||||
export async function updateUser(
|
||||
token: string,
|
||||
userId: string,
|
||||
userData: Partial<User>
|
||||
userData: Partial<UserCreate>
|
||||
): Promise<void> {
|
||||
await fetchServerAPI(`${membershipRoute}/${userId}`, token, 'PUT', userData);
|
||||
await fetchServerAPI(
|
||||
`${membershipRoute}/${userId}`,
|
||||
token,
|
||||
'PUT',
|
||||
userCreateSchema.parse(userData)
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteUser(token: string, userId: string): Promise<void> {
|
||||
@@ -54,5 +57,5 @@ export async function deleteUser(token: string, userId: string): Promise<void> {
|
||||
|
||||
export async function getRoles(token: string): Promise<Role[]> {
|
||||
const response = await fetchServerAPI<Role[]>(`${membershipRoute}/roles`, token);
|
||||
return response.data!;
|
||||
return roleSchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { fetchServerAPI } from './base';
|
||||
import { Server, ServiceStatus } from '@/lib/types/server';
|
||||
import { Server, serverSchema, ServiceStatus, serviceStatusSchema } from '@/lib/schemas/server';
|
||||
|
||||
const serverRoute = '/server';
|
||||
|
||||
export async function getServers(token: string): Promise<Server[]> {
|
||||
const response = await fetchServerAPI<Server[]>(serverRoute, token);
|
||||
return response.data!;
|
||||
return serverSchema.array().parse(response.data);
|
||||
}
|
||||
|
||||
export async function getServer(token: string, serverId: string): Promise<Server> {
|
||||
const response = await fetchServerAPI<Server>(`${serverRoute}/${serverId}`, token);
|
||||
return response.data!;
|
||||
return serverSchema.parse(response.data);
|
||||
}
|
||||
|
||||
export async function restartService(token: string, serverId: string): Promise<void> {
|
||||
@@ -27,12 +27,12 @@ export async function stopService(token: string, serverId: string): Promise<void
|
||||
|
||||
export async function getServiceStatus(token: string, serverId: string): Promise<ServiceStatus> {
|
||||
const response = await fetchServerAPI<ServiceStatus>(`${serverRoute}/${serverId}/service`, token);
|
||||
return response.data!;
|
||||
return serviceStatusSchema.parse(response.data);
|
||||
}
|
||||
|
||||
export async function createServer(token: string, name: string): Promise<Server> {
|
||||
const response = await fetchServerAPI<Server>(serverRoute, token, 'POST', { name });
|
||||
return response.data!;
|
||||
return serverSchema.parse(response.data);
|
||||
}
|
||||
|
||||
export async function deleteServer(token: string, serverId: string): Promise<void> {
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
import { fetchServerAPI } from './base';
|
||||
import type { StateHistoryStats } from '@/lib/types/statistics';
|
||||
import {
|
||||
StateHistoryStatsFilter,
|
||||
stateHistoryStatsFilterSchema,
|
||||
stateHistoryStatsSchema,
|
||||
type StateHistoryStats
|
||||
} from '@/lib/schemas/statistics';
|
||||
|
||||
const serverRoute = '/server';
|
||||
|
||||
export async function getServerStatistics(
|
||||
token: string,
|
||||
serverId: string,
|
||||
startDate: string,
|
||||
endDate: string
|
||||
filters: StateHistoryStatsFilter
|
||||
): Promise<StateHistoryStats> {
|
||||
const { startDate, endDate } = stateHistoryStatsFilterSchema.parse(filters);
|
||||
const response = await fetchServerAPI<StateHistoryStats>(
|
||||
`${serverRoute}/${serverId}/state-history/statistics?start_date=${startDate}&end_date=${endDate}`,
|
||||
token
|
||||
);
|
||||
return response.data!;
|
||||
return stateHistoryStatsSchema.parse(response.data);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { createContext, useContext, useState, ReactNode, useEffect, useCallback } from 'react';
|
||||
import { useWebSocket } from '@/lib/websocket/context';
|
||||
import { WebSocketMessage, StepData } from '@/lib/websocket/client';
|
||||
import type { WebSocketMessage, StepData } from '@/lib/schemas/websocket';
|
||||
|
||||
interface SteamCMDContextType {
|
||||
isSteamCMDRunning: boolean;
|
||||
|
||||
130
src/lib/schemas/config.ts
Normal file
130
src/lib/schemas/config.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
export enum ConfigFile {
|
||||
configuration = 'configuration.json',
|
||||
assistRules = 'assistRules.json',
|
||||
event = 'event.json',
|
||||
eventRules = 'eventRules.json',
|
||||
settings = 'settings.json'
|
||||
}
|
||||
export const configFileSchema = z.enum(ConfigFile);
|
||||
|
||||
export enum ServerTab {
|
||||
statistics = 'statistics',
|
||||
configuration = 'configuration',
|
||||
assistRules = 'assistRules',
|
||||
event = 'event',
|
||||
eventRules = 'eventRules',
|
||||
settings = 'settings'
|
||||
}
|
||||
export const serverTabSchema = z.enum(ServerTab);
|
||||
|
||||
export const configurationSchema = z.object({
|
||||
udpPort: z.number().min(1025).max(65535),
|
||||
tcpPort: z.number().min(1025).max(65535),
|
||||
maxConnections: z.number().min(1).max(64),
|
||||
lanDiscovery: z.number().min(0).max(1),
|
||||
registerToLobby: z.number().min(0).max(2),
|
||||
configVersion: z.number().min(1).max(2).default(1)
|
||||
});
|
||||
|
||||
export type Configuration = z.infer<typeof configurationSchema>;
|
||||
|
||||
export const assistRulesSchema = z.object({
|
||||
stabilityControlLevelMax: z.number().min(0).max(3),
|
||||
disableAutosteer: z.number().min(0).max(1),
|
||||
disableAutoLights: z.number().min(0).max(1),
|
||||
disableAutoWiper: z.number().min(0).max(1),
|
||||
disableAutoEngineStart: z.number().min(0).max(1),
|
||||
disableAutoPitLimiter: z.number().min(0).max(1),
|
||||
disableAutoGear: z.number().min(0).max(1),
|
||||
disableAutoClutch: z.number().min(0).max(1),
|
||||
disableIdealLine: z.number().min(0).max(1)
|
||||
});
|
||||
|
||||
export type AssistRules = z.infer<typeof assistRulesSchema>;
|
||||
|
||||
export const serverSettingsSchema = z.object({
|
||||
serverName: z.string().min(3).max(50),
|
||||
adminPassword: z.string().min(6).max(50),
|
||||
carGroup: z.string().min(1).max(50),
|
||||
trackMedalsRequirement: z.number().min(0).max(3),
|
||||
safetyRatingRequirement: z.number().min(0).max(5),
|
||||
racecraftRatingRequirement: z.number().min(0).max(5),
|
||||
password: z.string().max(50).optional().or(z.literal('')),
|
||||
spectatorPassword: z.string().max(50).optional().or(z.literal('')),
|
||||
maxCarSlots: z.number().min(1).max(64),
|
||||
dumpLeaderboards: z.number().min(0).max(1),
|
||||
isRaceLocked: z.number().min(0).max(1),
|
||||
randomizeTrackWhenEmpty: z.number().min(0).max(1),
|
||||
centralEntryListPath: z.string().max(255).optional().or(z.literal('')),
|
||||
allowAutoDQ: z.number().min(0).max(1),
|
||||
shortFormationLap: z.number().min(0).max(1),
|
||||
formationLapType: z.number().min(0).max(2),
|
||||
ignorePrematureDisconnects: z.number().min(0).max(1)
|
||||
});
|
||||
|
||||
export type ServerSettings = z.infer<typeof serverSettingsSchema>;
|
||||
|
||||
export const sessionSchema = z.object({
|
||||
hourOfDay: z.number().min(0).max(23),
|
||||
dayOfWeekend: z.number().min(0).max(6),
|
||||
timeMultiplier: z.number().min(1).max(120),
|
||||
sessionType: z.string().min(1).max(20),
|
||||
sessionDurationMinutes: z.number().min(1).max(180)
|
||||
});
|
||||
|
||||
export type Session = z.infer<typeof sessionSchema>;
|
||||
|
||||
export const eventConfigSchema = z.object({
|
||||
track: z.string().min(1).max(100),
|
||||
preRaceWaitingTimeSeconds: z.number().min(0).max(600),
|
||||
sessionOverTimeSeconds: z.number().min(0).max(300),
|
||||
ambientTemp: z.number().min(-20).max(50),
|
||||
cloudLevel: z.number().min(0).max(100),
|
||||
rain: z.number().min(0).max(100),
|
||||
weatherRandomness: z.number().min(0).max(100),
|
||||
postQualySeconds: z.number().min(0).max(600),
|
||||
postRaceSeconds: z.number().min(0).max(600),
|
||||
simracerWeatherConditions: z.number().min(0).max(2),
|
||||
isFixedConditionQualification: z.number().min(0).max(1),
|
||||
sessions: z.array(sessionSchema).min(1).max(10)
|
||||
});
|
||||
|
||||
export type EventConfig = z.infer<typeof eventConfigSchema>;
|
||||
|
||||
export const eventRulesSchema = z.object({
|
||||
qualifyStandingType: z.number().min(0).max(2),
|
||||
pitWindowLengthSec: z.number().min(0).max(3600),
|
||||
driverStintTimeSec: z.number().min(0).max(7200),
|
||||
mandatoryPitstopCount: z.number().min(0).max(10),
|
||||
maxTotalDrivingTime: z.number().min(0).max(14400),
|
||||
isRefuellingAllowedInRace: z.boolean(),
|
||||
isRefuellingTimeFixed: z.boolean(),
|
||||
isMandatoryPitstopRefuellingRequired: z.boolean(),
|
||||
isMandatoryPitstopTyreChangeRequired: z.boolean(),
|
||||
isMandatoryPitstopSwapDriverRequired: z.boolean(),
|
||||
tyreSetCount: z.number().min(0).max(20)
|
||||
});
|
||||
|
||||
export type EventRules = z.infer<typeof eventRulesSchema>;
|
||||
|
||||
export const configurationsSchema = z.object({
|
||||
configuration: configurationSchema,
|
||||
assistRules: assistRulesSchema,
|
||||
settings: serverSettingsSchema,
|
||||
event: eventConfigSchema,
|
||||
eventRules: eventRulesSchema
|
||||
});
|
||||
|
||||
export type Configurations = z.infer<typeof configurationsSchema>;
|
||||
|
||||
export const configSchemaMap = {
|
||||
[ConfigFile.configuration]: configurationSchema,
|
||||
[ConfigFile.assistRules]: assistRulesSchema,
|
||||
[ConfigFile.event]: eventConfigSchema,
|
||||
[ConfigFile.eventRules]: eventRulesSchema,
|
||||
[ConfigFile.settings]: serverSettingsSchema
|
||||
};
|
||||
|
||||
export type Config = Configuration | AssistRules | EventConfig | EventRules | ServerSettings;
|
||||
33
src/lib/schemas/lookups.ts
Normal file
33
src/lib/schemas/lookups.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
export const trackSchema = z.object({
|
||||
track: z.string(),
|
||||
uniquePitBoxes: z.number().int().nonnegative(),
|
||||
privateServerSlots: z.number().int().nonnegative()
|
||||
});
|
||||
|
||||
export type Track = z.infer<typeof trackSchema>;
|
||||
|
||||
export const carModelSchema = z.object({
|
||||
value: z.number().int().nonnegative(),
|
||||
carModel: z.string()
|
||||
});
|
||||
export type CarModel = z.infer<typeof carModelSchema>;
|
||||
|
||||
export const driverCategorySchema = z.object({
|
||||
value: z.number().int().nonnegative(),
|
||||
category: z.string()
|
||||
});
|
||||
export type DriverCategory = z.infer<typeof driverCategorySchema>;
|
||||
|
||||
export const cupCategorySchema = z.object({
|
||||
value: z.number().int().nonnegative(),
|
||||
category: z.string()
|
||||
});
|
||||
export type CupCategory = z.infer<typeof cupCategorySchema>;
|
||||
|
||||
export const sessionTypeSchema = z.object({
|
||||
value: z.number().int().nonnegative(),
|
||||
sessionType: z.string()
|
||||
});
|
||||
export type SessionType = z.infer<typeof sessionTypeSchema>;
|
||||
@@ -1,3 +1,5 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
export enum ServiceStatus {
|
||||
Unknown,
|
||||
Stopped,
|
||||
@@ -6,6 +8,7 @@ export enum ServiceStatus {
|
||||
Starting,
|
||||
Running
|
||||
}
|
||||
export const serviceStatusSchema = z.enum(ServiceStatus);
|
||||
|
||||
export const serviceStatusToString = (status: ServiceStatus): string => {
|
||||
switch (status) {
|
||||
@@ -41,16 +44,20 @@ export const getStatusColor = (status: ServiceStatus): string => {
|
||||
}
|
||||
};
|
||||
|
||||
interface State {
|
||||
session: string;
|
||||
playerCount: number;
|
||||
track: string;
|
||||
maxConnections: number;
|
||||
}
|
||||
export const stateSchema = z.object({
|
||||
session: z.string(),
|
||||
playerCount: z.number().min(0),
|
||||
track: z.string(),
|
||||
maxConnections: z.number().min(0)
|
||||
});
|
||||
|
||||
export interface Server {
|
||||
id: string;
|
||||
name: string;
|
||||
status: ServiceStatus;
|
||||
state: State;
|
||||
}
|
||||
export type State = z.infer<typeof stateSchema>;
|
||||
|
||||
export const serverSchema = z.object({
|
||||
id: z.uuid(),
|
||||
name: z.string().min(1),
|
||||
status: z.enum(ServiceStatus),
|
||||
state: stateSchema.optional()
|
||||
});
|
||||
|
||||
export type Server = z.infer<typeof serverSchema>;
|
||||
58
src/lib/schemas/statistics.ts
Normal file
58
src/lib/schemas/statistics.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
export const stateHistorySchema = z.object({
|
||||
dateCreated: z.string(),
|
||||
sessionStart: z.string(),
|
||||
playerCount: z.number(),
|
||||
track: z.string(),
|
||||
sessionDurationMinutes: z.number(),
|
||||
session: z.string()
|
||||
});
|
||||
export type StateHistory = z.infer<typeof stateHistorySchema>;
|
||||
|
||||
export const sessionCountSchema = z.object({
|
||||
name: z.string(),
|
||||
count: z.number()
|
||||
});
|
||||
export type SessionCount = z.infer<typeof sessionCountSchema>;
|
||||
|
||||
export const dailyActivitySchema = z.object({
|
||||
date: z.string(),
|
||||
sessionsCount: z.number()
|
||||
});
|
||||
export type DailyActivity = z.infer<typeof dailyActivitySchema>;
|
||||
|
||||
export const playerCountPointSchema = z.object({
|
||||
timestamp: z.string(),
|
||||
count: z.number()
|
||||
});
|
||||
export type PlayerCountPoint = z.infer<typeof playerCountPointSchema>;
|
||||
|
||||
export const recentSessionSchema = z.object({
|
||||
id: z.number(),
|
||||
date: z.string(),
|
||||
type: z.string(),
|
||||
track: z.string(),
|
||||
duration: z.number(),
|
||||
players: z.number()
|
||||
});
|
||||
export type RecentSession = z.infer<typeof recentSessionSchema>;
|
||||
|
||||
export const stateHistoryStatsSchema = z.object({
|
||||
averagePlayers: z.number(),
|
||||
peakPlayers: z.number(),
|
||||
totalSessions: z.number(),
|
||||
totalPlaytime: z.number(),
|
||||
playerCountOverTime: z.array(playerCountPointSchema),
|
||||
sessionTypes: z.array(sessionCountSchema),
|
||||
dailyActivity: z.array(dailyActivitySchema),
|
||||
recentSessions: z.array(recentSessionSchema)
|
||||
});
|
||||
export type StateHistoryStats = z.infer<typeof stateHistoryStatsSchema>;
|
||||
|
||||
export const stateHistoryStatsFilterSchema = z.object({
|
||||
startDate: z.string().min(10, 'Start date is required'),
|
||||
endDate: z.string().min(10, 'End date is required')
|
||||
});
|
||||
|
||||
export type StateHistoryStatsFilter = z.infer<typeof stateHistoryStatsFilterSchema>;
|
||||
69
src/lib/schemas/user.ts
Normal file
69
src/lib/schemas/user.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
export const permissionSchema = z.object({
|
||||
id: z.uuid(),
|
||||
name: z.string()
|
||||
});
|
||||
|
||||
export type Permission = z.infer<typeof permissionSchema>;
|
||||
|
||||
export const roleSchema = z.object({
|
||||
id: z.uuid(),
|
||||
name: z.string(),
|
||||
permissions: z.array(permissionSchema)
|
||||
});
|
||||
|
||||
export type Role = z.infer<typeof roleSchema>;
|
||||
|
||||
export const userSchema = z.object({
|
||||
id: z.uuid(),
|
||||
username: z.string(),
|
||||
role_id: z.uuid(),
|
||||
role: roleSchema
|
||||
});
|
||||
|
||||
export type User = z.infer<typeof userSchema>;
|
||||
|
||||
export const userCreateSchema = z.object({
|
||||
username: z
|
||||
.string()
|
||||
.min(1, 'Username is required')
|
||||
.max(50, 'Username must be at most 50 characters'),
|
||||
password: z
|
||||
.string()
|
||||
.min(6, 'Password must be at least 6 characters')
|
||||
.max(100, 'Password must be at most 100 characters'),
|
||||
role: z.string().min(1, 'Role is required')
|
||||
});
|
||||
|
||||
export type UserCreate = z.infer<typeof userCreateSchema>;
|
||||
|
||||
export const loginSchema = z.object({
|
||||
username: z.string().min(1, 'Username is required'),
|
||||
password: z.string().min(1, 'Password is required')
|
||||
});
|
||||
|
||||
export type Login = z.infer<typeof loginSchema>;
|
||||
|
||||
export const loginTokenSchema = z.object({
|
||||
token: z.string().min(1)
|
||||
});
|
||||
|
||||
export type LoginTokenResponse = z.infer<typeof loginTokenSchema>;
|
||||
|
||||
export const loginResponseSchema = loginTokenSchema.extend({
|
||||
user: userSchema
|
||||
});
|
||||
|
||||
export type LoginResponse = z.infer<typeof loginResponseSchema>;
|
||||
|
||||
export function hasPermission(user: User | null, permission: string): boolean {
|
||||
if (!user || !user.role || !user.role.permissions) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (user.role.name === 'Super Admin') {
|
||||
return true;
|
||||
}
|
||||
return user.role.permissions.some((p) => p.name === permission);
|
||||
}
|
||||
87
src/lib/schemas/websocket.ts
Normal file
87
src/lib/schemas/websocket.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
// Step data schema
|
||||
export const stepDataSchema = z.object({
|
||||
step: z.enum([
|
||||
'validation',
|
||||
'directory_creation',
|
||||
'steam_download',
|
||||
'config_generation',
|
||||
'service_creation',
|
||||
'firewall_rules',
|
||||
'database_save',
|
||||
'completed'
|
||||
]),
|
||||
status: z.enum(['pending', 'in_progress', 'completed', 'failed']),
|
||||
message: z.string(),
|
||||
error: z.string()
|
||||
});
|
||||
|
||||
export type StepData = z.infer<typeof stepDataSchema>;
|
||||
|
||||
// Steam output data schema
|
||||
export const steamOutputDataSchema = z.object({
|
||||
output: z.string(),
|
||||
is_error: z.boolean()
|
||||
});
|
||||
|
||||
export type SteamOutputData = z.infer<typeof steamOutputDataSchema>;
|
||||
|
||||
// Error data schema
|
||||
export const errorDataSchema = z.object({
|
||||
error: z.string(),
|
||||
details: z.string()
|
||||
});
|
||||
|
||||
export type ErrorData = z.infer<typeof errorDataSchema>;
|
||||
|
||||
// Complete data schema
|
||||
export const completeDataSchema = z.object({
|
||||
server_id: z.string(),
|
||||
success: z.boolean(),
|
||||
message: z.string()
|
||||
});
|
||||
|
||||
export type CompleteData = z.infer<typeof completeDataSchema>;
|
||||
|
||||
// WebSocket message schema using discriminated union
|
||||
export const webSocketMessageSchema = z.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('step'),
|
||||
server_id: z.string(),
|
||||
timestamp: z.number(),
|
||||
data: stepDataSchema
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('steam_output'),
|
||||
server_id: z.string(),
|
||||
timestamp: z.number(),
|
||||
data: steamOutputDataSchema
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('error'),
|
||||
server_id: z.string(),
|
||||
timestamp: z.number(),
|
||||
data: errorDataSchema
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('complete'),
|
||||
server_id: z.string(),
|
||||
timestamp: z.number(),
|
||||
data: completeDataSchema
|
||||
})
|
||||
]);
|
||||
|
||||
export type WebSocketMessage = z.infer<typeof webSocketMessageSchema>;
|
||||
|
||||
// Connection status schema
|
||||
export const connectionStatusSchema = z.enum(['connecting', 'connected', 'disconnected', 'error']);
|
||||
|
||||
export type ConnectionStatus = z.infer<typeof connectionStatusSchema>;
|
||||
|
||||
// Handler types (these remain as types since they're function signatures)
|
||||
export type MessageHandler = (message: WebSocketMessage) => void;
|
||||
export type ConnectionStatusHandler = (
|
||||
status: ConnectionStatus,
|
||||
error?: string
|
||||
) => void;
|
||||
@@ -1,5 +1,5 @@
|
||||
import { SessionOptions } from 'iron-session';
|
||||
import { User } from '@/lib/types';
|
||||
import { User } from '@/lib/schemas';
|
||||
|
||||
export interface SessionData {
|
||||
token?: string;
|
||||
|
||||
@@ -1,104 +0,0 @@
|
||||
export enum ConfigFile {
|
||||
configuration = 'configuration.json',
|
||||
assistRules = 'assistRules.json',
|
||||
event = 'event.json',
|
||||
eventRules = 'eventRules.json',
|
||||
settings = 'settings.json'
|
||||
}
|
||||
|
||||
export enum ServerTab {
|
||||
statistics = 'statistics',
|
||||
configuration = 'configuration',
|
||||
assistRules = 'assistRules',
|
||||
event = 'event',
|
||||
eventRules = 'eventRules',
|
||||
settings = 'settings'
|
||||
}
|
||||
|
||||
export interface Configuration {
|
||||
udpPort: number;
|
||||
tcpPort: number;
|
||||
maxConnections: number;
|
||||
lanDiscovery: number;
|
||||
registerToLobby: number;
|
||||
configVersion: number;
|
||||
}
|
||||
|
||||
export interface AssistRules {
|
||||
stabilityControlLevelMax: number;
|
||||
disableAutosteer: number;
|
||||
disableAutoLights: number;
|
||||
disableAutoWiper: number;
|
||||
disableAutoEngineStart: number;
|
||||
disableAutoPitLimiter: number;
|
||||
disableAutoGear: number;
|
||||
disableAutoClutch: number;
|
||||
disableIdealLine: number;
|
||||
}
|
||||
|
||||
export interface ServerSettings {
|
||||
serverName: string;
|
||||
adminPassword: string;
|
||||
carGroup: string;
|
||||
trackMedalsRequirement: number;
|
||||
safetyRatingRequirement: number;
|
||||
racecraftRatingRequirement: number;
|
||||
password: string;
|
||||
spectatorPassword: string;
|
||||
maxCarSlots: number;
|
||||
dumpLeaderboards: number;
|
||||
isRaceLocked: number;
|
||||
randomizeTrackWhenEmpty: number;
|
||||
centralEntryListPath: string;
|
||||
allowAutoDQ: number;
|
||||
shortFormationLap: number;
|
||||
formationLapType: number;
|
||||
ignorePrematureDisconnects: number;
|
||||
}
|
||||
|
||||
export interface Session {
|
||||
hourOfDay: number;
|
||||
dayOfWeekend: number;
|
||||
timeMultiplier: number;
|
||||
sessionType: string;
|
||||
sessionDurationMinutes: number;
|
||||
}
|
||||
|
||||
export interface EventConfig {
|
||||
track: string;
|
||||
preRaceWaitingTimeSeconds: number;
|
||||
sessionOverTimeSeconds: number;
|
||||
ambientTemp: number;
|
||||
cloudLevel: number;
|
||||
rain: number;
|
||||
weatherRandomness: number;
|
||||
postQualySeconds: number;
|
||||
postRaceSeconds: number;
|
||||
simracerWeatherConditions: number;
|
||||
isFixedConditionQualification: number;
|
||||
sessions: Session[];
|
||||
}
|
||||
|
||||
export interface EventRules {
|
||||
qualifyStandingType: number;
|
||||
pitWindowLengthSec: number;
|
||||
driverStintTimeSec: number;
|
||||
mandatoryPitstopCount: number;
|
||||
maxTotalDrivingTime: number;
|
||||
isRefuellingAllowedInRace: boolean;
|
||||
isRefuellingTimeFixed: boolean;
|
||||
isMandatoryPitstopRefuellingRequired: boolean;
|
||||
isMandatoryPitstopTyreChangeRequired: boolean;
|
||||
isMandatoryPitstopSwapDriverRequired: boolean;
|
||||
tyreSetCount: number;
|
||||
}
|
||||
|
||||
export interface Configurations {
|
||||
configuration: Configuration;
|
||||
assistRules: AssistRules;
|
||||
event: EventConfig;
|
||||
eventRules: EventRules;
|
||||
settings: ServerSettings;
|
||||
}
|
||||
|
||||
export type Config = Configuration | AssistRules | EventConfig | EventRules | ServerSettings;
|
||||
@@ -1,25 +0,0 @@
|
||||
export interface Track {
|
||||
track: string;
|
||||
uniquePitBoxes: number;
|
||||
privateServerSlots: number;
|
||||
}
|
||||
|
||||
export interface CarModel {
|
||||
value: number;
|
||||
carModel: string;
|
||||
}
|
||||
|
||||
export interface DriverCategory {
|
||||
value: number;
|
||||
category: string;
|
||||
}
|
||||
|
||||
export interface CupCategory {
|
||||
value: number;
|
||||
category: string;
|
||||
}
|
||||
|
||||
export interface SessionType {
|
||||
value: number;
|
||||
sessionType: string;
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
export interface StateHistory {
|
||||
dateCreated: string;
|
||||
sessionStart: string;
|
||||
playerCount: number;
|
||||
track: string;
|
||||
sessionDurationMinutes: number;
|
||||
session: string;
|
||||
}
|
||||
|
||||
interface SessionCount {
|
||||
name: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
interface DailyActivity {
|
||||
date: string;
|
||||
sessionsCount: number;
|
||||
}
|
||||
|
||||
interface PlayerCountPoint {
|
||||
timestamp: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
interface RecentSession {
|
||||
id: number;
|
||||
date: string;
|
||||
type: string;
|
||||
track: string;
|
||||
duration: number;
|
||||
players: number;
|
||||
}
|
||||
|
||||
export interface StateHistoryStats {
|
||||
averagePlayers: number;
|
||||
peakPlayers: number;
|
||||
totalSessions: number;
|
||||
totalPlaytime: number;
|
||||
playerCountOverTime: PlayerCountPoint[];
|
||||
sessionTypes: SessionCount[];
|
||||
dailyActivity: DailyActivity[];
|
||||
recentSessions: RecentSession[];
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
export interface Permission {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface Role {
|
||||
id: string;
|
||||
name: string;
|
||||
permissions: Permission[];
|
||||
}
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
username: string;
|
||||
role_id: string;
|
||||
role: Role;
|
||||
}
|
||||
|
||||
export function hasPermission(user: User | null, permission: string): boolean {
|
||||
if (!user || !user.role || !user.role.permissions) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (user.role.name === 'Super Admin') {
|
||||
return true;
|
||||
}
|
||||
return user.role.permissions.some((p) => p.name === permission);
|
||||
}
|
||||
@@ -1,46 +1,9 @@
|
||||
export interface WebSocketMessage {
|
||||
type: 'step' | 'steam_output' | 'error' | 'complete';
|
||||
server_id: string;
|
||||
timestamp: number;
|
||||
data: StepData | SteamOutputData | ErrorData | CompleteData;
|
||||
}
|
||||
|
||||
export interface StepData {
|
||||
step:
|
||||
| 'validation'
|
||||
| 'directory_creation'
|
||||
| 'steam_download'
|
||||
| 'config_generation'
|
||||
| 'service_creation'
|
||||
| 'firewall_rules'
|
||||
| 'database_save'
|
||||
| 'completed';
|
||||
status: 'pending' | 'in_progress' | 'completed' | 'failed';
|
||||
message: string;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export interface SteamOutputData {
|
||||
output: string;
|
||||
is_error: boolean;
|
||||
}
|
||||
|
||||
export interface ErrorData {
|
||||
error: string;
|
||||
details: string;
|
||||
}
|
||||
|
||||
export interface CompleteData {
|
||||
server_id: string;
|
||||
success: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export type MessageHandler = (message: WebSocketMessage) => void;
|
||||
export type ConnectionStatusHandler = (
|
||||
status: 'connecting' | 'connected' | 'disconnected' | 'error',
|
||||
error?: string
|
||||
) => void;
|
||||
import {
|
||||
webSocketMessageSchema,
|
||||
type WebSocketMessage,
|
||||
type MessageHandler,
|
||||
type ConnectionStatusHandler
|
||||
} from '@/lib/schemas/websocket';
|
||||
|
||||
export class WebSocketClient {
|
||||
private ws: WebSocket | null = null;
|
||||
@@ -89,7 +52,8 @@ export class WebSocketClient {
|
||||
|
||||
this.ws.onmessage = (event) => {
|
||||
try {
|
||||
const message: WebSocketMessage = JSON.parse(event.data);
|
||||
const rawMessage: WebSocketMessage = JSON.parse(event.data);
|
||||
const message = webSocketMessageSchema.parse(rawMessage);
|
||||
this.messageHandlers.forEach((handler) => handler(message));
|
||||
} catch (error) {
|
||||
console.error('Failed to parse WebSocket message:', error);
|
||||
|
||||
@@ -9,8 +9,9 @@ import {
|
||||
useCallback,
|
||||
useRef
|
||||
} from 'react';
|
||||
import { WebSocketClient, MessageHandler, ConnectionStatusHandler } from './client';
|
||||
import { WebSocketClient } from './client';
|
||||
import { websocketOptions } from './config';
|
||||
import type { ConnectionStatusHandler, MessageHandler } from '../schemas/websocket';
|
||||
|
||||
interface WebSocketContextType {
|
||||
client: WebSocketClient | null;
|
||||
|
||||
Reference in New Issue
Block a user