fork download
  1. #coding:utf-8
  2. require 'em-websocket'
  3. require 'net/http'
  4. require 'uri'
  5. require 'json'
  6.  
  7. class BitcoinRPC
  8. def initialize(service_url)
  9. @uri = URI.parse(service_url)
  10. end
  11. def method_missing(name, *args)
  12. post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
  13. resp = JSON.parse( http_post_request(post_body) )
  14. resp['result']
  15. end
  16.  
  17. def http_post_request(post_body)
  18. http = Net::HTTP.new(@uri.host, @uri.port)
  19. request = Net::HTTP::Post.new(@uri.request_uri)
  20. request.basic_auth @uri.user, @uri.password
  21. request.content_type = 'application/json'
  22. request.body = post_body
  23. http.request(request).body
  24. end
  25.  
  26. class JSONRPCError < RuntimeError; end
  27. end
  28.  
  29. h = BitcoinRPC.new('http://***:***@***:***')
  30.  
  31. Process.daemon(nochdir=true) if ARGV[0] == "-D"
  32. connections = Array.new
  33.  
  34. $ws = nil
  35.  
  36. $lasts = (h.getbalance("stock")).to_s
  37.  
  38. nxt = rand(15..25)
  39.  
  40. thr = Thread.new do
  41. while $ws == nil
  42. sleep(1)
  43. end
  44.  
  45. while true
  46. sleep(nxt)
  47.  
  48. hash = Hash.new
  49.  
  50. nxt = rand(25..35)
  51.  
  52. win = $win
  53. $win = nil
  54.  
  55. if win == nil then
  56. s = "no one won ... new game"
  57. $ws.each { |con|
  58. con.send( s )
  59. }
  60. next
  61. end
  62.  
  63. s = "winner is ... " + win
  64.  
  65. $ws.each { |con|
  66. con.send( s )
  67. }
  68.  
  69. s = win + " get 0.01MONA .... new game"
  70.  
  71. h.sendfrom( "stock" , win , 0.01 )
  72.  
  73. $ws.each { |con|
  74. con.send( s )
  75. }
  76. end
  77. end
  78.  
  79. hash = Hash.new
  80.  
  81. EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 51234) do |ws|
  82. ws.onopen {
  83. ip = (Socket.unpack_sockaddr_in(ws.get_peername))[1]
  84. puts "user " + ip + " come"
  85. connections.push(ws) unless connections.index(ws)
  86.  
  87. $ws = connections
  88. }
  89. ws.onmessage { |msg|
  90. t = h.validateaddress( msg )
  91. ip = (Socket.unpack_sockaddr_in(ws.get_peername))[1]
  92. if t["isvalid"] == true && ( hash.has_key?(ip) == false || hash[ip] + 4 < Time.now.to_i ) then
  93. hash[ip] = Time.now.to_i
  94. $win = msg
  95.  
  96. ws.send( "current = " + $win )
  97. connections.each {|con|
  98. con.send( "current = " + $win ) unless con == ws
  99. }
  100. end
  101. }
  102. ws.onclose { puts "" }
  103. end
  104.  
Runtime error #stdin #stdout #stderr 0.04s 8336KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- em-websocket (LoadError)
	from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
	from prog.rb:2:in `<main>'