introduce zod schemas
This commit is contained in:
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;
|
||||
5
src/lib/schemas/index.ts
Normal file
5
src/lib/schemas/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export * from './server';
|
||||
export * from './user';
|
||||
export * from './config';
|
||||
export * from './statistics';
|
||||
export * from './lookups';
|
||||
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>;
|
||||
63
src/lib/schemas/server.ts
Normal file
63
src/lib/schemas/server.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import * as z from 'zod';
|
||||
|
||||
export enum ServiceStatus {
|
||||
Unknown,
|
||||
Stopped,
|
||||
Stopping,
|
||||
Restarting,
|
||||
Starting,
|
||||
Running
|
||||
}
|
||||
export const serviceStatusSchema = z.enum(ServiceStatus);
|
||||
|
||||
export const serviceStatusToString = (status: ServiceStatus): string => {
|
||||
switch (status) {
|
||||
case ServiceStatus.Running:
|
||||
return 'Running';
|
||||
case ServiceStatus.Stopped:
|
||||
return 'Stopped';
|
||||
case ServiceStatus.Starting:
|
||||
return 'Starting';
|
||||
case ServiceStatus.Stopping:
|
||||
return 'Stopping';
|
||||
case ServiceStatus.Restarting:
|
||||
return 'Restarting';
|
||||
default:
|
||||
return 'Unknown';
|
||||
}
|
||||
};
|
||||
|
||||
export const getStatusColor = (status: ServiceStatus): string => {
|
||||
switch (status) {
|
||||
case ServiceStatus.Running:
|
||||
return 'bg-green-500';
|
||||
case ServiceStatus.Stopped:
|
||||
return 'bg-red-500';
|
||||
case ServiceStatus.Starting:
|
||||
return 'bg-blue-500';
|
||||
case ServiceStatus.Stopping:
|
||||
return 'bg-yellow-500';
|
||||
case ServiceStatus.Restarting:
|
||||
return 'bg-purple-500';
|
||||
default:
|
||||
return 'bg-gray-500';
|
||||
}
|
||||
};
|
||||
|
||||
export const stateSchema = z.object({
|
||||
session: z.string(),
|
||||
playerCount: z.number().min(0),
|
||||
track: z.string(),
|
||||
maxConnections: z.number().min(0)
|
||||
});
|
||||
|
||||
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;
|
||||
Reference in New Issue
Block a user