-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdckingdoms_git.py
executable file
·1637 lines (1300 loc) · 64.3 KB
/
dckingdoms_git.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
import discord
import pandas as pd
import pymysql
from datetime import datetime, timedelta
import threading
import json
import random
server = ""
user = ""
pwd = ""
schema = ""
db = pymysql.connect(server, user, pwd, schema)
db.db
cursor = db.cursor(pymysql.cursors.DictCursor)
#check connection to database
def ping_msql():
threading.Timer(3600.0, ping_msql).start()
db.ping(reconnect=True)
ping_msql()
def read_quotes():
quotes = []
with open("quotes.txt", 'r') as f:
for l in f:
quotes.append(l.strip())
return quotes
quotes = read_quotes()
def get_quote():
global quotes
quote = "```" + random.choice(quotes) + "```"
return quote
def get_datetime():
now = datetime.now()
formatted_date = now.strftime('%Y-%m-%d %H:%M:%S')
return formatted_date
def add_sub_hours(amount):
date = datetime.now() + timedelta(hours=amount)
formatted_date = date.strftime('%Y-%m-%d %H:%M:%S')
return formatted_date
def compare_date(database_date):
database_date = datetime.strptime(database_date, '%Y-%m-%d %H:%M:%S')
now = get_datetime()
now = datetime.strptime(now, '%Y-%m-%d %H:%M:%S')
if now > database_date:
return True
else:
return False
def time_diff(database_date):
database_date = datetime.strptime(database_date, '%Y-%m-%d %H:%M:%S')
diff = database_date - datetime.now()
return diff
def owner_admin(server_id, user_id):
query = "SELECT game.owner_id FROM game WHERE server_id = " + server_id + " AND game.owner_id = " + user_id
df = pd.read_sql(query, db)
index = df.index
count = len(index)
if count > 0:
return True
else:
citizen_id = get_citizen_id(server_id, user_id)
query = "SELECT citizen.id FROM citizen WHERE citizen.id = '%s' AND citizen.role = 'admin'" % (citizen_id)
index = df.index
count = len(index)
if count > 0:
return True
else:
return False
def game_started(server_id):
query = "SELECT game.id FROM game WHERE server_id = " + server_id
df = pd.read_sql(query, db)
index = df.index
count = len(index)
if count > 0:
return True
else:
return False
def update_game(server_id):
now = get_datetime()
sql = "UPDATE game SET game.updated = '%s' WHERE game.server_id = '%s'" % (now, server_id)
cursor.execute(sql)
db.commit()
def kingdom_exists(server_id, kingdom_name):
query = "SELECT kingdom.id FROM kingdom INNER JOIN game ON kingdom.game_id = game.id WHERE name = '%s' AND game.server_id = '%s'" % (kingdom_name, server_id)
df = pd.read_sql(query, db)
index = df.index
count = len(index)
if count > 0:
return True
else:
return False
def region_exists(server_id, region_name):
query = "SELECT kingdom.id FROM region INNER JOIN kingdom ON region.kingdom_id = kingdom.id INNER JOIN game ON kingdom.game_id = game.id WHERE region.name = '%s' AND game.server_id = '%s'" % (region_name, server_id)
df = pd.read_sql(query, db)
index = df.index
count = len(index)
if count > 0:
return True
else:
return False
def user_exists(server_id, user_id):
query = "SELECT citizen.id FROM citizen INNER JOIN kingdom ON citizen.kingdom_id = kingdom.id INNER JOIN game ON kingdom.game_id = game.id WHERE citizen.userid = '%s' AND game.server_id = '%s'" % (user_id, server_id)
df = pd.read_sql(query, db)
index = df.index
count = len(index)
if count > 0:
return True
else:
return False
def battle_exists(region_id):
query = "SELECT attack.id FROM attack WHERE attack.region_id = '%s'" % (region_id)
df = pd.read_sql(query, db)
index = df.index
count = len(index)
if count > 0:
return True
else:
return False
def end_battle(battle_id):
query = "SELECT attack.attacker_strength, attack.defender_strength, attack.region_id, attack.attacker_kingdom, attack.defender_kingdom FROM attack WHERE id = '%s'" % (battle_id)
df = pd.read_sql(query, db)
attacker_strength = df['attacker_strength'].iloc[0]
attacker_strength = int(attacker_strength)
defender_strength = df['defender_strength'].iloc[0]
defender_strength = int(defender_strength)
region_id = df['region_id'].iloc[0]
region_id = int(region_id)
attacker_kingdom = df['attacker_kingdom'].iloc[0]
attacker_kingdom = int(attacker_kingdom)
defender_kingdom = df['defender_kingdom'].iloc[0]
defender_kingdom = int(defender_kingdom)
if attacker_strength > defender_strength:
sql = "UPDATE region SET region.kingdom_id = '%s' WHERE region.id = '%s'" % (attacker_kingdom, region_id)
cursor.execute(sql)
db.commit()
sql = "DELETE from attack WHERE attack.id = '%s'" % (battle_id)
cursor.execute(sql)
db.commit()
msg = "The attacker won the battle."
return msg
else:
sql = "DELETE from attack WHERE attack.id = '%s'" % (battle_id)
cursor.execute(sql)
db.commit()
msg = "The defender won the battle."
return msg
def select_game_id(server_id):
query = "SELECT game.id FROM game WHERE game.server_id = " + server_id
df = pd.read_sql(query, db)
game_id = df['id'].iloc[0]
game_id = int(game_id)
return game_id
def get_kingdom_id(server_id, kingdom_name):
game_id = select_game_id(server_id)
query = "SELECT kingdom.id FROM kingdom INNER JOIN game ON kingdom.game_id = game.id WHERE name = '%s' AND game.id = '%s'" % (kingdom_name, game_id)
df = pd.read_sql(query, db)
kingdom_id = df['id'].iloc[0]
kingdom_id = int(kingdom_id)
return kingdom_id
def get_region_id(server_id, region_name):
query = "SELECT region.id FROM region INNER JOIN kingdom ON region.kingdom_id = kingdom.id INNER JOIN game ON kingdom.game_id = game.id WHERE region.name = '%s' AND game.server_id = '%s'" % (region_name, server_id)
df = pd.read_sql(query, db)
region_id = df['id'].iloc[0]
region_id = int(region_id)
return region_id
def get_citizen_id(server_id, user_id):
query = "SELECT citizen.id FROM citizen INNER JOIN kingdom ON citizen.kingdom_id = kingdom.id INNER JOIN game ON kingdom.game_id = game.id WHERE citizen.userid = '%s' AND game.server_id = '%s'" % (user_id, server_id)
df = pd.read_sql(query, db)
citizen_id = df['id'].iloc[0]
citizen_id = int(citizen_id)
return citizen_id
def get_battle_id(region_id):
query = "SELECT attack.id FROM attack WHERE attack.region_id = '%s'" % (region_id)
df = pd.read_sql(query, db)
battle_id = df['id'].iloc[0]
battle_id = int(battle_id)
return battle_id
def get_king_id(server_id, kingdom_name, user_id):
if game_started(server_id):
if kingdom_exists(server_id, kingdom_name):
kingdom_id = get_kingdom_id(server_id, kingdom_name)
query = "SELECT kingdom.king_id FROM kingdom WHERE id = '%s'" % (kingdom_id)
df = pd.read_sql(query, db)
king_id = df['king_id'].iloc[0]
if king_id != None:
king_id = int(king_id)
return king_id
else:
msg = "This kingdom does not exist."
return msg
else:
msg = "No game has been started on this server."
return msg
def user_is_king(kingdom_id, user_id):
query = "SELECT kingdom.king_id FROM kingdom WHERE id = '%s'" % (kingdom_id)
df = pd.read_sql(query, db)
king_id = df['king_id'].iloc[0]
user_id = int(user_id)
if king_id != None:
king_id = int(king_id)
if king_id == user_id:
return True
else:
return False
def start_game(server_id, user_id):
if game_started(server_id):
msg = "A game has already started on this server."
return msg
else:
now = get_datetime()
sql = "INSERT INTO game (server_id, owner_id, created, updated) VALUES (%s, %s, %s, %s)"
val = (server_id, user_id, now, now)
cursor.execute(sql, val)
db.commit()
msg = "Game has started."
return msg
def end_game(server_id, user_id):
if game_started(server_id):
if owner_admin(server_id, user_id):
sql = "DELETE FROM game WHERE game.server_id = '%s'" % (server_id)
cursor.execute(sql)
db.commit()
msg = "Game has ended."
return msg
else:
msg = "A game has not been started on this server."
return msg
def create_kingdom(server_id, user_id, kingdom_name):
if game_started(server_id):
if owner_admin(server_id, user_id):
if kingdom_exists(server_id, kingdom_name):
msg = "A kingdom with this name already exists."
return msg
else:
game_id = select_game_id(server_id)
sql = "INSERT INTO kingdom (game_id, name, workpower) VALUES (%s, %s, %s)"
val = (game_id, kingdom_name, 0)
cursor.execute(sql, val)
db.commit()
msg = "Created kingdom."
return msg
else:
msg = "You do not have permission for this command."
return msg
else:
msg = "No game has been started on this server."
return msg
def create_region(server_id, user_id, kingdom_name, region_name):
if game_started(server_id):
if owner_admin(server_id, user_id):
if kingdom_exists(server_id, kingdom_name):
if region_exists(server_id, region_name):
msg = "A region with this name already exists on this server."
return msg
else:
kingdom_id = get_kingdom_id(server_id, kingdom_name)
sql = "INSERT INTO region (kingdom_id, name) VALUES (%s, %s)"
val = (kingdom_id, region_name)
cursor.execute(sql, val)
db.commit()
msg = "Created region."
return msg
else:
msg = "There is no kingdom with this name on this server."
return msg
else:
msg = "You do not have permission for this command."
return msg
else:
msg = "No game has been started on this server."
return msg
def set_admin(server_id, user_id, new_admin_id):
if game_started(server_id):
if owner_admin(server_id, user_id):
if user_exists(server_id, new_admin_id):
new_admin_citizen_id = get_citizen_id(server_id, new_admin_id)
sql = "UPDATE citizen SET citizen.role = 'admin' WHERE citizen.id = '%s'" % (new_admin_citizen_id)
cursor.execute(sql)
db.commit()
msg = "Added new admin."
return msg
else:
msg = "This user did not joined the game yet."
return msg
else:
msg = "You do not have permission for this command."
return msg
else:
msg = "No game has been started on this server."
return msg
def demote_admin(server_id, user_id, new_admin_id):
if game_started(server_id):
if owner_admin(server_id, user_id):
if user_exists(server_id, new_admin_id):
new_admin_citizen_id = get_citizen_id(server_id, new_admin_id)
sql = "UPDATE citizen SET citizen.role = 'citizen' WHERE citizen.id = '%s'" % (new_admin_citizen_id)
cursor.execute(sql)
db.commit()
msg = "Demoted admin."
return msg
else:
msg = "This user did not joined the game yet."
return msg
else:
msg = "You do not have permission for this command."
return msg
else:
msg = "No game has been started on this server."
return msg
def set_king(server_id, user_id, kingdom_name, new_king_id):
if game_started(server_id):
if owner_admin(server_id, user_id):
if kingdom_exists(server_id, kingdom_name):
if user_exists(server_id, new_king_id):
kingdom_id = get_kingdom_id(server_id, kingdom_name)
game_id = select_game_id(server_id)
sql = "UPDATE kingdom SET kingdom.king_id = 'null' WHERE kingdom.king_id = '%s' AND kingdom.game_id = '%s'" % (new_king_id, game_id)
cursor.execute(sql)
db.commit()
sql = "UPDATE kingdom SET kingdom.king_id = '%s' WHERE kingdom.id = '%s' AND kingdom.game_id = '%s'" % (new_king_id, kingdom_id, game_id)
cursor.execute(sql)
db.commit()
msg = "Appointed new king."
return msg
else:
msg = "This user did not joined the game yet."
return msg
else:
msg = "There is no kingdom with this name on this server."
return msg
else:
msg = "You do not have permission for this command."
return msg
else:
msg = "No game has been started on this server."
return msg
def join_kingdom(server_id, user_id, kingdom_name, region_name):
if game_started(server_id):
if kingdom_exists(server_id, kingdom_name):
if region_exists(server_id, region_name):
if user_exists(server_id, user_id):
msg = "You already joined a kingdom."
return msg
else:
now = add_sub_hours(-2)
kingdom_id = get_kingdom_id(server_id, kingdom_name)
region_id = get_region_id(server_id, region_name)
sql = "INSERT INTO citizen (kingdom_id, userid, role, strength, trained, location, moved, worked) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
val = (kingdom_id, user_id, "citizen", 0, now, region_id, now, now)
cursor.execute(sql, val)
db.commit()
msg = "You joined a kingdom."
return msg
else:
msg = "A region with this name does not exists on this server."
return msg
else:
msg = "There is no kingdom with this name on this server."
return msg
else:
msg = "No game has been started on this server."
return msg
def train(server_id, user_id):
if game_started(server_id):
if user_exists(server_id, user_id):
citizen_id = get_citizen_id(server_id, user_id)
query = "SELECT citizen.trained, citizen.strength FROM citizen WHERE id = '%s'" % (citizen_id)
df = pd.read_sql(query, db)
trained = df['trained'].iloc[0]
trained = str(trained)
strength = df['strength'].iloc[0]
strength = int(strength)
if compare_date(trained):
new_date = add_sub_hours(1)
strength = strength + 5
sql = "UPDATE citizen SET citizen.trained = '%s', citizen.strength = '%s' WHERE citizen.id = '%s'" % (new_date, strength, citizen_id)
cursor.execute(sql)
db.commit()
msg = "You successfully trained."
return msg
else:
msg = "You can only train once every hour."
return msg
else:
msg = "You do not participate in this game yet."
return msg
else:
msg = "No game has been started on this server."
return msg
def work(server_id, user_id):
if game_started(server_id):
if user_exists(server_id, user_id):
citizen_id = get_citizen_id(server_id, user_id)
query = "SELECT citizen.worked, citizen.kingdom_id, citizen.currency FROM citizen WHERE id = '%s'" % (citizen_id)
df = pd.read_sql(query, db)
worked = df['worked'].iloc[0]
worked = str(worked)
kingdom_id = df['kingdom_id'].iloc[0]
kingdom_id = int(kingdom_id)
currency = df['currency'].iloc[0]
currency = float(currency)
query = "SELECT kingdom.workpower FROM kingdom WHERE id = '%s'" % (kingdom_id)
df = pd.read_sql(query, db)
workpower = df['workpower'].iloc[0]
workpower = int(workpower)
if compare_date(worked):
new_date = add_sub_hours(1)
workpower = workpower + 5
currency = currency + 5
sql = "UPDATE citizen SET citizen.worked = '%s', citizen.currency = '%s' WHERE citizen.id = '%s'" % (new_date, currency, citizen_id)
cursor.execute(sql)
db.commit()
sql = "UPDATE kingdom SET kingdom.workpower = '%s' WHERE kingdom.id = '%s'" % (workpower, kingdom_id)
cursor.execute(sql)
db.commit()
msg = "You successfully worked."
return msg
else:
msg = "You can only work once every hour."
return msg
else:
msg = "You do not participate in this game yet."
return msg
else:
msg = "No game has been started on this server."
return msg
def attack_region(server_id, user_id, kingdom_name, region_name):
if game_started(server_id):
if kingdom_exists(server_id, kingdom_name):
if region_exists(server_id, region_name):
if user_exists(server_id, user_id):
kingdom_id = get_kingdom_id(server_id, kingdom_name)
region_id = get_region_id(server_id, region_name)
if user_is_king(kingdom_id, user_id):
query = "SELECT attack.id FROM attack WHERE region_id = '%s'" % (region_id)
df = pd.read_sql(query, db)
index = df.index
count = len(index)
if count == 0:
attacker_id = kingdom_id
query = "SELECT region.kingdom_id FROM region WHERE id = '%s'" % (region_id)
df = pd.read_sql(query, db)
defender_id = df['kingdom_id'].iloc[0]
defender_id = int(defender_id)
new_date = add_sub_hours(12)
if attacker_id != defender_id:
sql = "INSERT INTO attack (region_id, attacker_kingdom, defender_kingdom, attacker_strength, defender_strength, started) VALUES (%s, %s, %s, %s, %s, %s)"
val = (region_id, attacker_id, defender_id, 0, 0, new_date)
cursor.execute(sql, val)
db.commit()
msg = "The attack has started."
return msg
else:
msg = "You can not attack your own kingdom."
return msg
else:
msg = "This region is already under attack."
return msg
else:
msg = "You are not the king of this kingdom."
return msg
else:
msg = "You did not join the game yet."
return msg
else:
msg = "A region with this name does not exists on this server."
return msg
else:
msg = "There is no kingdom with this name on this server."
return msg
else:
msg = "No game has been started on this server."
return msg
def change_location(server_id, region_name, user_id):
if game_started(server_id):
if region_exists(server_id, region_name):
if user_exists(server_id, user_id):
citizen_id = get_citizen_id(server_id, user_id)
query = "SELECT citizen.moved FROM citizen WHERE id = '%s'" % (citizen_id)
df = pd.read_sql(query, db)
moved = df['moved'].iloc[0]
moved = str(moved)
if compare_date(moved):
new_date = add_sub_hours(1)
region_id = get_region_id(server_id, region_name)
sql = "UPDATE citizen SET citizen.location = '%s', citizen.moved = '%s' WHERE citizen.id = '%s'" % (region_id, new_date, citizen_id)
cursor.execute(sql)
db.commit()
msg = "You successfully moved."
return msg
else:
msg = "You can only move once every hour."
return msg
else:
msg = "You do not participate in this game yet."
return msg
else:
msg = "This region does not exist on this server."
return msg
else:
msg = "No game has been started on this server."
return msg
def attack(server_id, region_name, user_id, amount):
if game_started(server_id):
if region_exists(server_id, region_name):
if user_exists(server_id, user_id):
citizen_id = get_citizen_id(server_id, user_id)
region_id = get_region_id(server_id, region_name)
if battle_exists(region_id):
battle_id = get_battle_id(region_id)
query = "SELECT attack.attacker_strength, attack.started FROM attack WHERE id = '%s'" % (battle_id)
df = pd.read_sql(query, db)
attacker_strength = df['attacker_strength'].iloc[0]
attacker_strength = int(attacker_strength)
started = df['started'].iloc[0]
started = str(started)
query = "SELECT citizen.strength, citizen.location FROM citizen WHERE id = '%s'" % (citizen_id)
df = pd.read_sql(query, db)
strength = df['strength'].iloc[0]
strength = int(strength)
location = df['location'].iloc[0]
location = int(location)
if location == region_id:
if compare_date(started):
msg = end_battle(battle_id)
return msg
else:
attacker_strength = attacker_strength + amount
strength = strength - amount
if strength >= 0:
sql = "UPDATE attack SET attack.attacker_strength = '%s' WHERE attack.id = '%s'" % (attacker_strength, battle_id)
cursor.execute(sql)
db.commit()
sql = "UPDATE citizen SET citizen.strength = '%s' WHERE citizen.id = '%s'" % (strength, citizen_id)
cursor.execute(sql)
db.commit()
msg = "You successfully attacked the region."
return msg
else:
msg = "You do not have enough strength"
return msg
else:
msg = "You are not in the region you want to attack."
return msg
else:
msg = "There is no ongoing battle in his region."
return msg
else:
msg = "You do not participate in this game yet."
return msg
else:
msg = "This region does not exist on this server."
return msg
else:
msg = "No game has been started on this server."
return msg
def defend(server_id, region_name, user_id, amount):
if game_started(server_id):
if region_exists(server_id, region_name):
if user_exists(server_id, user_id):
citizen_id = get_citizen_id(server_id, user_id)
region_id = get_region_id(server_id, region_name)
if battle_exists(region_id):
battle_id = get_battle_id(region_id)
query = "SELECT attack.defender_strength, attack.started FROM attack WHERE id = '%s'" % (battle_id)
df = pd.read_sql(query, db)
defender_strength = df['defender_strength'].iloc[0]
defender_strength = int(defender_strength)
started = df['started'].iloc[0]
started = str(started)
query = "SELECT citizen.strength, citizen.location FROM citizen WHERE id = '%s'" % (citizen_id)
df = pd.read_sql(query, db)
strength = df['strength'].iloc[0]
strength = int(strength)
location = df['location'].iloc[0]
location = int(location)
if location == region_id:
if compare_date(started):
msg = end_battle(battle_id)
return msg
else:
defender_strength = defender_strength + amount
strength = strength - amount
if strength >= 0:
sql = "UPDATE attack SET attack.defender_strength = '%s' WHERE attack.id = '%s'" % (defender_strength, battle_id)
cursor.execute(sql)
db.commit()
sql = "UPDATE citizen SET citizen.strength = '%s' WHERE citizen.id = '%s'" % (strength, citizen_id)
cursor.execute(sql)
db.commit()
msg = "You successfully defended the region."
return msg
else:
msg = "You do not have enough strength"
return msg
else:
msg = "You are not in the region you want to defend."
return msg
else:
msg = "There is no ongoing battle in his region."
return msg
else:
msg = "You do not participate in this game yet."
return msg
else:
msg = "This region does not exist on this server."
return msg
else:
msg = "No game has been started on this server."
return msg
def use_workpower(server_id, region_name, kingdom_name, user_id, amount):
if game_started(server_id):
if kingdom_exists(server_id, kingdom_name):
if region_exists(server_id, region_name):
if user_exists(server_id, user_id):
kingdom_id = get_kingdom_id(server_id, kingdom_name)
region_id = get_region_id(server_id, region_name)
if user_is_king(kingdom_id, user_id):
if battle_exists(region_id):
battle_id = get_battle_id(region_id)
query = "SELECT attack.attacker_strength, defender_strength, attack.started, attack.attacker_kingdom, attack.defender_kingdom FROM attack WHERE id = '%s'" % (battle_id)
df = pd.read_sql(query, db)
attacker_strength = df['attacker_strength'].iloc[0]
attacker_strength = int(attacker_strength)
defender_strength = df['defender_strength'].iloc[0]
defender_strength = int(defender_strength)
started = df['started'].iloc[0]
started = str(started)
attacker_kingdom = df['attacker_kingdom'].iloc[0]
attacker_kingdom = int(attacker_kingdom)
defender_kingdom = df['defender_kingdom'].iloc[0]
defender_kingdom = int(defender_kingdom)
query = "SELECT kingdom.workpower FROM kingdom WHERE id = '%s'" % (kingdom_id)
df = pd.read_sql(query, db)
workpower = df['workpower'].iloc[0]
workpower = int(workpower)
if kingdom_id == attacker_kingdom:
if compare_date(started):
msg = end_battle(battle_id)
return msg
else:
attacker_strength = attacker_strength + amount
workpower = workpower - amount
if workpower >= 0:
sql = "UPDATE attack SET attack.attacker_strength = '%s' WHERE attack.id = '%s'" % (attacker_strength, battle_id)
cursor.execute(sql)
db.commit()
sql = "UPDATE kingdom SET kingdom.workpower = '%s' WHERE kingdom.id = '%s'" % (workpower, kingdom_id)
cursor.execute(sql)
db.commit()
msg = "You successfully attacked the region."
return msg
else:
msg = "The kingdom does not have enough workpower"
return msg
else:
if compare_date(started):
msg = end_battle(battle_id)
return msg
else:
defender_strength = defender_strength + amount
workpower = workpower - amount
if workpower >= 0:
sql = "UPDATE attack SET attack.defender_strength = '%s' WHERE attack.id = '%s'" % (defender_strength, battle_id)
cursor.execute(sql)
db.commit()
sql = "UPDATE kingdom SET kingdom.workpower = '%s' WHERE kingdom.id = '%s'" % (workpower, kingdom_id)
cursor.execute(sql)
db.commit()
msg = "You successfully defended the region."
return msg
else:
msg = "The kingdom does not have enough workpower"
return msg
else:
msg = "There is no ongoing battle in his region."
return msg
else:
msg = "You are not the king of this kingdom."
return msg
else:
msg = "You do not participate in this game yet."
return msg
else:
msg = "This region does not exist on this server."
return msg
else:
msg = "This kingdom does not exist on this server."
return msg
else:
msg = "No game has been started on this server."
return msg
def profile(server_id, user_id, user_profile_id, user_profile_name):
if game_started(server_id):
if user_exists(server_id, user_id):
citizen_id = get_citizen_id(server_id, user_id)
query = "SELECT citizen.location, citizen.strength, citizen.kingdom_id, citizen.currency FROM citizen WHERE id = '%s'" % (citizen_id)
df = pd.read_sql(query, db)
location_id= df['location'].iloc[0]
location_id = int(location_id)
strength = df['strength'].iloc[0]
strength = str(strength)
kingdom_id = df['kingdom_id'].iloc[0]
kingdom_id = int(kingdom_id)
currency = df['currency'].iloc[0]
currency = round(float(currency), 3)
query = "SELECT kingdom.name FROM kingdom WHERE id = '%s'" % (kingdom_id)
df = pd.read_sql(query, db)
kingdom_name = df['name'].iloc[0]
kingdom_name = str(kingdom_name)
query = "SELECT region.name FROM region WHERE id = '%s'" % (location_id)
df = pd.read_sql(query, db)
region_name = df['name'].iloc[0]
region_name = str(region_name)
msg = "User: " + user_profile_name + ", strength: " + strength + ", kingdom: " + kingdom_name + ", location: " + region_name + ", currency: " + str(currency)
return msg
else:
msg = "This user does not participate in this game yet."
return msg
else:
msg = "No game has been started on this server."
return msg
def kingdom_info(server_id, kingdom_name, king_name, user_id):
if game_started(server_id):
if kingdom_exists(server_id, kingdom_name):
kingdom_id = get_kingdom_id(server_id, kingdom_name)
query = "SELECT kingdom.king_id, kingdom.workpower FROM kingdom WHERE id = '%s'" % (kingdom_id)
df = pd.read_sql(query, db)
king_id= df['king_id'].iloc[0]
if king_id != None:
king_id = int(king_id)
workpower = df['workpower'].iloc[0]
workpower = str(workpower)
query = "SELECT kingdom.name FROM kingdom WHERE id = '%s'" % (kingdom_id)
df = pd.read_sql(query, db)
kingdom_name = df['name'].iloc[0]
kingdom_name = str(kingdom_name)
query = "SELECT region.name FROM region WHERE kingdom_id = '%s'" % (kingdom_id)
df = pd.read_sql(query, db)
df_list = df.values.tolist()
msg = "Kingdom: " + kingdom_name + ", king: " + king_name + ", workpower: " + workpower + ", regions: "
for item in df_list:
msg = msg + str(item).strip('[]') + ", "
return msg
else:
msg = "This kingdom does not exist."
return msg
else:
msg = "No game has been started on this server."
return msg
def kingdom_list(server_id, user_id):
if game_started(server_id):
query = "SELECT kingdom.name FROM kingdom INNER JOIN game ON kingdom.game_id = game.id WHERE server_id = '%s'" % (server_id)
df = pd.read_sql(query, db)
df_list = df.values.tolist()
msg = "Kingdoms: "
for item in df_list:
msg = msg + str(item).strip('[]') + ", "
return msg
else:
msg = "No game has been started on this server."
return msg
def region_list(server_id, user_id):
if game_started(server_id):
query = "SELECT region.name FROM region INNER JOIN kingdom ON region.kingdom_id = kingdom.id INNER JOIN game ON kingdom.game_id = game.id WHERE server_id = '%s'" % (server_id)
df = pd.read_sql(query, db)
df_list = df.values.tolist()
msg = "Regions: "
for item in df_list:
msg = msg + str(item).strip('[]') + ", "
return msg
else:
msg = "No game has been started on this server."
return msg
def status_battle(server_id, region_name, user_id):
if game_started(server_id):
if region_exists(server_id, region_name):
region_id = get_region_id(server_id, region_name)
if battle_exists(region_id):
battle_id = get_battle_id(region_id)
query = "SELECT attack.attacker_strength, defender_strength, attack.started, attack.attacker_kingdom, attack.defender_kingdom FROM attack WHERE id = '%s'" % (battle_id)
df = pd.read_sql(query, db)
attacker_strength = df['attacker_strength'].iloc[0]
attacker_strength = str(attacker_strength)
defender_strength = df['defender_strength'].iloc[0]
defender_strength = str(defender_strength)
started = df['started'].iloc[0]
started = str(started)
attacker_kingdom = df['attacker_kingdom'].iloc[0]
attacker_kingdom = int(attacker_kingdom)
defender_kingdom = df['defender_kingdom'].iloc[0]