Ejemplo n.º 1
0
        public static AuthKey Get(string authenticationKey)
        {
            if (!String.IsNullOrEmpty(authenticationKey))
            {
                string plainText = Cryptography.Decrypt(authenticationKey);

                if (!String.IsNullOrEmpty(plainText))
                {
                    string[] values = plainText.Split('|');
                    if (values != null && values.Length >= 4)
                    {
                        var  token = new AuthKey(new Guid(values[0]), values[2], 0, null);
                        long ticks;
                        if (Int64.TryParse(values[1], out ticks))
                        {
                            token.Date = new DateTime(ticks);
                        }
                        int id;
                        if (Int32.TryParse(values[3], out id))
                        {
                            token.ID = id;
                        }
                        if (values.Length > 4)
                        {
                            for (int i = 4; i < values.Length; i++)
                            {
                                token.Args.Add(values[i]);
                            }
                        }
                        return(token);
                    }
                }
            }
            return(AuthKey.Empty);
        }
Ejemplo n.º 2
0
 public string GetUserNameFromCommand(RdlCommand cmd)
 {
     if (cmd.Group != null)
     {
         AuthKey key = AuthKey.Get(cmd.Group.AuthKey);
         return(key.UserName);
     }
     return(String.Empty);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Processes commands in the context of the virtual world.
        /// </summary>
        /// <param name="requestHandler">The ICommunicationHandler processing the request from the client.</param>
        /// <param name="commands">The commands to process.</param>
        /// <returns>The IClient instance of the current requestor.</returns>
        public IClient ProcessCommands(ICommunicationHandler requestHandler, RdlCommandGroup commands, Guid sessionId, string address)
        {
            bool    addClient = false;
            IClient client    = null;

            if (commands.Count > 0)
            {
                if (!String.IsNullOrEmpty(commands.AuthKey))
                {
                    AuthKey key = AuthKey.Get(commands.AuthKey);
                    if (this.ValidateAuthKey(key))
                    {
                        // Clients are stored with the UserName of the current user as the key. This
                        // should prevent a user from playing multiple characters at the same time unless
                        // they have multiple user names, which violates the TOS.
                        if (!this.Clients.ContainsKey(key.SessionId))
                        {
                            client          = requestHandler.CreateClient(key.SessionId);
                            client.Address  = address;
                            client.UserName = key.UserName;
                            addClient       = true;
                        }
                        else
                        {
                            client = this.Clients[key.SessionId];
                        }
                        if (client != null)
                        {
                            //client.LastHeartbeatDate = DateTime.Now;
                            client.AuthKey = key;
                        }
                    }
                }
                else
                {
                    // New client will implement the LoginCommandHandler.
                    client         = requestHandler.CreateClient(sessionId);
                    client.Address = address;
                    addClient      = true;
                }

                if (addClient)
                {
                    this.Clients.Add(client);
                    this.World.Provider.CreateSession(client, client.SessionId.ToString());
                    Logger.LogDebug("SERVER: New client connected from: {0}", client.SessionId.ToString());
                }

                if (client != null)
                {
                    ProcessCommands(client, commands);
                }
            }
            return(client);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a player instance in the virtual world.
        /// </summary>
        /// <param name="context">The current IMessageContext of the requestor.</param>
        /// <param name="cmd">The RdlCommand containing the player parameters.</param>
        /// <param name="player">The player instance to create.</param>
        public bool CreatePlayer(IMessageContext context, RdlCommand cmd, IPlayer player)
        {
            // Command arguments are grouped into key:value pairs, each argument is a key:value pair.

            // Find the user from rdlcommandgroup associated with this command.
            bool   success = false;
            string message = SR.CreateCharacterInvalidUser;

            if (cmd.Group != null)
            {
                AuthKey key = AuthKey.Get(cmd.Group.AuthKey);

                //=================================================================================
                // Ensure the user can create additional character.
                //=================================================================================
                UserDetail user = this.Provider.GetUserDetail(key.UserName);
                if (user == null)
                {
                    // Use the default error message.
                    goto finished;
                }
                int playerCount = this.Provider.GetPlayers(user.UserName).Count;
                if (user.MaxCharacters == 0)
                {
                    // Preset the max characters to the world default.
                    user.MaxCharacters = this.DefaultMaxCharacters;
                    this.Provider.SaveUserDetail(user);
                }
                if (playerCount >= user.MaxCharacters)
                {
                    message = SR.CreateCharacterMaxExceeded(this.DefaultMaxCharacters);
                    goto finished;
                }
                player.Properties.SetValue("UserName", user.UserName);

                //=================================================================================
                // Parse the key:value pairs.
                //=================================================================================
                foreach (var item in cmd.Args)
                {
                    if (item != null)
                    {
                        string[] pairs = item.ToString().Split(':');
                        if (pairs != null && pairs.Length == 2)
                        {
                            if (pairs[0].ToLower().Equals("name"))
                            {
                                player.Name = pairs[1];
                            }
                            else
                            {
                                player.Properties.SetValue(pairs[0], pairs[1]);
                            }
                        }
                    }
                }

                //=================================================================================
                // Validate player name.
                //=================================================================================
                if (!this.IsValidName(player.Name, out message))
                {
                    goto finished;
                }

                //=================================================================================
                // Ensure a gender is specified.
                //=================================================================================
                if (player.Gender == Gender.None)
                {
                    message = SR.CreateCharacterInvalidGender;
                    goto finished;
                }

                //=================================================================================
                // Ensure a race is specified.
                //=================================================================================
                if (String.IsNullOrEmpty(player.Race) || !this.Races.ContainsKey(player.Race))
                {
                    message = SR.CreateCharacterInvalidRace;
                    goto finished;
                }
                // Set the player's location from the race instance.
                player.Location = this.Races[player.Race].StartingLocation;

                //=================================================================================
                // Ensure that attributes are within acceptable range.
                //=================================================================================
                int attrTotal = 0;
                foreach (var item in player.Attributes)
                {
                    if (item.Value < -1 || item.Value > 8)
                    {
                        message = SR.CreateCharacterAttributeOutOfRange(item.Key);
                        goto finished;
                    }
                    else
                    {
                        attrTotal += item.Value;
                    }
                }
                int raceDefaultAttrTotal = 0;
                foreach (var item in this.Races[player.Race].Attributes)
                {
                    if (item.Value > 0)
                    {
                        raceDefaultAttrTotal += item.Value;
                    }
                }
                // The max number of attribute points depends largely on the value of the attributes selected.
                if (attrTotal > (AttributeList.MaxAttributePoints + raceDefaultAttrTotal))                 // plus race bonus defaults
                {
                    message = SR.CreateCharacterAttributePointsOverLimit;
                    goto finished;
                }

                //=================================================================================
                // Set Body and Mind values.
                //=================================================================================
                int body = player.Attributes.Stamina * this.RealismMultiplier;
                int mind = player.Attributes.Endurance * this.PowerMultiplier;
                if (body == 0)
                {
                    body = 4;
                }
                player.SetBody(body, body);
                player.SetMind(mind, mind);


                //=================================================================================
                // Ensure that skills contain acceptable values and that they do not exceed 32 in value.
                //=================================================================================
                double skillTotal = 0;
                foreach (var item in player.Skills)
                {
                    if (item.Value < 0 || item.Value > SkillManager.MaxSkillPointsInCharacterCreation)
                    {
                        message = SR.CreateCharacterSkillOutOfRange(item.Key);
                        goto finished;
                    }
                    else
                    {
                        skillTotal += item.Value;
                    }
                }
                if ((int)skillTotal > SkillManager.MaxSkillPointsInCharacterCreation)
                {
                    message = SR.CreateCharacterSkillPointsOverLimit;
                    goto finished;
                }

                //=================================================================================
                // Add any missing skills to the players list of skills.
                //=================================================================================
                foreach (var skill in this.Skills)
                {
                    if (!player.Skills.ContainsKey(skill.Key))
                    {
                        player.Skills.Add(skill.Key, 0);
                    }
                }

                //=================================================================================
                // Check to ensure the name has not been taken and it passes the word filter.
                //=================================================================================
                if (!this.IsValidName(player.Name, out message))
                {
                    goto finished;
                }

                //=================================================================================
                // Save the player instance and send back a success message.
                //=================================================================================
                this.Provider.SavePlayer <IPlayer>(player);
                if (player.ID > 0)
                {
                    success = true;
                    message = SR.CreateCharacterSuccess;
                }
            }

finished:
            context.Add(new RdlCommandResponse(cmd.TypeName, success, message));

            return(success);
        }
Ejemplo n.º 5
0
 private bool ValidateAuthKey(AuthKey key)
 {
     return(this.World.Provider.ValidateAuthKey(key));
 }