@@ -2,7 +2,8 @@ import { error, redirect } from "@sveltejs/kit"
22import { authorise } from "$lib/server/auth"
33import config from "$lib/server/config"
44import formData from "$lib/server/formData"
5- import { getGameserver } from "$lib/server/orbiter"
5+ import { getGameserver , startGameserver } from "$lib/server/orbiter"
6+ import ratelimit from "$lib/server/ratelimit"
67import { db , findWhere , Record } from "$lib/server/surreal"
78import { couldMatch , encode } from "$lib/urlName"
89import findPlaceQuery from "./findPlace.surql"
@@ -83,13 +84,10 @@ export async function load({ locals, params, url }) {
8384 }
8485}
8586
86- export const actions : import ( "./$types" ) . Actions = { }
87- actions . join = async ( { locals, params, request } ) => {
88- const { user } = await authorise ( locals )
87+ async function findPlace ( request : Request , id : number , user : User ) {
8988 const data = await formData ( request )
9089 const privateTicket = data ?. privateTicket
9190
92- const id = + params . id
9391 const placeR = Record ( "place" , id )
9492 const [ [ place ] ] = await db . query < FoundPlace [ ] [ ] > ( findPlaceQuery , {
9593 place : placeR ,
@@ -103,13 +101,27 @@ actions.join = async ({ locals, params, request }) => {
103101 )
104102 error ( 404 , "Place not found" )
105103
104+ return placeR
105+ }
106+
107+ async function checkUser ( locals : App . Locals ) {
108+ const { user } = await authorise ( locals )
109+
106110 const foundModerated = await findWhere (
107111 "moderation" ,
108112 "out = $user AND active = true" ,
109113 { user : Record ( "user" , user . id ) }
110114 )
111115 if ( foundModerated ) error ( 403 , "You cannot currently play games" )
112116
117+ return user
118+ }
119+
120+ export const actions : import ( "./$types" ) . Actions = { }
121+ actions . join = async ( { locals, params, request } ) => {
122+ const user = await checkUser ( locals )
123+ const placeR = await findPlace ( request , + params . id , user )
124+
113125 // Invalidate all game sessions and create valid playing
114126 const [ , [ ticket ] ] = await db . query < string [ ] [ ] > ( invalidatePlayingQuery , {
115127 user : Record ( "user" , user . id ) ,
@@ -118,3 +130,21 @@ actions.join = async ({ locals, params, request }) => {
118130
119131 return { ticket }
120132}
133+
134+ actions . start = async ( { locals, params, getClientAddress } ) => {
135+ await checkUser ( locals )
136+
137+ const limit = ratelimit ( null , "serverstart" , getClientAddress , 20 )
138+ if ( limit ) return limit
139+
140+ const id = + params . id
141+ const res = await startGameserver ( id )
142+ if ( ! res . ok ) {
143+ console . error (
144+ "Failed to start dedicated gameserver for id" ,
145+ id ,
146+ res . msg
147+ )
148+ error ( 500 , "Failed to start dedicated server" )
149+ }
150+ }
0 commit comments