Ejemplo n.º 1
0
        //obiekt przechowujący obiekty ubiegające się o dostęp do wątku
        //private Object Sync;
        public MainForm()
        {
            InitializeComponent();

            //Sync = new Object();

            //domyślnie serwer jest wyłączony
            isRunning = false;
            timer = new System.Windows.Forms.Timer();
            timer.Interval = 1000;
            timer.Tick += new EventHandler(DisplayServerTime);
            timer.Start();

            //utworzenie gniazda serwera
            try
            {
                server = new TcpListener(IPAddress.Any, 8001);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Nie udało się utworzyć gniazda serwera. Dalsze korzystanie z aplikacji może generować błędy! Uruchom aplikację jeszcze raz.\nDebuger message:\n" + ex.ToString(), "Błąd tworzenia gniazda serwera!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            dataBase = new GlobalMySql();

            try
            {
                dataBase.Connection.Open();
            }
            catch
            {
                AddLog("Nie udało się nawiązać połączenia z bazą danych. Aplikacja nie będzie działać poprawanie.");
            }

            //stworzenie obiektu z danymi mapy
            map = new Map(dataBase);

            skills = new Skills(dataBase);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            // Create server
            CheckConfig(Server.CONFIG_NAME);
            NativeMethods.SetConsoleCtrlHandler(new NativeMethods.HandlerRoutine(Close), true);
            try
            {
                server = new Server();
                if (!server.Start(new AcceptAction(AcceptConnection)))
                {
                    Console.WriteLine("Error starting server!");
                    server.Close();
                    return;
                }
                else
                {
                    if(IPAddress.Parse(server.ListenIP).AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        Console.WriteLine("Server started. Listening on: {0}:{1}\n", server.ListenIP, server.ListenPort);
                    else
                        Console.WriteLine("Server started. Listening on: [{0}]:{1}\n", server.ListenIP, server.ListenPort);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error starting server: " + ex.Message);

                if (server != null)
                    server.Close();

                return;
            }

            // Load map
            Map defaultMap = null;
            try
            {
                defaultMap = new Map(typeof(GameServer).Assembly.GetManifestResourceStream("GameServer.game.bmap"));

                World.CurrentMap = new Map(defaultMap);
            }
            catch
            {
            }

            // Initialize items
            World.PopulateBacteria();

            string[] input = { "" };

            // Input loop
            while (acceptServerConsoleInput(ref input))
            {
                List<KeyValuePair<IPEndPoint, string>> playerData = server.GetClientInfo(); //can't change ordering - otherwise we'll get bugs when kicking by index
                switch (input[0].ToLower())
                {
                    case "help":
                        Console.WriteLine("Commands:");
                        Console.WriteLine("\t clear");
                        Console.WriteLine("\t cls");
                        Console.WriteLine("\t exit");
                        Console.WriteLine("\t help");
                        Console.WriteLine("\t kick [playername|index]");
                        Console.WriteLine("\t prompte [playername]");
                        Console.WriteLine("\t restart");
                        Console.WriteLine("\t say all [message]");
                        Console.WriteLine("\t status");
                        break;
                    case "kick":
                        if (input.Length != 2)
                        {
                            Console.WriteLine(errorMessage);
                            break;
                        }

                        if (playerData.Count == 0)
                        {
                            Console.WriteLine(noPlayersMessage);
                            break;
                        }

                        // Figure out kick data
                        int index = 0;
                        KickData kd = int.TryParse(input[1], out index) ? new KickData(index) : new KickData(input[1]);

                        if (!server.SendToClient(ServerAction.ServerKick, kd))
                            Console.WriteLine("There was an error with your request.");
                        break;
                    case "promote":
                        if(input.Length != 2)
                        {
                            Console.WriteLine(errorMessage);
                            break;
                        }

                        if (server.PromoteUser(input[1]))
                        {
                            Console.WriteLine("Promoted " + input[1] + " to admin!");
                            Console.WriteLine("You should probably kick them to force a re-log so everything works");
                        }
                        else
                            Console.WriteLine("Error promoting user " + input[1]);
                        break;
                    case "say":
                        if (input.Length < 3 || input[1] != "all")
                        {
                            Console.WriteLine(errorMessage + " Try \"say all [message]\"");
                            break;
                        }

                        if (playerData.Count == 0)
                        {
                            Console.WriteLine(noPlayersMessage);
                            break;
                        }

                        // Send server message
                        string output = string.Join(" ", input.Skip(2));
                        if (!server.SendToClient(ServerAction.ServerMessage, new TalkData(output)))
                            Console.WriteLine("There was an error with your request.");
                        break;
                    case "status":
                        if (input.Length != 1)
                        {
                            Console.WriteLine(errorMessage);
                            break;
                        }

                        if (playerData.Count == 0)
                        {
                            Console.WriteLine(noPlayersMessage);
                            break;
                        }

                        int i = 0;

                        foreach (KeyValuePair<IPEndPoint, string> info in playerData)
                        {
                            if (!string.IsNullOrEmpty(info.Value))
                                Console.WriteLine("  [{0}]: {1,-21} ({2})", i, info.Key.ToString(), info.Value);
                            else
                                Console.WriteLine("  [{0}]: {1,-21}", i, info.Key.ToString());
                            i++;
                        }
                        break;
                    case "clear":
                    case "cls":
                        if (input.Length != 1)
                        {
                            Console.WriteLine(errorMessage);
                            break;
                        }

                        Console.Clear();
                        break;
                    case "restart":
                        try
                        {
                            server.Dispose();
                            Console.Write("Closed server. Restarting...");
                        }
                        catch
                        {
                            Console.Write("Error closing server! Restarting...");
                        }

                        try
                        {
                            server = new Server();
                            if (!server.Start(new AcceptAction(AcceptConnection)))
                                throw new ServerStartException("Error restarting server!");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error: " + ex.Message);
                        }

                        if (!server.Started)
                            break;
                        else
                            Console.WriteLine("Restarted.");
                        break;
                    default:
                        Console.WriteLine(errorMessage);
                        break;
                }
            }

            Close(CtrlTypes.CTRL_CLOSE_EVENT);
            Console.WriteLine("Press any key to continue . . .");
            Console.ReadKey(true);
        }