teeworlds_network/README.md

42 lines
1.1 KiB
Markdown
Raw Normal View History

# teeworlds_network
A teeworlds 0.7 client & server library written in ruby
2022-11-01 13:25:56 +00:00
2022-11-04 15:57:50 +00:00
## Sample
Here a simple sample usage of the library.
Connecting a client to localhost on port 8303.
2022-11-05 10:08:19 +00:00
Acting as a simple chat bot.
2022-11-04 15:57:50 +00:00
Also properly disconnect when the program is killed gracefully.
For more sample usages checkout the [examples/](examples/) folder.
2022-11-04 12:22:29 +00:00
2022-11-01 13:25:56 +00:00
```ruby
require_relative 'lib/teeworlds_client'
2022-11-01 13:25:56 +00:00
2022-11-04 15:26:24 +00:00
client = TeeworldsClient.new(verbose: false)
2022-11-01 13:25:56 +00:00
client.on_chat do |_, msg|
# note use `next` instead of `return` in the block
2022-11-05 15:40:00 +00:00
next unless 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')
2022-11-05 10:07:16 +00:00
end
2022-11-01 13:25:56 +00:00
end
2022-11-05 10:07:16 +00:00
# properly disconnect on ctrl+c
2022-11-04 12:07:34 +00:00
Signal.trap('INT') do
client.disconnect
end
2022-11-05 10:07:16 +00:00
# connect to localhost and block the current thread
2022-11-04 12:22:29 +00:00
client.connect('localhost', 8303, detach: false)
```
2022-11-04 15:57:50 +00:00
## Documentation
2022-11-04 12:22:29 +00:00
2022-11-13 12:31:52 +00:00
Checkout [docs/v0.0.1/READE.md](docs/v0.0.1/README.md) for a full library documentation.