Something went wrong. Try again.
Starship Simulator Controls thoriumsim.com
Something went wrong. Try again.
1.7 kB · 49 lines
TypeScript
at develop
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950import { FlightDataModel } from "@thorium/.server/classes/FlightDataModel";import { ServerDataModel } from "@thorium/.server/classes/ServerDataModel";import { thoriumContext } from "@thorium/utils/.server/context";import randomWords from "@thorium/utils/random-words";
export const databaseName = process.env.NODE_ENV === "production" ? /* istanbul ignore next */ "db.yml" : process.env.NODE_ENV === "test" ? "db-test.yml" : /* istanbul ignore next */ "db-dev.yml";
export async function buildDatabase(loadPlugins: (this: ServerDataModel) => Promise<void>) { // Create the primary database // This is for any user data that is persisted between flights // but that isn't part of a plugin. Not much goes in here. const serverModel = new ServerDataModel( { thoriumId: randomWords(3).join("-"), activeFlightName: null }, { meta: { filePath: databaseName } }, loadPlugins, );
// Wait for the plugins to load. Shouldn't take long. await serverModel.loadPlugins(); await serverModel.getInitialData();
// If a flight is in progress, load it. // This helps in situations where the server is shut // down or crashes unexpectedly. let flight = null; if (serverModel.activeFlightName) { const flightName = serverModel.activeFlightName; flight = await FlightDataModel.create( { name: flightName, initialLoad: false, entities: [], serverDataModel: serverModel, }, { meta: { filePath: `/flights/${flightName}/data.yml`, flightName } }, ); await flight.initPhysics(); }
thoriumContext.getStore()!.database = { server: serverModel, flight }; return thoriumContext.getStore()!.database;}