def add_card_to_user(self, card_name: str, user_name: str):
     card_cost = 377  # 13 sickles
     user_doc_ref = self.firestore_db.collection(
         self.user_coll_name).document(user_name)
     user = User.from_dict(user_doc_ref.get().to_dict())
     if user.total_currency_value >= card_cost:
         new_value = user.total_currency_value - card_cost
         user.set_coins(*User.get_coins_from_value(new_value))
         card_doc_ref = self.firestore_db.collection(
             self.card_coll_name).document(card_name)
         user_doc_ref.update({
             u'cards':
             firestore_api.ArrayUnion([card_doc_ref.path]),
             u'galleons':
             user.galleons,
             u'sickles':
             user.sickles,
             u'knuts':
             user.knuts
         })
         return user  # Return the new values
     else:
         raise InsufficientFundsException(
             "You do not have enough wizard gold to purchase a chocolate frog",
             user=user)
    def give_item_to_user(self, item_name: str, giver: str,
                          receiver: str) -> bool:
        giver_doc_ref = self.firestore_db.collection(
            self.user_coll_name).document(giver)
        giver_user = User.from_dict(giver_doc_ref.get().to_dict())
        giver_owned_items = [
            ref.split("/")[-1] for ref in giver_user.inventory
        ]
        item_doc_ref = self.firestore_db.collection(
            self.item_coll_name).document(item_name)

        if item_name in giver_owned_items:
            receiver_doc_ref = self.firestore_db.collection(
                self.user_coll_name).document(receiver)
            # Add item to receiver inventory
            receiver_doc_ref.update(
                {u'inventory': firestore_api.ArrayUnion([item_doc_ref.path])})
            # Remove item from giver inventory
            giver_doc_ref.update(
                {u'inventory': firestore_api.ArrayRemove([item_doc_ref.path])})
        else:
            raise UnownedItemException(
                f"You can't give {item_name} because you don't own it",
                user=giver_user,
                item_name=item_name)
예제 #3
0
def test_array_union(client, cleanup):
    doc_ref = client.document("gcp-7523", "test-document")
    cleanup(doc_ref.delete)
    doc_ref.delete()
    tree_1 = {"forest": {"tree-1": "oak"}}
    tree_2 = {"forest": {"tree-2": "pine"}}
    tree_3 = {"forest": {"tree-3": firestore.ArrayUnion(["spruce"])}}

    doc_ref.set(tree_1)
    expected = tree_1.copy()
    assert doc_ref.get().to_dict() == expected

    doc_ref.set(tree_2, merge=True)
    expected["forest"]["tree-2"] = tree_2["forest"]["tree-2"]
    assert doc_ref.get().to_dict() == expected

    doc_ref.set(tree_3, merge=True)
    expected["forest"]["tree-3"] = ["spruce"]
    assert doc_ref.get().to_dict() == expected

    tree_3_part_2 = {"forest": {"tree-3": firestore.ArrayUnion(["palm"])}}
    expected["forest"]["tree-3"].append("palm")
    doc_ref.set(tree_3_part_2, merge=True)
    assert doc_ref.get().to_dict() == expected
 def add_item_to_user(self, item_name: str, user_name: str):
     user_doc_ref = self.firestore_db.collection(
         self.user_coll_name).document(user_name)
     user = User.from_dict(user_doc_ref.get().to_dict())
     item_doc_ref = self.firestore_db.collection(
         self.item_coll_name).document(item_name)
     item = Item.from_dict(item_doc_ref.get().to_dict())
     if user.total_currency_value >= item.value:
         new_value = user.total_currency_value - item.value
         user.set_coins(*User.get_coins_from_value(new_value))
         user_doc_ref.update({
             u'inventory':
             firestore_api.ArrayUnion([item_doc_ref.path]),
             u'galleons':
             user.galleons,
             u'sickles':
             user.sickles,
             u'knuts':
             user.knuts
         })
         return user  # Return the new values
     else:
         raise InsufficientFundsException(
             f"You do not have enough wizard gold to purchase {item_name}")
예제 #5
0
                  'anything'})  # Non-existent new field added by update

# Add some unknown ids
'''
db.collection ('users').add ({
    u'age': 60,
    u'department': u'ACC',
    u'name': u'Susy',
    u'born': 1958
}) '''

# Update array (list) elements
city_ref = db.collection(u'cities').document(u'CA')
city_ref.update({
    u'regions':
    firestore_v1.ArrayUnion(
        [u'greater_virginia', u'lesser_poland', u'mid-dakota'])
})  # Add array
city_ref.update({
    u'regions':
    firestore_v1.ArrayRemove([u'east_coast', u'lesser_poland'])
})  # Remove element from the array

city_ref = db.collection(u'cities').document(u'BU')
city_ref.set({u'population': 100})
city_ref.update({'population': firestore_v1.Increment(50)})

# Update unknown documents
docs = db.collection('users').get()
for doc in docs:
    if doc.to_dict()['age'] >= 30:
        db.collection('users').document(doc.id).update(