Beispiel #1
0
        public static bool BuySkin(AccountModel acc, int skinType)
        {
#if DEBUG
            if (acc == null)
            {
                throw new Exception("Undefined account");
            }
#endif
            if (!Resources.Type2Skin.ContainsKey((ushort)skinType))
            {
                return(false);
            }
            if (acc.OwnedSkins.Contains(skinType))
            {
                return(false);
            }
            if (acc.Stats.Credits < SkinPrice)
            {
                return(false);
            }
            acc.Stats.Credits -= SkinPrice;
            acc.OwnedSkins.Add(skinType);
            acc.Save();
            return(true);
        }
Beispiel #2
0
        public static bool IsAccountInUse(AccountModel acc)
        {
            bool accountInUse = acc.Connected && Manager.GetClient(acc.Id) != null;

            if (!accountInUse && acc.Connected)
            {
                acc.Connected = false;
                acc.Save();
            }
            return(accountInUse);
        }
Beispiel #3
0
        public static bool BuyCharSlot(AccountModel acc)
        {
#if DEBUG
            if (acc == null)
            {
                throw new Exception("Undefined account");
            }
#endif
            if (acc.Stats.Fame < CharSlotPrice)
            {
                return(false);
            }
            acc.Stats.Fame -= CharSlotPrice;
            acc.MaxNumChars++;
            acc.Save();
            return(true);
        }
Beispiel #4
0
        public static bool DeleteCharacter(AccountModel acc, int charId)
        {
            if (!acc.AliveChars.Contains(charId))
            {
                return(false);
            }

            CharacterModel character = new CharacterModel(acc.Id, charId);

            character.Load();

            character.Deleted = true;
            character.Save();

            acc.AliveChars.Remove(charId);
            acc.Save();
            return(true);
        }
Beispiel #5
0
        public static void Death(string killer, AccountModel acc, CharacterModel character)
        {
#if DEBUG
            if (character == null)
            {
                throw new Exception("Undefined character model");
            }
#endif
            acc.AliveChars.Remove(character.Id);
            if (acc.DeadChars.Count == AccountModel.MaxDeadCharsStored)
            {
                acc.DeadChars.RemoveAt(AccountModel.MaxDeadCharsStored - 1);
            }
            acc.DeadChars.Insert(0, character.Id);

            int deathTime = UnixTime();
            int baseFame  = character.Fame;
            int totalFame = character.Fame;

            XElement fame = new XElement("Fame");
            XElement ce   = character.ExportFame();
            ce.Add(new XAttribute("id", character.Id));
            ce.Add(new XElement("Account", new XElement("Name", acc.Name)));
            fame.Add(ce);
            character.FameStats.ExportTo(fame);

            ClassStatsInfo classStats = acc.Stats.GetClassStats(character.ClassType);
            FameStats      fameStats  = CalculateStats(acc, character, killer);
            totalFame = fameStats.TotalFame;
            foreach (FameBonus bonus in fameStats.Bonuses)
            {
                fame.Add(new XElement("Bonus", new XAttribute("id", bonus.Name), bonus.Fame));
            }

            fame.Add(new XElement("CreatedOn", character.CreationTime));
            fame.Add(new XElement("KilledOn", deathTime));
            fame.Add(new XElement("KilledBy", killer));
            fame.Add(new XElement("BaseFame", baseFame));
            fame.Add(new XElement("TotalFame", totalFame));

            if (classStats.BestFame < baseFame)
            {
                classStats.BestFame = baseFame;
            }

            if (classStats.BestLevel < character.Level)
            {
                classStats.BestLevel = character.Level;
            }


            character.Dead      = true;
            character.DeathTime = UnixTime();
            character.DeathFame = totalFame;
            character.Save();

            acc.Stats.Fame         += totalFame;
            acc.Stats.TotalCredits += totalFame;
            acc.Save();

            if (character.Fame >= MinFameRequiredToEnterLegends)
            {
                PushLegend(acc.Id, character.Id, totalFame, deathTime);
            }

            CreateKey($"death.{acc.Id}.{character.Id}", fame.ToString());
        }
Beispiel #6
0
        public static RegisterStatus RegisterAccount(string username, string password, string ip)
        {
            if (!CanRegisterAccount(ip))
            {
                return(RegisterStatus.TooManyRegisters);
            }

            if (!IsValidUsername(username))
            {
                return(RegisterStatus.InvalidUsername);
            }

            if (!IsValidPassword(password))
            {
                return(RegisterStatus.InvalidPassword);
            }

            if (IdFromUsername(username) != -1)
            {
                return(RegisterStatus.UsernameTaken);
            }

            int    id   = int.Parse(GetKey("nextAccId", true));
            string salt = MathUtils.GenerateSalt();

            SetKey("nextAccId", (id + 1).ToString(), true);

            SetKey($"login.username.{username}", id.ToString());
            SetKey($"login.id.{id}", username);
            SetKey($"login.hash.{id}", (password + salt).ToSHA1());
            SetKey($"login.salt.{id}", salt);

            AccountModel acc = new AccountModel(id)
            {
                Stats = new StatsInfo
                {
                    BestCharFame = 0,
                    TotalFame    = 0,
                    Fame         = 0,
                    TotalCredits = 0,
                    Credits      = 0,
                    ClassStats   = CreateClassStats()
                },

                MaxNumChars   = 1,
                NextCharId    = 0,
                AliveChars    = new List <int>(),
                DeadChars     = new List <int>(),
                OwnedSkins    = new List <int>(),
                Ranked        = false,
                Muted         = false,
                Banned        = false,
                GuildName     = null,
                GuildRank     = 0,
                Connected     = false,
                LockedIds     = new List <int>(),
                IgnoredIds    = new List <int>(),
                AllyDamage    = true,
                AllyShots     = true,
                Effects       = true,
                Sounds        = true,
                Notifications = true,
                RegisterTime  = UnixTime()
            };

            acc.Save();
            AddRegisteredAccount(ip);
            return(RegisterStatus.Success);
        }
Beispiel #7
0
        public static CharacterModel CreateCharacter(AccountModel acc, int classType, int skinType)
        {
#if DEBUG
            if (acc == null)
            {
                throw new Exception("Account is null.");
            }
#endif
            if (!HasEnoughCharacterSlots(acc))
            {
                return(null);
            }

            if (!Resources.Type2Player.TryGetValue((ushort)classType, out PlayerDesc player))
            {
                return(null);
            }

            if (skinType != 0)
            {
                if (!Resources.Type2Skin.TryGetValue((ushort)skinType, out SkinDesc skin))
                {
                    return(null);
                }
                if (skin.PlayerClassType != classType)
                {
                    return(null);
                }
            }

            int newId = acc.NextCharId;
            acc.NextCharId++;
            acc.AliveChars.Add(newId);
            acc.Save();

            CharacterModel character = new CharacterModel(acc.Id, newId)
            {
                ClassType     = classType,
                Level         = 1,
                Experience    = 0,
                Fame          = 0,
                Inventory     = player.Equipment.ToArray(),
                ItemDatas     = player.ItemDatas.ToArray(),
                Stats         = player.StartingValues.ToArray(),
                HP            = player.StartingValues[0],
                MP            = player.StartingValues[1],
                Tex1          = 0,
                Tex2          = 0,
                SkinType      = skinType,
                HasBackpack   = false,
                HealthPotions = Player.MaxPotions,
                MagicPotions  = Player.MaxPotions,
                CreationTime  = UnixTime(),
                Deleted       = false,
                Dead          = false,
                DeathFame     = -1,
                DeathTime     = -1,
                FameStats     = new FameStatsInfo(),
                PetId         = -1
            };

            character.Save();
            return(character);
        }