From 8e2802052ccde9b383a0ae8bac5d0d4ab1b5f386 Mon Sep 17 00:00:00 2001
From: SilicaAndPina <earsyum@gmail.com>
Date: Wed, 7 Jul 2021 02:12:29 +1200
Subject: [PATCH] add playersonline.php

---
 WebInterface/game-site/account.php           |  2 +-
 WebInterface/game-site/web/crosserver.php    | 35 ++++++++++++++++++--
 WebInterface/game-site/web/playersonline.php | 33 +++++++++++++++++-
 WebInterface/master-site/crosserver.php      | 35 ++++++++++++++++++--
 4 files changed, 99 insertions(+), 6 deletions(-)

diff --git a/WebInterface/game-site/account.php b/WebInterface/game-site/account.php
index f1f5a39..ebb215b 100755
--- a/WebInterface/game-site/account.php
+++ b/WebInterface/game-site/account.php
@@ -145,7 +145,7 @@ function ajaxDone(target) {
 }
 
 function loadplayers() {
-  ajax('web/playersonline.php?id=588112','PLAYERS');
+  <?php echo("ajax('web/playersonline.php?id=".htmlspecialchars($_SESSION['PLAYER_ID'], ENT_QUOTES)."','PLAYERS');"); ?>
   window.setTimeout("loadplayers()", 30000);  //reload player list every millisecs
 }
 window.setTimeout("loadplayers()", 10); ///load player list first time quick
diff --git a/WebInterface/game-site/web/crosserver.php b/WebInterface/game-site/web/crosserver.php
index 984a444..9918230 100644
--- a/WebInterface/game-site/web/crosserver.php
+++ b/WebInterface/game-site/web/crosserver.php
@@ -12,7 +12,38 @@ function GenHmacMessage(string $data, string $channel)
 	return $hmac;
 }
 
-function  getNoPlayersOnlineInServer($database)
+function getPlayerList($database)
+{
+	include('config.php');
+	$dbname = $database;
+	$connect = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname) or die("Unable to connect to '$dbhost'");
+	$onlineUsers = mysqli_query($connect, "SELECT * FROM OnlineUsers");
+	
+	$users_on = [];
+		
+
+	while ($row = $onlineUsers->fetch_row()) {
+		$arr = [ ['id' => $row[0], 'admin' => ($row[1] == 'YES'), 'mod' => ($row[2] == 'YES'), 'subbed' => ($row[3] == 'YES'), 'new' => ($row[4] == 'YES')] ];
+		$users_on = array_merge($users_on, $arr);
+	}
+	
+	return $users_on;
+}
+
+function checkUserBuddy($database, $yourId, $friendsId)
+{
+	include('config.php');
+	$dbname = $database;
+	$connect = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname) or die("Unable to connect to '$dbhost'");
+	$stmt = $connect->prepare("SELECT COUNT(1) FROM BuddyList WHERE (Id=? AND IdFriend=?) OR (Id=? AND IdFriend=?)");
+	$stmt->bind_param("iiii", $yourId, $friendsId, $friendsId, $yourId);
+	$stmt->execute();
+	$result = $stmt->get_result();
+	return $result->fetch_row()[0];
+}
+
+
+function getNoPlayersOnlineInServer($database)
 {
 	include('config.php');
 	$dbname = $database;
@@ -21,7 +52,7 @@ function  getNoPlayersOnlineInServer($database)
 	return $onlineUsers->fetch_row()[0];
 }
 
-function  getNoSubbedPlayersOnlineInServer($database)
+function getNoSubbedPlayersOnlineInServer($database)
 {
 	include('config.php');
 	$dbname = $database;
diff --git a/WebInterface/game-site/web/playersonline.php b/WebInterface/game-site/web/playersonline.php
index 09bed9b..7ab908a 100755
--- a/WebInterface/game-site/web/playersonline.php
+++ b/WebInterface/game-site/web/playersonline.php
@@ -1 +1,32 @@
-<B>3 players<BR>online now:</B><BR>BackInTime <FONT SIZE=-2 COLOR=660000>[new]</FONT><BR>Banaja<BR>Mudkip <FONT SIZE=-2 COLOR=660000>[new]</FONT><BR><I><FONT COLOR=BLUE>(0 buddies)</FONT></I><BR><FONT COLOR=222222 SIZE=-1><I>This list refreshes every 30 seconds.</I></FONT>
\ No newline at end of file
+<?php
+include("config.php");
+include("crosserver.php");
+include("common.php");
+$id = -1;
+if(isset($_GET['id'])){
+	$id = intval($_GET['id']);
+}
+$on = getPlayerList($dbname);
+$numbOn = count($on);
+$budsOn = 0;
+?>
+<B><?php echo($numbOn)?> players<BR>online now:</B><?php
+
+for($i = 0; $i < $numbOn; $i++){
+	$name = get_username($on[$i]['id']);
+	$admin = $on[$i]['admin'];
+	$mod = $on[$i]['mod'];
+	$subbed = $on[$i]['subbed'];
+	$new = $on[$i]['new'];
+	$bud = checkUserBuddy($dbname, $id ,$on[$i]['id']);
+	
+	echo("<BR>");
+	if($bud) { echo('<B><FONT COLOR=BLUE>'); echo(htmlspecialchars($name)); echo('</FONT></B>'); $budsOn++; }
+	else if($admin) { echo('<B><FONT COLOR=RED>'); echo(htmlspecialchars($name)); echo('</FONT></B>'); }
+	else if($mod) { echo('<B><FONT COLOR=GREEN>'); echo(htmlspecialchars($name)); echo('</FONT></B>'); }	
+	else { echo(htmlspecialchars($name)); }
+	
+	if($new) { echo(' <FONT SIZE=-2 COLOR=660000>[new]</FONT>'); };
+	
+}
+?><BR><I><FONT COLOR=BLUE>(<?php echo($budsOn); ?> buddies)</FONT></I><BR><FONT COLOR=222222 SIZE=-1><I>This list refreshes every 30 seconds.</I></FONT>
\ No newline at end of file
diff --git a/WebInterface/master-site/crosserver.php b/WebInterface/master-site/crosserver.php
index 984a444..9918230 100644
--- a/WebInterface/master-site/crosserver.php
+++ b/WebInterface/master-site/crosserver.php
@@ -12,7 +12,38 @@ function GenHmacMessage(string $data, string $channel)
 	return $hmac;
 }
 
-function  getNoPlayersOnlineInServer($database)
+function getPlayerList($database)
+{
+	include('config.php');
+	$dbname = $database;
+	$connect = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname) or die("Unable to connect to '$dbhost'");
+	$onlineUsers = mysqli_query($connect, "SELECT * FROM OnlineUsers");
+	
+	$users_on = [];
+		
+
+	while ($row = $onlineUsers->fetch_row()) {
+		$arr = [ ['id' => $row[0], 'admin' => ($row[1] == 'YES'), 'mod' => ($row[2] == 'YES'), 'subbed' => ($row[3] == 'YES'), 'new' => ($row[4] == 'YES')] ];
+		$users_on = array_merge($users_on, $arr);
+	}
+	
+	return $users_on;
+}
+
+function checkUserBuddy($database, $yourId, $friendsId)
+{
+	include('config.php');
+	$dbname = $database;
+	$connect = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname) or die("Unable to connect to '$dbhost'");
+	$stmt = $connect->prepare("SELECT COUNT(1) FROM BuddyList WHERE (Id=? AND IdFriend=?) OR (Id=? AND IdFriend=?)");
+	$stmt->bind_param("iiii", $yourId, $friendsId, $friendsId, $yourId);
+	$stmt->execute();
+	$result = $stmt->get_result();
+	return $result->fetch_row()[0];
+}
+
+
+function getNoPlayersOnlineInServer($database)
 {
 	include('config.php');
 	$dbname = $database;
@@ -21,7 +52,7 @@ function  getNoPlayersOnlineInServer($database)
 	return $onlineUsers->fetch_row()[0];
 }
 
-function  getNoSubbedPlayersOnlineInServer($database)
+function getNoSubbedPlayersOnlineInServer($database)
 {
 	include('config.php');
 	$dbname = $database;