static void Main(string[] args) { var messageHandler = new CCCMessageHandlerBuilder() .AddCommandHandler(new ConnectHandler()) .AddCommandHandler(new DieHandler()) .AddCommandHandler(new PlayHandler()) .AddCommandHandler(new ListHandler()) .AddCommandHandler(new MoveHandler()) .AddFallbackHandler(new FallbackHandler()) .Build(); var server = new GameServer(new GameServerConfig { Address = IPAddress.Parse(Ip), AcceptQueueLength = 10, ListenDelay = 100, Port = Port, Handler = messageHandler, }); server.ServerStarted += OnStart; server.UserConnected += OnUserConnected; server.UserMessaged += OnUserMessaged; server.UserDisconnected += OnUserDisconnected; server.Start(); Console.ReadLine(); server.Stop(); }
public static int Main(string[] args) { if (args.Length > 1 && args[0] == "--makeconfig") { MakeConfig(); return(0); } var config = JSON.Deserialize <Config>(File.ReadAllText("librelancerserver.config.json")); var srv = new GameServer(config.freelancer_path); srv.DbConnectionString = config.dbconnectionstring; srv.ServerName = config.server_name; srv.ServerDescription = config.server_description; srv.Start(); bool running = true; while (running) { var cmd = Console.ReadLine(); switch (cmd.Trim().ToLowerInvariant()) { case "stop": case "quit": case "exit": running = false; break; } } srv.Stop(); return(0); }
static void Main(string[] args) { GameServer server = new GameServer(); server.Start(); Console.WriteLine("Server has been started!"); while (true) { //Console.Write("> "); string input = Console.ReadLine(); switch (input) { case "stop": server.Stop(); Console.WriteLine("Server has been stopped!"); return; case "end": server.Game.End(); server.CancelTurnTimer(); server.SendGameOver(); Console.WriteLine("The game has been ended."); break; default: Console.WriteLine("'" + input + "' is not a valid command!"); break; } } }
public async Task StopAsync(CancellationToken cancellationToken) { if (!_gameServer.Stop()) { throw new InvalidOperationException("Could not stop server."); } Console.WriteLine($"Server stopped listening for game clients."); }
static void Main(string[] args) { var port = 9530; var srvAddr = "127.0.0.1"; var noConsole = false; var onlyConsoleAgent = false; var runScripts = new List <string>(); foreach (var p in args) { if (p == "-noconsole") { noConsole = true; } else if (p.StartsWith("-p")) { port = int.Parse(p.Substring(2)); } else if (p == "-console-agent") { onlyConsoleAgent = true; } else if (p.StartsWith("-h")) { srvAddr = p.Substring(2); } else if (p.StartsWith("-r")) { runScripts.Add(Path.Combine("./init", p.Substring(2))); } else { Console.WriteLine("unknown parameter: " + p); } } var srv = new GameServer(); try { if (!onlyConsoleAgent) { var startSrv = ServerBuilder.BuildLibServer(srv, port); var css = srv.Get <CsScriptShell <ScriptObject> >(); foreach (var s in runScripts) { css.RunScript(s, "Init", new object[] { srv }); } if (!noConsole) { srv.Get <ConsoleInput>().Start(); // 启用控制台输入 } startSrv(); } else if (onlyConsoleAgent) { ServerBuilder.BuildConsoleAgent(srv, srvAddr, port)(); } } catch (Exception ex) { Console.WriteLine("!!!! EXCEPTION: " + ex.Message); Console.WriteLine("!!!! STACK: " + ex.StackTrace); srv.Get <ILog>().Error("[!!!! EXCEPTION]:" + ex.Message); srv.Get <ILog>().Error("[!!!! STACK]:" + ex.StackTrace); srv.Stop(); } }
public override void Init() { dic = new Dictionary <string, Dictionary <string, string> >(); var up = GetCom <UserPort>(); up.OnRequest("GMCmd", (Connection conn, IReadableBuffer data, IWriteableBuffer buff, Action end) => { var strs = data.ReadString().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var cmd = strs[0]; var ps = strs.Length == 1 ? null : new string[strs.Length - 1]; if (ps != null) { Array.Copy(strs, 1, ps, 0, ps.Length); } if (!cmdHandlers.ContainsKey(cmd)) { buff.Write("unknown command: " + cmd); end(); } else { PushCommand((r) => { buff.Write(r); end(); }, cmd, ps); } }); OnCommand("stop", (ps) => { running = false; Srv.Stop(); return("stopped"); }); OnCommand("runfile", (ps) => { if (ps == null || ps.Length < 1) { return("no file specified"); } else if (ps.Length < 2) { return("no function name specified"); } var f = ps[0]; var fun = ps[1]; try { var css = GetCom <CsScriptShell <ScriptObject> >(); css.RunScript(f, fun, new object[] { Srv }); } catch (Exception ex) { return("run file " + f + "::" + fun + "[ex]\r\n" + ex.Message + "\r\n" + ex.StackTrace); } return("run file " + f + "::" + fun + "[ok]\r\n"); }); OnCommand("cfg", (ps) => { if (ps == null) { return("parameter error!, try cfg --help"); } string result = ""; switch (ps[0]) { case "get": if (ps.Length != 2) { return("parameter error!, try cfg --help"); } switch (ps[1]) { case "-a": var allUnitTypes = UnitConfiguration.AllUnitTypes; for (int i = 0; i < allUnitTypes.Length; i++) { result += "\n" + GetCfgInfo(allUnitTypes[i]); } break; default: result = GetCfgInfo(ps[1]); break; } break; case "set": if (ps.Length < 4) { return("parameter error!, try cfg --help"); } return(result = SetCfgInfo(ps) ? "successful!" : "failed!"); case "count": return(result = UnitConfiguration.AllUnitTypes.Length.ToString()); case "-m": foreach (var para in dic) { result += para.Key + "\n "; foreach (var p in para.Value) { result += p.Key + " : "; result += p.Value + "\n "; } result += "\n"; } break; case "--help": result = "count" + " : " + "查看配置表数量"; result += "\n"; result += "get" + " : " + "查看配置表信息"; result += "\n"; result += " get -a" + " : " + "查看所有配置表信息"; result += "\n"; result += " get type" + " : " + "查看类型为type的配置表信息"; result += "\n"; result += "set" + " : " + "设置配置表信息"; result += "\n"; result += " set type field value" + " : " + "设置类型为type的配置表信息,将field字段的值设置为value"; result += "\n"; return(result); default: return("parameter error!, try cfg --help"); } result += "\n"; return(result); }); OnCommand("pve", (ps) => { if (ps == null) { return("parameter error!"); } if (ps.Length != 2) { return("parameter error!"); } switch (ps[0]) { case "ai": if (ps[1] != "0" && ps[1] != "1" && ps[1] != "2") { return("not exist!"); } else { if (OnChangePVEAI != null) { OnChangePVEAI(ps[1]); } return("successful!"); } } return(""); }); OnCommand("onlines", (ps) => { var ss = GetCom <SessionContainer>(); return(ss.Count.ToString()); }); OnCommand("set_auth_lv", (ps) => { if (ps.Length < 1) { return("no name specified"); } var name = ps[0]; var lv = ps.Length > 1 ? int.Parse(ps[1]) : 0; var ss = GetCom <SessionContainer>(); var sArr = ss.GetByName(name); foreach (var s in sArr) { s.Usr.Info.AuthLv = lv; s.Usr.Update(); } return("set " + sArr.Length + " to " + lv); }); }
public static int Main(string[] args) { if (args.Length > 0 && args[0] == "--makeconfig") { MakeConfig(); return(0); } if (!File.Exists("librelancerserver.config.json")) { Console.Error.WriteLine("Can't find librelancerserver.config.json"); return(2); } var config = JSON.Deserialize <Config>(File.ReadAllText("librelancerserver.config.json")); config.DatabasePath = Path.GetFullPath(config.DatabasePath); var srv = new GameServer(config.FreelancerPath); var ctxFactory = new SqlDesignTimeFactory(config); if (!File.Exists(config.DatabasePath)) { FLLog.Info("Server", $"Creating database file {config.DatabasePath}"); using (var ctx = ctxFactory.CreateDbContext(new string[0])) { ctx.Database.Migrate(); } } using (var ctx = ctxFactory.CreateDbContext(new string[0])) { //Force create model early if (ctx.Database.GetPendingMigrations().Any()) { FLLog.Info("Server", "Migrating database"); ctx.Database.Migrate(); } FLLog.Debug("model", ctx.Model.ToString()); } srv.DbContextFactory = ctxFactory; srv.ServerName = config.ServerName; srv.ServerDescription = config.ServerDescription; srv.Start(); bool running = true; while (running) { var cmd = Console.ReadLine(); switch (cmd.Trim().ToLowerInvariant()) { case "stop": case "quit": case "exit": running = false; break; } } srv.Stop(); return(0); }