def open_pack(username: str, pack_name: str): """ Given a user by its username and the name of a pack type, which the user owns, this function will open that pack for the user. This means it will evaluate the slot probabilities for all 5 slots of the pack. Based on these probabilities 5 rewards are randomly chosen from the set of available rewards. These rewards will be added to the user. The pack will be removed, as it has been used up. CHANGELOG Added 12.06.2019 :param username: :param pack_name: :return: """ user = User.get(User.name == username) pack = user.get_packs(pack_name)[0] rarity_reward_map = available_rewards_by_rarity() for i in range(1, 6, 1): slot = getattr(pack, f'slot{i}') rarity = slot.choice() reward = np.random.choice(rarity_reward_map[rarity], 1)[0] parameters_adapter = RewardParametersAdapter(reward['name'], reward) user.add_reward(parameters_adapter.parameters()) user.use_pack(pack_name) user.save()
def test_user_adding_gold_and_dust_works(self): # Setting up the database self.setup_sample_user() # Loading the user, adding to its gold and dust, saving it again and loading it again to check if the # changes have stayed user = User.get(User.name == self.SAMPLE_USER_NAME) self.assertEqual(user.gold, 0) self.assertEqual(user.dust, 0) user.gold += 100 user.dust += 100 user.save() user = User.get(User.name == self.SAMPLE_USER_NAME) self.assertEqual(user.gold, 100) self.assertEqual(user.dust, 100)
def test_database_working(self): # Creating the user and saving it into the database user = User(name=self.SAMPLE_USER_NAME, password=self.SAMPLE_USER_PASSWORD, gold=0, dust=0) user.save() # Attempting to load it user = User.get(User.name == self.SAMPLE_USER_NAME) self.assertTrue(user.password.check(self.SAMPLE_USER_PASSWORD))
def setup_sample_pack(self): # Loading the sample user to be the owner of this pack user = User.get(User.name == self.SAMPLE_USER_NAME) pack = Pack(name=self.SAMPLE_PACK_NAME, slug=self.SAMPLE_PACK_SLUG, description=self.SAMPLE_PACK_DESCRIPTION, gold_cost=self.SAMPLE_PACK_GOLD, date_obtained=self.SAMPLE_PACK_DATE, slot1=self.SAMPLE_PACK_PROBABILITY, slot2=self.SAMPLE_PACK_PROBABILITY, slot3=self.SAMPLE_PACK_PROBABILITY, slot4=self.SAMPLE_PACK_PROBABILITY, slot5=self.SAMPLE_PACK_PROBABILITY, user=user) pack.save()
def test_pack_creation_works(self): # Setting up the database self.setup_sample_user() user = User.get(User.name == self.SAMPLE_USER_NAME) pack = Pack(name='Simple Pack', slug='simple_pack', description='The most basic of rewards', gold_cost=1000, date_obtained=datetime.datetime.now(), slot1=self.SAMPLE_PACK_PROBABILITY, slot2=self.SAMPLE_PACK_PROBABILITY, slot3=self.SAMPLE_PACK_PROBABILITY, slot4=self.SAMPLE_PACK_PROBABILITY, slot5=self.SAMPLE_PACK_PROBABILITY, user=user) pack.save() # Loading the pack to see it it worked pack = Pack.get(Pack.slug == 'simple_pack') self.assertEqual(pack.name, 'Simple Pack')
def get_sample_user(self) -> User: user = User.get(User.name == self.SAMPLE_USER_PARAMETERS['name']) return user
def update(self): self.user = User.get(User.name == self.username)
def get_standard_user(self): return User.get(User.name == self.STANDARD_USER_PARAMETERS['name'])
def get_sample_user(self): return User.get(User.name == self.SAMPLE_USER_NAME)