/
app.py
1022 lines (848 loc) · 33.6 KB
/
app.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""RanchoStop is a site focusing on a modern rebranding of Tarantulas."""
from flask import Flask, render_template, request, redirect, url_for, session
from pymongo import MongoClient
from bson.objectid import ObjectId
import os
from datetime import datetime
from random import choice, randint
from functools import wraps
from bson import json_util
from bson.json_util import loads, dumps
import json
from lvl_calc import level_calc
host = os.environ.get('MONGODB_URI', 'mongodb://127.0.0.1:27017/RanchoStop')
client = MongoClient(host=f'{host}?retryWrites=false')
db = client.get_default_database()
users = db.users
ranchos = db.ranchos
listings = db.listings
comments = db.comments
hatcheries = db.hatcheries
broods = db.broods
app = Flask(__name__)
app.config['SECRET_KEY'] = 'THISISMYSECRETKEY'
# SESSION_TYPE = 'mongodb'
# app.config.from_object(__name__)
# Session(app)
def login_required(f):
"""
Require login to access a page.
Adapted from:
https://flask.palletsprojects.com/en/1.1.x/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if 'user' not in session:
return redirect(url_for('login'))
return f(*args, **kwargs)
return decorated_function
# ---------------------------HOME---------------------------
@app.route('/')
def home():
"""Return homepage."""
current_user = None
if 'user' in session:
current_user = session['user']
return render_template('home.html', current_user=current_user)
@app.route('/home')
def home_page_redirect():
"""Redirect to homepage."""
return redirect(url_for('home'))
# ---------------------------LOGIN/OUT---------------------------
@app.route('/login')
def login():
"""Login from."""
if 'user' in session:
current_user = session['user']
return render_template('users/logged_in.html',
current_user=current_user)
return render_template('users/login.html')
@app.route('/login/submit', methods=['POST'])
def login_submit():
"""Login submit."""
current_user = None
if 'user' in session:
current_user = session['user']
user = users.find_one({'username': request.form.get('username')})
if user is None:
return redirect(url_for('login'))
if user['password'] != request.form.get('password'):
return redirect(url_for('login'))
data = {
'username': request.form.get('username'),
'user_id': str(user['_id'])
}
session['user'] = json.loads(json_util.dumps(data))
current_user = session['user']
return redirect(url_for('home', current_user=current_user))
@app.route('/logout')
def logout():
"""Remove user from session."""
# session.pop('user', None)
session.clear()
return redirect(url_for('home'))
# ---------------------------USERS---------------------------
@app.route('/users/new')
def users_new():
"""Return a user creation page."""
if 'user' in session:
current_user = session['user']
return render_template('users/logged_in.html',
current_user=current_user)
return render_template('users/new_user.html', user={}, title='New User')
@app.route('/users/directory')
def users_directory():
"""Return a directory of users."""
current_user = None
if 'user' in session:
current_user = session['user']
return render_template('users/users_directory.html', users=users.find(),
current_user=current_user)
@app.route('/users', methods=['POST'])
def users_submit():
"""Submit a new user."""
if 'user' in session:
current_user = session['user']
return render_template('users/logged_in.html',
current_user=current_user)
if users.find_one({'username': request.form.get('username')}) is not None:
return redirect(url_for('users_new'))
user = {
'username': request.form.get('username'),
'password': request.form.get('password'),
'bio': request.form.get('content'),
'created_at': datetime.now(),
'crikits': 200,
'last_paid': datetime.now()
}
user_id = users.insert_one(user).inserted_id
data = {
'username': request.form.get('username'),
'user_id': str(user_id)
}
session['user'] = json.loads(json_util.dumps(data))
# print(user_id)
# print(users.find_one({'username': request.form.get('username')})['_id'])
# print(session['user']['user_id'])
return redirect(url_for('users_show', user_id=user_id))
@app.route('/users/<user_id>/edit')
@login_required
def users_edit(user_id):
"""Show the edit form for a user profile."""
current_user = session['user']
user = users.find_one({'_id': ObjectId(user_id)})
if ObjectId(current_user['user_id']) != user['_id']:
return render_template('go_back.html', current_user=current_user)
return render_template('users/users_edit.html', user=user,
title='Edit User Profile',
current_user=current_user)
@app.route('/daily_crikits')
@login_required
def daily_crikits():
"""Pay daily 25 crikits."""
current_user = session['user']
usid = current_user['user_id']
a_user = users.find_one({'_id': ObjectId(current_user['user_id'])})
timediff = datetime.now() - a_user['last_paid']
if timediff.days >= 1:
new_balance = a_user['crikits'] + 25
users.update_one(
{'_id': ObjectId(current_user['user_id'])},
{'$set': {'crikits': new_balance, 'last_paid': datetime.now()}})
return redirect(url_for('users_show', user_id=usid))
error = {
'error_message': "You've already claimed your daily reward in the last 24 hours.",
'error_link': '/',
'back_message': 'Back to home?'
}
return render_template('error_message.html', error=error, current_user=current_user)
@app.route('/users/<user_id>', methods=['POST'])
@login_required
def users_update(user_id):
"""Submit an edited user profile."""
current_user = session['user']
if ObjectId(current_user['user_id']) != ObjectId(user_id):
return render_template('go_back.html', current_user=current_user)
updated_user = {
'bio': request.form.get('content')
}
users.update_one(
{'_id': ObjectId(user_id)},
{'$set': updated_user})
return redirect(url_for('users_show', user_id=user_id))
@app.route('/users/<user_id>')
def users_show(user_id):
"""Show a single user page."""
current_user = None
if 'user' in session:
current_user = session['user']
user = users.find_one({'_id': ObjectId(user_id)})
# print(user['crikits'])
user_ranchos = ranchos.find({'user_id': ObjectId(user_id)})
return render_template('users/users_show.html', user=user,
ranchos=user_ranchos,
current_user=current_user)
@app.route('/users/<user_id>/delete', methods=['POST'])
@login_required
def users_delete(user_id):
"""Delete one user."""
current_user = session['user']
if ObjectId(current_user['user_id']) != ObjectId(user_id):
return render_template('go_back.html', current_user=current_user)
users.delete_one({'_id': ObjectId(user_id)})
session.clear() # Clear Session
return redirect(url_for('users_directory'))
# ---------------------------LISTINGS---------------------------
@app.route('/listings_home')
def listings_home():
"""Return listings homepage."""
current_user = None
if 'user' in session:
current_user = session['user']
return render_template('/listings/listings_index.html',
listings=listings.find(), current_user=current_user)
@app.route('/listings/new')
@login_required
def listings_new():
"""Create a new listing."""
current_user = session['user']
return render_template('listings/new_listing.html',
listing={}, title='New Listing',
current_user=current_user)
@app.route('/listings', methods=['POST'])
@login_required
def listing_submit():
"""Submit a new listing."""
current_user = session['user']
listing = {
'title': request.form.get('title'),
'description': request.form.get('description'),
'views': 0,
'created_at': datetime.now(),
'author': current_user['username'],
'user_id': ObjectId(current_user['user_id'])
}
listing_id = listings.insert_one(listing).inserted_id
return redirect(url_for('listings_show', listing_id=listing_id))
@app.route('/listings/<listing_id>')
def listings_show(listing_id):
"""Show a single listing."""
current_user = None
if 'user' in session:
current_user = session['user']
listing = listings.find_one({'_id': ObjectId(listing_id)})
listing_comments = comments.find({'listing_id': ObjectId(listing_id)})
listing_author = users.find({'_id': listing['user_id']})
updated_views = {
'views': listing['views'] + 1
}
listings.update_one(
{'_id': ObjectId(listing_id)},
{'$set': updated_views})
return render_template('listings/listings_show.html', listing=listing,
comments=listing_comments, user=listing_author,
current_user=current_user)
@app.route('/listings/<listing_id>/edit')
@login_required
def listings_edit(listing_id):
"""Show the edit form for a listing."""
current_user = session['user']
listing = listings.find_one({'_id': ObjectId(listing_id)})
if ObjectId(current_user['user_id']) != listing['user_id']:
return render_template('go_back.html', current_user=current_user)
return render_template('listings/listings_edit.html', listing=listing,
title='Edit Listing', current_user=current_user)
@app.route('/listings/<listing_id>', methods=['POST'])
@login_required
def listings_update(listing_id):
"""Submit an edited listing."""
current_user = session['user']
listing = listings.find_one({'_id': ObjectId(listing_id)})
if ObjectId(current_user['user_id']) != listing['user_id']:
return render_template('go_back.html', current_user=current_user)
updated_listing = {
'title': request.form.get('title'),
'description': request.form.get('description')
}
listings.update_one(
{'_id': ObjectId(listing_id)},
{'$set': updated_listing})
return redirect(url_for('listings_show', listing_id=listing_id))
@app.route('/listings/<listing_id>/delete', methods=['POST'])
@login_required
def listings_delete(listing_id):
"""Delete one listing."""
current_user = session['user']
listing = listings.find_one({'_id': ObjectId(listing_id)})
if ObjectId(current_user['user_id']) != listing['user_id']:
return render_template('go_back.html', current_user=current_user)
listings.delete_one({'_id': ObjectId(listing_id)})
return redirect(url_for('listings_home'))
# -------------------------RANCHOS-------------------------
@app.route('/ranchos/adoption_center')
@login_required
def adoption_center():
"""Show the adoption page with randomized ranchos."""
current_user = session['user']
rancho_species = ['Goliath Birdeater', 'Cobalt Blue']
rancho_sex = ['Male', 'Female']
ranchos_list = []
for x in range(0, 9):
rancho = {
'species': choice(rancho_species),
'sex': choice(rancho_sex),
'stats': {
'hardiness': randint(0, 100),
'dexterity': randint(0, 100),
'docility': randint(0, 100),
'conformation': randint(0, 100),
},
'created_at': datetime.now()
}
ranchos_list.append(rancho)
user_crikits = users.find_one({'username': current_user['username']})['crikits']
return render_template('ranchos/adoption_center.html',
ranchos_list=ranchos_list,
current_user=current_user,
user_crikits=user_crikits)
@app.route('/ranchos/new', methods=['POST'])
@login_required
def ranchos_new():
"""Submit a new Rancho."""
current_user = session['user']
a_user = users.find_one({'_id': ObjectId(current_user['user_id'])})
new_balance = a_user['crikits'] - 50
if new_balance < 0:
return render_template('go_back.html', current_user=current_user)
users.update_one(
{'_id': ObjectId(current_user['user_id'])},
{'$set': {'crikits': new_balance}})
stats = {
'hardiness': request.form.get('hardiness'),
'dexterity': request.form.get('dexterity'),
'docility': request.form.get('docility'),
'conformation': request.form.get('conformation')
}
needs = {
'food': 100,
'water': 100,
'health': 100,
'happiness': 100,
'last_cared': datetime.now(),
'cared_by': current_user['username'],
'cared_by_id': ObjectId(current_user['user_id'])
}
rancho = {
'name': 'New Rancho',
'bio': request.form.get('sex') + ' ' + request.form.get('species'),
'adopted_at': datetime.now(),
'xp': 1000,
'level': level_calc(1000),
'stats': stats,
'needs': needs,
'species': request.form.get('species'),
'sex': request.form.get('sex'),
'owner': current_user['username'],
'user_id': ObjectId(current_user['user_id'])
}
rancho_id = ranchos.insert_one(rancho).inserted_id
return redirect(url_for('ranchos_edit', rancho_id=rancho_id))
@app.route('/ranchos/<rancho_id>')
def ranchos_show(rancho_id):
"""Show a single Rancho."""
current_user = None
if 'user' in session:
current_user = session['user']
rancho = ranchos.find_one({'_id': ObjectId(rancho_id)})
if rancho is None:
error = {
'error_message': "That Rancho was not found. It may have been released.",
'error_link': '/',
'back_message': 'Back to home?'
}
return render_template('error_message.html', error=error,
current_user=current_user)
# Update needs
timediff = datetime.now() - rancho['needs']['last_cared']
if timediff.days > 0:
if timediff.days >= 4:
# Been more than four days since last cared for
new_needs = {
'food': 0,
'water': 0,
'health': 0,
'happiness': 0,
'last_cared': rancho['needs']['last_cared'],
'cared_by': rancho['needs']['cared_by'],
'cared_by_id': rancho['needs']['cared_by_id']
}
elif timediff.days >= 3:
# Been more than three days since last cared for
new_needs = {
'food': 25,
'water': 0,
'health': 50,
'happiness': 0,
'last_cared': rancho['needs']['last_cared'],
'cared_by': rancho['needs']['cared_by'],
'cared_by_id': rancho['needs']['cared_by_id']
}
elif timediff.days >= 2:
# Been more than two days since last cared for
new_needs = {
'food': 50,
'water': 0,
'health': 100,
'happiness': 50,
'last_cared': rancho['needs']['last_cared'],
'cared_by': rancho['needs']['cared_by'],
'cared_by_id': rancho['needs']['cared_by_id']
}
elif timediff.days >= 1:
# Been more than a day since last cared for
new_needs = {
'food': 75,
'water': 50,
'health': 100,
'happiness': 75,
'last_cared': rancho['needs']['last_cared'],
'cared_by': rancho['needs']['cared_by'],
'cared_by_id': rancho['needs']['cared_by_id']
}
ranchos.update_one(
{'_id': ObjectId(rancho_id)},
{'$set': {'needs': new_needs}}
)
return render_template('ranchos/ranchos_show.html',
rancho=ranchos.find_one({'_id': ObjectId(rancho_id)}),
broods=broods.find({'$or': [{'mother_id': ObjectId(rancho_id)}, {'father_id': ObjectId(rancho_id)}]}),
current_user=current_user)
@app.route('/ranchos/<rancho_id>/care')
@login_required
def ranchos_care(rancho_id):
"""Care for a Rancho."""
current_user = session['user']
rancho = ranchos.find_one({'_id': ObjectId(rancho_id)})
timediff = datetime.now() - rancho['needs']['last_cared']
if timediff.days >= 1:
# Been more than a day since last cared for
new_health = rancho['needs']['health'] + 50
if new_health > 100:
new_health = 100
new_needs = {
'food': 100,
'water': 100,
'health': new_health,
'happiness': 100,
'last_cared': datetime.now(),
'cared_by': current_user['username'],
'cared_by_id': ObjectId(current_user['user_id'])
}
new_xp = rancho['xp'] + 250
ranchos.update_one(
{'_id': ObjectId(rancho_id)},
{'$set': {
'needs': new_needs,
'xp': new_xp,
'level': level_calc(new_xp)
}}
)
# Can care for other people's Ranchos
# if ObjectId(current_user['user_id']) != rancho['user_id']:
# return render_template('go_back.html', current_user=current_user)
return redirect(url_for('ranchos_show', rancho_id=rancho_id))
@app.route('/ranchos/<rancho_id>/edit')
@login_required
def ranchos_edit(rancho_id):
"""Show the edit form for a Rancho profile."""
current_user = session['user']
rancho = ranchos.find_one({'_id': ObjectId(rancho_id)})
if ObjectId(current_user['user_id']) != rancho['user_id']:
return render_template('go_back.html', current_user=current_user)
return render_template('ranchos/ranchos_edit.html', rancho=rancho,
title='Edit Rancho Profile',
current_user=current_user)
@app.route('/ranchos/<rancho_id>', methods=['POST'])
@login_required
def ranchos_update(rancho_id):
"""Submit an edited rancho profile."""
current_user = session['user']
rancho = ranchos.find_one({'_id': ObjectId(rancho_id)})
if ObjectId(current_user['user_id']) != rancho['user_id']:
return render_template('go_back.html', current_user=current_user)
updated_prof = {
'name': request.form.get('rancho_name'),
'bio': request.form.get('description')
}
ranchos.update_one(
{'_id': ObjectId(rancho_id)},
{'$set': updated_prof})
# update rancho dict
rancho = ranchos.find_one({'_id': ObjectId(rancho_id)})
# update broods
for brood in broods.find({'mother_id': rancho['_id']}):
broods.update_one(
{'_id': ObjectId(brood['_id'])},
{'$set': {'mother_name': rancho['name']}})
for brood in broods.find({'father_id': rancho['_id']}):
broods.update_one(
{'_id': ObjectId(brood['_id'])},
{'$set': {'father_name': rancho['name']}})
# update ancestry
for other_rancho in ranchos.find({'ancestry.mother_id': rancho['_id']}):
ranchos.update_one(
{'_id': ObjectId(other_rancho['_id'])},
{'$set': {'ancestry.mother_name': rancho['name']}})
for other_rancho in ranchos.find({'ancestry.father_id': rancho['_id']}):
ranchos.update_one(
{'_id': ObjectId(other_rancho['_id'])},
{'$set': {'ancestry.father_name': rancho['name']}})
# update hatchery
for hatchery in hatcheries.find({'mother_id': rancho['_id']}):
hatcheries.update_one(
{'_id': ObjectId(hatchery['_id'])},
{'$set': {'mother_name': rancho['name']}})
for hatchery in hatcheries.find({'father_id': rancho['_id']}):
hatcheries.update_one(
{'_id': ObjectId(hatchery['_id'])},
{'$set': {'father_name': rancho['name']}})
return redirect(url_for('ranchos_show', rancho_id=rancho_id))
@app.route('/ranchos/<rancho_id>/release', methods=['POST'])
@login_required
def ranchos_delete(rancho_id):
"""Release (delete) one Rancho."""
current_user = session['user']
rancho = ranchos.find_one({'_id': ObjectId(rancho_id)})
# correct owner
if ObjectId(current_user['user_id']) != rancho['user_id']:
return render_template('go_back.html', current_user=current_user)
# not currenly breeding
if hatcheries.find(
{'$or': [
{'mother_id': ObjectId(rancho_id)},
{'father_id': ObjectId(rancho_id)}
]}
).count() > 0:
error = {
'error_message': "That Rancho cannot be released because it is currently breeding.",
'error_link': f'/ranchos/{ rancho_id }',
'back_message': 'Back to Rancho?'
}
return render_template('error_message.html', error=error,
current_user=current_user)
ranchos.delete_one({'_id': ObjectId(rancho_id)})
a_user = users.find_one({'_id': ObjectId(current_user['user_id'])})
new_balance = a_user['crikits'] + 25
users.update_one(
{'_id': ObjectId(current_user['user_id'])},
{'$set': {'crikits': new_balance}})
return redirect(url_for('users_show',
user_id=rancho.get('user_id')))
# ---------------------------COMMENTS---------------------------
@app.route('/listings/comments', methods=['POST'])
@login_required
def comments_new():
"""Submit a new comment."""
current_user = session['user']
comment = {
'title': request.form.get('title'),
'content': request.form.get('content'),
'listing_id': ObjectId(request.form.get('listing_id')),
'author': current_user['username'],
'user_id': ObjectId(current_user['user_id'])
}
comments.insert_one(comment)
return redirect(url_for('listings_show',
listing_id=request.form.get('listing_id')))
@app.route('/listings/comments/<comment_id>', methods=['POST'])
@login_required
def comments_delete(comment_id):
"""Delete a comment."""
current_user = session['user']
comment = comments.find_one({'_id': ObjectId(comment_id)})
if ObjectId(current_user['user_id']) != comment['user_id']:
return render_template('go_back.html', current_user=current_user)
comments.delete_one({'_id': ObjectId(comment_id)})
return redirect(url_for('listings_show',
listing_id=comment.get('listing_id')))
# ---------------------------HATCHERIES---------------------------
@app.route('/hatcheries/new')
@login_required
def hatcheries_new():
"""Return a hatchery creation page."""
current_user = session['user']
return render_template('hatcheries/new_hatchery.html',
current_user=current_user)
@app.route('/hatcheries/my_hatcheries')
@login_required
def my_hatcheries():
"""Return a list of hatcheries extant broods belonging to the user."""
current_user = session['user']
for rancho in ranchos.find({'user_id': current_user['user_id']}):
print(rancho['name'])
return render_template('hatcheries/my_hatcheries.html',
hatcheries=hatcheries.find({
'user_id': ObjectId(current_user['user_id'])
}),
broods=broods.find({
'user_id': ObjectId(current_user['user_id'])
}),
f_ranchos=ranchos.find({
'user_id': ObjectId(current_user['user_id']),
'sex': 'Female'
}),
m_ranchos=ranchos.find({
'user_id': ObjectId(current_user['user_id']),
'sex': 'Male'
}),
current_user=current_user)
@app.route('/hatcheries', methods=['POST'])
@login_required
def hatcheries_submit():
"""Submit a new hatchery pairing."""
current_user = session['user']
a_user = users.find_one({'_id': ObjectId(current_user['user_id'])})
mother = ranchos.find_one({'_id': ObjectId(request.form.get('mother'))})
father = ranchos.find_one({'_id': ObjectId(request.form.get('father'))})
if not check_compatible(mother, father):
error = {
'error_message': "These Ranchos are incompatible.",
'error_link': f'/hatcheries/my_hatcheries',
'back_message': 'Back to hatchery?'
}
return render_template('error_message.html', error=error,
current_user=current_user)
hatchery = {
'mother_name': mother['name'],
'mother_id': mother['_id'],
'father_name': father['name'],
'father_id': father['_id'],
'created_at': datetime.now(),
'owner': a_user['username'],
'user_id': a_user['_id']
}
hatchery_id = hatcheries.insert_one(hatchery).inserted_id
return redirect(url_for('hatcheries_show', hatchery_id=hatchery_id))
@app.route('/hatcheries/<hatchery_id>')
def hatcheries_show(hatchery_id):
"""Show a single hatchery page."""
current_user = None
if 'user' in session:
current_user = session['user']
hatchery = hatcheries.find_one({'_id': ObjectId(hatchery_id)})
mother = ranchos.find_one({'_id': ObjectId(hatchery['mother_id'])})
father = ranchos.find_one({'_id': ObjectId(hatchery['father_id'])})
ready_to_hatch = False
timediff = datetime.now() - hatchery['created_at']
if timediff.days >= 0:
ready_to_hatch = True
return render_template('hatcheries/hatcheries_show.html',
hatchery=hatchery,
mother=mother,
father=father,
ready=ready_to_hatch,
current_user=current_user)
@app.route('/hatcheries/<hatchery_id>/hatch', methods=['POST'])
@login_required
def hatchery_hatch(hatchery_id):
"""Hatch a brood and move record of the pairing to brood db."""
current_user = session['user']
hatchery = hatcheries.find_one({'_id': ObjectId(hatchery_id)})
mother = ranchos.find_one({'_id': ObjectId(hatchery['mother_id'])})
father = ranchos.find_one({'_id': ObjectId(hatchery['father_id'])})
if ObjectId(current_user['user_id']) != ObjectId(hatchery['user_id']):
error = {
'error_message': "You don't own these Ranchos!",
'error_link': f'/hatcheries/{hatchery_id}',
'back_message': 'Back to hatchery?'
}
return render_template('error_message.html', error=error,
current_user=current_user)
timediff = datetime.now() - hatchery['created_at']
if timediff.days < 0:
error = {
'error_message': "This brood is not ready to hatch.",
'error_link': f'/hatcheries/{hatchery_id}',
'back_message': 'Back to hatchery?'
}
return render_template('error_message.html', error=error,
current_user=current_user)
hatchling_ids = generate_hatchlings(mother, father)
brood = {
'mother_name': hatchery['mother_name'],
'mother_id': hatchery['mother_id'],
'father_name': hatchery['father_name'],
'father_id': hatchery['father_id'],
'breeder': hatchery['owner'],
'user_id': hatchery['user_id'],
'species': mother['species'],
'hatched_at': datetime.now()}
brood_id = broods.insert_one(brood).inserted_id
for hatchling_id in hatchling_ids:
ranchos.update_one(
{'_id': ObjectId(hatchling_id)},
{'$set': {'hatched_at': brood['hatched_at'],
'brood_id': brood_id}})
hatcheries.delete_one({'_id': ObjectId(hatchery_id)})
return redirect(url_for('broods_show',
brood_id=brood_id))
def check_compatible(mother, father):
"""Return true if the pairing is compatible."""
# father is male and mother is female
if mother['sex'] == father['sex']:
return False
# different species
if mother['species'] != father['species']:
return False
# related
if 'ancestry' in mother.keys():
if mother['ancestry']['father_id'] == father['_id']:
return False
if 'ancestry' in father.keys():
if mother['ancestry']['mother_id'] == father['ancestry']['mother_id']:
return False
if mother['ancestry']['father_id'] == father['ancestry']['father_id']:
return False
if 'ancestry' in father.keys():
if father['ancestry']['mother_id'] == mother['_id']:
return False
# already breeding
if hatcheries.find({'mother_id': mother['_id']}).count() > 0:
return False
if hatcheries.find({'father_id': father['_id']}).count() > 0:
return False
# both owned by current_user
current_user = session['user']
if mother['user_id'] != father['user_id']:
return False
if str(mother['user_id']) != str(current_user['user_id']):
return False
# level 3+
if int(mother['level']) < 3:
return False
if int(father['level']) < 3:
return False
# full health
if int(mother['needs']['health']) < 100:
return False
return True
def generate_hatchlings(mother, father):
"""Return list of new rancho hatchlings based on parents."""
egg_count = randint(6, 10) # 6-10 hatchlings per brood
species = mother['species'] # parents should be same species
rancho_sexes = ['Male', 'Female'] # pick random from this
hatchlings = [] # list will contain the brood's hatchlings
for count in range(1, egg_count):
# Stat generation: between parents' stats, +/-20, 0<= stat<= 100
hardiness = randint(
min(
int(mother['stats']['hardiness']),
int(father['stats']['hardiness'])
) - 20,
max(
int(mother['stats']['hardiness']),
int(father['stats']['hardiness'])
) + 20
)
if hardiness > 100:
hardiness = 100
elif hardiness < 0:
hardiness = 0
dexterity = randint(
min(
int(mother['stats']['dexterity']),
int(father['stats']['dexterity'])
) - 20,
max(
int(mother['stats']['dexterity']),
int(father['stats']['dexterity'])
) + 20
)
if dexterity > 100:
dexterity = 100
elif dexterity < 0:
dexterity = 0
docility = randint(
min(
int(mother['stats']['docility']),
int(father['stats']['docility'])
) - 20,
max(
int(mother['stats']['docility']),
int(father['stats']['docility'])
) + 20
)
if docility > 100:
docility = 100
elif docility < 0:
docility = 0
conformation = randint(
min(
int(mother['stats']['conformation']),
int(father['stats']['conformation'])
) - 20,
max(
int(mother['stats']['conformation']),
int(father['stats']['conformation'])
) + 20
)
if conformation > 100:
conformation = 100
elif conformation < 0:
conformation = 0
stats = {
'hardiness': str(hardiness),
'dexterity': str(dexterity),
'docility': str(docility),
'conformation': str(conformation)
}
needs = {
'food': 100,
'water': 100,
'health': 100,
'happiness': 100,
'last_cared': datetime.now(),
'cared_by': mother['owner'],
'cared_by_id': ObjectId(mother['user_id'])
}
ancestry = {
'mother_name': mother['name'],
'mother_id': mother['_id'],
'father_name': father['name'],
'father_id': father['_id']
}
rancho_sex = choice(rancho_sexes)
rancho = {
'name': 'Hatchling Rancho',
'bio': rancho_sex + ' ' + species,
'xp': 0,
'level': 0,
'stats': stats,
'needs': needs,
'ancestry': ancestry,
'species': species,
'sex': rancho_sex,
'owner': mother['owner'],
'user_id': ObjectId(mother['user_id'])
}
rancho_id = ranchos.insert_one(rancho).inserted_id
hatchlings.append(rancho_id)
return hatchlings