Holy sh*t this lib is actually usable

This commit is contained in:
ChillerDragon 2022-11-05 11:07:16 +01:00
parent 7689d2725a
commit 771bdfe859
10 changed files with 92 additions and 25 deletions

View file

@ -16,13 +16,22 @@ require_relative 'lib/teeworlds-client'
client = TeeworldsClient.new(verbose: false)
client.on_chat do |msg|
puts "[chat] #{msg}"
if msg.message[0] == '!'
case msg.message[1..]
when 'ping' then client.send_chat('pong')
when 'whoami' then client.send_chat("You are: #{msg.author.name}")
when 'list' then client.send_chat(client.game_client.players.values.map(&:name).join(', '))
else client.send_chat('Unkown command! Commands: !ping, !whoami, !list')
end
end
end
# properly disconnect on ctrl+c
Signal.trap('INT') do
client.disconnect
end
# connect to localhost and block the current thread
client.connect('localhost', 8303, detach: false)
```

View file

@ -39,11 +39,28 @@ client.on_map_change do |context|
end
```
### @client [[TeeworldsClient](#teeworldsclient)]
Access the network client to send packets.
**Example:**
Reimplement your on on_connected logic and cancle the default one
```ruby
client.on_connected do |ctx|
ctx.client.send_msg_startinfo
ctx.cancle
end
```
### @data [Hash]
This hash holds all the current data. They keys might vary depending on the current context.
You can read and write those values. If you set an unused key the program will panic.
**Example:**
Here an example to see what keys you are given for a client info event.
```ruby

6
examples/01_chat_logger.rb Normal file → Executable file
View file

@ -1,6 +1,8 @@
#!/usr/bin/env ruby
# Print all incoming chat messages to stdout
require_relative 'lib/teeworlds-client'
require_relative '../lib/teeworlds-client'
client = TeeworldsClient.new(verbose: false)
@ -25,4 +27,4 @@ Signal.trap('INT') do
end
# connect to localhost and block the current thread
client.connect('localhost', 8303, detach: false)
client.connect('localhost', 8303, detach: false)

6
examples/02_packet_debugger.rb Normal file → Executable file
View file

@ -1,8 +1,10 @@
#!/usr/bin/env ruby
# Prints out all network traffic
require_relative 'lib/teeworlds-client'
require_relative '../lib/teeworlds-client'
client = TeeworldsClient.new(verbose: true)
# connect to localhost and block the current thread
client.connect('localhost', 8303, detach: false)
client.connect('localhost', 8303, detach: false)

6
examples/03_send_chat.rb Normal file → Executable file
View file

@ -1,6 +1,8 @@
#!/usr/bin/env ruby
# Chat spamming client
require_relative 'lib/teeworlds-client'
require_relative '../lib/teeworlds-client'
client = TeeworldsClient.new(verbose: true)
@ -11,4 +13,4 @@ loop do
# send a chat message every 5 seconds
sleep 5
client.send_chat('hello friends!')
end
end

6
examples/04_player_infos.rb Normal file → Executable file
View file

@ -1,6 +1,8 @@
#!/usr/bin/env ruby
# Set custom skin and other player infos
require_relative 'lib/teeworlds-client'
require_relative '../lib/teeworlds-client'
client = TeeworldsClient.new(verbose: true)
@ -30,4 +32,4 @@ client.set_startinfo(
color_eyes: 0)
# connect to localhost and block the current thread
client.connect('localhost', 8303, detach: false)
client.connect('localhost', 8303, detach: false)

30
examples/05_chatbot.rb Executable file
View file

@ -0,0 +1,30 @@
#!/usr/bin/env ruby
# Reply to ! prefixed commands in chat
#
# ruby ./examples/05_chatbot.rb
#
# Then connect to localhost and write !ping in the chat
require_relative '../lib/teeworlds-client'
client = TeeworldsClient.new(verbose: false)
client.on_chat do |msg|
if msg.message[0] == '!'
case msg.message[1..]
when 'ping' then client.send_chat('pong')
when 'whoami' then client.send_chat("You are: #{msg.author.name}")
when 'list' then client.send_chat(client.game_client.players.values.map(&:name).join(', '))
else client.send_chat('Unkown command! Commands: !ping, !whoami, !list')
end
end
end
# properly disconnect on ctrl+c
Signal.trap('INT') do
client.disconnect
end
# connect to localhost and block the current thread
client.connect('localhost', 8303, detach: false)

View file

@ -3,10 +3,11 @@ require_relative 'packer'
require_relative 'chat_message'
class Context
attr_reader :old_data
attr_reader :old_data, :client
attr_accessor :data
def initialize(keys = {})
def initialize(client, keys = {})
@client = client
@cancle = false
@old_data = keys
@data = keys
@ -56,6 +57,7 @@ class GameClient
# are currently ignored
context = Context.new(
@client,
player: player,
chunk: chunk
)
@ -75,11 +77,17 @@ class GameClient
end
def on_connected
context = Context.new(@client)
if @client.hooks[:connected]
@client.hooks[:connected].call(context)
context.verify
return if context.cancled?
end
@client.send_msg_startinfo
end
def on_map_change(chunk)
context = Context.new(chunk: chunk)
context = Context.new(@client, chunk: chunk)
if @client.hooks[:map_change]
@client.hooks[:map_change].call(context)
context.verify

View file

@ -15,7 +15,7 @@ require_relative 'player'
require_relative 'game_client'
class TeeworldsClient
attr_reader :state, :hooks
attr_reader :state, :hooks, :game_client
def initialize(options = {})
@verbose = options[:verbose] || false
@ -64,6 +64,10 @@ class TeeworldsClient
@hooks[:client_info] = block
end
def on_connected(&block)
@hooks[:connected] = block
end
def send_chat(str)
@netbase.send_packet(
NetChunk.create_vital_header({vital: true}, 4 + str.length) +
@ -348,9 +352,9 @@ class TeeworldsClient
if @ticks % 8 == 0
send_ctrl_keepalive
end
if @ticks % 20 == 0
send_chat("hello world")
end
# if @ticks % 20 == 0
# send_chat("hello world")
# end
end
def connection_loop

View file

@ -34,15 +34,6 @@ client.on_chat do |msg|
puts "[chat] #{msg}"
end
client.on_map_change do |ctx|
puts "GOT MAP CHANGOS"
end
client.on_client_info do |ctx|
puts "player info: #{ctx.data[:player]}"
ctx.data[:player].name = "yee"
end
Signal.trap('INT') do
client.disconnect
end