forked from Tibcsi4500/getzeroserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaceholderCalls.py
123 lines (117 loc) · 4.63 KB
/
placeholderCalls.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import util
import json
def getAcceptedChallengesOfUser():
data = util.getBody()
try:
userid = int(data['userid'])
if('type' in data):
typetext = data['type']
else:
typetext = 'none'
if('room' in data):
room = data['room']
else:
room = 'none'
relationsFile = open("relations.txt")
relations = json.loads(relationsFile.read())
challengesFile = open("challenges.txt")
challenges = json.loads(challengesFile.read())
result = ""
for relation in relations['relations']:
if(relation['userid'] == userid):
for challenge in challenges['challenges']:
if(relation['challengeid'] == challenge['challengeid']
and (challenge['appliance'] == typetext or typetext == 'none')
and (challenge['room'] == room or room == 'none')):
result += challenge['title'] + ";"
result += challenge['description'] + ";"
result += str(challenge['challengeid']) + ";"
result += str(relation['progress']) + ";"
result += str(challenge['outof']) + ";"
result += str(challenge['appliance']) + ";"
result += str(challenge['room']) + "\n"
challengesFile.close()
relationsFile.close()
return result
except Exception as e:
return "Something went wrong: " + str(e)
return "Something went wrong"
def getUnacceptedChallengesOfUser():
data = util.getBody()
try:
userid = int(data['userid'])
if('type' in data):
typetext = data['type']
else:
typetext = 'none'
if('room' in data):
room = data['room']
else:
room = 'none'
relationsFile = open("relations.txt")
relations = json.loads(relationsFile.read())
challengesFile = open("challenges.txt")
challenges = json.loads(challengesFile.read())
acceptedChallenges = []
result = ""
for relation in relations['relations']:
if(relation['userid'] == userid):
acceptedChallenges.append(relation['challengeid'])
for challenge in challenges['challenges']:
if((not util.contains(challenge['challengeid'], acceptedChallenges))
and (challenge['appliance'] == typetext or typetext == 'none')
and (challenge['room'] == room or room == 'none')):
result += challenge['title'] + ";"
result += challenge['description'] + ";"
result += str(challenge['challengeid']) + ";"
result += str(0) + ";"
result += str(challenge['outof']) + ";"
result += str(challenge['appliance']) + ";"
result += str(challenge['room']) + "\n"
challengesFile.close()
relationsFile.close()
return result
except Exception as e:
return "Something went wrong: " + str(e)
return "Something went wrong"
def incrementChallengeProgress():
data = util.getBody()
try:
userid = int(data['userid'])
challengeid = int(data['challengeid'])
relationsFile = open("relations.txt")
relations = json.loads(relationsFile.read())
relationsFile.close()
relationsFile = open("relations.txt", "w")
for relation in relations['relations']:
if(relation['userid'] == userid and relation['challengeid'] == challengeid):
relation['progress'] += 1
json.dump(relations, relationsFile)
relationsFile.close()
return "1"
except Exception as e:
return "Something went wrong: " + str(e)
return "Something went wrong"
def acceptChallengesOfUser():
data = util.getBody()
try:
userid = int(data['userid'])
challengeidstring = data['challenges']
relationsFile = open("relations.txt")
relations = json.loads(relationsFile.read())
relationsFile.close()
relationsFile = open("relations.txt", "w")
challengeids = challengeidstring.split("-")
for challengeid in challengeids:
toInsert = {
"userid":userid,
"challengeid":int(challengeid),
"progress":0
}
relations['relations'].append(toInsert)
json.dump(relations, relationsFile)
relationsFile.close()
return "1"
except Exception as e:
return "Something went wrong: " + str(e)
return "Something went wrong"