Skip to content

Instantly share code, notes, and snippets.

@McPolemic
Created July 24, 2020 19:09
Show Gist options
  • Save McPolemic/9616980fe7740ab64d8102804906e092 to your computer and use it in GitHub Desktop.
Save McPolemic/9616980fe7740ab64d8102804906e092 to your computer and use it in GitHub Desktop.
require 'io/console'
require 'game-client'
def readKey
c = ''
result = ''
$stdin.raw do |stdin|
c = stdin.getc
result << c
if c == "\e"
begin
while (c = Timeout::timeout(0.0001) { stdin.getc })
result << c
end
rescue Timeout::Error
# no action required
end
end
end
result
end
Position = Struct.new(:x, :y)
GameClient.configure do |config|
# Configure Bearer authorization: token
config.host = 'http://td-capture-the-flag.herokuapp.com' # https://example.com
config.access_token = 'adam@testdouble.com' # alice@example.com
end
api_instance = GameClient::GameApi.new
current_position = Position.new(x: 0, y: 0)
puts "Directions:"
puts "==========="
puts " k "
puts " hjl"
loop do
c = $stdin.getch
puts "Get #{c}"
direction = case c
when 'h'
'west'
when 'j'
'south'
when 'k'
'north'
when 'l'
'east'
end
result = api_instance.post_moves(direction)
pp result
player = result.player
current_position = Position.new(x: player.x, y: player.y)
rescue GameClient::ApiError
sleep 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment