Guest User

Untitled

a guest
Apr 26th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. require 'sqlite3'
  4.  
  5. @db = SQLite3::Database.new( "game.sqlite3" )
  6.  
  7. def get_current_counter
  8. current = @db.execute( 'select current from counter limit 1' )
  9. current.each { |c| @counter = c[0].to_i }
  10. end
  11.  
  12. def increment_counter
  13. @counter = @counter + 1
  14. @db.execute('update counter set current=' + @counter.to_s)
  15. end
  16.  
  17. def increment_thinker
  18. @next_think = Time.now.to_f + 0.1
  19. end
  20.  
  21. def think
  22. get_current_counter
  23. increment_counter
  24. increment_thinker
  25.  
  26. @go = @counter < 100
  27. end
  28.  
  29. def think_time?
  30. Time.now.to_f >= @next_think
  31. end
  32.  
  33. #########
  34.  
  35. increment_thinker
  36.  
  37. @go = true
  38.  
  39. while @go
  40. if think_time? then
  41. Thread.new do think end
  42. else
  43. sleep(@next_think - Time.now.to_f)
  44. end
  45. end
Add Comment
Please, Sign In to add comment