-
Notifications
You must be signed in to change notification settings - Fork 4
/
process_laws.php
1326 lines (1190 loc) · 55 KB
/
process_laws.php
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
<?php
//Description: Check for expired laws and process them. Run every 3 min.
include('/home/u153769202/domains/vmundus.online/public_html/connect_db.php');
include('/home/u153769202/domains/vmundus.online/public_html/php_functions/get_time_for_id.php'); //function getTimeForId().
include('/home/u153769202/domains/vmundus.online/public_html/php_functions/send_notification.php'); //function sendNotification($notification, $user_id).
$query = "SELECT law_id, country_id, responsibility_id, yes, no, proposed_by FROM country_law_info
WHERE DATE_ADD(TIMESTAMP(proposed_date, proposed_time), INTERVAL 24 HOUR) <= NOW() AND is_processed = 0";
$result = $conn->query($query);
while($row = $result->fetch_row()) {
list($law_id, $country_id, $responsibility_id, $yes, $no, $proposed_by) = $row;
processLaw($law_id, $country_id, $responsibility_id, $yes, $no, $proposed_by);
}
$query = "SELECT law_id, country_id, responsibility_id, yes, no, proposed_by FROM country_law_info
WHERE is_processed = 0";
$result = $conn->query($query);
while($row = $result->fetch_row()) {
list($law_id, $country_id, $responsibility_id, $yes, $no, $proposed_by) = $row;
$query = "SELECT COUNT(user_id) FROM users_voted_for_laws
WHERE law_id = '$law_id' HAVING COUNT(user_id) >=
CEIL(((SELECT COUNT(user_id) FROM country_government WHERE position_id
IN (SELECT position_id FROM government_country_responsibilities
WHERE country_id = '$country_id' AND responsibility_id = '$responsibility_id' AND must_sign_vote = TRUE)
AND country_id = '$country_id') +
(SELECT COUNT(user_id) FROM congress_members WHERE country_id = '$country_id'
AND ((SELECT must_sign_vote FROM government_country_responsibilities
WHERE country_id = '$country_id' AND responsibility_id = '$responsibility_id' AND position_id = 3)) = TRUE)) * 0.65)";
$result_check = $conn->query($query);
if($result_check->num_rows >= 1) {
processLaw($law_id, $country_id, $responsibility_id, $yes, $no, $proposed_by);
}
}
function processLaw($law_id, $country_id, $responsibility_id, $yes, $no, $proposed_by) {
global $conn;
//change gov term length
if($responsibility_id == 1) {
if($yes > $no) {
$query = "SELECT position_id, new_term FROM change_gov_term_length WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($position_id, $new_term) = $row_law;
$query = "UPDATE government_term_length SET term = '$new_term' WHERE country_id = '$country_id'
AND position_id = '$position_id'";
$conn->query($query);
}
setLawToProcessed($law_id);
}
//join leave union
else if($responsibility_id == 2) {
if($yes > $no) {
$query = "SELECT action, union_id FROM join_leave_union WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($action, $union_id) = $row_law;
if($action == 0) {//leave
$query = "DELETE FROM country_unions WHERE country_id = '$country_id'";
$conn->query($query);
//delete from processing_union_application if country is founder and new member wants to join union
$query = "SELECT law_id FROM country_law_info WHERE responsibility_id = 7 AND country_id = '$country_id'";
$result_app = $conn->query($query);
if($result_app->num_rows > 0) {
while($row_app = $result_app->fetch_row()) {
list($law_id_app) = $row_app;
$query = "DELETE FROM processing_union_application WHERE founder_law_id = '$law_id_app'";
$conn->query($query);
$query = "DELETE FROM users_voted_for_laws WHERE law_id = '$law_id_app'";
$conn->query($query);
$query = "DELETE FROM country_law_info WHERE law_id = '$law_id_app'";
$conn->query($query);
}
}
//delete union if no members left
$query = "SELECT COUNT(*) FROM country_unions WHERE union_id = '$union_id'";
$result_unions = $conn->query($query);
$row_unions = $result_unions->fetch_row();
list($members) = $row_unions;
if($members == 0) {
$query = "DELETE FROM unions WHERE union_id = '$union_id'";
$conn->query($query);
}
}
else if ($action == 1) {//join
//update join_leave_union to "processing application (action = 2)"
$query = "UPDATE join_leave_union SET action = 2 WHERE law_id = '$law_id'";
$conn->query($query);
//issue law for all union founders, to accept or decline country
$query = "SELECT country_id FROM country_unions WHERE union_id = '$union_id' AND is_founder = TRUE";
$result_founders = $conn->query($query);
while($row_founders = $result_founders->fetch_row()) {
list($founder_id) = $row_founders;
$law_id_founder = getTimeForId() . $founder_id;
//register new law
$query = "INSERT INTO country_law_info VALUES('$law_id_founder', '$founder_id', 7, 0, 0,
'$proposed_by', CURRENT_DATE, CURRENT_TIME, 0)";
$conn->query($query);
$query = "INSERT INTO processing_union_application VALUES('$law_id', '$law_id_founder')";
$conn->query($query);
//inform government
notifyToVote($founder_id, 7);
}
}
}
setLawToProcessed($law_id);
}
//Print money
else if($responsibility_id == 3) {
setLawToProcessed($law_id);
if($yes > $no) {
$query = "SELECT pm.amount AS printing_amount, pmc.amount AS stack, pmc.product_amount AS required_products_per_stack,
IFNULL(mp.amount,0) AS available_products, currency_id, pmc.product_id, pm.position_id
FROM print_money pm, print_money_cost pmc, ministry_product mp WHERE mp.product_id = pmc.product_id
AND mp.country_id = '$country_id' AND law_id = '$law_id' AND mp.position_id = pm.position_id";
$result_law = $conn->query($query);
if($result_law->num_rows != 1) {
return;
}
$row_law = $result_law->fetch_row();
list($printing_amount, $stack, $required_products_per_stack, $available_products, $currency_id, $product_id, $position_id) = $row_law;
$required_products = round(($printing_amount / $stack) * $required_products_per_stack, 2);
if($available_products >= $required_products) {
//update ministry product table
$query = "UPDATE ministry_product SET amount = (SELECT * FROM (SELECT amount FROM ministry_product
WHERE country_id = '$country_id' AND product_id = '$product_id' AND position_id = '$position_id')
AS temp) - '$required_products' WHERE country_id = '$country_id' AND product_id = '$product_id'
AND position_id = '$position_id'";
$conn->query($query);
//update country_currency table with new currency
$query = "SELECT * FROM country_currency WHERE country_id = '$country_id' AND currency_id = '$currency_id'";
$result_law = $conn->query($query);
if($result_law->num_rows == 1) {
$query = "UPDATE country_currency SET amount = (SELECT * FROM (SELECT amount FROM country_currency
WHERE country_id = '$country_id' AND currency_id = '$currency_id') AS temp) + '$printing_amount'
WHERE country_id = '$country_id' AND currency_id = '$currency_id'";
}
else {
$query = "INSERT INTO country_currency VALUES('$country_id', '$currency_id', '$printing_amount')";
}
$conn->query($query);
}
else {
$notification = 'Print money law has been failed. Not enough paper in the ministry warehouse.';
notifyLawFailed($notification, $responsibility_id, $country_id);
}
}
}
//Impeach president.
else if($responsibility_id == 4) {
setLawToProcessed($law_id);
if(($yes + $no) != 0) {
//determine if more than 65% decided to impeach president.
$percentage_yes = (100 / ($yes + $no)) * $yes;
if($percentage_yes >= 65) {
//notify president
$query = "SELECT user_id FROM country_government WHERE country_id = '$country_id' AND position_id = 1";
$result_law = $conn->query($query);
$notification = "Government decided to impeach you. You\'re not a president anymore.";
while($row_law = $result_law->fetch_row()) {
list($president_id) = $row_law;
sendNotification($notification, $president_id);
}
//impeach
$query = "UPDATE country_government SET user_id = NULL WHERE country_id = '$country_id' AND position_id = 1";
$conn->query($query);
}
}
}
//Dissolve Congress.
else if($responsibility_id == 5) {
setLawToProcessed($law_id);
if(($yes + $no) != 0) {
//determine if more than 65% decided to dissolve congress.
$percentage_yes = (100 / ($yes + $no)) * $yes;
if($percentage_yes >= 65) {
//notify congress
$query = "SELECT user_id FROM congress_members WHERE country_id = '$country_id'";
$result_law = $conn->query($query);
$notification = "Congress has been dissolved. You\'re not a congressman anymore.";
while($row_law = $result_law->fetch_row()) {
list($congressman_id) = $row_law;
sendNotification($notification, $congressman_id);
}
//disslove
$query = "DELETE FROM congress_members WHERE country_id = '$country_id'";
$conn->query($query);
}
}
}
//Import permission/embargo.
else if($responsibility_id == 6) {
setLawToProcessed($law_id);
if($yes > $no) {
$query = "SELECT from_country_id, sale_tax, product_id, days, action FROM product_import_law_info
WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($from_country_id, $sale_tax, $product_id, $days, $action) = $row_law;
if($action == 0) {//embargo
$query = "UPDATE product_import_tax SET permission = FALSE WHERE country_id = '$country_id'
AND from_country_id = '$from_country_id' AND product_id = '$product_id'";
$conn->query($query);
}
else if($action == 1) {//permission
$query = "SELECT * FROM product_import_tax WHERE country_id = '$country_id'
AND from_country_id = '$from_country_id' AND product_id = '$product_id'";
$result = $conn->query($query);
if($result->num_rows == 1) {
$query = "UPDATE product_import_tax SET permission = TRUE WHERE country_id = '$country_id'
AND from_country_id = '$from_country_id' AND product_id = '$product_id'";
$conn->query($query);
$query = "UPDATE product_import_tax SET sale_tax = '$sale_tax' WHERE country_id = '$country_id'
AND from_country_id = '$from_country_id' AND product_id = '$product_id'";
$conn->query($query);
$query = "UPDATE product_import_tax SET days = '$days' WHERE country_id = '$country_id'
AND from_country_id = '$from_country_id' AND product_id = '$product_id'";
$conn->query($query);
}
else {
$query = "INSERT INTO product_import_tax VALUES('$country_id', '$from_country_id',
'$sale_tax', '$product_id', TRUE, '$days')";
$conn->query($query);
}
}
}
}
//Accept/decline new member application to union.
else if($responsibility_id == 7) {
setLawToProcessed($law_id);
//check if all founders finished voting
$query = "SELECT COUNT(*) FROM country_law_info WHERE law_id IN
(SELECT founder_law_id FROM processing_union_application WHERE applicant_law_id =
(SELECT applicant_law_id FROM processing_union_application WHERE founder_law_id = '$law_id'))
AND is_processed = 0";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($founders_not_voted) = $row_law;
$voted_yes = 0;
$voted_no = 0;
if($founders_not_voted == 0) {//if yes, determine if new member is accepted or not.
//get country president
$query = "SELECT c.country_id, union_name, u.union_id,
(SELECT as_founder FROM join_leave_union WHERE law_id =
(SELECT applicant_law_id FROM processing_union_application WHERE founder_law_id = '$law_id'))
FROM unions u, country_unions cu, country c
WHERE u.union_id = cu.union_id AND
c.country_id = (SELECT country_id FROM country_law_info WHERE law_id =
(SELECT applicant_law_id FROM processing_union_application WHERE founder_law_id = '$law_id'))
AND cu.country_id = (SELECT country_id FROM country_law_info WHERE law_id = '$law_id')";
$result_president = $conn->query($query);
$row_president = $result_president->fetch_row();
list($applicant_id, $union_name, $union_id, $is_founder) = $row_president;
//get information about votes
$query = "SELECT cli.country_id, country_name, yes, no FROM country_law_info cli, country c WHERE law_id IN
(SELECT founder_law_id FROM processing_union_application WHERE applicant_law_id =
(SELECT applicant_law_id FROM processing_union_application WHERE founder_law_id = '$law_id'))
AND is_processed = 1 AND cli.country_id = c.country_id";
$result_votes = $conn->query($query);
while($row_votes = $result_votes->fetch_row()) {
list($country_id, $country_name, $yes, $no) = $row_votes;
if($yes > $no) {
$voted_yes++;
$notification = "$country_name voted for your country\'s application to join $union_name union.";
}
else {
$voted_no++;
$notification = "$country_name voted against your country\'s application to join $union_name union.";
}
//notify applicants
notifyCitizens($notification, $applicant_id);
}
//determine if more than 65% decided to accept
$percentage_yes = (100 / ($voted_yes + $voted_no)) * $voted_yes;
if($percentage_yes >= 65) {
$query = "INSERT INTO country_unions VALUES('$applicant_id', '$union_id', '$is_founder')";
$conn->query($query);
$notification = "Congratulations! Your country is now a member of $union_name union.";
}
else {
$notification = "Only $percentage_yes out of 65% and more decided to accept your country to join union.";
}
//notify applicants
notifyCitizens($notification, $applicant_id);
}
}
//Change responsibilities.
else if($responsibility_id == 8) {
setLawToProcessed($law_id);
if($yes > $no) {
$query = "SELECT position_id, responsibility_id, must_sign_vote, add_remove, can_issue
FROM change_responsibilities WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($position_id, $responsibility_id, $must_sign_vote, $add_remove, $can_issue) = $row_law;
if($add_remove == 0) {//remove
if($must_sign_vote == TRUE && $can_issue == TRUE) {
$query = "DELETE FROM government_country_responsibilities
WHERE country_id = '$country_id' AND position_id = '$position_id'
AND responsibility_id = '$responsibility_id'";
$conn->query($query);
}
else if ($must_sign_vote == TRUE || $can_issue == TRUE) {
if($must_sign_vote == TRUE) {
$query = "UPDATE government_country_responsibilities SET must_sign_vote = FALSE
WHERE country_id = '$country_id' AND position_id = '$position_id'
AND responsibility_id = '$responsibility_id'";
$conn->query($query);
}
if($can_issue == TRUE) {
$query = "UPDATE government_country_responsibilities SET have_access = FALSE
WHERE country_id = '$country_id' AND position_id = '$position_id'
AND responsibility_id = '$responsibility_id'";
$conn->query($query);
}
}
}
else if($add_remove == 1) {//add
//check if had responsibility
$query = "SELECT * FROM government_country_responsibilities
WHERE country_id = '$country_id' AND position_id = '$position_id'
AND responsibility_id = '$responsibility_id'";
$result_res = $conn->query($query);
if($result_res->num_rows >= 1) {
if($must_sign_vote == TRUE) {
$query = "UPDATE government_country_responsibilities SET must_sign_vote = TRUE
WHERE country_id = '$country_id' AND position_id = '$position_id'
AND responsibility_id = '$responsibility_id'";
$conn->query($query);
}
if($can_issue == TRUE) {
$query = "UPDATE government_country_responsibilities SET have_access = TRUE
WHERE country_id = '$country_id' AND position_id = '$position_id'
AND responsibility_id = '$responsibility_id'";
$conn->query($query);
}
}
else {
$query = "INSERT INTO government_country_responsibilities VALUES('$country_id', '$position_id',
'$responsibility_id', '$must_sign_vote', '$can_issue')";
$conn->query($query);
}
}
}
}
//Declare war.
else if($responsibility_id == 9) {
setLawToProcessed($law_id);
if($yes > $no) {
//select info about war
$query = "SELECT with_country_id FROM declare_war
WHERE law_id = '$law_id'";
$result_war = $conn->query($query);
$row_war = $result_war->fetch_row();
list($with_country_id) = $row_war;
//check if allies
$query = "SELECT * FROM defence_agreements WHERE country_id = '$country_id' AND with_country_id = '$with_country_id'";
$result_law = $conn->query($query);
if($result_law->num_rows == 1) {
$query = "UPDATE defence_agreements SET is_allies = FALSE WHERE country_id = '$country_id'
AND with_country_id = '$with_country_id'";
$conn->query($query);
}
//Declare war
$war_id = $law_id;
$query = "INSERT INTO country_wars VALUES('$war_id', '$country_id', '$with_country_id', CURRENT_DATE, CURRENT_TIME, 1, 0)";
$conn->query($query);
//notify country president to who war was declared
$query = "SELECT user_id, country_name FROM country_government cg, country c
WHERE cg.country_id = '$with_country_id' AND position_id = 1 AND c.country_id = '$country_id'";
$result_president = $conn->query($query);
$row_president = $result_president->fetch_row();
list($president, $country_name) = $row_president;
sendNotification("$country_name declared war to your country!", $president);
}
}
//Sign peace treaty.
else if($responsibility_id == 10) {
setLawToProcessed($law_id);
if($yes > $no) {
//select info about war
$query = "SELECT with_country_id, status FROM sign_peace_treaty
WHERE law_id = '$law_id'";
$result_war = $conn->query($query);
$row_war = $result_war->fetch_row();
list($with_country_id, $status) = $row_war;
if($status == 0) {//voted, but not approved by the other side
$new_law_id = getTimeForId() . $proposed_by;
//propose/issue law
$query = "INSERT INTO country_law_info VALUES('$new_law_id', '$with_country_id', '$responsibility_id', 0, 0,
'$proposed_by', CURRENT_DATE, CURRENT_TIME, 0)";
$conn->query($query);
$query = "INSERT INTO sign_peace_treaty VALUES('$new_law_id', '$country_id', 1)";
$conn->query($query);
notifyToVote($with_country_id, $responsibility_id);
}
else if($status == 1) {//approved by both sides. end war.
$query = "UPDATE country_wars SET active = FALSE WHERE country_id = '$country_id' AND with_country_id = '$with_country_id'
AND active = TRUE";
$conn->query($query);
$query = "UPDATE country_wars SET active = FALSE WHERE country_id = '$with_country_id' AND with_country_id = '$country_id'
AND active = TRUE";
$conn->query($query);
//notify country presidents
$query = "SELECT user_id, country_name FROM country_government cg, country c
WHERE cg.country_id = '$with_country_id' AND position_id = 1 AND c.country_id = '$country_id'";
$result_president = $conn->query($query);
$row_president = $result_president->fetch_row();
list($president, $country_name) = $row_president;
sendNotification("$country_name agreed to sign peace treaty with your country!", $president);
$query = "SELECT user_id, country_name FROM country_government cg, country c
WHERE cg.country_id = '$country_id' AND position_id = 1 AND c.country_id = '$with_country_id'";
$result_president = $conn->query($query);
$row_president = $result_president->fetch_row();
list($president, $country_name) = $row_president;
sendNotification("$country_name agreed to sign peace treaty with your country!", $president);
}
}
}
//Assign ministers. //Assign Prime Minister
else if($responsibility_id == 11 || $responsibility_id == 12) {
setLawToProcessed($law_id);
if($yes > $no) {
//select info about assign_ministers_law
$query = "SELECT user_id, aml.position_id, assign, name FROM assign_ministers_law aml,
government_positions gp WHERE law_id = '$law_id' AND gp.position_id = aml.position_id";
$result_war = $conn->query($query);
$row_war = $result_war->fetch_row();
list($new_minister_id, $position_id, $assign, $position_name) = $row_war;
if($assign == 0) {//fire w/o replacing
$query = "UPDATE country_government SET user_id = NULL WHERE
position_id = '$position_id' AND country_id = '$country_id'";
$conn->query($query);
}
else if($assign == 1) {//assign new minister
//if minister of different ministry or congress member then remove
$query = "DELETE FROM congress_members WHERE user_id = '$new_minister_id'";
$conn->query($query);
$query = "UPDATE country_government SET user_id = NULL WHERE user_id = '$new_minister_id'";
$conn->query($query);
$query = "UPDATE country_government SET user_id = '$new_minister_id'
WHERE country_id = '$country_id' AND position_id = '$position_id'";
$conn->query($query);
$query = "UPDATE country_government SET elected = CURRENT_DATE
WHERE country_id = '$country_id' AND position_id = '$position_id'";
$conn->query($query);
sendNotification("You have been assigned as a new $position_name!", $new_minister_id);
}
}
}
//Change taxes.
else if($responsibility_id == 13) {
setLawToProcessed($law_id);
if($yes > $no) {
$query = "SELECT tax, product_id, type FROM new_tax_law
WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($tax, $product_id, $type) = $row_law;
if($type == 0) {//income
$query = "UPDATE income_tax SET tax = '$tax' WHERE country_id = '$country_id'";
$conn->query($query);
}
else if($type == 1) {//product
$query = "UPDATE product_sale_tax SET sale_tax = '$tax' WHERE country_id = '$country_id'
AND product_id = '$product_id'";
$conn->query($query);
}
}
}
//Travel agreement.
else if($responsibility_id == 14) {
setLawToProcessed($law_id);
if($yes > $no) {
$query = "SELECT from_country_id, days, action FROM travel_agreement_law
WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($from_country_id, $days, $action) = $row_law;
if($action == 0) {//ban
$query = "UPDATE travel_agreement SET permission = FALSE WHERE country_id = '$country_id'
AND from_country_id = '$from_country_id'";
$conn->query($query);
}
else if($action == 1) {//allow
$query = "SELECT * FROM travel_agreement WHERE country_id = '$country_id'
AND from_country_id = '$from_country_id'";
$result = $conn->query($query);
if($result->num_rows == 1) {
$query = "UPDATE travel_agreement SET permission = TRUE WHERE country_id = '$country_id'
AND from_country_id = '$from_country_id'";
$conn->query($query);
$query = "UPDATE travel_agreement SET days = '$days' WHERE country_id = '$country_id'
AND from_country_id = '$from_country_id'";
$conn->query($query);
}
else {
$query = "INSERT INTO travel_agreement VALUES('$country_id', '$from_country_id', TRUE, '$days')";
$conn->query($query);
}
}
}
}
//Give/Ban permission for foreigners to build companies.
else if($responsibility_id == 15) {
setLawToProcessed($law_id);
if($yes > $no) {
$query = "SELECT foreign_country_id, price, building_id, action FROM foreign_building_policy_law
WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($foreign_country_id, $price, $building_id, $action) = $row_law;
if($action == 0) {//ban
$query = "UPDATE foreign_building_policy SET foreigners = FALSE WHERE country_id = '$country_id'
AND foreign_country = '$foreign_country_id' AND building_id = '$building_id'";
$conn->query($query);
}
else if($action == 1) {//allow
$query = "SELECT * FROM foreign_building_policy WHERE country_id = '$country_id'
AND foreign_country = '$foreign_country_id' AND building_id = '$building_id'";
$result = $conn->query($query);
if($result->num_rows != 0) {
$query = "UPDATE foreign_building_policy SET foreigners = TRUE WHERE country_id = '$country_id'
AND foreign_country = '$foreign_country_id' AND building_id = '$building_id'";
$conn->query($query);
$query = "UPDATE foreign_building_policy SET price = '$price' WHERE country_id = '$country_id'
AND foreign_country = '$foreign_country_id' AND building_id = '$building_id'";
$conn->query($query);
}
else {
$query = "INSERT INTO foreign_building_policy VALUES('$country_id', '$building_id', '$price', TRUE, '$foreign_country_id')";
$conn->query($query);
}
}
}
}
//Assign new union leader
else if($responsibility_id == 16) {
setLawToProcessed($law_id);
//check if all founders finished voting
$query = "SELECT COUNT(*) FROM country_law_info WHERE law_id IN
(SELECT founders_law_id FROM processing_union_leader WHERE primary_law_id =
(SELECT primary_law_id FROM processing_union_leader WHERE founders_law_id = '$law_id'))
AND is_processed = 0";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($founders_not_voted) = $row_law;
$voted_yes = 0;
$voted_no = 0;
if($founders_not_voted == 0) {//if yes, determine if new leader is assigned.
//get info about new leader
$query = "SELECT nul.user_id, user_name, nul.union_id, union_name FROM new_union_leader nul, unions u, users up
WHERE law_id = (SELECT primary_law_id FROM processing_union_leader WHERE founders_law_id = '$law_id')
AND u.union_id = nul.union_id AND up.user_id = nul.user_id";
$result_leader = $conn->query($query);
$row_leader = $result_leader->fetch_row();
list($new_leader_id, $new_leader_name, $union_id, $union_name) = $row_leader;
//inform countries founders government
$president_country = array();
$x = 0;
$query = "SELECT user_id, cu.country_id FROM country_government cg, country_unions cu, government_country_responsibilities gcr
WHERE cg.country_id = cu.country_id
AND union_id = '$union_id' AND is_founder = TRUE
AND responsibility_id = '16' AND must_sign_vote = true AND gcr.country_id = cu.country_id
AND gcr.position_id = cg.position_id AND user_id IS NOT NULL
UNION
SELECT user_id, country_id FROM congress_members WHERE country_id IN
(SELECT country_id FROM government_country_responsibilities WHERE country_id IN
(SELECT country_id FROM country_unions
WHERE union_id = '$union_id' AND is_founder = TRUE)
AND responsibility_id = '16' AND must_sign_vote = true AND position_id = 3)";
$result_president = $conn->query($query);
while($row_president = $result_president->fetch_row()) {
list($president, $country_founder) = $row_president;
$president_country[$x][0] = $president;
$president_country[$x][1] = $country_founder;
$x++;
}
//get information about votes
$query = "SELECT cli.country_id, country_name, yes, no FROM country_law_info cli, country c WHERE law_id IN
(SELECT founders_law_id FROM processing_union_leader WHERE primary_law_id =
(SELECT primary_law_id FROM processing_union_leader WHERE founders_law_id = '$law_id'))
AND is_processed = TRUE AND cli.country_id = c.country_id";
$result_votes = $conn->query($query);
while($row_votes = $result_votes->fetch_row()) {
list($country_id, $country_name, $yes, $no) = $row_votes;
if($yes > $no) {
$voted_yes++;
$notification = "$country_name voted for new union leader $new_leader_name.";
}
else {
$voted_no++;
$notification = "$country_name voted against new union leader $new_leader_name.";
}
for($x = 0; $x < count($president_country); $x++) {
if($president_country[$x][1] != $country_id) {
sendNotification($notification, $president_country[$x][0]);
}
}
}
//determine if more than 75% decided to assign new leader.
$percentage_yes = (100 / ($voted_yes + $voted_no)) * $voted_yes;
if($percentage_yes >= 75) {
$query = "UPDATE unions SET leader = '$new_leader_id' WHERE union_id = '$union_id'";
$conn->query($query);
sendNotification("Congratulations! You have been elected for union leader of $union_name union.", $new_leader_id);
}
else {
sendNotification("Only $percentage_yes out of 75% and more decided to assign you new union leader.", $new_leader_id);
}
}
}
//Change government salaries.
else if($responsibility_id == 17) {
setLawToProcessed($law_id);
if($yes > $no) {
$query = "SELECT position_id, salary FROM change_gov_salary WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($position_id, $salary) = $row_law;
if($position_id == 3) {//congress
$query = "UPDATE congress_details SET salary = '$salary' WHERE country_id = '$country_id'";
}
else {
$query = "UPDATE country_government SET salary = '$salary' WHERE country_id = '$country_id'
AND position_id = '$position_id'";
}
$conn->query($query);
}
}
//Change credit/deposit rate
else if($responsibility_id == 18) {
setLawToProcessed($law_id);
if($yes > $no) {
$query = "SELECT rate, type, credit_deposit_type FROM change_credit_deposit_rate WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($rate, $type, $credit_deposit_type) = $row_law;
if($type == 'gold' && $credit_deposit_type == 'credit') {
$query = "UPDATE bank_details SET gold_credit_rate = '$rate' WHERE country_id = '$country_id'";
}
else if($type == 'currency' && $credit_deposit_type == 'credit') {
$query = "UPDATE bank_details SET currency_credit_rate = '$rate' WHERE country_id = '$country_id'";
}
else if($type == 'gold' && $credit_deposit_type == 'deposit') {
$query = "UPDATE bank_details SET gold_deposit_rate = '$rate' WHERE country_id = '$country_id'";
}
else if($type == 'currency' && $credit_deposit_type == 'deposit') {
$query = "UPDATE bank_details SET currency_deposit_rate = '$rate' WHERE country_id = '$country_id'";
}
$conn->query($query);
}
}
//Create new union
else if($responsibility_id == 19) {
setLawToProcessed($law_id);
if($yes > $no) {
//select info about union
$query = "SELECT union_id, union_name, abbreviation, leader, color, country_id
FROM create_new_union cnu, country_law_info cli
WHERE cnu.law_id = '$law_id' AND cli.law_id = cnu.law_id";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($union_id, $union_name, $abbreviation, $leader, $color, $country_id) = $row_law;
//create union
$query = "INSERT INTO unions VALUES('$union_id', '$union_name', '$abbreviation', '$leader', NULL, NULL, '$color')";
$conn->query($query);
$query = "INSERT INTO country_unions VALUES('$country_id', '$union_id', 1)";
$conn->query($query);
//notify country president
$query = "SELECT user_id FROM country_government WHERE country_id = '$country_id' AND position_id = 1";
$result_president = $conn->query($query);
$row_president = $result_president->fetch_row();
list($president) = $row_president;
sendNotification("Congratulations! Your country is now a founder of a $union_name union.", $president);
}
}
//Assign new Secretary of the Treasury
else if($responsibility_id == 20) {
setLawToProcessed($law_id);
if($yes > $no) {
$query = "SELECT user_id, salary FROM assign_bank_manager WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($new_manager, $salary) = $row_law;
if($new_manager != 0) {
//check if still citizen of the country
$query = "SELECT * FROM user_profile WHERE user_id = '$new_manager' AND citizenship = '$country_id'";
$result = $conn->query($query);
if($result->num_rows != 1) {
return;
}
//get old manager
$query = "SELECT user_id FROM bank_details WHERE country_id = '$country_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($old_manager) = $row_law;
//assign new bank manager
$query = "UPDATE bank_details SET user_id = '$new_manager' WHERE country_id = '$country_id'";
$conn->query($query);
$query = "UPDATE bank_details SET manager_from_date = CURRENT_DATE WHERE country_id = '$country_id'";
$conn->query($query);
$query = "UPDATE bank_details SET manager_salary = '$salary' WHERE country_id = '$country_id'";
$conn->query($query);
$notification = "You have been fired from Secretary of the Treasury position.";
sendNotification($notification, $old_manager);
$notification = "You have been assigned to the Secretary of the Treasury position.";
sendNotification($notification, $new_manager);
}
else {
//get old manager
$query = "SELECT user_id FROM bank_details WHERE country_id = '$country_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($old_manager) = $row_law;
//fire old bank manager
$query = "UPDATE bank_details SET user_id = NULL WHERE country_id = '$country_id'";
$conn->query($query);
$notification = "You have been fired from Secretary of the Treasury position.";
sendNotification($notification, $old_manager);
}
}
}
//Change production taxes
else if($responsibility_id == 21) {
setLawToProcessed($law_id);
if($yes > $no) {
$query = "SELECT tax, product_id FROM new_production_tax_law WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($tax, $product_id) = $row_law;
$query = "UPDATE product_production_tax SET tax = '$tax' WHERE country_id = '$country_id'
AND product_id = '$product_id'";
$conn->query($query);
}
}
//Budget Allocation.
else if($responsibility_id == 22) {
setLawToProcessed($law_id);
$query = "SELECT position_id, currency_id, amount FROM budget_allocation WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($position_id, $currency_id, $amount) = $row_law;
if($yes > $no) {
//determine if ministry already have this currency type and insert/update
$query = "SELECT * FROM ministry_budget WHERE country_id = '$country_id' AND currency_id = '$currency_id'
AND position_id = '$position_id'";
$result_law = $conn->query($query);
if($result_law->num_rows == 1) {
//update
$query = "UPDATE ministry_budget SET amount = (SELECT * FROM (SELECT amount FROM ministry_budget
WHERE country_id = '$country_id' AND currency_id = '$currency_id' AND position_id = '$position_id') AS temp)
+ '$amount' WHERE country_id = '$country_id' AND currency_id = '$currency_id' AND position_id = '$position_id'";
}
else {
//insert
$query = "INSERT INTO ministry_budget VALUES('$country_id', '$position_id', '$currency_id', '$amount')";
}
$conn->query($query);
}
else {
//update country budget
$query = "UPDATE country_currency SET amount = (SELECT * FROM (SELECT amount FROM country_currency
WHERE country_id = '$country_id' AND currency_id = '$currency_id') AS temp)
+ '$amount' WHERE country_id = '$country_id' AND currency_id = '$currency_id'";
$conn->query($query);
}
}
//Sign defence agreement.
else if($responsibility_id == 24) {
setLawToProcessed($law_id);
if($yes > $no) {
//select info about war
$query = "SELECT with_country_id, days, status FROM sign_defence_agreement
WHERE law_id = '$law_id'";
$result_war = $conn->query($query);
$row_war = $result_war->fetch_row();
list($with_country_id, $days, $status) = $row_war;
if($status == 0) {//voted, but not approved by the other side
$new_law_id = getTimeForId() . $proposed_by;
//propose/issue law
$query = "INSERT INTO country_law_info VALUES('$new_law_id', '$with_country_id', '$responsibility_id', 0, 0,
'$proposed_by', CURRENT_DATE, CURRENT_TIME, 0)";
$conn->query($query);
$query = "INSERT INTO sign_defence_agreement VALUES('$new_law_id', '$country_id', '$days', 1)";
$conn->query($query);
notifyToVote($with_country_id, $responsibility_id);
}
else if($status == 1) {//approved by both sides. sign defence agreement.
$query = "SELECT * FROM defence_agreements WHERE country_id = '$country_id'
AND with_country_id = '$with_country_id'";
$result = $conn->query($query);
if($result->num_rows == 1) {
//Sign agreement
$query = "UPDATE defence_agreements SET is_allies = TRUE WHERE country_id = '$country_id'
AND with_country_id = '$with_country_id'";
$conn->query($query);
$query = "UPDATE defence_agreements SET days = '$days' WHERE country_id = '$country_id'
AND with_country_id = '$with_country_id'";
$conn->query($query);
//Sign agreement
$query = "UPDATE defence_agreements SET is_allies = TRUE WHERE country_id = '$with_country_id'
AND with_country_id = '$country_id'";
$conn->query($query);
$query = "UPDATE defence_agreements SET days = '$days' WHERE country_id = '$with_country_id'
AND with_country_id = '$country_id'";
$conn->query($query);
}
else {
$query = "INSERT INTO defence_agreements VALUES('$country_id', '$with_country_id', '$days', TRUE)";
$conn->query($query);
$query = "INSERT INTO defence_agreements VALUES('$with_country_id', '$country_id', '$days', TRUE)";
$conn->query($query);
}
//notify country presidents
$query = "SELECT user_id, country_name FROM country_government cg, country c
WHERE cg.country_id = '$with_country_id' AND position_id = 1 AND c.country_id = '$country_id'";
$result_president = $conn->query($query);
$row_president = $result_president->fetch_row();
list($president, $country_name) = $row_president;
sendNotification("$country_name agreed to sign defense agreement with your country!", $president);
$query = "SELECT user_id, country_name FROM country_government cg, country c
WHERE cg.country_id = '$country_id' AND position_id = 1 AND c.country_id = '$with_country_id'";
$result_president = $conn->query($query);
$row_president = $result_president->fetch_row();
list($president, $country_name) = $row_president;
sendNotification("$country_name agreed to sign defense agreement with your country!", $president);
}
}
}
//Product Allocation.
else if($responsibility_id == 25) {
setLawToProcessed($law_id);
$query = "SELECT position_id, product_id, amount FROM product_allocation WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($position_id, $product_id, $amount) = $row_law;
if($yes > $no) {
//determine if ministry already have this currency type and insert/update
$query = "SELECT * FROM ministry_product WHERE country_id = '$country_id' AND product_id = '$product_id'
AND position_id = '$position_id'";
$result_law = $conn->query($query);
if($result_law->num_rows == 1) {
//update
$query = "UPDATE ministry_product SET amount = (SELECT * FROM (SELECT amount FROM ministry_product
WHERE country_id = '$country_id' AND product_id = '$product_id' AND position_id = '$position_id') AS temp)
+ '$amount' WHERE country_id = '$country_id' AND product_id = '$product_id' AND position_id = '$position_id'";
}
else {
//insert
$query = "INSERT INTO ministry_product VALUES('$country_id', '$position_id', '$product_id', '$amount')";
}
$conn->query($query);
}
else {
//update country budget
$query = "UPDATE country_product SET amount = (SELECT * FROM (SELECT amount FROM country_product
WHERE country_id = '$country_id' AND product_id = '$product_id') AS temp)
+ '$amount' WHERE country_id = '$country_id' AND product_id = '$product_id'";
$conn->query($query);
}
}
//Change timezone.
else if($responsibility_id == 26) {
setLawToProcessed($law_id);
if($yes > $no) {
$query = "SELECT timezone_id FROM change_timezone WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($timezone_id) = $row_law;
$query = "UPDATE country SET timezone_id = '$timezone_id' WHERE country_id = '$country_id'";
$conn->query($query);
}
}
//Price change to build companies for citizens.
else if($responsibility_id == 27) {
setLawToProcessed($law_id);
if($yes > $no) {
$query = "SELECT price, building_id FROM building_policy_law WHERE law_id = '$law_id'";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($price, $building_id) = $row_law;
$query = "UPDATE building_policy SET price = '$price' WHERE country_id = '$country_id'
AND building_id = '$building_id'";
$conn->query($query);
}
}
//Expel country from the union
else if($responsibility_id == 29) {
setLawToProcessed($law_id);
$query = "SELECT action, rcu.country_id, union_id, country_name
FROM remove_country_union rcu, country c WHERE law_id = '$law_id' AND c.country_id = rcu.country_id";
$result_law = $conn->query($query);
$row_law = $result_law->fetch_row();
list($action, $remove_country_id, $union_id, $remove_country_name) = $row_law;
if($action == 1) {
if($yes > $no) {
//update remove_country_union to "processing (action = 2)"
$query = "UPDATE remove_country_union SET action = 2 WHERE law_id = '$law_id'";
$conn->query($query);
//issue law for all union founders, to accept or decline country
$query = "SELECT country_id FROM country_unions WHERE union_id = '$union_id' AND is_founder = TRUE
AND country_id != '$country_id'";
$result_founders = $conn->query($query);