Browse Source

Uppdaterat gamePlayers med playerRound

Axel Nordh 5 years ago
parent
commit
ec3d2d2f97

+ 9 - 7
Assets/Scripts/Database/OnlineDatabase.cs

@@ -100,6 +100,7 @@ public class OnlineDatabase : MonoBehaviour {
     public class PlayerInfo {
         public string username;
         public string status;
+        public int playerRound;
     }
 
     [Serializable]
@@ -111,6 +112,7 @@ public class OnlineDatabase : MonoBehaviour {
     public class GamePlayerInfo {
         public string username;
         public string userLockedQuestions;
+        public string playerRound;
     }
 
     [Serializable]
@@ -152,6 +154,7 @@ public class OnlineDatabase : MonoBehaviour {
         public string username;
         public string userLockedQuestions;
         public string playerStatus;
+        public string playerRound;
     }
 
     [Serializable]
@@ -217,14 +220,14 @@ public class OnlineDatabase : MonoBehaviour {
         form.AddField("message", message);
         form.AddField("title", title);
         form.AddField("winningPlayer", currentPlayer);
-        form.AddField("messageType", "gameFinishedMessage");
+        form.AddField("type", "gameFinishedMessage");
 
         int index = 0;
         foreach (KeyValuePair<String, int> player in players) {
             form.AddField("player" + index++, player.Key);
         }
 
-        CallDatabase("FCMMessageing.php", form);
+        CallDatabase("FirebaseCaller.php", form);
     }
 
     private void Start() {
@@ -382,7 +385,7 @@ public class OnlineDatabase : MonoBehaviour {
         ogs.LastPlayedDate = game.lastPlayedDate;
     }
 
-    internal List<KeyValuePair<string, string>> GetGameInfo(int gameId) {
+    internal List<KeyValuePair<string, string>> GetGameInfo(int gameId) { // TODO Return new class GameInfo
         List<KeyValuePair<string, string>> returnList = new List<KeyValuePair<string, string>>();
 
         WWWForm form = new WWWForm();
@@ -508,7 +511,7 @@ public class OnlineDatabase : MonoBehaviour {
         return round;
     }
 
-    internal List<KeyValuePair<string, int>> GetPlayersForGame(int gameId) {
+    internal List<KeyValuePair<string, int>> GetPlayersForGame(int gameId) { // TODO update with new class GamePlayers as return
         WWWForm form = new WWWForm();
         form.AddField("f", "GetPlayers");
         form.AddField("gameId", gameId);
@@ -675,8 +678,7 @@ public class OnlineDatabase : MonoBehaviour {
         form.AddField("message", message);
         form.AddField("type", "FCMNextPlayer");
         
-        //string response = CallOnlineDatabaseWithResponse("FCMNextPlayer.php", form);
-        CallDatabase("FCMMessageing.php", form);
+        CallDatabase("FirebaseCaller.php", form);
     }
 
     internal void SendInviteForNewGame(int gameId, List<String> Players, String inviter) {
@@ -691,7 +693,7 @@ public class OnlineDatabase : MonoBehaviour {
         form.AddField("title", LocalizationManager.Instance.GetText("FCM_NEW_GAME_TITLE"));
         form.AddField("message", String.Format(LocalizationManager.Instance.GetText("FCM_NEW_GAME_MESSAGE"), inviter));
         form.AddField("type", "InviteMessage");
-        CallDatabase("FCMMessageing.php", form);
+        CallDatabase("FirebaseCaller.php", form);
     }
 
     

BIN
Assets/narKampenLocal.db


+ 0 - 83
dbFiles/FCMMessageing.php

@@ -1,83 +0,0 @@
-<?php
-/* FCM - Messageing */
-	require_once '../vendor/autoload.php';
-
-	putenv('GOOGLE_APPLICATION_CREDENTIALS=/home/axel/configs/narkampen-firebase-adminsdk-k42j5-d3d0354e37.json');
-
-	$client = new Google_Client();
-	$client->useApplicationDefaultCredentials();
-	$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
-	$httpClient = $client->authorize();
-
-	$hostname = 'localhost';
-	$username = 'narKampen';
-	$pass = 'IfRLzj2HJBXA9eei';
-	$database = 'narKampen';
-	
-	$conn = new mysqli($hostname, $username, $pass, $database);
-	if (!$conn) {
-		die("Connection Failed. ". mysqli_connect_error());
-	}
-	mysqli_set_charset($conn,'utf8');
-	$messageType = $conn->real_escape_string(isset($_POST['type'])?$_POST['type']:"");
-	
-	$title = $conn->real_escape_string($_POST['title']);
-	$messageToSend = $conn->real_escape_string($_POST['message']);
-	$gameId = $conn->real_escape_string($_POST['gameId']);
-	
-	file_put_contents("FCMMessageDebug.log", "Called $messageType at " . Date(), FILE_APPEND);
-	if ($messageType === "FCMNextPlayer") {
-		$playerName = $conn->real_escape_string($_POST['playerName']);
-		$token = getToken($conn, $gameId, $playerName);
-		
-		if ($token != null && $token != "") {
-			sendMessage($httpClient, $token, $title, $messageToSend, "NextPlayer");
-		}
-	} else if ($messageType === "InviteMessage") {
-		$i = 0;
-		while ($_POST['player' . $i] != null) {
-			$token = getToken($conn, $gameId, $conn->real_escape_string($_POST['player' . $i]));
-			sendMessage($httpClient, $token, $title, $messageToSend, "GameInvite");
-		}
-
-	} else if ($messageType === "gameFinishedMessage") {
-		$i = 0;
-		while ($_POST['player' . $i] != null) {
-			$token = getToken($conn, $gameId, $conn->real_escape_string($_POST['player' . $i]));
-			sendMessage($httpClient, $token, $title, $messageToSend, "GameFinished");
-		}
-	}
-	
-	function getToken($conn, $gameId, $playerName) {
-		$sql = "SELECT messageToken FROM `gamePlayers` INNER JOIN users ON users.id = playerId WHERE gameId = $gameId and users.username = '$playerName'";
-
-		$result = $conn->query($sql);
-		if ($result->num_rows == 1) {
-			$returnArray = array();
-			$data = $result->fetch_assoc();
-			$token = $data['messageToken'];
-		} else {
-			echo "No games found for user";
-		}
-		return $token;
-	}
-	
-	function sendMessage($httpClient, $token, $title, $messageToSend, $tag) {
-		// Your Firebase project ID
-		$project = "narkampen";
-		// Creates a notification for subscribers to the debug topic
-		$message = [
-			"message" => [
-				"token" => $token,
-				"notification" => [
-					"body" => $messageToSend,
-					"title" => $title,
-					"tag" => $tag,
-				]
-			]
-		];
-		// Send the Push Notification - use $response to inspect success or errors
-		$response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]);
-
-		file_put_contents("FCMMessageDebugResponse.log", print_r($response,true), FILE_APPEND);
-	}

+ 0 - 55
dbFiles/FCMNextPlayer.php

@@ -1,55 +0,0 @@
-<?php
-require_once '../vendor/autoload.php';
-
-putenv('GOOGLE_APPLICATION_CREDENTIALS=/home/axel/configs/narkampen-firebase-adminsdk-k42j5-d3d0354e37.json');
-
-$client = new Google_Client();
-$client->useApplicationDefaultCredentials();
-$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
-$httpClient = $client->authorize();
-
-$hostname = 'localhost';
-$username = 'narKampen';
-$pass = 'IfRLzj2HJBXA9eei';
-$database = 'narKampen';
-
-$conn = new mysqli($hostname, $username, $pass, $database);
-if (!$conn) {
-	die("Connection Failed. ". mysqli_connect_error());
-}
-mysqli_set_charset($conn,'utf8');
-
-$gameId = $conn->real_escape_string($_POST['gameId']);
-$playerName = $conn->real_escape_string($_POST['playerName']);
-
-$title = $conn->real_escape_string($_POST['title']);
-$message = $conn->real_escape_string($_POST['message']);
-
-$sql = "SELECT messageToken FROM `gamePlayers` INNER JOIN users ON users.id = playerId WHERE gameId = $gameId and users.username = '$playerName'";
-
-$result = $conn->query($sql);
-if ($result->num_rows == 1) {
-	$returnArray = array();
-	$data = $result->fetch_assoc();
-	$token = $data['messageToken'];
-} else {
-	echo "No games found for user";
-}
-
-if ($token != null && $token != "") {
-// Your Firebase project ID
-$project = "narkampen";
-// Creates a notification for subscribers to the debug topic
-$message = [
-    "message" => [
-        "token" => $token,
-        "notification" => [
-            "body" => $message,
-            "title" => $title,
-        ]
-    ]
-];
-// Send the Push Notification - use $response to inspect success or errors
-$response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]);
-
-}

+ 36 - 21
dbFiles/NewFirebaseCaller.php → dbFiles/FirebaseCaller.php

@@ -1,5 +1,5 @@
 <?php
-    require __DIR__ . "/vendor/autoload.php";
+    require __DIR__ . "/../vendor/autoload.php";
 
     use Kreait\Firebase\Factory;
 	use Kreait\Firebase\Messaging\Notification;
@@ -24,10 +24,6 @@
 	$messageToSend = $conn->real_escape_string($_POST['message']);
 	$gameId = $conn->real_escape_string($_POST['gameId']);
 
-	
-	$factory = (new Factory)->withServiceAccount("narkampen-firebase-adminsdk-k42j5-d3d0354e37.json");
-
-	
 	if ($messageType === "FCMNextPlayer") {
 		$playerName = $conn->real_escape_string($_POST['playerName']);
 		$deviceToken = getToken($conn, $gameId, $playerName);
@@ -35,26 +31,45 @@
 		if ($deviceToken != null && $deviceToken != "") {
 			sendMessage($title, $messageToSend, $deviceToken, "NextPlayer");
 		}
-	}
-	
-	function sendMessage($title, $messageToSend, $deviceToken, $tag = "") {
-		$messaging = $factory->createMessaging();
-		$notification = Notification::create($title, $messageToSend);
-		$message = CloudMessage::withTarget(
-			'token' => $deviceToken);
-			
-		if ($tag !== "") {
-			$fcmOptions = FcmOptions::create()->withAnalyticsLabel($tag);	
+	} else if ($messageType === "gameFinishedMessage") {
+		$i = 0;
+		while ($_POST['player' . $i] != null) {
+			$deviceToken = getToken($conn, $gameId, $conn->real_escape_string($_POST['player' . $i]));
+			sendMessage($title, $messageToSend, $deviceToken, "GameFinished");
 		}
-		$message->withNotification($notification);
-		
-		if ($fcmOptions != null) {
-			$message->withFcmOptions($fcmOptions);
+	} else if ($messageType === "InviteMessage") {
+		$i = 0;
+		while ($_POST['player' . $i] != null) {
+			$deviceToken = getToken($conn, $gameId, $conn->real_escape_string($_POST['player' . $i]));
+			sendMessage($title, $messageToSend, $deviceToken, "GameInvite");
 		}
-		
-		$messaging->send($message);
 	}
 	
+    function sendMessage($title, $messageToSend, $deviceToken, $tag = "") {
+        $factory = (new Factory)->withServiceAccount("../narkampen-firebase-adminsdk-k42j5-d3d0354e37.json");
+        $messaging = $factory->createMessaging();
+        $notification = Notification::create($title, $messageToSend);
+
+        if ($tag !== "") {
+            $fcmOptions = FcmOptions::create()->withAnalyticsLabel($tag);
+        }
+
+        if ($fcmOptions != null) {
+                $message = CloudMessage::withTarget(
+            'token', $deviceToken)
+            ->withNotification($notification)
+            ->withFcmOptions($fcmOptions);
+
+        } else {
+            $message = CloudMessage::withTarget(
+                'token', $deviceToken)
+                ->withNotification($notification);
+        }
+
+        $messaging->send($message);
+    }
+
+	
 	/*
 	// NOTIFICATION EXAMPLE
 	$title = 'My Notification Title';

+ 3 - 3
dbFiles/NewOnlineGame.php

@@ -56,13 +56,13 @@
 	$playerPartInsertSql = "";
 	foreach ($playerIdsArray AS $playerId) {
 		if ($currentUser == $playerId) {
-			$playerPartInsertSql .= "($gameId, $playerId, 1, 1, 'ACCEPTED'),";
+			$playerPartInsertSql .= "($gameId, $playerId, 1, 1, 1, 'ACCEPTED'),";
 		} else {
-			$playerPartInsertSql .= "($gameId, $playerId, 1, 1, 'WAITING'),";
+			$playerPartInsertSql .= "($gameId, $playerId, 1, 1, 1, 'WAITING'),";
 		}
 	}
 	$playerPartInsertSql = rtrim($playerPartInsertSql,",");
-	$playerSql = "INSERT INTO gamePlayers (gameId, playerId, userLockedQuestions, questionsLost, status) VALUES $playerPartInsertSql";
+	$playerSql = "INSERT INTO gamePlayers (gameId, playerId, userLockedQuestions, questionsLost, playerRound status) VALUES $playerPartInsertSql";
 	
 	$result = mysqli_query($conn,$playerSql);
 	$error = mysqli_error($conn);

+ 1 - 2
dbFiles/OnlineGameInfo.php

@@ -14,10 +14,9 @@
 	$callFunction = $conn->real_escape_string($_POST['f']);
 	
 	if ($callFunction === "GetGameInfo") {
-		$sql = "SELECT username, status FROM gamePlayers INNER JOIN users ON users.id = playerId WHERE gameId = $gameId";
+		$sql = "SELECT username, status, playerRound FROM gamePlayers INNER JOIN users ON users.id = playerId WHERE gameId = $gameId";
 		$result = $conn->query($sql);
 
-
 		if ($result->num_rows > 0) {
 			$returnArray = array();
 			$i = 0;

+ 2 - 2
dbFiles/OnlineGames.php

@@ -26,7 +26,7 @@
 				"ORDER BY FIELD(PlayerToAct, '$userName') DESC, lastPlayedDate DESC";
 */
 
-		$sql = "SELECT game.*, users.id as userId, users.username as username, gamePlayers.userLockedQuestions, gamePlayers.status playerStatus FROM game " . 
+		$sql = "SELECT game.*, users.id as userId, users.username as username, gamePlayers.userLockedQuestions, gamePlayers.status playerStatus, gamePlayers.playerRound FROM game " . 
 			"INNER JOIN gamePlayers ON gamePlayers.gameId = game.id " . 
 			"INNER JOIN users ON users.id = playerId " .
 			"WHERE game.id IN (SELECT gameId FROM gamePlayers WHERE playerId = $userId) " .
@@ -183,7 +183,7 @@
 		
 		echo $data['round'];
 	} else if ($callFunction === "GetPlayers") {
-		$sql = "SELECT username, userLockedQuestions FROM gamePlayers INNER JOIN users ON users.id = gamePlayers.playerId WHERE gameId = $gameId";
+		$sql = "SELECT username, userLockedQuestions, playerRound FROM gamePlayers INNER JOIN users ON users.id = gamePlayers.playerId WHERE gameId = $gameId";
 		$result = $conn->query($sql);
 		$returnArray = array();
 		$i = 0;