示例#1
0
        public List<IModel> GetUsers()
        {
            using (IDal dal = new ProtoDBContext())
            {
                var data = dal.GetUsers().ToList();

                var users = new List<IModel>();

                var mapper = new ProtoUserMap(dal);
                var petmapper = new ProtoPetMap(dal);
                foreach (var user in data)
                {
                    var userModel = mapper.Map(user as IDBModel) as UserModel;

                    var pets = new List<IPet>();

                    foreach (var pet in userModel.Pets)
                    {
                        pets.Add(petmapper.Map(pet as IDBModel) as IPet);
                    }
                    userModel.Pets = pets;
                    users.Add(userModel);
                }

                return users;
            }
        }
示例#2
0
        public bool AddPet(IPet pet)
        {
            using (IDal dal = new ProtoDBContext())
            {
                var mapper = new ProtoPetMap(dal);

                var dbmodel = mapper.Map(pet as IModel);

                return dal.AddPet(dbmodel as ProtoPetModel);
            }
        }
示例#3
0
        public bool Authenticate(string username, string password)
        {
            using (IDal dal = new ProtoDBContext())
            {
                var user = dal.GetUserByName(username);

                if (user == null)
                    return false;

                if (user.Password != password)
                    return false;
            }
            return true;
        }
示例#4
0
        public bool? AddPet(AddPetMessage message)
        {
            using (IDal dal = new ProtoDBContext())
            {
                var user = dal.GetUserByName(Thread.CurrentPrincipal.Identity.Name);

                if (user == null)
                    return null;

                if (user.ID != message.UserId)
                    return null;
                var petmapper = new ProtoPetMap(dal);

                return dal.AddPet(petmapper.Map(message.Model) as IPet);
            }
        }
示例#5
0
        public IEnumerable<IModel> GetPets()
        {
            using (IDal dal = new ProtoDBContext())
            {
                var data = dal.GetPets().ToList();

                var pets = new List<IModel>();

                var mapper = new ProtoPetMap(dal);

                foreach (var pet in data)
                {
                    var petModel = mapper.Map(pet as IDBModel);

                    pets.Add(petModel);
                }
                return pets;
            }
        }
示例#6
0
        public IModel GetUser(Guid id)
        {
            using (IDal dal = new ProtoDBContext())
            {
                var data = dal.GetUserById(id);

                var userModel = new ProtoUserMap(dal).Map(data as IDBModel) as UserModel;

                var pets = new List<IPet>();

                var petmapper = new ProtoPetMap(dal);

                foreach (var pet in userModel.Pets)
                {
                    pets.Add(petmapper.Map(pet as IDBModel) as IPet);
                }

                userModel.Pets = pets;

                return userModel;
            }
        }
示例#7
0
        public IModel UpdatePet(PetUpdateMessage message)
        {
            using (IDal dal = new ProtoDBContext())
            {
                var pet =
                    dal.GetPets().FirstOrDefault(x => x.ID == message.PetID && x.OwnerID == message.UserID) as
                        ProtoPetModel;

                if (pet == null)
                {
                    return null;
                }

                var now = DateTime.Now;

                var delta = (float) ((now - pet.LastChangeDate).TotalSeconds);

                var happinessReduction = pet.HappinessDecay*delta;
                var hungerIncrease = pet.HungerDecay*delta;

                //floats clamp
                pet.Happiness = pet.Happiness - happinessReduction;
                pet.Hunger = pet.Hunger + hungerIncrease;
                pet.LastChangeDate = now;

                //hacky implementation, going to seperate this in to seperate updaters so they can be a bit more interesting.
                switch (pet.Type)
                {
                    case PetType.Aloof:
                        if (message.UpdateActions.HasFlag(PetActions.Feed))
                        {
                            pet.Hunger -= 1000;
                        }
                        if (message.UpdateActions.HasFlag(PetActions.Pet))
                        {
                            pet.Happiness += 200;
                        }
                        break;
                    case PetType.Needy:
                        if (message.UpdateActions.HasFlag(PetActions.Feed))
                        {
                            pet.Hunger -= 800;
                        }
                        if (message.UpdateActions.HasFlag(PetActions.Pet))
                        {
                            pet.Happiness += 2000;
                        }
                        break;
                    case PetType.Big:
                        if (message.UpdateActions.HasFlag(PetActions.Feed))
                        {
                            pet.Hunger -= 200;
                            pet.Happiness += 500;
                        }
                        if (message.UpdateActions.HasFlag(PetActions.Pet))
                        {
                            pet.Happiness += 500;
                        }
                        break;
                    case PetType.Small:
                        if (message.UpdateActions.HasFlag(PetActions.Feed))
                        {
                            pet.Hunger -= 5000;
                        }
                        if (message.UpdateActions.HasFlag(PetActions.Pet))
                        {
                            pet.Happiness += 500;
                        }
                        break;
                }

                dal.UpdatePet(pet);

                return new ProtoPetMap(dal).Map(pet);
            }
        }