private void OnConnectButtonClick()
 {
     if (m_remoteGameServer.IsConnected)
     {
         m_remoteGameServer.Disconnect();
     }
     else
     {
         m_remoteGameServer.Connect();
     }
 }
Пример #2
0
        private static void InitGameOnConnectionStateChanged(bool preferRemote, string mapName, int playersCount, int botsCount, Action callback, Action <Error> error, INotification notification, IGameServer remoteGameServer)
        {
            ServerEventHandler <ValueChangedArgs <bool> > connectionStateChanged = null;

            connectionStateChanged = (Error e, ValueChangedArgs <bool> payload) =>
            {
#warning CHECK if actually unsubscribed

                if (!remoteGameServer.HasError(e))
                {
                    if (preferRemote)
                    {
                        if (!remoteGameServer.IsConnected)
                        {
                            remoteGameServer.Connect();
                            return;
                        }
                    }
                    else
                    {
                        if (remoteGameServer.IsConnected)
                        {
                            remoteGameServer.Disconnect();
                            return;
                        }
                    }

                    LogoffIfNeeded(() =>
                    {
                        InitGame(mapName, 0, 0, playersCount, botsCount, callback, error);
                    });
                }
                else
                {
                    notification.ShowError(e);

                    LogoffIfNeeded(() =>
                    {
                        InitGame(mapName, 0, 0, playersCount, botsCount, callback, error);
                    });
                }

                remoteGameServer.ConnectionStateChanged -= connectionStateChanged;
            };

            remoteGameServer.ConnectionStateChanged += connectionStateChanged;
        }
Пример #3
0
        private void OnGameServerReconnected(Error error, ValueChangedArgs <bool> payload)
        {
            if (!m_gameServer.IsConnected)
            {
                bool wasConnected = payload.OldValue;
                if (wasConnected)
                {
                    m_gameServerReconnectAttempts = GameConstants.ReconnectAttemptsCount;
                }

                if (m_gameServerReconnectAttempts > 0)
                {
                    Debug.LogWarning("GameServer is not connected. Trying to reconnect. Attempt = " + ((GameConstants.ReconnectAttemptsCount - m_gameServerReconnectAttempts) + 1));
                    m_gameServer.Connect();
                    m_gameServerReconnectAttempts--;
                }
                else
                {
                    m_notification.ShowError("Unable to connect to GameServer");
                }
            }
        }
Пример #4
0
        /*
        /// <summary>
        /// Accept incoming connection for NPC-Control
        /// </summary>
        /// <param name="ar"></param>
        protected void NCControl_Accept(IAsyncResult ar)
        {
            // Accept Socket
            NCConnection Client = new NCConnection(this, (Socket)NCListen.EndAcceptSocket(ar));
            Client.ReceiveData();
            NCList.Add(Client);

            // Listen for incoming connections
            NCListen.BeginAcceptSocket(cNCAccept, NCListen);
        }
        */
        /// <summary>
        /// Run NPC Server
        /// </summary>
        public void RunServer()
        {
            // Run Timeouts
            /*
            foreach (KeyValuePair<String, ServerWeapon> wep in WeaponList)
            {
                ScriptObj obj = (ScriptObj)wep.Value.ScriptObject;
                if (obj != null)
                    obj.RunEvents();
            }

            // Level-Npcs
            lock (TimerLock)
            {
                foreach (KeyValuePair<String, GraalLevel> lvl in LevelList)
                {
                    foreach (KeyValuePair<int, GraalLevelNPC> npc in lvl.Value.NpcList)
                    {
                        ScriptObj obj = (ScriptObj)npc.Value.ScriptObject;
                        if (obj != null)
                            obj.RunEvents();
                    }
                }
            }

            while (Compiler.RunList.Count > 0)
            {
                IRefObject obj;
                lock (Compiler.RunList)
                    obj = Compiler.RunList.Dequeue();
                obj.Call("onCreated", null);
            }
            */
            this.form = Abstraction.GetInstance();

            this.form.Activate();
            this.form.WriteText("--- OpenGraal Instant Messenger / Remote Control ---");
            this.serverWindow = ServerWindow.GetInstance();
            // Default PM
            this.NCMsg = CString.tokenize("I am the Framework for\nthis game server. Almost\nall npc actions are controled\nby me.");

            // Create Compiler
            //Compiler = new GameCompiler(this);

            // Create Player Manager
            PlayerManager = new GraalPlayerList();

            // Connect to GServer
            GSConn = (IGameServer)new Connections.GraalServer();
            GSConn.RunServer();

            if (this.serverWindow.nickname != null && this.serverWindow.nickname != "")
                this.nickname = this.serverWindow.nickname;

            if (this.serverWindow.username != null && this.serverWindow.username != "")
                this.username = this.serverWindow.username;

            if (this.serverWindow.password != null && this.serverWindow.password != "")
                this.password = this.serverWindow.password;

            if (this.serverWindow.gserver != null && this.serverWindow.gserver != "")
                server = this.serverWindow.gserver;

            if (this.serverWindow.gport != null && this.serverWindow.gport != 0)
                this.port = this.serverWindow.gport;

            //GSConn.Disconnect();
            if (!GSConn.Connected)
            {
                this.form.WriteText("Not connected, connecting...");
                this.form.WriteText("Address:\t" + this.server);
                this.form.WriteText("Port:\t\t" + this.port.ToString());
                GSConn.Connect(this.server, this.port);

            }
            else
                this.form.WriteText("Already connected!");

            if (GSConn.Connected)
            {
                GSConn.SendLogin(this.username, this.password, this.nickname);
            }
        }
Пример #5
0
        public static void Init(string mapName, int playersCount, int botsCount, bool preferRemote, Action callback, Action <Error> error)
        {
            if (playersCount < 0)
            {
                throw new ArgumentOutOfRangeException("playersCount");
            }

            if (botsCount < 0)
            {
                throw new ArgumentOutOfRangeException("botsCount");
            }

            if (playersCount > GameConstants.MaxLocalPlayers)
            {
                throw new ArgumentException("playersCount > " + GameConstants.MaxLocalPlayers);
            }

            if (playersCount + botsCount > 8 || playersCount + botsCount < 2)
            {
                throw new ArgumentException("players + bots should be >= 2 and <= 8");
            }

            //IProgressIndicator progress = Dependencies.Progress;
            //progress.IsVisible = true;

            INotification notification     = Dependencies.Notification;
            IGameServer   remoteGameServer = Dependencies.RemoteGameServer;

            if (preferRemote)
            {
                if (remoteGameServer.IsConnectionStateChanging)
                {
                    InitGameOnConnectionStateChanged(preferRemote, mapName, playersCount, botsCount, callback, error, notification, remoteGameServer);
                }
                else if (!remoteGameServer.IsConnected)
                {
                    InitGameOnConnectionStateChanged(preferRemote, mapName, playersCount, botsCount, callback, error, notification, remoteGameServer);
                    remoteGameServer.Connect();
                }
                else
                {
                    LogoffIfNeeded(() =>
                    {
                        InitGame(mapName, 0, 0, playersCount, botsCount, callback, error);
                    });
                }
            }
            else
            {
                if (remoteGameServer != null && remoteGameServer.IsConnectionStateChanging)
                {
                    InitGameOnConnectionStateChanged(preferRemote, mapName, playersCount, botsCount, callback, error, notification, remoteGameServer);
                }
                else if (remoteGameServer != null && remoteGameServer.IsConnected)
                {
                    InitGameOnConnectionStateChanged(preferRemote, mapName, playersCount, botsCount, callback, error, notification, remoteGameServer);
                    remoteGameServer.Disconnect();
                }
                else
                {
                    LogoffIfNeeded(() =>
                    {
                        InitGame(mapName, 0, 0, playersCount, botsCount, callback, error);
                    });
                }
            }
        }