Forums

OverviewFelgo 2 Support (Qt 5) › How to make a thing can only be happened once at a day ?

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #18961

    Yusuf Umut Piynar

    I want something to be happen only once per day, how can I do that ?

    And one more seperate question :

    How to dont let users choosing an already picked username in v-play game network ?

     

    #18966

    Alex
    Felgo Team

    Hi,

    we provide a daily bonus in our ChickenOutbreak 2 demo game. Basically anytime the user starts the app, we take the current day and check if the last stored day was a lower one. I just copy the code from the demo here:

        var today = new Date()
        var todayTruncated = new Date(today.getTime()-today.getHours()*3600000-today.getMinutes()*60000-today.getSeconds()*1000-today.getMilliseconds())
        console.debug("Today truncated is " + todayTruncated.getTime().toString())
        var storedLastLoginMS = settings.getValue("lastLogin")
        console.debug("Read from database " + storedLastLoginMS)
        if(!storedLastLoginMS) {
          settings.setValue("lastLogin",todayTruncated.getTime().toString())
          dailyBonus.giveBonus()
          console.debug("Set lastLogin to" + settings.getValue("lastLogin"))
        } else {
          if(todayTruncated.getTime().toString() > storedLastLoginMS) {
            console.debug("Last login was a day ago, give player bonus")
            settings.setValue("lastLogin",todayTruncated.getTime().toString())
            dailyBonus.giveBonus()
          }
        }

    You will also find other solutions for this online I guess, this example might lead you in the right direction.

     

    We do not filter for unique user names server side. So you would need to store and filter all usernames yourself, e.g. using our WebStorage or Firebase plugin.

     

    Cheers,
    Alex

    #18979

    Yusuf Umut Piynar

    Can I do this with timer ? Button will be enabled at every 1 day.

     Timer {
               id: hakTimer
               interval: 86400
               repeat: true
               running: true
               triggeredOnStart: false
               onTriggered: hak()
           }
        function hak(){
            if(cevaplaButton.enabled == true)
            {
                cevaplaButton.enabled = false;
            }else if(cevaplaButton.enabled == false){
                cevaplaButton.enabled = true;
            }
        }

     

    #18980

    Yusuf Umut Piynar

    If I can set/get the remaining interval from web maybe but anyway the code you commented which script is that from ?

    #18981

    Marcin

    Hi
    The Timer will only work if application will be running non stop.
    The moment somebody closes the application, it is over.

    Solution and ideas suggested by Alex is the best option in this situation, imho.
    You can check any JavaScript solution on the Internet, most of them will look similar.

    The demo is from: Chicken Outbreak 2

    #18982

    Yusuf Umut Piynar

    @Marcin I understand my ignorance now.I need to do this like how @alex is suggested.But Its too complex for me to understand it now.Im going to search online more.I tried to implement the script in CO2 to my app but I couldn’t make it work.

    #18983

    Yusuf Umut Piynar

    I tried something like that :

    WebStorage{
            id:myData
            perUserStore: true
        }
        function enterScene() {
            opacity = 1
            var today = new Date()
            var todayTruncated = new Date(today.getTime()-today.getHours()*3600000-today.getMinutes()*60000-today.getSeconds()*1000-today.getMilliseconds())
            console.debug("Today truncated is " + todayTruncated.getTime().toString())
            var storedLastLoginMS = myData.getValue("lastLogin")
            console.debug("Read from database " + storedLastLoginMS)
            if(!storedLastLoginMS) {
              myData.setValue("lastLogin",todayTruncated.getTime().toString())
    
              console.debug("Set lastLogin to" + settings.getValue("lastLogin"))
            } else {
              if(todayTruncated.getTime().toString() > storedLastLoginMS) {
                console.debug("Last login was a day ago, give player bonus")
                myData.setValue("lastLogin",todayTruncated.getTime().toString())
                cevaplaButton.enabled = true;
                cevaplaButton.color = "lightgrey";
              }
              if(todayTruncated.getTime().toString() < storedLastLoginMS){
                cevaplaButton.enabled = false;
                cevaplaButton.color = "grey";
                hakState.text = "Günde bir kez cevaplayabilirsiniz.."
              }
            }
            //lockTimer.restart()
          }
    
          // use a timer to lock the screen for a second, that users can not press buttons before the scene is loaded. E.g. when pressing back in store and it is pressed a few times, the press would also have affect on the twitter/facebook buttons.
         /* Timer {
            id: lockTimer
            interval: 500
            onTriggered: {
              lockArea.enabled = false
            }
          }*/

     

    #18984

    Yusuf Umut Piynar

    I changed it to this, it still doesnt work….

        function myFoo() {
        var lastCevap = myData.getValue("lastCevap");
        var currentDate = new Date().getTime();
        var nextDate = new Date();
        if(!lastCevap){
           nextDate = currentDate.setDate(currentDate.getDate() + 1);
           myData.setValue("lastCevap",currentDate); //Son cevap şuan alındı.
           myData.setValue("nextDate",nextDate);
        }else if(myData.getValue("nextDate") < currentDate){
            //Hedef zaman, şuanki zamandan az ise yani zaman dolduysa.
            cevaplaButton.enabled = true;
            cevaplaButton.color = "lightblue";
            currentDate = new Date().getTime();
            nextDate = currentDate.setDate(currentDate.getDate() + 1);
            myData.setValue("nextDate",nextDate);
            myData.setValue("lastCevap",currentDate);
        }else if(myData.getValue("nextDate") > currentDate){
            cevaplaButton.enabled = false;
            cevaplaButton.color = "grey";
    
        }

     

    #18985

    Yusuf Umut Piynar

    I ended with this which doesnt work too..

    WebStorage{
            id:myData
            perUserStore: true
         }
        function myFoo() {
        var lastCevap = myData.getValue("lastCevap");
        var currentDate = new Date().getTime();
        var nextDate = new Date();
        if(!lastCevap){
           nextDate = currentDate.setDate(currentDate.getDate() + 1);
           myData.setValue("lastCevap",currentDate.getTime()); //Son cevap şuan alındı.
           myData.setValue("nextDate",nextDate.getTime());
        }else if(nextDate.getTime() < currentDate.getTime()){
            //Hedef zaman, şuanki zamandan az ise yani zaman dolduysa.
            cevaplaButton.enabled = true;
            cevaplaButton.color = "lightblue";
            currentDate = new Date().getTime();
            nextDate = currentDate.setDate(currentDate.getDate() + 1);
            myData.setValue("nextDate",nextDate.getTime());
            myData.setValue("lastCevap",currentDate.getTime());
        }else if(nextDate.getTime() > currentDate.getTime()){
            cevaplaButton.enabled = false;
            cevaplaButton.color = "grey";
    
        }

    I really need help, I cant finish my project, that is the last step :((

    #18986

    Marcin

    Hi,
    What do you really compare in:

    }else if(nextDate.getTime() < currentDate.getTime()){

    First, looks like a type error.
    nextData is a Date object, so it’s fine, but the second one is an integer already(because you do this at the beginning of the function):

    var currentDate = new Date().getTime();

    How do you want to call getTime in the if, if variable currentDate is not an object anymore.
    Probably you wanted to set currentDate as just new Date() and not currentDate().getTime().

    This looks like an issue in types, but in general I don’t get the logic you do as well.

    In the if we are talking about, you set nextDate every time to new value, this will probably never work because you set both nextDate and currentDate to new values always:

    var currentDate = new Date().getTime();
    var nextDate = new Date();

    Don’t you want to read nextDate, when the  <code class=" language-qml"><span class="token string">lastCevap is set, from the saved value and not set the new one?
    Another thing is Date.setDate returns an time(integer), not an object.

    Please try this, I used internal Storage functionality with root.settings, but it is compatible with Storage and WebStorage, so use what you want:

    import QtQuick 2.2
    import Felgo 3.0
    
    GameWindow {
        id: root
    
        width: 320
        height: 570
        activeScene: scene
    
        Scene {
            id: scene
            width: 320
            height: 570
    
            function myFoo() {
                var nextGift = root.settings.getValue("nextGift");
                var currentDate = new Date()
                if(!nextGift){
                    //set new next gift from scratch
                    var nextGiftTime = currentDate.setDate(currentDate.getDate() + 1);
                    var nextGiftDate = new Date()
                    nextGiftDate.setTime(nextGiftTime)
                    root.settings.setValue("nextGift", nextGiftDate.getTime());
                    console.log('There was no next gift date saved, save new date to ' + nextGiftDate.toString())
                } else {
                    // next date is set, check it first and use it
                    var savedNextDate = new Date()
                    savedNextDate.setTime(root.settings.getValue('nextGift'))
                    console.log('Next gift date from saved value is ' + savedNextDate.toString())
                    if(savedNextDate.getTime() <= currentDate.getTime()){
                        var newNextTime = currentDate.setDate(currentDate.getDate() + 1);
                        var newNextGiftDate = new Date()
                        newNextGiftDate.setTime(newNextTime)
                        root.settings.setValue("nextGift", newNextDate.getTime());
                        console.log('Gift given! Set new next gift date to ' + newNextDate.toString())
                    }else if(savedNextDate.getTime() > currentDate.getTime()){
                        console.log('Gift NOT given! current date: ' + currentDate.toString()
                                    + ' is not older than saved next gift date:' + savedNextDate.toString())
                    }
                }
            }
        }
        Component.onCompleted: scene.myFoo()
    }

     

     

    #18988

    Yusuf Umut Piynar

    I will study your code, What my purpose is letting user to click my button once a day.

    #18989

    Yusuf Umut Piynar

    I changed your code to this :

     function myFoo() {
                 var nextGift = settings.getValue("nextGift");
                 var currentDate = new Date();
                 var nextGiftTime = new Date();
                 var newNextTime = new Date();
                 if(!nextGift){
                     //set new next gift from scratch
                     nextGiftTime = currentDate.setDate(currentDate.getDate() + 1);
                     var nextGiftDate = new Date();
                     nextGiftDate.setTime(nextGiftTime);
                     settings.setValue("nextGift", nextGiftDate.getTime());
                     console.log('There was no next gift date saved, save new date to ' + nextGiftDate.toString());
                     cevaplaButton.enabled = true;
                     cevaplaButton.color = "lightblue";
                 } else {
                     // next date is set, check it first and use it
                     var savedNextDate = new Date();
                     savedNextDate.setTime(settings.getValue('nextGift'));
                     console.log('Next gift date from saved value is ' + savedNextDate.toString());
                     if(savedNextDate.getTime() <= currentDate.getTime()){
                         newNextTime = currentDate.setDate(currentDate.getDate() + 1);
                         var newNextGiftDate = new Date();
                         newNextGiftDate.setTime(newNextTime);
                         settings.setValue("nextGift", newNextGiftDate.getTime());
                         cevaplaButton.enabled = false;
                         cevaplaButton.color = "grey";
                         hakState.text = "Günde bir kez cevap verebilirsiniz..";
                         console.log('Gift given! Set new next gift date to ' + newNextGiftDate.toString());
                     }else if(savedNextDate.getTime() > currentDate.getTime()){
                         console.log('Gift NOT given! current date: ' + currentDate.toString()
                                     + ' is not older than saved next gift date:' + savedNextDate.toString());
                         cevaplaButton.enabled = false;
                         cevaplaButton.color = "grey";
                         hakState.text = "Günde bir kez cevap verebilirsiniz..";
                     }

    Now its disabling the button after one click but i couldn’t test if it let user to click it again one day after.

    I changed the date in my phone to next day but it doesnt work.

    I changed some of your code because it was giving errors in console and function was not working.

    Thank you for your big help. @Marcin

    #18990

    Yusuf Umut Piynar
    #18997

    Yusuf Umut Piynar

    My code is working now, thanks for all helps.

    My last working code is that :

    function myFoo() {
    
                 var nextGift = settings.getValue("nextGift");
                 var currentDate = new Date();
                 var nextGiftTime = new Date();
                 var newNextTime = new Date();
                 if(!nextGift){
                     //set new next gift from scratch
                     nextGiftTime = currentDate.setMinutes(currentDate.getMinutes() + 1); //Bunu date den minutese geçirdim
                     var nextGiftDate = new Date();
                     nextGiftDate.setTime(nextGiftTime.getTime());
                     settings.setValue("nextGift", nextGiftDate.getTime());
                     console.log('There was no next gift date saved, save new date to ' + nextGiftDate.toString());
                     cevaplaButton.enabled = true;
                     cevaplaButton.color = "lightblue";
                 } else {
                     // next date is set, check it first and use it
                     var savedNextDate = new Date();
                     savedNextDate.setTime(settings.getValue('nextGift'));
                     console.log('Next gift date from saved value is ' + savedNextDate.toString());
                     if(savedNextDate.getTime() <= currentDate.getTime()){
                         newNextTime = currentDate.setMinutes(currentDate.getMinutes() + 1); //Bunu dateden minutese çevirdim
                         var newNextGiftDate = new Date();
                         newNextGiftDate.setTime(newNextTime);
                         settings.setValue("nextGift", newNextGiftDate.getTime());
                         cevaplaButton.enabled = true;
                         cevaplaButton.color = "lightblue";
                         hakState.text = "Cevapla aslanım";
                         console.log('Gift given! Set new next gift date to ' + newNextGiftDate.toString());
                     }else if(savedNextDate.getTime() > currentDate.getTime()){
                         console.log('Gift NOT given! current date: ' + currentDate.toString()
                                     + ' is not older than saved next gift date:' + savedNextDate.toString());
                         cevaplaButton.enabled = false;
                         cevaplaButton.color = "grey";
                         hakState.text = "Günde bir kez cevaplayabilirsiniz..";
                     }
                     //soruPage.title = "deneme";
                 }}

    Let me know if there is something silly about this script.

    #18999

    Marcin

    Hi,
    No problem, happy I could help.
    I don’t see anything wrong or silly with your code.

Viewing 15 posts - 1 through 15 (of 15 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support