Created
July 24, 2020 19:09
-
-
Save McPolemic/9616980fe7740ab64d8102804906e092 to your computer and use it in GitHub Desktop.
This file contains 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 '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