From 90a9b0fc3951d3746a023dda892e376401200b7a Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sun, 20 Nov 2022 10:49:00 +0100 Subject: [PATCH] Add interactive chat client example --- examples/09_interactive_terminal_client.rb | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 examples/09_interactive_terminal_client.rb diff --git a/examples/09_interactive_terminal_client.rb b/examples/09_interactive_terminal_client.rb new file mode 100755 index 0000000..d5d9202 --- /dev/null +++ b/examples/09_interactive_terminal_client.rb @@ -0,0 +1,67 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require_relative '../lib/teeworlds_client' + +args = { verbose: false, ip: nil, port: nil } + +ARGV.each do |arg| + if ['--help', '-h'].include?(arg) + puts 'usage: teeworlds.rb [OPTIONS..] [host] [port]' + puts 'options:' + puts ' --help|-h show this help' + puts ' --verbose|-v verbose output' + puts 'example:' + puts ' teeworlds.rb --verbose localhost 8303' + exit(0) + elsif ['--verbose', '-v'].include?(arg) + args[:verbose] = true + elsif args[:ip].nil? + args[:ip] = arg + elsif args[:port].nil? + args[:port] = arg.to_i + end +end + +args[:ip] = args[:ip] || '127.0.0.1' +args[:port] = args[:port] || 8303 + +client = TeeworldsClient.new(verbose: args[:verbose]) + +client.on_chat do |_, msg| + puts "[chat] #{msg}" +end + +client.on_client_info do |ctx| + puts "'#{ctx.data[:player].name}' joined the game" +end + +client.on_client_drop do |ctx| + unless ctx.data[:silent] + reason = ctx.data[:reason] ? " (#{ctx.data[:reason]})" : '' + puts "'#{ctx.data[:player].name}' left the game#{reason}" + end +end + +Signal.trap('INT') do + client.disconnect + exit +end + +# connect and detach thread +client.connect(args[:ip], args[:port], detach: true) + +def on_input(client, input) + case input + when ':quit' + return false + else + client.send_chat(input) + end + true +end + +loop do + input = $stdin.gets.chomp + break unless on_input(client, input) +end