public RequestContext(int connectionId, Configuration configuration, StarlightServer server, ConnectedUserManager connectedUserManager) { this.ConnectionId = connectionId; this.Configuration = configuration; this.Server = server; this.ConnectedUserManager = connectedUserManager; this.DbContext = DbContextFactory.CreateApplicationDbContext(configuration.ConnectionString); if (this.ConnectedUserManager.TryGetUser(connectionId, out var user)) { this.User = user; } }
public static void RunSetup(Configuration configuration) { var dbContext = DbContextFactory.CreateApplicationDbContext(configuration.ConnectionString); Log.Information("Updating database..."); try { dbContext.Database.Migrate(); } catch (Exception e) { Log.Error(e, "Error encountered during update!"); return; }; if (!dbContext.Maps.Any()) { Log.Information("Creating initial map."); var map = Map.Create(); dbContext.Maps.Add(map); } if (!dbContext.GlobalTimeEvents.Any()) { Log.Information("Creating global events"); var events = GlobalTimeEvents.Create(); dbContext.GlobalTimeEvents.Add(events); } if (!dbContext.GlobalConstellation.Any()) { Log.Information("Creating sun and moons"); var constellations = GlobalConstellation.Create(); foreach (var constellation in constellations) { dbContext.GlobalConstellation.Add(constellation); } } if (!dbContext.GlobalData.Any()) { Log.Information("Creating global data"); var database = GlobalData.Create(); foreach (var data in database) { dbContext.GlobalData.Add(data); } } if (!dbContext.Clock.Any()) { Log.Information("Creating clock"); var clock = Clock.Create(); dbContext.Clock.Add(clock); } dbContext.SaveChanges(); }
/// <summary> /// /// </summary> /// <param name="debug">To turn debugging on or off</param> /// <param name="config">Location of the configuration file</param> static void Main(bool debug = false, string config = "config.json") { var server = new StarlightServer(new Telepathy.Server()); #if DEBUG debug = true; config = "config.development.json"; #endif try { var configuration = LoadConfiguration(config); if (string.IsNullOrEmpty(configuration.LogFile)) { configuration.LogFile = "server.log"; } ; Log.Logger = (ILogger) new LoggerConfiguration() .WriteTo.Console() .WriteTo.File(path: configuration.LogFile, rollingInterval: RollingInterval.Day) .MinimumLevel.Information() .CreateLogger(); if (debug) { Log.Logger = (ILogger) new LoggerConfiguration() .WriteTo.Console() .WriteTo.File(path: configuration.LogFile, rollingInterval: RollingInterval.Day) .MinimumLevel.Debug() .CreateLogger(); } if (string.IsNullOrEmpty(configuration.ConnectionString) || configuration.Port == 0) { throw new IOException("No valid configuration found!"); } Log.Information("Starlight Server " + Assembly.GetEntryAssembly().GetName().Version.ToString()); Log.Debug("Configuration loaded: \r\n" + configuration.ToString()); TranslationManager.Instance.ImportFromDocument(Path.Combine(Directory.GetCurrentDirectory(), "Content", "Languages", "en-us.json")); DenyList.Instance.ImportFromDocument(Path.Combine(Directory.GetCurrentDirectory(), "denylist.txt")); Setup.RunSetup(configuration); //Validate denylist if (!DenyList.Instance.CheckDenied("root") || !DenyList.Instance.CheckDenied("admin") || !DenyList.Instance.CheckDenied("demo") || !DenyList.Instance.CheckDenied("sql") || !DenyList.Instance.CheckDenied("test") || !DenyList.Instance.CheckDenied("system")) { Log.Warning("Denylist is incomplete or missing!"); } // start the server server.Server.Start(configuration.Port); var networkDispatch = new NetworkDispatch(configuration, server); var database = DbContextFactory.CreateApplicationDbContext(configuration.ConnectionString); //Setting universe ticking time if (database.GlobalData.Where(x => x.Name == "SecondsPerMinute").FirstOrDefault().Value == null) { Log.Fatal("No timedelay found!"); throw new TimeZoneNotFoundException(); } var timedelay = int.Parse(database.GlobalData.Where(x => x.Name == "SecondsPerMinute").FirstOrDefault().Value) * 1000; if (timedelay <= 1000) { //You don't want to have 1 sec = 1 minute ingame Log.Fatal("Timedelay is too short! Setting to default."); timedelay = 60000; } long lastTick = 0; var tickcounter = 0; networkDispatch.ResolveHandlers(); Log.Debug("Server is running..."); var isRunning = true; while (isRunning) { isRunning = server.Server.Active; while (server.Server.GetNextMessage(out var msg)) { switch (msg.eventType) { case Telepathy.EventType.Connected: Log.Information("[" + msg.connectionId + "] Connected from " + server.Server.GetClientAddress(msg.connectionId)); break; case Telepathy.EventType.Data: networkDispatch.HandleData(msg.connectionId, msg.data); break; case Telepathy.EventType.Disconnected: Log.Information("[" + msg.connectionId + "] Disconnected"); break; } } if (DateTime.UtcNow.Ticks > lastTick + timedelay * 10000) { Log.Debug("tps: " + (tickcounter / (timedelay / 1000))); lastTick = DateTime.UtcNow.Ticks; tickcounter = 0; PersistentUniverse.DoTick(database); } tickcounter++; Thread.Sleep(1); } } catch (Exception e) { Log.Fatal(e, "Error found in Main Loop"); } Log.Information("Stopping server..."); server.Server.Stop(); Log.CloseAndFlush(); }