-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpets.py
105 lines (83 loc) · 3.27 KB
/
pets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import discord
from discord.ext import commands
from databaseConnection import databaseConnection
import copy
class pet:
def __init__(self, type, cost, picture, name=None, petID=None, userID=None):
if petID is None:
petID = -1
if userID is None:
userID = -1
if name is None:
name = type
self.type = type
self.name = name
self.cost = cost
self.picture = picture
self.link = picture
self.petID = petID
self.userID = userID
return
def savePet(dbConnection, pet):
if pet.userID < 0 or pet.petID < 0:
return False
# first check if the pet already exists
petDoc = dbConnection.getUserPet({"userID": pet.userID, "petID": pet.petID})
if petDoc is None:
# the pet has not been created yet
# insert it as a new document
dbConnection.insertUserPet({"userID": pet.userID, "petID": pet.petID, "Type": pet.type,
"Nickname": pet.name, "Picture": pet.picture, "Cost": pet.cost})
else:
# update the current pets information
dbConnection.updateUserPet({"userID": pet.userID, "petID": pet.petID},
{"userID": pet.userID, "petID": pet.petID, "Type": pet.type,
"Nickname": pet.name, "Picture": pet.picture, "Cost": pet.cost})
return True
def readPetOutline(dbConnection):
allPets = dbConnection.getAllPetOutlines()
listOfPets = {}
for petDoc in allPets:
newPet = pet(petDoc["Type"], petDoc["Cost"], petDoc["Picture"])
listOfPets.update({petDoc["Type"]: newPet})
return listOfPets
def readUserPet(dbConnection, userID, petID):
petDoc = dbConnection.getUserPet({"userID": userID, "petID": petID})
if petDoc is None:
return None
newPet = pet(petDoc["Type"], petDoc["Cost"], petDoc["Picture"], petDoc["Nickname"], petDoc["petID"],
petDoc["userID"])
return newPet
def updatePetDetails(dbConnection, userDoc, userID, usersPet):
newPetID = userDoc["petIDCount"]
newPetID += 1
usersPet.userID = userID
usersPet.petID = newPetID
# update the users petIDCount and their pet array
dbConnection.profileUpdate({"id": userID}, {"$set": {"petIDCount": newPetID}})
# and point their profile to that pet
dbConnection.profileUpdate({"id": userID}, {"$push": {"pets": newPetID}})
def generatePet(dbConnection, petName, userDoc, id):
petObj = readSpecificPetOutline(dbConnection, petName)
usersPet = copy.copy(petObj)
updatePetDetails(dbConnection, userDoc, id, usersPet)
savePet(dbConnection, usersPet)
def readSpecificPetOutline(dbConnection, name):
petDoc = dbConnection.getSpecificPetOutline({"Type": name})
newPet = pet(petDoc["Type"], petDoc["Cost"], petDoc["Picture"])
return newPet
# def readPetStructure(dbConnection, type):
# """
#
# :type dbConnection:databaseConnection
# :type type:str
#
# :return: a pet object if we found the outline for a pet, none if we failed to find the
# outline
# """
#
# petDoc = dbConnection.getSpecificPetOutline({"Type": type})
# if petDoc is None:
# return None
# else:
# return pet(petDoc["Type"], petDoc["Cost"], petDoc["Picture"])