forked from RetroAchievements/RAWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_db_activity.php
845 lines (713 loc) · 29 KB
/
_db_activity.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
<?php
require_once('db.inc.php');
//////////////////////////////////////////////////////////////////////////////////////////
// Activity/Feed
//////////////////////////////////////////////////////////////////////////////////////////
// 01:00 23/02/2013
function getMostRecentActivity( $user, $type, $data )
{
$innerClause = "Activity.user = '$user'";
if( isset( $type ) )
$innerClause .= " AND Activity.activityType = $type";
if( isset( $data ) )
$innerClause .= " AND Activity.data = $data";
$query = "SELECT * FROM Activity AS act
WHERE act.ID =
( SELECT MAX(Activity.ID) FROM Activity WHERE $innerClause ) ";
//error_log( __FUNCTION__ . ">" . $query );
$dbResult = s_mysql_query( $query );
if( $dbResult == FALSE )
{
error_log( $query );
error_log( __FUNCTION__ . " failed! $user, $type, $data" );
return FALSE;
}
return mysqli_fetch_assoc( $dbResult );
}
// 01:01 23/02/2013
function updateActivity( $activityID )
{
// Update the last update value of given activity
$query = "UPDATE Activity
SET Activity.lastupdate = NOW()
WHERE Activity.ID = $activityID ";
$dbResult = s_mysql_query( $query );
if( $dbResult == FALSE )
{
error_log( $query );
error_log( __FUNCTION__ . " failed! $activityID" );
}
}
// 08:17 22/09/2014
function RecentlyPostedCompletionActivity( $user, $gameID, $isHardcore )
{
settype( $isHardcore, 'integer' );
$query = "SELECT act.ID
FROM Activity AS act
WHERE act.user='$user' AND act.data='$gameID' AND act.data2='$isHardcore' AND act.lastupdate >= DATE_SUB( NOW(), INTERVAL 1 HOUR )
LIMIT 1";
$dbResult = s_mysql_query( $query );
SQL_ASSERT( $dbResult );
return ( mysqli_num_rows( $dbResult ) > 0 );
}
// 01:04 23/02/2013
function postActivity( $userIn, $activity, $customMsg, $isalt = NULL )
{
$user = correctUserCase( $userIn );
userActivityPing( $user );
// Remove single quotes!
$customMsg = str_replace( "'", "''", $customMsg );
$query = "INSERT INTO Activity (lastupdate, activitytype, user, data, data2) VALUES ";
switch( $activity )
{
case ActivityType::EarnedAchivement:
// earned an achievement
$achID = $customMsg;
getAchievementMetadata( $achID, $achData );
$gameName = $achData[ 'GameTitle' ];
$gameID = $achData[ 'GameID' ];
$achName = $achData[ 'AchievementTitle' ];
$gameLink = "<a href='/Game/$gameID'>$gameName</a>";
$achLink = "<a href='/Achievement/$achID'>$achName</a>";
$gameLink = str_replace( "'", "''", $gameLink );
$achLink = str_replace( "'", "''", $achLink );
$query .= "(NOW(), $activity, '$user', '$achID', $isalt )";
break;
case ActivityType::Login:
// login
$lastLoginActivity = getMostRecentActivity( $user, $activity, null );
if( isset( $lastLoginActivity ) )
{
$nowTimestamp = time();
$lastLoginTimestamp = strtotime( $lastLoginActivity[ 'timestamp' ] );
$diff = $nowTimestamp - $lastLoginTimestamp;
if( $diff < 60 * 60 * 6 ) // 6 hours
{
//error_log( __FUNCTION__ . " new login activity from $user, duplicate of recent login " . ($diff/60) . " mins ago, so ignoring!" );
return;
}
}
$query .= "(NOW(), $activity, '$user', NULL, NULL)";
break;
case ActivityType::StartedPlaying:
// start playing game
$gameID = $customMsg;
getGameTitleFromID( $gameID, $gameTitle, $consoleIDOut, $consoleName, $forumTopicID, $gameData );
// Check for recent duplicate:
$lastPlayedActivityData = getMostRecentActivity( $user, $activity, $gameID );
if( isset( $lastPlayedActivityData ) )
{
$nowTimestamp = time();
$lastPlayedTimestamp = strtotime( $lastPlayedActivityData[ 'timestamp' ] );
$diff = $nowTimestamp - $lastPlayedTimestamp;
if( $diff < 60 * 60 * 12 ) // 12 hours
{
//error_log( __FUNCTION__ . " new playing $gameTitle activity from $user, duplicate of recent activity " . ($diff/60) . " mins ago. Updating db, but not posting!" );
updateActivity( $lastPlayedActivityData[ 'ID' ] );
return;
}
else
{
//error_log( __FUNCTION__ . " recognises that $user has played $gameTitle recently, but longer than 12 hours ago (" . ($diff/60) . " mins) so still posting activity! $nowTimestamp - $lastPlayedTimestamp = $diff" );
}
}
$gameTitle = str_replace( "'", "''", $gameTitle );
$query .= "(NOW(), $activity, '$user', '$gameID', NULL)";
//error_log( $query );
break;
case ActivityType::UploadAchievement:
// upload achievement
$achID = $customMsg;
$achievementName = getAchievementTitle( $achID, $gameTitle, $gameID );
//$achievementName = str_replace( "'", "''", $achievementName );
//$gameTitle = str_replace( "'", "''", $gameTitle );
$achievementLink = "<a href='/Achievement/$achID'>$achievementName</a>";
$achievementLink = str_replace( "'", "''", $achievementLink );
$query .= "(NOW(), $activity, '$user', '$achID', NULL)";
break;
case ActivityType::EditAchievement:
// commit achievement changes
$achID = $customMsg;
$achievementName = getAchievementTitle( $achID, $gameTitle, $gameID );
//$achievementName = str_replace( "'", "''", $achievementName );d
//$gameTitle = str_replace( "'", "''", $gameTitle );
$achievementLink = "<a href='/Achievement/$achID'>$achievementName</a>";
$achievementLink = str_replace( "'", "''", $achievementLink );
$query .= "(NOW(), $activity, '$user', '$achID', NULL)";
break;
case ActivityType::CompleteGame:
// Completed a game!
$gameID = $customMsg;
getGameTitleFromID( $gameID, $gameTitle, $consoleIDOut, $consoleName, $forumTopicID, $gameData );
$gameLink = "<a href='/Game/$gameID'>$gameTitle</a>";
$gameLink = str_replace( "'", "''", $gameLink );
AddSiteAward( $user, 1, $gameID, $isalt );
$query .= "(NOW(), $activity, '$user', '$gameID', $isalt)";
break;
case ActivityType::NewLeaderboardEntry:
// New LB Entry
$lbID = $customMsg[ 'LBID' ];
$lbTitle = $customMsg[ 'LBTitle' ];
$score = $customMsg[ 'Score' ];
$gameID = $customMsg[ 'GameID' ];
$scoreFormatted = $customMsg[ 'ScoreFormatted' ];
getGameTitleFromID( $gameID, $gameTitle, $consoleIDOut, $consoleName, $forumTopicID, $gameData );
$gameLink = "<a href='/Game/$gameID'>$gameTitle</a>";
$gameLink = str_replace( "'", "''", $gameLink );
$lbLinkScore = "<a href='/leaderboardinfo.php?i=$lbID'>$scoreFormatted</a>";
$lbLinkScore = str_replace( "'", "''", $lbLinkScore );
$lbLinkTitle = "<a href='/leaderboardinfo.php?i=$lbID'>$lbTitle</a>";
$lbLinkTitle = str_replace( "'", "''", $lbLinkTitle );
$query .= "(NOW(), $activity, '$user', '$lbID', '$score')";
break;
case ActivityType::ImprovedLeaderboardEntry:
// Updated LB Entry
$lbID = $customMsg[ 'LBID' ];
$lbTitle = $customMsg[ 'LBTitle' ];
$score = $customMsg[ 'Score' ];
$gameID = $customMsg[ 'GameID' ];
$scoreFormatted = $customMsg[ 'ScoreFormatted' ];
getGameTitleFromID( $gameID, $gameTitle, $consoleIDOut, $consoleName, $forumTopicID, $gameData );
$gameLink = "<a href='/Game/$gameID'>$gameTitle</a>";
$gameLink = str_replace( "'", "''", $gameLink );
$lbLinkScore = "<a href='/leaderboardinfo.php?i=$lbID'>$scoreFormatted</a>";
$lbLinkScore = str_replace( "'", "''", $lbLinkScore );
$lbLinkTitle = "<a href='/leaderboardinfo.php?i=$lbID'>$lbTitle</a>";
$lbLinkTitle = str_replace( "'", "''", $lbLinkTitle );
$query .= "(NOW(), $activity, '$user', '$lbID', '$score')";
break;
default: //ft
case ActivityType::Unknown:
error_log( __FUNCTION__ . " received unknown activity: $activity" );
$query .= "(NOW(), $activity, '$user', '$customMsg', '$customMsg')";
break;
}
//error_log( __FUNCTION__ . " - " . $query );
//log_sql( $query );
global $db;
$dbResult = mysqli_query( $db, $query );
if( $dbResult == FALSE )
{
log_sql_fail();
}
else
{
// Update UA
global $db;
$newActID = mysqli_insert_id( $db );
$query = "UPDATE UserAccounts AS ua SET ua.LastActivityID = $newActID, ua.LastLogin = NOW() WHERE ua.User = '$user'";
$dbResult = s_mysql_query( $query );
if( $dbResult == FALSE )
{
log_sql_fail();
}
}
return ( $dbResult !== FALSE );
}
function userActivityPing( $user )
{
if( !isset( $user ) || strlen( $user ) < 2 )
{
//error_log( __FUNCTION__ . " fucked up somehow for $user" );
//log_email( __FUNCTION__ . " fucked up" );
return false;
}
$query = "UPDATE UserAccounts AS ua
SET ua.LastLogin = NOW()
WHERE ua.User = '$user' ";
$dbResult = s_mysql_query( $query );
if( $dbResult == false )
{
error_log( __FUNCTION__ . " fucked up somehow for $user" );
//log_sql_fail();
//log_email( __FUNCTION__ . " fucked up somehow for $user" );
return false;
}
return true;
}
// 23:13 12/01/2014
function UpdateUserRichPresence( $user, $gameID, $presenceMsg )
{
if( !isset( $user ) || strlen( $user ) < 2 )
{
//log_email( __FUNCTION__ . " fucked up ($user, $gameID, $presenceMsg)" );
error_log( __FUNCTION__ . " fucked up ($user, $gameID, $presenceMsg)" );
return FALSE;
}
global $db;
settype( $gameID, 'integer' );
$presenceMsg = mysqli_real_escape_string( $db, $presenceMsg );
$user = mysqli_real_escape_string( $db, $user );
$query = "UPDATE UserAccounts AS ua
SET ua.RichPresenceMsg = '$presenceMsg', ua.LastGameID = '$gameID', ua.RichPresenceMsgDate = NOW()
WHERE ua.User = '$user' ";
$dbResult = mysqli_query( $db, $query ); // Allow direct: we have sanitized all variables
if( $dbResult == FALSE )
{
log_email( __FUNCTION__ . " fucked up somehow for $user" );
return FALSE;
}
return TRUE;
}
// 13:33 27/04/2013
function informAllSubscribersAboutActivity( $activityAuthor, $threadAuthor, $articleID, $articleType )
{
$requiredSubscription = 0;
$altURLTarget = NULL;
// $articleType
// 1 = Game
// 2 = Achievement
// 3 = User
// 4 = News (unused)
// 5 = feed Activity
// 6 = LB
// 7 = Ticket
if( $articleType == 1 ) // Game
{
// None
$requiredSubscription = (1 << 1); // Matches Achievement
// Fetch all users that have also commented on this game:
$query = "SELECT DISTINCT ua.User, ua.EmailAddress, ua.websitePrefs
FROM Comment AS c
INNER JOIN UserAccounts AS ua ON ua.ID = c.UserID
WHERE c.ArticleID=$articleID AND c.ArticleType=1 ";
}
else if( $articleType == 2 ) // Achievement
{
$requiredSubscription = (1 << 1);
// Walks a thread and emails all users that are subscribed to updates
// Fetch all users that are involved in this thread:
$query = "SELECT DISTINCT ua.User, ua.EmailAddress, ua.websitePrefs
FROM Comment AS c
INNER JOIN UserAccounts AS ua ON ua.ID = c.UserID
WHERE ( c.ArticleID=$articleID AND c.ArticleType=$articleType ) OR ua.User = '$threadAuthor'";
}
else if( $articleType == 3 ) // User
{
$requiredSubscription = (1 << 2);
$altURLTarget = $threadAuthor; // Override: this means we should provide a link to the target user's wall instead!
// Walks a thread and emails all users that are subscribed to updates
// Fetch all users that are involved in this thread:
$query = "SELECT DISTINCT ua.User, ua.EmailAddress, ua.websitePrefs
FROM Comment AS c
INNER JOIN UserAccounts AS ua ON ua.ID = c.UserID
WHERE ( c.ArticleID=$articleID AND c.ArticleType=$articleType ) OR ua.User = '$threadAuthor'";
}
else if( $articleType == 4 ) // News
{
// None (me?)
error_log( __FUNCTION__ . " cannot deal with articleType $articleType (News)" );
return;
}
else if( $articleType == 5 ) // Activity (feed)
{
$requiredSubscription = (1 << 0);
// Walks a thread and emails all users that are subscribed to updates
// Fetch all users that are involved in this thread:
$query = "SELECT DISTINCT ua.User, ua.EmailAddress, ua.websitePrefs
FROM Comment AS c
INNER JOIN UserAccounts AS ua ON ua.ID = c.UserID
WHERE ( c.ArticleID=$articleID AND c.ArticleType=$articleType ) OR ua.User = '$threadAuthor'";
}
else if( $articleType == 7 ) // Ticket
{
$requiredSubscription = (1 << 1); // Matches Achievement (?)
// Walks a thread and emails all users that are subscribed to updates
// Fetch all users that are involved in this thread:
$query = "SELECT DISTINCT ua.User, ua.EmailAddress, ua.websitePrefs
FROM Comment AS c
INNER JOIN UserAccounts AS ua ON ua.ID = c.UserID
WHERE ( c.ArticleID=$articleID AND c.ArticleType=$articleType ) OR ua.User = '$threadAuthor'";
}
else
{
log_email( __FUNCTION__ . " cannot deal with articleType $articleType (Unknown)" );
return;
}
//error_log( $query );
$dbResult = s_mysql_query( $query );
if( $dbResult !== FALSE )
{
// Foreach person who has commented on this thread, if they want it, send them an email.
while( $data = mysqli_fetch_assoc( $dbResult ) )
{
if( $data !== FALSE )
{
if( ( ( $data[ 'websitePrefs' ] & (1 << 0) ) !== 0 ) || $articleType == 7 ) // Always receive ticket emails :P
{
$justInvolvedInThread = NULL;
if( ( $data[ 'User' ] != $threadAuthor ) && ( $data[ 'User' ] != $activityAuthor ) )
$justInvolvedInThread = TRUE;
sendActivityEmail( $data[ 'User' ], $data[ 'EmailAddress' ], $articleID, $activityAuthor, $articleType, $justInvolvedInThread, $altURLTarget );
}
else
{
error_log( "Not sending email to " . $data[ 'User' ] . ", prefs are " . $data[ 'websitePrefs' ] );
}
}
else
{
error_log( $query );
error_log( __FUNCTION__ . "error: $activityAuthor, $articleID, $commentPayload" );
}
}
return TRUE;
}
else
{
error_log( $query );
error_log( __FUNCTION__ . "error2: $activityAuthor, $articleID, $commentPayload" );
return FALSE;
}
}
// 08/19/2014 13:13:04
function RemoveComment( $articleID, $commentID )
{
settype( $articleID, 'integer' );
settype( $commentID, 'integer' );
$query = "DELETE FROM Comment
WHERE ArticleID = $articleID AND ID = $commentID";
log_sql( $query );
global $db;
$dbResult = mysqli_query( $db, $query );
if( $dbResult == FALSE )
{
log_sql_fail();
return FALSE;
}
else
{
return( mysqli_affected_rows( $db ) > 0 );
}
}
// 20:05 06/04/2013
function addArticleComment( $user, $articleType, $articleID, $commentPayload )
{
// Note: $user is the person who just made a comment.
$userID = getUserIDFromUser( $user );
if( $userID == 0 )
{
error_log( __FUNCTION__ . "error3: $user, $articleType, $articleID, $commentPayload" );
return FALSE;
}
//error_log( __FUNCTION__ . $commentPayload );
// Replace all single quotes with double quotes (to work with MYSQL DB)
//$commentPayload = str_replace( "'", "''", $commentPayload );
global $db;
$commentPayload = mysqli_real_escape_string( $db, $commentPayload );
$query = "INSERT INTO Comment VALUES( NULL, $articleType, $articleID, $userID, '$commentPayload', NOW(), NULL )";
log_sql( $query );
$dbResult = mysqli_query( $db, $query );
if( $dbResult == FALSE )
{
log_sql_fail();
return FALSE;
}
// Inform Subscribers of this comment:
if( $articleType == 5 ) // Activity
{
error_log( __FUNCTION__ . " Comment on Activity... notifying author and thread" );
// Get activity's original author:
$activityData = getActivityMetadata( $articleID );
informAllSubscribersAboutActivity( $user, $activityData[ 'User' ], $articleID, $articleType );
}
else if( $articleType == 2 ) // Achievement
{
error_log( __FUNCTION__ . " Comment on Achievement... notifying author and thread" );
// Get achievement's original author:
$achievementData = getAchievementMetadata( $articleID );
informAllSubscribersAboutActivity( $user, $achievementData[ 'Author' ], $articleID, $articleType );
}
else if( $articleType == 3 ) // User
{
error_log( __FUNCTION__ . " Comment on User... notifying author and thread" );
// $articleID = ID of the User who's page we've written on.
// $userData['User'] = the name of the user who's wall has just been written on.
// $user = the user who has just written on this wall.
// Get
$userData = getUserMetadataFromID( $articleID );
informAllSubscribersAboutActivity( $user, $userData[ 'User' ], $articleID, $articleType );
}
else if( $articleType == 4 ) // News
{
// Not interested in any further activity at this stage.
error_log( __FUNCTION__ . " Comment on News article... notify nobody? " );
}
else if( $articleType == 1 ) // Game
{
//log_sql_fail();
getGameTitleFromID( $articleID, $gameName, $consoleID, $consoleName, $forumTopicID, $gameData );
// $user commented on $gameName, which is game ID $articleID (type 1).
informAllSubscribersAboutActivity( $user, $gameName, $articleID, $articleType );
//error_log( __FUNCTION__ . " Comment on Game... notify nobody? " );
}
else if( $articleType == 7 ) // Ticket
{
// $user commented on ticket $articleID
$ticketData = getTicket( $articleID );
informAllSubscribersAboutActivity( $user, $ticketData[ 'ReportedBy' ], $articleID, $articleType );
}
return TRUE;
}
// 00:08 19/03/2013
function getActivityMetadata( $activityID )
{
$query = "SELECT * FROM Activity
WHERE ID='$activityID'";
$dbResult = s_mysql_query( $query );
return mysqli_fetch_assoc( $dbResult );
}
// 00:08 19/03/2013
function getFeed( $user, $maxMessages, $offset, &$dataOut, $latestFeedID = 0, $type = 'global' )
{
settype( $maxMessages, "integer" );
settype( $offset, "integer" );
if( $type == 'activity' ) // Find just this activity, ONLY!
$subquery = "act.ID = $latestFeedID ";
else if( $type == 'friends' ) // User has been provided: find my friends!
$subquery = "act.ID > $latestFeedID AND ( act.user = '$user' OR act.user IN ( SELECT f.Friend FROM Friends AS f WHERE f.User = '$user' ) )";
else if( $type == 'individual' ) // User and 'individual', just this user's feed!
$subquery = "act.ID > $latestFeedID AND ( act.user = '$user' )";
else //if( $type == 'global' ) // Otherwise, global feed
$subquery = "act.ID > $latestFeedID ";
$query = "
SELECT InnerTable.ID, UNIX_TIMESTAMP(timestamp) AS timestamp, activitytype, InnerTable.User, uaLocal.RAPoints, uaLocal.Motto, data, data2, gd.Title AS GameTitle, gd.ID AS GameID, gd.ImageIcon AS GameIcon, cons.Name AS ConsoleName,
ach.Title AS AchTitle, ach.Description AS AchDesc, ach.Points AS AchPoints, ach.BadgeName AS AchBadge,
lb.Title AS LBTitle, lb.Description AS LBDesc, lb.Format AS LBFormat,
ua.User AS CommentUser, ua.Motto AS CommentMotto, ua.RAPoints AS CommentPoints, c.Payload AS Comment, UNIX_TIMESTAMP(c.Submitted) AS CommentPostedAt, c.ID AS CommentID
FROM
(
SELECT act.ID, act.timestamp, act.activitytype, act.User, act.data, act.data2
FROM Activity AS act
LEFT JOIN UserAccounts AS ua ON ua.User = act.User
WHERE ( !ua.Untracked || ua.User=\"$user\" ) AND $subquery
ORDER BY act.ID DESC
LIMIT $offset, $maxMessages
) AS InnerTable
LEFT JOIN LeaderboardDef AS lb ON
( activitytype = 7 AND InnerTable.data = lb.ID ) OR
( activitytype = 8 AND InnerTable.data = lb.ID )
LEFT JOIN Achievements AS ach ON
( activitytype = 1 AND ach.ID = InnerTable.data ) OR
( activitytype = 4 AND ach.ID = InnerTable.data ) OR
( activitytype = 5 AND ach.ID = InnerTable.data )
LEFT JOIN GameData AS gd ON
( activitytype = 1 AND gd.ID = ach.GameID ) OR
( activitytype = 3 AND gd.ID = InnerTable.data ) OR
( activitytype = 4 AND gd.ID = ach.GameID ) OR
( activitytype = 5 AND gd.ID = ach.GameID ) OR
( activitytype = 6 AND gd.ID = InnerTable.data ) OR
( activitytype = 7 AND gd.ID = lb.GameID ) OR
( activitytype = 8 AND gd.ID = lb.GameID )
LEFT JOIN Console AS cons ON cons.ID = gd.ConsoleID
LEFT JOIN Comment AS c ON c.ArticleID = InnerTable.ID
LEFT JOIN UserAccounts AS ua ON ua.ID = c.UserID
LEFT JOIN UserAccounts AS uaLocal ON uaLocal.User = InnerTable.User
ORDER BY InnerTable.ID ASC, c.ID DESC
";
//error_log( $query );
$dbResult = s_mysql_query( $query );
if( $dbResult !== FALSE )
{
$dataOut = array();
$i = 0;
while( $db_entry = mysqli_fetch_assoc( $dbResult ) )
{
$dataOut[ $i ] = $db_entry;
$i++;
}
return $i;
}
else
{
error_log( $query );
error_log( __FUNCTION__ . " Failed! user=$user" );
}
return 0;
}
//01:50 22/03/2013
function getRecentlyPlayedGames( $user, $offset, $count, &$dataOut )
{
// $query = "SELECT g.ID AS GameID, g.ConsoleID, c.Name AS ConsoleName, g.Title, MAX(act.lastupdate) AS LastPlayed, g.ImageIcon
// FROM Activity AS act
// LEFT JOIN GameData AS g ON g.ID = act.data
// LEFT JOIN Console AS c ON c.ID = g.ConsoleID
// WHERE act.user='$user' AND act.activitytype=3
// GROUP BY g.ID
// ORDER BY MAX(act.lastupdate) DESC
// LIMIT $offset, $count ";
// 19:30 02/02/2014 rewritten without MAX() and using an inner query. ~300% faster but I don't know why... :(
// 02:51 03/02/2014 re-rewritten with MAX()
// 01:38 15/02/2014 re-readded 'AND act.activitytype = 3' to inner query. act.data is not necessarily a game, therefore we need this '3' part.
// 22:56 18/02/2014 re-re-readded 'MAX() to inner.
// 08:05 01/10/2014 removed outer activitytype=3, added rating
//{$ObjectType::Game}
$query = "
SELECT Inner1.data AS GameID, gd.ConsoleID, c.Name AS ConsoleName, gd.Title, gd.ImageIcon, Inner1.lastupdate AS LastPlayed, r.RatingValue AS MyVote
FROM (
SELECT act.ID, MAX( act.lastupdate ) AS lastupdate, act.data, act.activitytype
FROM Activity AS act
WHERE act.user='$user' AND act.activitytype = 3
GROUP BY act.data
ORDER BY MAX( act.lastupdate ) DESC
) AS Inner1
LEFT JOIN GameData AS gd ON gd.ID = Inner1.data
LEFT JOIN Console AS c ON c.ID = gd.ConsoleID
LEFT JOIN Rating AS r ON r.RatingObjectType = " . ObjectType::Game . " AND r.RatingID=Inner1.data AND r.User='$user'
LIMIT $offset, $count";
$dbResult = s_mysql_query( $query );
$numFound = 0;
if( $dbResult !== FALSE )
{
while( $data = mysqli_fetch_assoc( $dbResult ) )
{
$dataOut[ $numFound ] = $data;
//$dataOut[$data['GameID']] = $data;
$numFound++;
}
}
else
{
error_log( $query );
error_log( __FUNCTION__ . " had issues... $user, $offset, $count" );
}
return $numFound;
}
function getArticleComments( $articleTypeID, $articleID, $offset, $count, &$dataOut )
{
// $articleTypeID
// 1 = Game
// 2 = Achievement
// 3 = User
// 4 = News (unused)
// 5 = feed Activity
// 6 = LB
// 7 = Ticket
$dataOut = array();
$numArticleComments = 0;
$query = "
SELECT ua.User, ua.RAPoints, ua.Motto, LatestComments.ID, LatestComments.UserID, LatestComments.Payload AS CommentPayload, UNIX_TIMESTAMP(LatestComments.Submitted) AS Submitted, LatestComments.Edited
FROM
(
SELECT c.ID, c.UserID, c.Payload, c.Submitted, c.Edited
FROM Comment AS c
WHERE c.ArticleType=$articleTypeID AND c.ArticleID=$articleID
ORDER BY c.Submitted DESC
LIMIT $offset, $count
) AS LatestComments
LEFT JOIN UserAccounts AS ua ON ua.ID = LatestComments.UserID";
$dbResult = s_mysql_query( $query );
if( $dbResult !== FALSE )
{
while( $db_entry = mysqli_fetch_assoc( $dbResult ) )
{
$dataOut[ $numArticleComments ] = $db_entry;
$numArticleComments++;
}
}
else
{
error_log( __FUNCTION__ . " failed, $articleTypeID, $articleID, $offset, $count " );
error_log( $query );
}
// Fetch the last elements by submitted, but return them here in top-down order.
$dataOut = array_reverse( $dataOut );
return $numArticleComments;
}
function getCurrentlyOnlinePlayers()
{
$recentMinutes = 10;
$playersFound = array();
// Select all users active in the last 10 minutes:
$query = "SELECT ua.User, ua.RAPoints, ua.Motto, act.timestamp AS LastActivityAt, ua.RichPresenceMsg AS LastActivity
FROM UserAccounts AS ua
LEFT JOIN Activity AS act ON act.ID = ua.LastActivityID
WHERE ua.LastLogin > TIMESTAMPADD( MINUTE, -$recentMinutes, NOW() )
ORDER BY ua.ID ";
$dbResult = s_mysql_query( $query );
if( $dbResult !== FALSE )
{
while( $db_entry = mysqli_fetch_assoc( $dbResult ) )
{
settype( $db_entry[ 'RAPoints' ], 'integer' );
settype( $db_entry[ 'GameID' ], 'integer' );
$playersFound[] = $db_entry;
}
}
else
{
error_log( $query );
error_log( __FUNCTION__ . " failed3: user:$user gameID:$gameID" );
}
return $playersFound;
}
function getLatestRichPresenceUpdates()
{
$playersFound = array();
$recentMinutes = 10;
$query = "SELECT ua.User, ua.RAPoints, ua.Motto, ua.RichPresenceMsg, gd.ID AS GameID, gd.Title AS GameTitle, gd.ImageIcon AS GameIcon, c.Name AS ConsoleName
FROM UserAccounts AS ua
LEFT JOIN GameData AS gd ON gd.ID = ua.LastGameID
LEFT JOIN Console AS c ON c.ID = gd.ConsoleID
WHERE ua.RichPresenceMsgDate > TIMESTAMPADD( MINUTE, -$recentMinutes, NOW() ) AND ua.LastGameID !=0";
$dbResult = s_mysql_query( $query );
if( $dbResult !== FALSE )
{
while( $db_entry = mysqli_fetch_assoc( $dbResult ) )
{
settype( $db_entry[ 'GameID' ], 'integer' );
settype( $db_entry[ 'RAPoints' ], 'integer' );
$playersFound[] = $db_entry;
}
}
else
{
error_log( $query );
error_log( __FUNCTION__ . " failed3: user:$user gameID:$gameID" );
}
return $playersFound;
}
function getLatestNewAchievements( $numToFetch, &$dataOut )
{
$numFound = 0;
$query = "SELECT ach.ID, ach.GameID, ach.Title, ach.Description, ach.Points, gd.Title AS GameTitle, gd.ImageIcon as GameIcon, ach.DateCreated, UNIX_TIMESTAMP(ach.DateCreated) AS timestamp, ach.BadgeName, c.Name AS ConsoleName
FROM Achievements AS ach
LEFT JOIN GameData AS gd ON gd.ID = ach.GameID
LEFT JOIN Console AS c ON c.ID = gd.ConsoleID
ORDER BY DateCreated DESC
LIMIT 0, $numToFetch ";
//echo $query;
$dbResult = s_mysql_query( $query );
if( $dbResult !== FALSE )
{
while( $db_entry = mysqli_fetch_assoc( $dbResult ) )
{
$dataOut[ $numFound ] = $db_entry;
$numFound++;
}
}
else
{
//log_email( __FUNCTION__ . " cannot deal with this function..." );
error_log( $query );
error_log( __FUNCTION__ . " failed: $numToFetch" );
}
return $numFound;
}
function GetMostPopularTitles( $daysRange = 7, $offset = 0, $count = 10 )
{
$data = array();
$query = "SELECT COUNT(*) as PlayedCount, gd.ID, gd.Title, gd.ImageIcon, c.Name as ConsoleName
FROM Activity AS act
LEFT JOIN GameData AS gd ON gd.ID = act.data
LEFT JOIN Console AS c ON c.ID = gd.ConsoleID
WHERE ( act.timestamp BETWEEN TIMESTAMPADD( DAY, -$daysRange, NOW() ) AND NOW() ) AND ( act.activitytype = 3 ) AND ( act.data > 0 )
GROUP BY act.data
ORDER BY PlayedCount DESC
LIMIT $offset, $count";
$dbResult = s_mysql_query( $query );
while( $nextData = mysqli_fetch_assoc( $dbResult ) )
$data[] = $nextData;
return $data;
}
?>