Jump to content


Photo

how to create a timer countdown


21 replies to this topic

#1 Kenshi

Kenshi

    Advanced Member

  • Members
  • PipPipPip
  • 334 posts

    Posted 20 February 2015 - 05:12 PM

    I know how to create a timer to go up, but what you do to create it to countdown from a given time? for example 10min countdown?

    #2 Iryoku

    Iryoku

      Advanced Member

    • Windower Staff
    • 488 posts

      Posted 20 February 2015 - 05:28 PM

      It's supposed to be timers c name 600 down icon but looking at the code it seems that it's not actually implemented. If you could open an issue for this so we actually remember to do it, it would be appreciated.



      #3 Kenshi

      Kenshi

        Advanced Member

      • Members
      • PipPipPip
      • 334 posts

        Posted 20 February 2015 - 05:37 PM

        I mean in lua not for timers plugin, and its implemented cause I use custom timers and it works.

        #4 Arcon

        Arcon

          Advanced Member

        • Windower Staff
        • 1189 posts
        • LocationMunich, Germany

        Posted 20 February 2015 - 06:47 PM

        As for Timers it's definitely not implemented and you can't do it. What you can do is change the settings entry "CustomLowToHigh", but it will be that for all custom timers created.

         

        Lua doesn't have timers, so I don't understand your question.



        #5 Kenshi

        Kenshi

          Advanced Member

        • Members
        • PipPipPip
        • 334 posts

          Posted 20 February 2015 - 07:15 PM

          well dunno about timers but I have rules in gearswap for mythic aftermath and they work. Anyway what I mean its create a text with a timer that counts down "10:00", "9:59" and so on, if this is possible within lua.

          #6 Arcon

          Arcon

            Advanced Member

          • Windower Staff
          • 1189 posts
          • LocationMunich, Germany

          Posted 20 February 2015 - 09:24 PM

          How do you make a timer counting up? It's simple to adjust from there.



          #7 Kenshi

          Kenshi

            Advanced Member

          • Members
          • PipPipPip
          • 334 posts

            Posted 21 February 2015 - 01:53 AM

            os.time() - os.time() should make the timer start from 00:00:00 and go up

            #8 Iryoku

            Iryoku

              Advanced Member

            • Windower Staff
            • 488 posts

              Posted 21 February 2015 - 04:47 AM

              Um, actually no. The semantics of the value returned by os.time are implementation defined. To get the difference between two times you need to use the os.difftime function.

              local start_time = os.time()
              local elapsed_time = os.difftime(os.time(), start_time)

              To get the time remaining until some future event you can do this.

              local end_time = os.time() + delay
              local time_remaining = os.difftime(end_time, os.time())

               
              Edit: This is what I get for replying right before bed.
               
              While the second snippet above will probably work, it is incorrect. The correct way to do it is to construct a table with the correct date and time of the future event and then pass that to os.time.



              #9 Arcon

              Arcon

                Advanced Member

              • Windower Staff
              • 1189 posts
              • LocationMunich, Germany

              Posted 21 February 2015 - 09:39 AM

              Honestly, the easiest way would be to use the Timers plugin for that. I'm currently implementing the feature to set the counting direction and will push an update later today. But if you want just a draggable text box you can use what Iryoku said to calculate the remaining time and update a text box with it (for example in the pre-render event). Let me know if you want a code example for that.



              #10 Kenshi

              Kenshi

                Advanced Member

              • Members
              • PipPipPip
              • 334 posts

                Posted 21 February 2015 - 06:02 PM

                well I'll be more specific cause I got to set a 5min clock but is not goin down. What I want to do is set a 5 min clock when something drops in the treasure, I think my problems is that I don't have an event to start the clock so it gets restarting the clock to 5min. Is there any event that I can use to start the clock when something drops in the treasure?

                #11 Arcon

                Arcon

                  Advanced Member

                • Windower Staff
                • 1189 posts
                • LocationMunich, Germany

                Posted 21 February 2015 - 11:33 PM

                That's actually a neat idea. I would love to display timers next to each item when you look into the treasure pool. For that to work I'd need to figure out how menus are being used, which I already looked into in the past. Alternatively one could make a treasure pool replacement that always shows items, current lot/lotter and time remaining, and so on.

                 

                You can use packets to find the right event. Every time a treasure pool packet comes in you can update the timer.



                #12 Kenshi

                Kenshi

                  Advanced Member

                • Members
                • PipPipPip
                • 334 posts

                  Posted 21 February 2015 - 11:48 PM

                  The idea was to do a treasure pool box. I already got the names and wanted to add a timer to have a idea how much time left in pool and was thinking in coloring them to green when more than 3 min, orange between 2min and 1min, and red when less than 1min. And adding lotters might be cool too.

                  #13 Arcon

                  Arcon

                    Advanced Member

                  • Windower Staff
                  • 1189 posts
                  • LocationMunich, Germany

                  Posted 22 February 2015 - 12:26 AM

                  Could make it so that when you hover over any item, it shows an ordered list of lotters and their lots :)



                  #14 Kenshi

                  Kenshi

                    Advanced Member

                  • Members
                  • PipPipPip
                  • 334 posts

                    Posted 22 February 2015 - 12:36 AM

                    I still need to figure out the time part, and I have no idea from where to start hehe.

                    #15 Arcon

                    Arcon

                      Advanced Member

                    • Windower Staff
                    • 1189 posts
                    • LocationMunich, Germany

                    Posted 22 February 2015 - 05:50 PM

                    require('maths')
                    texts = require('texts')
                     
                    t = texts.new('${hours||%.2u:}${minutes||%.2u:}${seconds||%.2u}${milliseconds||.%.1u}')
                     
                    goal = 0
                     
                    windower.register_event('prerender', function()
                        local diff = goal - os.clock()
                        if diff > 0 then
                            t.hours = (diff / 3600):floor()
                            t.minutes = ((diff / 60) % 60):floor()
                            t.seconds = (diff % 60):floor()
                            t.milliseconds = ((diff * 10) % 10):floor()
                            t:show()
                        elseif diff > -5 then
                            if diff:floor() % 2 == 1 then
                                t:color(153, 255, 102)
                            else
                                t:color(255, 255, 255)
                            end
                        else
                            t:hide()
                        end
                    end)
                     
                    windower.register_event('addon command', function(seconds)
                        goal = os.clock() + tonumber(seconds)
                    end)


                    #16 Kenshi

                    Kenshi

                      Advanced Member

                    • Members
                    • PipPipPip
                    • 334 posts

                      Posted 22 February 2015 - 07:28 PM

                      thx for the example but, how exactly do you add the clock when an item enter in a treasure index? I don't know how to check the packets, also will need a clock for each index.

                      #17 Arcon

                      Arcon

                        Advanced Member

                      • Windower Staff
                      • 1189 posts
                      • LocationMunich, Germany

                      Posted 22 February 2015 - 10:40 PM

                      require('maths')
                      texts = require('texts')
                      config = require('config')
                      packets = require('packets')
                      res = require('resources')
                       
                      settings = config.load()
                       
                      clock = texts.new(settings)
                       
                      for i = 0, 9 do
                          clock:appendline('${item' .. i .. '||%s - }${minutes' .. i .. '||%.2u:}${seconds' .. i .. '||%.2u}')
                      end
                       
                      clock:show()
                       
                      goals = {}
                       
                      windower.register_event('prerender', function()
                          for i = 0, 9 do
                              local diff = 300 - os.difftime(os.time(), packet.Timestamp)
                              if diff > 0 then
                                  clock['minutes' .. i] = ((diff / 60) % 60):floor()
                                  clock['seconds' .. i] = (diff % 60):floor()
                              else
                                  clock['minutes' .. i] = nil
                                  clock['seconds' .. i] = nil
                              end
                          end
                      end)
                       
                      windower.register_event('incoming chunk', function(id, data)
                          -- Ignore all non-drop packets
                          if id ~= 0x0D2 then
                              return
                          end
                       
                          local packet = packets.parse('incoming', data)
                       
                          -- Ignore gil drop
                          if packet.Item == 0xFFFF then
                              return
                          end
                       
                          goals[packet.Index] = packet.Timestamp + 300
                          clock['item' .. packet.Index] = res.items[packet.Item].name
                      end)


                      #18 Kenshi

                      Kenshi

                        Advanced Member

                      • Members
                      • PipPipPip
                      • 334 posts

                        Posted 22 February 2015 - 10:57 PM

                        well this is beyond my knowledge and trying to test but I get a error on line 21 attemp to index global 'packet' (a nil value). An explanation of whats happening would be apreciated :)

                        #19 Arcon

                        Arcon

                          Advanced Member

                        • Windower Staff
                        • 1189 posts
                        • LocationMunich, Germany

                        Posted 23 February 2015 - 06:42 AM

                        Too lazy :)
                         
                        Change line 21 to:
                               local diff = os.difftime(goals[i], os.time())


                        #20 Kenshi

                        Kenshi

                          Advanced Member

                        • Members
                        • PipPipPip
                        • 334 posts

                          Posted 23 February 2015 - 01:57 PM

                          I getting now a bad argument #1 to 'difftime' (number expected, got nil)




                          1 user(s) are reading this topic

                          0 members, 1 guests, 0 anonymous users