Holy sh*t this lib is actually usable
This commit is contained in:
parent
7689d2725a
commit
771bdfe859
11
README.md
11
README.md
|
@ -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)
|
||||
```
|
||||
|
||||
|
|
17
docs/01.md
17
docs/01.md
|
@ -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
|
||||
|
|
4
examples/01_chat_logger.rb
Normal file → Executable file
4
examples/01_chat_logger.rb
Normal file → Executable 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)
|
||||
|
||||
|
|
4
examples/02_packet_debugger.rb
Normal file → Executable file
4
examples/02_packet_debugger.rb
Normal file → Executable file
|
@ -1,6 +1,8 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
# Prints out all network traffic
|
||||
|
||||
require_relative 'lib/teeworlds-client'
|
||||
require_relative '../lib/teeworlds-client'
|
||||
|
||||
client = TeeworldsClient.new(verbose: true)
|
||||
|
||||
|
|
4
examples/03_send_chat.rb
Normal file → Executable file
4
examples/03_send_chat.rb
Normal file → Executable 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)
|
||||
|
||||
|
|
4
examples/04_player_infos.rb
Normal file → Executable file
4
examples/04_player_infos.rb
Normal file → Executable 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
examples/05_chatbot.rb
Executable file
30
examples/05_chatbot.rb
Executable 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)
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue