Created
          October 8, 2014 21:18 
        
      - 
      
 - 
        
Save alyssais/2948cc90ae1298592b1e to your computer and use it in GitHub Desktop.  
    HTTP Plays Pokémon!
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | require 'java' | |
| require 'sinatra' | |
| def keycode(key) | |
| name = "VK_#{key.upcase}" | |
| java.awt.event.KeyEvent.const_get(name) | |
| end | |
| robot = java.awt.Robot.new | |
| BUTTONS = %w[ | |
| up down left right | |
| a b start select | |
| ] | |
| BUTTONS.each do |button| | |
| post "/api/v1/moves/#{button}" do | |
| code = keycode case button | |
| when :select then ?q | |
| when :start then 'enter' | |
| when :a then ?s | |
| when :b then ?a | |
| else button | |
| end | |
| robot.keyPress(code) | |
| sleep 0.1 | |
| robot.keyRelease(code) | |
| end | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment