Search Unity

Change variable after 1 minute?

Discussion in 'Scripting' started by Cellenseres, Jan 7, 2017.

  1. Cellenseres

    Cellenseres

    Joined:
    Mar 30, 2015
    Posts:
    68
    Hey Guys,

    I want to create a simple login script with PHP and MySQL and everything is working fine.
    I'm able to Add new Users to Database.
    I'm able to login an Account. I'm unable to login with this account twice as long as "LastActivity" gets Updated at least every minute through the Game (which happens automatically every 10seconds if the Player has logged in). When the player lose the internet connection, the game crashed or was closed unexpectedly, "LastActivity" won't get Updated and after one minute, the player can login to it's account again.
    Code (CSharp):
    1.  
    2. if($row['LastActivity'] > $curr_timestamp){
    3.            echo "AlreadyOnline";
    4.          } else {
    5.            echo "LoginSuccess";
    6.            $setonlinesql = "UPDATE users SET LastActivity ='".$future_timestamp."' WHERE username = '".$username."' ";
    7.            $resultsetonline = mysqli_query($conn, $setonlinesql);
    8.          }
    This one works fine.

    But now, the Client (UnityGame) should stay online as long as it is connected to the Server (checks the connection every 10 seconds + updates the LastActivity).
    I want to change the variable "isonline" on Clientside, so I did this:


    Code (CSharp):
    1.         yield return www;
    2.  
    3.         switch (www.text)
    4.         {
    5.         case "LoginSuccess":
    6.             Debug.Log("You are online now, Variable 'isonline' set to '1'.");
    7.             Global.isonline = 1;
    8.             Global.logintime = DateTime.Now;
    9.             Global.logouttime = DateTime.Now.AddMinutes(1);
    10.             break;
    everytime the Game connects to the Server and updates the LastActivity, logintime and logouttime gets updated on Clientside.

    If the Game can't connect to the server the game should change the variable "Global.isonline" back to 0.

    here's the shortened script for this, but it doesn't work :(
    Code (CSharp):
    1.     void Update () {
    2.         if(Global.isonline == 1){
    3.             if(Global.logintime < Global.logouttime){
    4.                 Debug.Log("is online");
    5.             } else {
    6.                 Debug.Log("Logged out");
    7.                 Global.isonline = 0;
    8.             }
    9.         }
    10.     }
    Any Idea what I can do?