Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/nutrinoweb/controllers/sparetime.py
blob: beda0380b153df9e7f84f4876b42ca168685f5d2 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# gettext import
from gettext import gettext as _

# server import
from server.flask import app, logger, render, request, jsonify

#database import
import database_manager

#Avatar import
import Avatar

@app.route('/sparetime', methods=['GET', 'POST'])
def sparetime():
    database_manager.init_db('activity')
    database_manager.load_inputfileactivities()
    entries = database_manager.entriessparetime()
    content = [ ]
    position = [70, 0]
    # POST
    if request.method == 'POST':
        if 'activity' in request.form:
    	    i = Avatar.Character(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, position)
    	    i.load_status()
	    result = request.form['activity']
	    factor = float(request.form['time'])
	    pathi = '../../static/images/journal/activities/' + result + '.png'
	    _result = calculateDimension(factor, result, entries, i)

	    #calculate new position for the sun
            startfactor = i.Time
	    positionstart = calculateTime(startfactor)
            timefactor = factor + startfactor
	    if (timefactor >= 720.0):
                timefactor = 0.0
            newPosition = calculateTime(timefactor)
	    i.Position = newPosition
            i.Time = timefactor

    	    #calculate time
	    daytime = "am"
	    hours = int(round(i.Time//60)) + 8
	    minutes = int((i.Time % 60))
	    if (minutes == 0):
	        minutes = "00"
	    else:
                minutes = str(minutes)
	    if (hours > 11):
	        daytime = "pm"
	    if (hours > 12):
        	hours = hours - 12
	    time = str(hours) + ":" + minutes + " " + daytime


    	    #Start and Endposition for the sun
    	    positionSun = {
		   'positionX': float(newPosition[0]),
		   'positionstartX': float(positionstart[0]),
		   'positionY': float(newPosition[1]),
		   'positionstartY': float(positionstart[1]),
		   'timeString': time
                   }


	    progressbar = {
		'water': _result[0],
		'energie': _result[1],
		'vitamine': _result[2],
		'construction': _result[3],
		'waterstart': _result[4],
		'energiestart': _result[5],
		'vitaminestart': _result[6],
		'constructionstart': _result[7],
		'pathimg': pathi,
               	}
	    content.append(progressbar)

	    _content = { 'progressbar': content, 'positionSun': positionSun }

	    #log activity
	    line = str(factor) + ' min. ' + result
            database_manager.write_log(line, 's')

	    #save status in status.json
	    i.save_status()

    	    # render result
    	    return render('nutrinoweb/main.html', **_content)

	else:
            _result = '??'
    # GET
    else:
 	entries = entries
	_result = ''
    #  
    _content = {
                'title': _('What do you want to do?'),
                'activities': entries,
		'result': _result,
                }
    # render result
    return render('nutrinoweb/sparetime.html', **_content)

def calculateDimension(factor, result, entries, i):
    water = None
    energie = None
    vitamine = None
    construction = None
    vitstart = None
    if result != None and entries != None:
       for entry in entries:
	   if result in entry['title']:

	      #part water
	      waterstart = i.Water
	      i.Water = controllValue(i.Water - ((factor*calculateWater(float(entry['water']))) / 2))
	      water = calculateHeight(i.Water)
	      
	      #part energie
	      energiestart = i.Energie        
	      i.Energie = controllValue(i.Energie - (factor*calculateEnergie(float(entry['energie']))))
	      energie = calculateHeight(i.Energie)    
	      
	      #part vitamine
	      vitaminestart = i.Vit_total
	      i.Vit_total = calculateVitamine(1, entry, i)
	      vitamine = calculateHeight(i.Vit_total)  

	      #part construction
	      constructionstart = i.Construction
	      i.Construction = controllValue(i.Construction - (factor*calculateConstruction(float(entry['protein']))))
	      construction = calculateHeight(i.Construction)
	          
	   else:
               pass	  

    _result = [water, energie, vitamine, construction, calculateHeight(waterstart), calculateHeight(energiestart), calculateHeight(vitaminestart), calculateHeight(constructionstart)]
    return _result

def calculateEnergie(energie):
   height = energie/4.0
   return height

def calculateVitamine(f, entry, i):
   i.Calcium = controllValue(i.Calcium - (f*(float(entry['calcium']) / 9.0)))
   i.Iron = controllValue(i.Iron - (f*(float(entry['iron']) / 0.125)))
   i.Magnesium = controllValue(i.Magnesium- (f*(float(entry['magnesium']) / 2.1)))
   i.Phosphorus = controllValue(i.Phosphorus - (f*(float(entry['phosphorus']) / 10)))	
   i.Potassium = controllValue(i.Potassium - (f*(float(entry['potassium']) / 15.5)))
   i.Sodium = controllValue(i.Sodium - (f*(float(entry['sodium']) / 4.85)))
   i.Zinc = controllValue(i.Zinc - (f*(float(entry['zinc']) / 0.08)))
   i.Copper = controllValue(i.Copper - (f*(float(entry['copper']) / 0.0115)))
   i.Vit_c = controllValue(i.Vit_c - (f*(float(entry['vit_c']) / 0.85)))
   i.Thiamin = controllValue(i.Thiamin - (f*(float(entry['thiamin']) / 0.012)))
   i.Riboflavin = controllValue(i.Riboflavin - (f*(float(entry['riboflavin']) / 0.011)))
   i.Niacin = controllValue(i.Niacin - (f*(float(entry['niacin']) / 0.13)))
   i.Panto_acid = controllValue(i.Panto_acid - (f*(float(entry['panto_acid']) / 0.05)))
   i.Vit_b6 = controllValue(i.Vit_b6 - (f*(float(entry['vit_b6']) / 0.0085)))
   i.Folate_tot = controllValue(i.Folate_tot - (f*(float(entry['folate_tot']) / 3.5)))
   i.Vit_b12 = controllValue(i.Vit_b12 - (f*(float(entry['vit_b12']) / 0.019)))
   i.Vit_a = controllValue(i.Vit_a - (f*(float(entry['vit_a']) / 0.11)))
   i.Vit_e = controllValue(i.Vit_e - (f*(float(entry['vit_e']) / 0.05)))
   i.Vit_d = controllValue(i.Vit_d - (f*(float(entry['vit_d']) / 0.315)))

   height = (i.Calcium + i.Iron + i.Magnesium + i.Phosphorus + i.Potassium + i.Sodium + i.Zinc + i.Copper + i.Vit_c + i.Thiamin + i.Riboflavin + i.Niacin + i.Panto_acid + i.Vit_b6 + i.Folate_tot + i.Vit_b12 + i.Vit_a + i.Vit_e + i.Vit_d)/19.0
   return height

def controllValue(value):
    if value < 0.00:
       return 0.00
    return value


def calculateWater(water):
   height = water/17.5
   return height

def calculateConstruction(construction):
   height = construction/0.215                #daily value 0.9g/kg weight, a child of 35kg needs 21.5g
   return height

def calculateHeight(height):
    height = 100.0 - height
    if height < 0.0:
       return 5.0
    elif height == 0.0:
       return 5.0
    elif height > 100.0:
       return 99.00
    return int(height)

def calculateTime(f):
    if f == 0.0:
        position = [70, 0]
    if f == 15.0:
	position = [60 , 5]
    elif f == 30.0:
	position = [50 , 10]
    elif f == 45.0:
	position = [40 , 15]
    elif f == 60.0:
	position = [30 , 20]
    elif f == 75.0:
	position = [20 , 25]
    elif f == 90.0:
	position = [10 , 30]
    elif f == 105.0:
	position = [0 , 35]
    elif f == 120.0:
	position = [0 , 40]
    elif f == 135.0:
	position = [0 , 45]
    elif f == 150.0:
	position = [0 , 50]
    elif f == 165.0:
	position = [0 , 55]
    elif f == 180.0:
	position = [0 , 60]
    elif f == 195.0:
	position = [0 , 65]
    elif f == 210.0:
	position = [0 , 70]
    elif f == 225.0:
	position = [0 , 75]
    elif f == 240.0:
	position = [0 , 80]
    elif f == 255.0:
	position = [0 , 85]
    elif f == 270.0:
	position = [0 , 90]
    elif f == 285.0:
	position = [0 , 95]
    elif f == 300.0:
	position = [0 , 100]
    elif f == 315.0:
	position = [0 , 105]
    elif f == 330.0:
	position = [0 , 110]
    elif f == 345.0:
	position = [0 , 115]
    elif f == 360.0:
	position = [0 , 120]
    elif f == 375.0:
	position = [0 , 125]
    elif f == 390.0:
	position = [0 , 130]
    elif f == 405.0:
	position = [0 , 135]
    elif f == 420.0:
	position = [0 , 140]
    elif f == 435.0:
	position = [0 , 145]
    elif f == 450.0:
	position = [0 , 150]
    elif f == 465.0:
	position = [0 , 155]
    elif f == 480.0:
	position = [0 , 160]
    elif f == 495.0:
	position = [0 , 165]
    elif f == 510.0:
	position = [0 , 170]
    elif f == 525.0:
	position = [0 , 175]
    elif f == 540.0:
	position = [0 , 180]
    elif f == 555.0:
	position = [0 , 185]
    elif f == 570.0:
	position = [0 , 190]
    elif f == 585.0:
	position = [0 , 195]
    elif f == 600.0:
	position = [0 , 200]
    elif f == 615.0:
	position = [0 , 205]
    elif f == 630.0:
	position = [10 , 210]
    elif f == 645.0:
	position = [20 , 215]
    elif f == 660.0:
	position = [30 , 220]
    elif f == 675.0:
	position = [40 , 225]
    elif f == 690.0:
	position = [50 , 230]
    elif f == 705.0:
	position = [60 , 235]
    elif f == 720.0:
	position = [70 , 240]
    else:
	pass
    return position