Esempio n. 1
0
        public static void Main(string[] cmdArgs)
        {
            Console.Title = "Bang# Server Interface";
            ConsoleHelper.PrintLine("Bang# Server Interface");
            ConsoleHelper.PrintLine("----------------------");
            ConsoleHelper.PrintLine("Interface version: {0}.{1}", Utils.InterfaceVersionMajor, Utils.InterfaceVersionMinor);
            ConsoleHelper.PrintLine("Server interface version: {0}.{1}", ServerUtils.InterfaceVersionMajor, ServerUtils.InterfaceVersionMinor);
            ConsoleHelper.PrintLine("Operating system: {0}", Environment.OSVersion);
            ConsoleHelper.PrintLine("----------------------");

            string address;
            string portString;
            if(cmdArgs.Length != 2)
            {
                ConsoleHelper.Print("Server Address: ");
                address = ConsoleHelper.ReadLine();
                ConsoleHelper.Print("Server Administration Port: ");
                portString = ConsoleHelper.ReadLine();
            }
            else
            {
                address = cmdArgs[0];
                portString = cmdArgs[1];
            }
            int port;
            try
            {
                port = int.Parse(portString);
            }
            catch(FormatException)
            {
                ConsoleHelper.ErrorLine("Bad number format!");
                return;
            }
            try
            {
                ConsoleHelper.PrintLine("Connecting to {0} on port {1}...", address, port);
                ServerUtils.OpenClientAdminChannel();
                IServerBase server = ServerUtils.ConnectAdmin(address, port);

                ConsoleHelper.PrintLine();

                if(!Utils.IsServerCompatible(server))
                {
                    ConsoleHelper.ErrorLine("Server version {0}.{1} not compatible with client version {2}.{3}!",
                        server.InterfaceVersionMajor, server.InterfaceVersionMinor,
                        Utils.InterfaceVersionMajor, Utils.InterfaceVersionMinor);
                    return;
                }
                if(!ServerUtils.IsServerCompatible(server))
                {
                    ConsoleHelper.ErrorLine("Server administration version {0}.{1} not compatible with interface version {2}.{3}!",
                        server.ServerInterfaceVersionMajor, server.ServerInterfaceVersionMinor,
                        ServerUtils.InterfaceVersionMajor, ServerUtils.InterfaceVersionMinor);
                    return;
                }
                ConsoleHelper.PrintLine("Server name: {0}", server.Name);
                ConsoleHelper.PrintLine("Server description: {0}", server.Description);
                server.RegisterListener(Instance);
                ConsoleHelper.SuccessLine("Connection estabilished!");
                ConsoleHelper.PrintLine();

                ConsoleHelper.Print("Server password: "******"Password rejected!");
                    return;
                }
                ConsoleHelper.SuccessLine("Password accepted!");
                ConsoleHelper.PrintLine();

                NestedCommand rootCmd = new NestedCommand();
                NestedCommand<IServer> serverCmd = new NestedCommand<IServer>(cmd => server);
                serverCmd.MakeServerCommand();
                rootCmd["server"] = serverCmd;
                NestedCommand<IServerAdmin> serverAdminCmd = new NestedCommand<IServerAdmin>(cmd => serverAdmin);
                serverAdminCmd.MakeServerAdminCommand();
                rootCmd["serveradmin"] = serverAdminCmd;
                rootCmd["changepassword"] = new FinalCommand(cmd =>
                {
                    ConsoleHelper.Print("Current password: "******"New password: "******"Confirm new password: "******"The new passwords don't match!");
                        return;
                    }
                    try
                    {
                        server.ChangePassword(currentPassword, newPassword);
                    }
                    catch(BadServerPasswordException)
                    {
                        ConsoleHelper.ErrorLine("Bad server password!");
                        return;
                    }
                    ConsoleHelper.SuccessLine("Server password changed successfully!");
                });
                rootCmd["exit"] = new FinalCommand(cmd =>
                {
                    server.UnregisterListener(Instance);
                    Environment.Exit(0);
                });
                while(true) // command-line loop
                {
                    try
                    {
                        rootCmd.ReadAndExecute();
                    }
                    catch(InvalidOperationException)
                    {
                        ConsoleHelper.ErrorLine("Invalid command!");
                    }
                }
            }
            catch(RemotingException e)
            {
                ConsoleHelper.ErrorLine("Remoting error!");
            #if DEBUG
                ConsoleHelper.DebugLine(e.ToString());
            #endif
                return;
            }
            catch(SerializationException e)
            {
                ConsoleHelper.ErrorLine("Serialization error!");
            #if DEBUG
                ConsoleHelper.DebugLine(e.ToString());
            #endif
                return;
            }
        }
Esempio n. 2
0
        public static void Main(string[] cmdArgs)
        {
            Console.Title = "Bang# Command-Line Client";
            ConsoleHelper.PrintLine("Bang# Command-Line Client");
            ConsoleHelper.PrintLine("-------------------------");
            ConsoleHelper.PrintLine("Interface version: {0}.{1}", Utils.InterfaceVersionMajor, Utils.InterfaceVersionMinor);
            ConsoleHelper.PrintLine("Operating system: {0}", Environment.OSVersion);
            ConsoleHelper.PrintLine("-------------------------");
            string address;
            string portString;
            if(cmdArgs.Length != 2)
            {
                ConsoleHelper.Print("Server Address: ");
                address = ConsoleHelper.ReadLine();
                ConsoleHelper.Print("Server Port: ");
                portString = ConsoleHelper.ReadLine();
            }
            else
            {
                address = cmdArgs[0];
                portString = cmdArgs[1];
            }
            int port;
            try
            {
                port = int.Parse(portString);
            }
            catch(FormatException)
            {
                ConsoleHelper.ErrorLine("Bad number format!");
                return;
            }
            try
            {
                ConsoleHelper.PrintLine("Connecting to {0} on port {1}...", address, port);
                Utils.OpenClientChannel();
                IServer _server = Utils.Connect(address, port);

                ConsoleHelper.PrintLine();

                if(!Utils.IsServerCompatible(_server))
                {
                    ConsoleHelper.ErrorLine("Server version {0}.{1} not compatible with client version {2}.{3}!",
                        _server.InterfaceVersionMajor, _server.InterfaceVersionMinor,
                        Utils.InterfaceVersionMajor, Utils.InterfaceVersionMinor);
                    return;
                }
                ConsoleHelper.PrintLine("Server name: {0}", _server.Name);
                ConsoleHelper.PrintLine("Server description: {0}", _server.Description);
                _server.RegisterListener(Instance);
                ConsoleHelper.SuccessLine("Connection estabilished!");

                ConsoleHelper.PrintLine();

                NestedCommand rootCmd = new NestedCommand();
                NestedCommand<IServer> serverCmd = new NestedCommand<IServer>(cmd => _server);
                serverCmd.MakeServerCommand();
                NestedCommand<IServer, ISession> sessionCmd = (NestedCommand<IServer, ISession>)serverCmd["session"];
                sessionCmd["join"] = new FinalCommand<ISession>((session, cmd) =>
                {
                    CreatePlayerData cpd;
                    string playerName;
                    Password playerPassword;
                    Password password;
                    if(Instance.sessionControl != null)
                    {
                        ConsoleHelper.ErrorLine("Already connected to a session!");
                        return;
                    }

                    ConsoleHelper.Print("Session Password: "******"Player Name: ");
                    playerName = ConsoleHelper.ReadLine();
                    ConsoleHelper.Print("Player Password: "******"Joined session!");
                    }
                    catch(GameException e)
                    {
                        ConsoleHelper.ErrorLine("Cannot join session: {0}", e.GetType());
                    }
                });
                sessionCmd["joinai"] = new FinalCommand<ISession>((session, cmd) =>
                {
                    CreatePlayerData cpd;
                    Password password;
                    if(Instance.sessionControl != null)
                    {
                        ConsoleHelper.ErrorLine("Already connected to a session!");
                        return;
                    }

                    ConsoleHelper.Print("Session Password: "******"TestAI";

                    try
                    {
                        Instance.SetAI(ai);
                        session.Join(password, cpd, Instance.mainSessionListener);
                        ConsoleHelper.SuccessLine("Joined session!");
                    }
                    catch(GameException e)
                    {
                        Instance.UnsetAI();
                        ConsoleHelper.ErrorLine("Cannot join session: {0}", e.GetType());
                    }
                });
                sessionCmd["replace"] = new FinalCommand<ISession>((session, cmd) =>
                {
                    CreatePlayerData cpd;
                    string playerName;
                    Password playerPassword;
                    Password password;
                    int id;
                    try
                    {
                        id = int.Parse(cmd.Dequeue());
                    }
                    catch(FormatException)
                    {
                        ConsoleHelper.ErrorLine("Bad number format!");
                        return;
                    }

                    if(Instance.sessionControl != null)
                    {
                        ConsoleHelper.ErrorLine("Already connected to a session!");
                        return;
                    }

                    ConsoleHelper.Print("Session Password: "******"Player Name: ");
                    playerName = ConsoleHelper.ReadLine();
                    ConsoleHelper.Print("Player Password: "******"Joined session!");
                    }
                    catch(GameException e)
                    {
                        ConsoleHelper.ErrorLine("Cannot join session: {0}", e.GetType());
                    }
                });
                sessionCmd["replaceai"] = new FinalCommand<ISession>((session, cmd) =>
                {
                    CreatePlayerData cpd;
                    Password playerPassword;
                    Password password;
                    int id;
                    try
                    {
                        id = int.Parse(cmd.Dequeue());
                    }
                    catch(FormatException)
                    {
                        ConsoleHelper.ErrorLine("Bad number format!");
                        return;
                    }

                    if(Instance.sessionControl != null)
                    {
                        ConsoleHelper.ErrorLine("Already connected to a session!");
                        return;
                    }

                    ConsoleHelper.Print("Session Password: "******"Player Password: "******"TestAI";
                    cpd.Password = playerPassword;

                    try
                    {
                        Instance.SetAI(ai);
                        session.Replace(id, password, cpd, Instance.mainSessionListener);
                        ConsoleHelper.SuccessLine("Joined session!");
                    }
                    catch(GameException e)
                    {
                        Instance.UnsetAI();
                        ConsoleHelper.ErrorLine("Cannot join session: {0}", e.GetType());
                    }
                });
                sessionCmd["aitestcontinue"] = new FinalCommand<ISession>((session, cmd) =>
                {
                    CreatePlayerData cpd;
                    Password password;
                    int id;
                    try
                    {
                        id = int.Parse(cmd.Dequeue());
                    }
                    catch(FormatException)
                    {
                        ConsoleHelper.ErrorLine("Bad number format!");
                        return;
                    }

                    if(Instance.sessionControl != null)
                    {
                        ConsoleHelper.ErrorLine("Already connected to a session!");
                        return;
                    }

                    password = new Password("_aitest");

                    AI.AIPlayer ai = new AI.AIPlayer();
                    cpd = ai.CreateData;
                    cpd.Name = "TestAI";
                    cpd.Password = new Password("_aitest");

                    try
                    {
                        Instance.SetAI(ai);
                        Instance.aiTest = true;
                        session.Replace(id, password, cpd, Instance.mainSessionListener);
                        if(Instance.sessionControl.Session.State != SessionState.Playing)
                            Instance.sessionControl.StartGame();
                        ConsoleHelper.SuccessLine("Joined AI Test session!");
                        Console.ReadKey(true);
                        Instance.sessionControl.Disconnect();
                        Instance.sessionControl = null;
                        Instance.gameControl = null;
                        ConsoleHelper.SuccessLine("AI test session disconnected!");
                    }
                    catch(GameException e)
                    {
                        ConsoleHelper.ErrorLine("Cannot join session: {0}", e.GetType());
                    }
                    Instance.UnsetAI();
                    Instance.aiTest = false;
                });
                serverCmd["session"] = sessionCmd;
                serverCmd["test"] = new FinalCommand<IServer>((server, cmd) =>
                {
                    if(Instance.sessionControl != null)
                    {
                        ConsoleHelper.ErrorLine("Already connected to a session!");
                        return;
                    }
                    int playerCount = 4;
                    if(cmd.Count != 0)
                    {
                        try
                        {
                            playerCount = int.Parse(cmd.Dequeue());
                        }
                        catch(FormatException)
                        {
                            ConsoleHelper.ErrorLine("Bad number format!");
                            return;
                        }
                    }

                    CreateSessionData csd = new CreateSessionData { Name = "Test", Description = "", MinPlayers = playerCount, MaxPlayers = playerCount, MaxSpectators = 0, DodgeCity = true };
                    CreatePlayerData cpd = new CreatePlayerData { Name = "Human" };
                    try
                    {
                        server.CreateSession(csd, cpd, Instance.mainSessionListener);
                        ConsoleHelper.SuccessLine("Test session created!");
                    }
                    catch(GameException e)
                    {
                        ConsoleHelper.ErrorLine("Cannot create session: {0}", e.GetType());
                    }
                });
                serverCmd["testai"] = new FinalCommand<IServer>((server, cmd) =>
                {
                    if(Instance.sessionControl != null)
                    {
                        ConsoleHelper.ErrorLine("Already connected to a session!");
                        return;
                    }
                    int playerCount = 4;
                    if(cmd.Count != 0)
                    {
                        try
                        {
                            playerCount = int.Parse(cmd.Dequeue());
                        }
                        catch(FormatException)
                        {
                            ConsoleHelper.ErrorLine("Bad number format!");
                            return;
                        }
                    }

                    CreateSessionData csd = new CreateSessionData { Name = "Test", Description = "", MaxPlayers = playerCount, MinPlayers = playerCount, MaxSpectators = 0, DodgeCity = true };
                    AI.AIPlayer ai = new AI.AIPlayer();
                    CreatePlayerData cpd = ai.CreateData;
                    cpd.Name = "TestAI";
                    try
                    {
                        server.CreateSession(csd, cpd, Instance.mainSessionListener);
                        Instance.SetAI(ai);
                        ConsoleHelper.SuccessLine("Test AI session created!");
                    }
                    catch(GameException e)
                    {
                        ConsoleHelper.ErrorLine("Cannot create session: {0}", e.GetType());
                    }
                });
                serverCmd["aitest"] = new FinalCommand<IServer>((server, cmd) =>
                {
                    if(Instance.sessionControl != null)
                    {
                        ConsoleHelper.ErrorLine("Already connected to a session!");
                        return;
                    }
                    int playerCount = 4;
                    if(cmd.Count != 0)
                    {
                        try
                        {
                            playerCount = int.Parse(cmd.Dequeue());
                        }
                        catch(FormatException)
                        {
                            ConsoleHelper.ErrorLine("Bad number format!");
                            return;
                        }
                    }

                    CreateSessionData csd = new CreateSessionData
                    {
                        Name = "AI Test",
                        Description = "An AI testing session.",
                        MaxPlayers = playerCount,
                        MinPlayers = playerCount,
                        MaxSpectators = 0,
                        PlayerPassword = new Password("_aitest"),
                        DodgeCity = true
                    };
                    AI.AIPlayer ai = new AI.AIPlayer();
                    CreatePlayerData cpd = ai.CreateData;
                    cpd.Name = "TestAI";
                    cpd.Password = new Password("_aitest");
                    try
                    {
                        server.CreateSession(csd, cpd, Instance.mainSessionListener);
                        Instance.SetAI(ai);
                        Instance.aiTest = true;
                        Instance.sessionControl.StartGame();
                        ConsoleHelper.SuccessLine("AI test session created!");
                        Console.ReadKey(true);
                        Instance.sessionControl.Disconnect();
                        Instance.sessionControl = null;
                        Instance.gameControl = null;
                        Instance.UnsetAI();
                        Instance.aiTest = false;
                        ConsoleHelper.SuccessLine("AI test session disconnected!");
                    }
                    catch(GameException e)
                    {
                        ConsoleHelper.ErrorLine("Cannot create session: {0}", e.GetType());
                    }
                });
                serverCmd["createsession"] = new FinalCommand<IServer>((server, cmd) =>
                {
                    if(Instance.sessionControl != null)
                    {
                        ConsoleHelper.ErrorLine("Already connected to a session!");
                        return;
                    }

                    ConsoleHelper.Print("Session Name: ");
                    string sessionName = ConsoleHelper.ReadLine();
                    ConsoleHelper.Print("Session Description: ");
                    string sessionDescription = ConsoleHelper.ReadLine();
                    int sessionMinPlayers;
                    int sessionMaxPlayers;
                    int sessionMaxSpectators;
                    try
                    {
                        ConsoleHelper.Print("Session MinPlayers: ");
                        sessionMinPlayers = int.Parse(ConsoleHelper.ReadLine());
                        ConsoleHelper.Print("Session MaxPlayers: ");
                        sessionMaxPlayers = int.Parse(ConsoleHelper.ReadLine());
                        ConsoleHelper.Print("Session MaxSpectators: ");
                        sessionMaxSpectators = int.Parse(ConsoleHelper.ReadLine());
                    }
                    catch(FormatException)
                    {
                        ConsoleHelper.ErrorLine("Bad number format!");
                        return;
                    }

                    ConsoleHelper.Print("Session PlayerPassword: "******"Session SpectatorPassword: "******"Session ShufflePlayers: ");
                    bool sessionShufflePlayers = ConsoleHelper.ReadLine().ToLower() == "y";
                    ConsoleHelper.Print("Session DodgeCity: ");
                    bool sessionDodgeCity = ConsoleHelper.ReadLine().ToLower() == "y";
                    ConsoleHelper.Print("Session HighNoon: ");
                    bool sessionHighNoon = ConsoleHelper.ReadLine().ToLower() == "y";
                    ConsoleHelper.Print("Session FistfulOfCards: ");
                    bool sessionFistfulOfCards = ConsoleHelper.ReadLine().ToLower() == "y";
                    ConsoleHelper.Print("Session WildWestShow: ");
                    bool sessionWildWestShow = ConsoleHelper.ReadLine().ToLower() == "y";
                    CreateSessionData csd = new CreateSessionData { Name = sessionName, Description = sessionDescription, MinPlayers = sessionMinPlayers, MaxPlayers = sessionMaxPlayers, MaxSpectators = sessionMaxSpectators, PlayerPassword = sessionPlayerPassword, SpectatorPassword = sessionSpectatorPassword, ShufflePlayers = sessionShufflePlayers, DodgeCity = sessionDodgeCity, HighNoon = sessionHighNoon,
                    FistfulOfCards = sessionFistfulOfCards, WildWestShow = sessionWildWestShow };

                    ConsoleHelper.Print("Player Name: ");
                    string playerName = ConsoleHelper.ReadLine();
                    ConsoleHelper.Print("Player Password: "******"Session created!");
                    }
                    catch(GameException e)
                    {
                        ConsoleHelper.ErrorLine("Cannot create session: {0}", e.GetType());
                    }
                });
                rootCmd["server"] = serverCmd;
                NestedCommand<IPlayerSessionControl> sessionControlCommand = new NestedCommand<IPlayerSessionControl>(cmd =>
                {
                    IPlayerSessionControl sessionControl = Instance.sessionControl;
                    if(sessionControl == null)
                    {
                        ConsoleHelper.ErrorLine("Not connected to any session!");
                        return null;
                    }
                    return sessionControl;
                });
                sessionControlCommand.MakePlayerSessionControlCommand(() =>
                {
                    Instance.sessionControl = null;
                    Instance.gameControl = null;
                });
                rootCmd["sessioncontrol"] = sessionControlCommand;
                NestedCommand<IPlayerControl> gameControlCommand = new NestedCommand<IPlayerControl>(cmd =>
                {
                    IPlayerControl gameControl = Instance.gameControl;
                    if(gameControl == null)
                    {
                        ConsoleHelper.ErrorLine("Not playing any game!");
                        return null;
                    }
                    return gameControl;
                });
                gameControlCommand.MakePlayerGameControlCommand();
                rootCmd["gamecontrol"] = gameControlCommand;
                rootCmd["exit"] = new FinalCommand(cmd =>
                {
                    _server.UnregisterListener(Instance);
                    Environment.Exit(0);
                });
                while(true) // command-line loop
                {
                    try
                    {
                        rootCmd.ReadAndExecute();
                    }
                    catch(InvalidOperationException)
                    {
                        ConsoleHelper.ErrorLine("Invalid command!");
                    }
                }
            }
            catch(RemotingException e)
            {
                ConsoleHelper.ErrorLine("Remoting error!");
            #if DEBUG
                ConsoleHelper.DebugLine(e.ToString());
            #endif
                return;
            }
            catch(SerializationException e)
            {
                ConsoleHelper.ErrorLine("Serialization error!");
            #if DEBUG
                ConsoleHelper.DebugLine(e.ToString());
            #endif
                return;
            }
        }