-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.php
5092 lines (4158 loc) · 131 KB
/
lib.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
global $DB;
//memcache map
//$user_id:t -- maps to users team
//$user_id:a -- maps to users action
//$user_id:z -- maps to whether to show ad or not
//$user_id:m -- maps to miles
//$user_id:j -- maps to whether the user was attacked
//$user_id:l -- maps to users level
//$user_id:d -- maps to users damage
//$user_id:b -- maps to whether user was bombed
//$user_id:c -- maps to captcha correct result
//$user_id:f -- maps to captcha fail count
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
error_reporting(6135);
//session_start();
//id, user_id, comment, created_at, xid
global $memcache;
global $memcache_temp;
//if using script config, then don't include config.php
if($script_config != true) {
require_once("config.php");
}
require_once("adodb/adodb-exceptions.inc.php");
require_once("adodb/adodb.inc.php");
//include_once '../client/facebook.php';
//require_once(dirname(__FILE__)."/../client/facebook.php");
//$facebook = new Facebook($api_key, $secret);
$connect = "mysql://$db_user:$db_pass@$db_ip/$db_name?persist";
$DB = NewADOConnection($connect);
$connect = "mysql://$db_user:$db_pass@$db_ip/$db_name";
$DBNoPersist = NewADOConnection($connect);
function get_postcount($key) {
global $DB;
return $DB->GetOne('select count(*) from comments where xid = ?', array($key));
}
function get_last_10_posts($key) {
global $DB;
return $DB->GetArray('select * from comments where xid = ? order by created_at desc limit 10', array($key));
}
function get_square_profile_pic($profile_id) {
global $network_id;
if($network_id == 0) {
return "<fb:profile-pic uid='$profile_id' size='square' linked='true' />";
}
else {
try {
require_once('Space.php');
$key = 'CHANGEME';
$secret = 'CHANGEME';
$s = new Space($key, $secret);
$hProfile = $s->profile($profile_id);
$imageuri = $hProfile['imageuri'];
return "<img src='$imageuri' >";
}
catch(Exception $e) {
//some problem talking to myspace to get pic
return;
}
}
}
function get_square_profile_pic_50($profile_id) {
global $network_id;
if($network_id == 0) {
return "<fb:profile-pic uid='$profile_id' size='square' linked='true' />";
}
else {
try {
require_once('Space.php');
$key = 'CHANGEME';
$secret = 'CHANGEME';
$s = new Space($key, $secret);
$hProfile = $s->profile($profile_id);
$imageuri = $hProfile['imageuri'];
return "<img src='$imageuri' width='50'>";
}
catch(Exception $e) {
//some problem talking to myspace to get pic
return;
}
}
}
function get_friends($user) {
require_once('Space.php');
$key = 'CHANGEME';
$secret = 'CHANGEME';
$s = new Space($key, $secret);
$hProfile = $s->friends($user);
return $hProfile['friends'];
}
function get_friend_ids($user) {
$friends = get_friends($user);
$ids = array();
foreach($friends as $friend) {
$ids[] = $friend['userid'];
}
return $ids;
}
function href($link, $params = NULL) {
global $network_id, $query_string;
if(true) {
echo "href = '" . $link . $params . "'";
}
else {
if($params != NULL) {
echo "href = '" . $link . $params . "'";
}
else {
echo "href = '$link";
}
}
}
function href_return($link, $params = NULL) {
global $network_id, $query_string;
if(true) {
return "href = '$link'";
}
else {
if($params != NULL) {
return "href = '" . $link . $params . "'";
}
else {
return "href = '$link'";
}
}
}
function image($image_name) {
global $network_id, $image_uid, $image_url;
//temporary hopefully
//$network_id = 1;
//$image_url = 'CHANGEME/images/a';
//echo "<img border='0' src='$image_url/$image_name' />";
//return;
///*
if($network_id == 0) {
echo "<fb:photo pid='$image_name' uid=\"$image_uid\" />";
}
else if($network_id == 1 || $network_id == 2) {
echo "<img border='0' src='$image_url/$image_name' />";
}
//*/
}
function image_return($image_name) {
global $network_id, $image_uid, $image_url;
//temporary hopefully
//$network_id = 1;
//$image_url = 'CHANGEME/images/a';
//return "<img src='$image_url/$image_name' />";
///*
if($network_id == 0) {
return "<fb:photo pid='$image_name' uid=\"$image_uid\" />";
}
else if($network_id == 1 || $network_id == 2) {
return "<img src='$image_url/$image_name' />";
}
//*/
}
function get_race_time_left() {
global $DB;
$sql = "select created_at as a, now() as b from race_results order by created_at desc";
$r = $DB->GetRow($sql);
//print_r($r);
//YYYYMMDDHHMMSS
$a = $r['a'];
$b = $r['b'];
$c = date("YmdHis", strtotime($a));
$d = date("YmdHis", strtotime($b));
//print $created_at;
//print_r($a);
//print_r($b);
$s = 1800 + strtotime($a) - strtotime($b);
//print $s;
//return relative_date($c, $d);
$m =round( $s /60 );
if($m == 1) {
return '1 minute';
}
else {
return $m . ' minutes';
}
}
function get_consecutive_merchant_trades($user) {
return 0;
}
function get_merchant_trade($user) {
global $memcache;
//try and get trade out of memcache
$mt = $memcache->get($user . 'mtrade');
if($mt != false) {
return $mt;
}
$pvp_toggle = $memcache->get($user . 'pvp');
if($pvp_toggle == 'off') {
//echo "pvp off!";
$gold_amount = rand(25,100);
}
else {
$gold_amount = 0;
}
global $DB;
$consecutive_trades = $DB->GetOne('select consecutive_merchant_trades from users where id = ?', array($user));
$amount1 = rand(2,5);
$amount2 = rand(2,5);
if($consecutive_trades > rand(3,5) && $pvp_toggle != 'off') {
$amount2 = round($amount2 * rand(1.5,2.5));
}
if($consecutive_trades > rand(10,15) && $pvp_toggle != 'off') {
$amount2 = $amount2 * rand(5,10);
$DB->Execute('update users set consecutive_merchant_trades = 0 where id = ?', array($user));
}
$stuffid1 = rand(1,13);
$stuffid2 = rand(1,13);
if($stuffid2 == $stuffid1) {
$stuffid2 = $stuffid1 + 1;
}
if($stuffid2 == 14) {
$stuffid2 = 1;
}
$stuff1 = get_booty_data_from_id($stuffid1);
$stuff2 = get_booty_data_from_id($stuffid2);
if( $stuff1['0'] == 'Dynamite' || $stuff1['0'] == 'Gold Bars' || $stuff1['0'] == 'Rum') {
$what_you_give_name = $stuff1['0'];
}
else if ($stuff1['0'] == 'Message in a Bottle'){
$what_you_give_name = 'Messages in Bottles';
}
else {
$what_you_give_name = $stuff1['0'] . 's';
}
if( $stuff2['0'] == 'Dynamite' || $stuff2['0'] == 'Gold Bars' || $stuff2['0'] == 'Rum') {
$what_you_get_name = $stuff2['0'];
}
else if ($stuff2['0'] == 'Message in a Bottle'){
$what_you_get_name = 'Messages in Bottles';
}
else {
$what_you_get_name = $stuff2['0'] . 's';
}
$mt = array(
'what_you_give_name'=> $what_you_give_name,
'what_you_get_name' => $what_you_get_name,
'what_you_give_amount' => $amount1,
'what_you_get_amount' => $amount2,
'what_you_give_pic' => $stuff1['3'],
'what_you_get_pic' => $stuff2['3'],
'what_you_give_id' => $stuffid1,
'what_you_get_id' => $stuffid2,
'gold_amount' => $gold_amount);
$memcache->set($user . 'mtrade', $mt);
return $mt;
}
function get_number_entries($animal_char) {
global $DB;
$sql = "select count(*) from races where type = '$animal_char' and completed = 0";
$r = $DB->GetOne($sql);
return $r;
}
function get_past_winner_amount($a) {
if($a == 'parrot') {
return 5000;
}
else {
return 600;
}
}
function not_enough_to_race($user, $animal_char) {
//now check if they have enough parrotrs or monkeys
if($animal_char == 'p') {
$animal_count = get_parrot_count($user);
}
else {
$animal_count = get_monkey_count($user);
}
//print_r($animal_count);
if($animal_count < 1) {
return true;
}
return false;
}
function user_can_enter_race($user, $animal_char) {
global $DB;
$sql = "select count(*) from races where user_id = $user and type = '$animal_char' and completed = 0";
$r = $DB->GetOne($sql);
if($r == 0) {
return true;
}
else {
return false;
}
}
function get_past_winner_id($a) {
if($a == 'parrot') {
return 3423444;
}
else {
return 1807681;
}
}
function get_jackpot_amount($a) {
if($a == 'p') {
$entries = get_number_entries($a);
$jackpot = $entries * 200;
if($jackpot < 5000) {
$jackpot = 5000;
}
}
else {
$entries = get_number_entries($a);
$jackpot = $entries * 50;
if($jackpot < 2500) {
$jackpot = 2500;
}
}
return $jackpot;
}
function get_races_won_by_id($user) {
global $DB;
$sql = "select type, jackpot, created_at, entrants from race_results where user_id = $user order by created_at DESC";
$results_array = $DB->GetArray($sql);
return $results_array;
}
function user_eligible_for_offer($user, $offer_id) {
global $DB;
$r = $DB->GetOne('select count(*) from offers where user_id = ? and offer_id = ?', array($user, $offer_id));
//print $r;
return !$r;
}
//moderators and banned users on the discussion board
function get_moderators() {
return array(1807687);
}
function get_no_captcha() {
return array();
}
function get_banned() {
return array();
}
function get_audited_users() {
global $DB, $memcache;
require_once 'cheater_ids.php';
//todo don't hardcode ids in teh query so when new ones are added...
global $cheaters;
if(false) {
$n = $memcache->get('audied_users');
if($n == FALSE) {
$r =$DB->GetArray('select logged_users.id, level from logged_users, users where users.id = logged_users.id and logged_users.id not in () order by users.name');
$n = array();
foreach($r as $key => $value) {
$n[]=$value['id'];
}
$memcache->set('audited_users', $n, false, 3600);
}
}
else {
//$r =$DB->GetArray('select logged_users.id, users.level from logged_users, users where logged_users.id = users.id order by users.name;');
$r =$DB->GetArray('select logged_users.id, level from logged_users, users where users.id = logged_users.id and logged_users.id not in () order by users.name');
}
return $r;
}
function get_audited_users_ids() {
global $DB, $memcache;
require_once 'cheater_ids.php';
//todo don't hardcode ids in teh query so when new ones are added...
global $cheaters;
if($memcache) {
$n = $memcache->get('audited_users_ids');
if($n == FALSE) {
$r =$DB->GetArray('select id from logged_users');
$n = array();
foreach($r as $key => $value) {
$n[]=$value['id'];
}
$memcache->set('audited_users_ids', $n, false, 3600);
}
}
else {
$r =$DB->GetArray('select id from logged_users');
$n = array();
foreach($r as $key => $value) {
$n[]=$value['id'];
}
}
return $n;
}
function dashboard($show_make_better = FALSE) {
global $network_id;
if($network_id == 1) {
include 'style.php';
}
$output = "<table><tr><td width=\"620\"><div class=\"dashboard_header\"><div class=\"dh_links clearfix\">";
$output .= "<a " . href_return('index.php') . "\">Go Sailin'</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a " . href_return('recruit.php') . "\">Recruit</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a " . href_return('user_profile.php') . ">Stats</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a " . href_return('user_profile.php?action=inventory') . "\">Booty</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a " . href_return('leaderboard.php') . "\">Leaderboard</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a " . href_return('user_profile.php?action=fellow_pirates') . "\">My Mates</a>";
if($network_id == 0) {
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a href=\"surveys.php\">Free Booty</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a " . href_return('settings.php') . "\">Settings</a>";
}
if($network_id == 0) {
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a href=\"http://www.facebook.com/apps/application.php?api_key=CHANGEME\">About</a>";
}
if($network_id == 1) {
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a target='_blank' href=\"http://www.myspace.com/piratewars\">About</a>";
}
$output .= "</div></div></td>";
$output .= "</tr></table><br>";
return $output;
}
function success_msg($msg) {
global $network_id;
if($msg == '') {
return;
}
if($network_id == 0)
{
?>
<center>
<fb:success>
<fb:message><?php echo $msg; ?></fb:message>
</fb:success>
</center>
<?php
}
else {
?>
<center>
<div style='width:650px; text-align:center'>
<div class="standard_message has_padding"><h1 class="status"><?php echo $msg; ?></h1>
</div>
</div>
</center>
<?php
}
}
function success_msg_return($msg) {
global $network_id;
if($network_id == 0)
{
$str = "<center><fb:success><fb:message>$msg</fb:message></fb:sucess></center>";
}
else {
$str = "<center><div style='width:650px; text-align:center'><div class='standard_message has_padding'><h1 class='status'>$msg</h1></div></div></center>";
}
return $str;
}
function explanation_msg($msg) {
global $network_id;
if($network_id == 0)
{
?>
<fb:explanation>
<fb:message><?php echo $msg; ?></fb:message>
</fb:explanation>
<div style='width:650px'>
<div class="standard_message has_padding">
<h1 class="explanation_note">
<?php echo $msg; ?></h1>
</div>
</div>
<?php
}
else {
?>
<div style='width:650px'>
<div class="standard_message has_padding">
<h1 class="explanation_note">
<?php echo $msg; ?></h1>
</div>
</div>
<?php
}
}
function explanation_msg_return($msg) {
global $network_id;
if($network_id == 0)
{
$str = "<center><fb:explanation><fb:message>$msg</fb:message></fb:explanation></center>";
}
else {
$str = "<center><div style='width:650px; text-align:center'><div class='standard_message has_padding'><h1 class='explanation_note'>$msg</h1></div></div></center>";
}
return $str;
}
function error_msg($msg) {
global $network_id;
if($network_id == 0)
{
?>
<fb:error>
<fb:message><?php echo $msg; ?></fb:message>
</fb:error>
<?php
}
else {
?>
<div style='width:650px'>
<div class="standard_message has_padding">
<h1 id="error"><?php echo $msg; ?></h1></div>
</div>
</div>
<?php
}
}
function error_msg_return($msg) {
global $network_id;
if($network_id == 0)
{
$str = "<center><fb:error><fb:message>$msg</fb:message></fb:error></center>";
}
else {
$str = "<center><div style='width:650px; text-align:center'><div class='standard_message has_padding'><h1 id='error'>$msg</h1></div></div></center>";
}
return $str;
}
function dashboard_loggedout() {
$output = "<table><tr><td width=\"620\"><div class=\"dashboard_header\"><div class=\"dh_links clearfix\">";
$output .= "<a href=\"index.php\">Go Sailin'</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a href=\"playnow.php\">Recruit</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a href=\"playnow.php\">Stats</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a href=\"playnow.php?action=inventory\">Booty</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a href=\"playnow.php\">Leaderboard</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a href=\"playnow.php?action=fellow_pirates\">My Mates</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a href=\"playnow.php\">Free Booty</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a href=\"playnow.php\">Settings</a>";
$output .= "<span class=\"pipe\">|</span>";
$output .= "<a href=\"http://www.facebook.com/apps/application.php?api_key=CHANGEME\">About</a>";
$output .= "</div></div></td>";
$output .= "</tr></table><br>";
return $output;
}
function get_num_ads_complete($user) {
global $DB;
$sql = "SELECT offer_id FROM offers where user_id = $user";
$value = $DB->GetArray($sql);
return 1;
return count($value);
}
function get_total_user_count() {
global $memcache, $DB;
$r = $memcache->get("total_user_count");
if($r == FALSE) {
$sql = "SELECT max(auto_id) from users";
$r = $DB->GetOne($sql);
$memcache->set("total_user_count", $r, false, 3600 * 12);
}
//echo "total user count: $r";
return $r;
}
function get_gender($user) {
global $memcache, $DB, $facebook;
$r = $memcache->get($user . "s_gender");
if($r == FALSE) {
$sql = "SELECT gender from users where id = ?";
$r = $DB->GetOne($sql, array($user));
if($r == 'm' || $r == 'f') {
$memcache->set($user . "s_gender", $r, false, 1);
}
else {
$re = $facebook->api_client->users_getInfo($user, array('sex'));
//wtf why does this something throw exception!
if($re[0]) {
$gender = $re[0]['sex'];
}
else {
$re0 = $re[0];
error_log("error getting user info sex: re: $re re0 $re0 user: $user", 0);
$gender = 'male';
}
$r = $gender;
if($r == 'male') {
$r = 'm';
}
else if($r == 'female') {
$r = 'f';
}
else {
$r = 'm';
}
$DB->Execute('update users set gender = ? where id = ?', array($r, $user));
}
}
else {
if ($r != 'm' && $r != 'f') {
$re = $facebook->api_client->users_getInfo($user, array('sex'));
$gender = $re[0]['sex'];
$r = $gender;
if($r == 'male') {
$r = 'm';
}
else if($r == 'female') {
$r = 'f';
}
else {
$r = 'm';
}
$DB->Execute('update users set gender = ? where id = ?', array($r, $user));
}
}
return $r;
}
function get_joke_on($user) {
global $memcache, $DB;
$r = $memcache->get($user . "s_joke_on_off");
if($r == FALSE) {
$sql = "SELECT joke_on from users where id = ?";
$r = $DB->GetRow($sql, array($user));
$memcache->set($user . "s_joke_on_off", $r, false, 1);
}
//echo "total user count: $r";
return $r['joke_on'];
}
function get_use_old_image($user) {
global $memcache, $DB;
$r = $memcache->get($user . "s_use_old_image");
if($r == FALSE) {
$sql = "SELECT use_old_image from users where id = ?";
$r = $DB->GetOne($sql, array($user));
$memcache->set($user . "s_use_old_image", $r, false, 1);
}
//echo "total user count: $r";
return $r;
}
function get_bomb_on($user) {
global $memcache, $DB;
$r = $memcache->get($user . "s_bomb_on_off");
if($r == FALSE) {
$sql = "SELECT bomb_on from users where id = ?";
$r = $DB->GetRow($sql, array($user));
$memcache->set($user . "s_bomb_on_off", $r, false, 1);
}
//echo "total user count: $r";
return $r['bomb_on'];
}
function get_animate_on($user) {
global $memcache, $DB;
$r = $memcache->get($user . "s_an_on_off");
if($r == FALSE) {
$sql = "SELECT animate_on from users where id = ?";
$r = $DB->GetRow($sql, array($user));
$memcache->set($user . "s_an_on_off", $r, false, 1);
}
//echo "total user count: $r";
return $r['animate_on'];
}
function get_total_joke_count() {
global $memcache, $DB;
$r = $memcache->get("total_joke_count");
if($r == FALSE) {
$sql = "SELECT max(id) from jokes_approved";
$r = $DB->GetOne($sql);
$memcache->set("total_joke_count", $r, false, 60);
}
//echo "total user count: $r";
return $r;
}
function get_first_name_for_id($user) {
global $network_id;
if($network_id == 0) {
return "<fb:name uid='$user' firstnameonly='true' shownetwork='false' useyou='false' linked='false'/>";
}
else {
return get_name_for_id($user);
}
}
function get_name_for_id($user) {
global $DB;
return $DB->GetOne('select name from users where id = ?', array($user));
}
function new_directed_battle($user, $touser) {
global $memcache, $DB;
$e = $touser;
$memcache->set($e. 'attacked', 1, FALSE, rand(3000, 6000));
$enemy = $DB->GetRow('select id, level, damage, team, coin_total from users where id = ?', array($e));
//print_r($enemy);
//store the battle
$sql = 'insert into battles(user_id, user_id_2, battle_type, opponent_type, created_at) values (?, ?, ?, ?, now())';
$enemy_id = $enemy['id'];
$enemy_team = $enemy['team'];
$DB->Execute($sql, array($user, $enemy_id, 's', $enemy_team));
//print_r($enemy);
return $enemy;
}
//return a battle id
function new_ship_battle_smart($user) {
// echo 'new ship';
global $DB, $facebook, $memcache;
$your_level = get_level($user);
$your_team = get_team($user);
//$level_low = floor($your_level * (1/3));
//$level_high = ceil($your_level * (9/2));
//if($level_low ==0) { //have to have 1 hit point at least...
// $level_low = 1;
//}
$total_users = get_total_user_count();
// $random_enemies = array();
//generate 500 random numbers
//for($i = 0; $i < 10; $i++) {
// $random_enemies[]=rand(1, $total_users);
//}
// $random_enemy_string = implode(', ', $random_enemies);
//echo "random enemy: $random_enemy";
$msg = "Arrrr... The attack is on!";
if($your_level > 100000) {
$active_users = $memcache->get('active_hi_3');
}
else if($your_level > 1000) {
$active_users = $memcache->get('active_hi_2');
}
else if($your_level > 100) {
$active_users = $memcache->get('active_hi');
}
else {
$active_users = $memcache->get('active_lo');
}
//print_r($active_users);
$max_active_users = count($active_users);
$ra = rand(0, $max_active_users - 1);
//echo " max active users is $max_active_users ";
//echo " ra is $ra ";
$enemy = $active_users[$ra];
//$enemy = random_enemy_id_for_battle_backup($user);
$pvp_off = $memcache->get($enemy . 'pvp');
if($pvp_off == 'off' || $enemy == $user) {
$enemy = random_enemy_id_for_battle_backup($user);
}
else {
//print " enemy $enemy ";
//set the enemy as being attacked and keep this for 15 minutes
//it won't put them back in the pool
unset($active_users[$ra]);
$active_users = array_values($active_users);
if(is_array($active_user)) {
$active_users = array();
}
if($your_level > 100000) {
$active_users = $memcache->set('active_hi_3', $active_users);
}
else if($your_level > 1000) {
$active_users = $memcache->set('active_hi_2', $active_users);
}
else if($your_level > 100) {
$active_users = $memcache->set('active_hi', $active_users);
//echo 'unsetting high';
//print_r($active_users);
}
else {
$active_users = $memcache->set('active_lo', $active_users);
}
}
// $was_attacked = $memcache->get($enemy . 'attacked');
$enemy_action = get_current_action($user);
if($enemy == FALSE || $enemy == $user || $enemy_action == 'treasure_hunt') {
$enemy = random_enemy_id_for_battle_backup($user);
}
if($enemy == FALSE) {
return FALSE;
}
//echo "new enemy";
$e = $enemy;
$memcache->set($e. 'attacked', 1, FALSE, rand(3000, 6000));
$enemy = $DB->GetRow('select id, level, damage, team, coin_total from users where id = ?', array($e));
$enemy_id = $enemy['id'];
$sql = 'update users set battling_enemy_id = ? where id = ?';
$DB->Execute($sql, array($enemy_id, $user));
$sql = 'insert into battles(user_id, user_id_2, battle_type, opponent_type, created_at) values (?, ?, ?, ?, now())';
//print_r($enemy);
$body = "Avast! <a href='http://facebook.com/profile.php?id=$user'><fb:name uid='$user'/></a> attacked <a href='http://facebook.com/profile.php?id=$enemy_id'><fb:name uid = '$enemy_id'/></a>!";
global $facebook_canvas_url;
global $base_url;
$image_1 = "$base_url/images/flag_200.jpg";
$image_1_link = "$facebook_canvas_url/?i=$user";
$title = "attacked a Pirate. Arrr...";
/*
//TODO add templatitzed feeds
try {
$re2 = $facebook->api_client->feed_publishActionOfUser($title, $body);
}
catch (FacebookRestClientException $fb_e) {
}
*/
//notify user on attack
//$facebook->api_client->notifications_send(array($enemy_id) , " attacked you! Attack <fb:pronoun uid='$user' useyou='false' objective='true' /> back!");