question

stacoverflow399 avatar image
stacoverflow399 asked

Banning System In Realtime Code

So i was adding a level system and a level cap and I found out I needed a ban system to ban the player if they are cheating the game to go over the level cap. I could not figure it out and all i got is this. a script to ban a player is with a reason, and time. i don't know how to fix it im really stupid when coding playfab.`using PlayFab; using PlayFab.ClientModels; using System; using System.Collections.Generic; using UnityEngine;

namespace Dev.PlayFab.Player.Ban { public class PlayfabBans { // Ban a player with specified values public static void BanPlayer(string playerIdToBan, string reason = "Not Specified.", int timehours = 1, Action successCallback = null, Action failCallback = null) { // Construct JSON object for BanReason var banReasonObj = new { reason = reason }; string banReasonJson = JsonUtility.ToJson(banReasonObj);

         // Retrieve the player's last login information
         GetPlayerProfile(playerIdToBan, (profile) =>
         {
             if (profile != null && profile.PlayerProfile != null)
             {
                 // Extract the last login IP address
                 string lastIpAddress = GetLastLoginIpAddress(profile.PlayerProfile);

                 // Update player data to indicate ban
                 var request = new UpdateUserDataRequest
                 {
                     Data = new Dictionary<string, string>
                     {
                         { "Banned", "true" },
                         { "BanReason", banReasonJson },
                         { "BanEndTime", DateTime.UtcNow.AddHours(timehours).ToString("o") }, // Calculate ban end time
                         { "LastBanIpAddress", lastIpAddress } // Store the last login IP address
                     }
                 };

                 // Call PlayFab API to update user data
                 PlayFabClientAPI.UpdateUserData(request, result =>
                 {
                     Debug.Log("Player banned successfully.");
                     successCallback?.Invoke();
                 }, error =>
                 {
                     Debug.LogError("Failed to ban player: " + error.GenerateErrorReport());
                     failCallback?.Invoke(error);
                 });
             }
             else
             {
                 Debug.LogError("Failed to retrieve player profile to ban player.");
                 failCallback?.Invoke(null);
             }
         }, (error) =>
         {
             Debug.LogError("Failed to retrieve player profile to ban player: " + error.ErrorMessage);
             failCallback?.Invoke(error);
         });
     }

     // Retrieve player profile
     private static void GetPlayerProfile(string playerId, Action<GetPlayerProfileResult> successCallback, Action<PlayFabError> failCallback)
     {
         var request = new GetPlayerProfileRequest
         {
             PlayFabId = playerId,
             ProfileConstraints = new PlayerProfileViewConstraints
             {
                 ShowStatistics = true // Request to include player statistics
             }
         };

         PlayFabClientAPI.GetPlayerProfile(request, result =>
         {
             successCallback?.Invoke(result);
         }, error =>
         {
             failCallback?.Invoke(error);
         });
     }

     // Helper method to extract the last login IP address
     private static string GetLastLoginIpAddress(PlayerProfileModel playerProfile)
     {
         // Implement your logic here to extract the last login IP address from player profile
         // For example, you might need to loop through player statistics or retrieve it from another property
         // Return the last login IP address as a string
         return "not falling for that lol"; // Sample IP address for demonstration
     }
     // Other methods unchanged...
     public void GetLastLoginIPAddress(string playerId, Action<string> callback)
     {
         var request = new GetPlayerStatisticsRequest
         {
             //PlayFabId = playerId
         };

         PlayFabClientAPI.GetPlayerStatistics(request, result =>
         {
             // Search for the last login IP address statistic
             string lastIPAddress = FindLastIPAddress(result.Statistics);
             callback(lastIPAddress);
         }, error =>
         {
             Debug.LogError("Failed to retrieve player statistics: " + error.ErrorMessage);
             callback(null);
         });
     }

     // Helper method to find the last login IP address statistic
     private string FindLastIPAddress(List<StatisticValue> statistics)
     {
         foreach (var stat in statistics)
         {
             if (stat.StatisticName == "LastLoginIPAddress")
             {
                 return stat.Value.ToString();
             }
         }
         return "IP address not found"; // Return a default value if the statistic is not found
     }
 }

} `

Player Data
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Xiao Zha avatar image
Xiao Zha answered

Typically, your leveling logic should be on the server side (refer to https://learn.microsoft.com/en-us/gaming/playfab/features/automation/cloudscript-af/ ), and within your leveling logic you should validate player levels and prevent any player from exceeding the level cap. In this case, no player will exceed the level limit and no need to ban the player. May I ask where and how you implement the upgrade logic and under what circumstances will the player exceed the level cap?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.