private void LoginRequestReceived(MessageReceivedEventArgs ev)
        {
            var msg = (LoginMessage)ev.Message;

            ISession session = this.sessionfactory.OpenSession ();

            Account account = session.CreateCriteria<Account>()
                .Add(Restrictions.InsensitiveLike("Username", msg.Username ))
                .Add(Restrictions.Eq("Password", msg.Password ))
                .List<Account>().FirstOrDefault();

            if (account == null || this.players.ContainsPlayer((uint)account.ID))
            {
                ev.Connection.Send (new LoginFailedMessage (ConnectionRejectedReason.BadLogin));
                return;
            }

            Character character = session.CreateCriteria<Character> ()
                .Add(Restrictions.Eq("AccountID", account.ID))
                .List<Character>().FirstOrDefault();

            character.Location = new ScreenPoint(character.MapX * 32, character.MapY * 32);
            character.Animation = Enum.GetName (typeof(Directions), character.Direction);
            character.NextMoveActive = true;

            // Create player
            NetworkPlayer player = new NetworkPlayer();
            player.Character = character;
            player.AccountID = account.ID;
            //player.NetworkID = ++networkid;
            player.NetworkID = (uint) account.ID;
            player.Character.NetworkID = player.NetworkID;
            player.Connection = ev.Connection;
            player.Character.MapChunkName = this.GetChunkName(player.Character.WorldName, player.Character.MapLocation);
            player.State = PlayerState.LoggedIn;

            // Add to the list of players in the servers memory
            this.players.AddPlayer(player);

            // Send them authentication ID
            player.Connection.Send(new LoginSuccessMessage () { NetworkIDAssigned = player.NetworkID });

            var handler = this.UserLoggedIn;
            if(handler != null)
                handler(this, new UserEventArgs(player));

            // Update other players
            var updatemsg = new PlayerUpdateMessage ()
            {
                CharacterName = player.Character.Name,
                NetworkID = player.NetworkID,
                Action = PlayerUpdateAction.Add
            };

            foreach (var tmp in this.players.GetPlayers())
                tmp.Connection.Send(updatemsg);

            session.Close ();
        }
 public void AddPlayer(NetworkPlayer player)
 {
     lock(this.PlayerLock)
     {
         this.players.Add(player.NetworkID, player);
         this.ranges.Add (player.NetworkID, new List<NetworkPlayer> ());
     }
 }
        public void Trigger(NetworkPlayer player)
        {
            ServerEventMessage message = new ServerEventMessage ();

            message.EventCommands.Add (new JumpCommand (player.Character.Direction, 2));

            player.Connection.Send (message);
        }
        public void Trigger(NetworkPlayer player)
        {
            ServerEventMessage message = new ServerEventMessage ();

            message.EventCommands.Add (new LockCommand ());
            message.EventCommands.Add (new JumpCommand (player.Character.Direction, 0)); // Jump in place
            message.EventCommands.Add (new PauseEventCommand ()); // Wait for jumping to finish
            message.EventCommands.Add (new ApplyMovementCommand (Directions.East, Directions.East, Directions.East, Directions.North));
            message.EventCommands.Add (new PauseEventCommand ()); // Wait for movement to finish
            message.EventCommands.Add (new ReleaseCommand ());

            player.Connection.Send (message);
        }
 public bool ContainsPlayer(NetworkPlayer player)
 {
     return this.players.ContainsValue (player);
 }
 public RangeChangeInfo(NetworkPlayer player, RangeChangeStates state)
 {
     this.Player = player;
     this.State = state;
 }
        public bool SetPlayer(uint NetworkID, NetworkPlayer player)
        {
            lock(this.PlayerLock)
            {
                if(!this.players.ContainsKey(NetworkID))
                    return false;

                this.players[NetworkID] = player;
            }

            return true;
        }