Add on_client_drop()
This commit is contained in:
parent
8fe595b0a6
commit
db106ba70e
29
docs/01.md
29
docs/01.md
|
@ -125,6 +125,35 @@ end
|
||||||
client.connect('localhost', 8303, detach: true)
|
client.connect('localhost', 8303, detach: true)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### #on_client_drop(&block)
|
||||||
|
|
||||||
|
**Parameter: block [Block |[context](#context)|]**
|
||||||
|
|
||||||
|
Takes a block that will be called when the client receives a client drop packet.
|
||||||
|
|
||||||
|
Context:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
[
|
||||||
|
player: player_object,
|
||||||
|
chunk: raw_packet_data,
|
||||||
|
client_id: 0,
|
||||||
|
reason: '',
|
||||||
|
silent: false
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
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
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### connect(ip, port, options)
|
### connect(ip, port, options)
|
||||||
|
|
||||||
|
|
23
examples/06_join_leave_messages.rb
Executable file
23
examples/06_join_leave_messages.rb
Executable file
|
@ -0,0 +1,23 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require_relative '../lib/teeworlds-client'
|
||||||
|
|
||||||
|
client = TeeworldsClient.new
|
||||||
|
|
||||||
|
client.on_client_info do |ctx|
|
||||||
|
unless ctx.data[:silent]
|
||||||
|
reason = ctx.data[:reason] ? " (#{ctx.data[:reason]})" : ''
|
||||||
|
puts "'#{ctx.data[:player].name}' joined the game#{reason}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
client.on_client_drop do |ctx|
|
||||||
|
puts "'#{ctx.data[:player].name}' has left the game"
|
||||||
|
end
|
||||||
|
|
||||||
|
Signal.trap('INT') do
|
||||||
|
client.disconnect
|
||||||
|
end
|
||||||
|
|
||||||
|
# connect and detach thread
|
||||||
|
client.connect('localhost', 8303, detach: false)
|
|
@ -64,7 +64,29 @@ class GameClient
|
||||||
|
|
||||||
player = context.data[:player]
|
player = context.data[:player]
|
||||||
@players[player.id] = player
|
@players[player.id] = player
|
||||||
puts "'#{player.name}' joined the game"
|
end
|
||||||
|
|
||||||
|
def on_client_drop(chunk)
|
||||||
|
u = Unpacker.new(chunk.data[1..])
|
||||||
|
client_id = u.get_int()
|
||||||
|
reason = u.get_string()
|
||||||
|
silent = u.get_int()
|
||||||
|
|
||||||
|
context = Context.new(
|
||||||
|
@cliemt,
|
||||||
|
player: @players[client_id],
|
||||||
|
chunk: chunk,
|
||||||
|
client_id: client_id,
|
||||||
|
reason: reason == '' ? nil : reason,
|
||||||
|
silent: silent
|
||||||
|
)
|
||||||
|
if @client.hooks[:client_drop]
|
||||||
|
@client.hooks[:client_drop].call(context)
|
||||||
|
context.verify
|
||||||
|
return if context.cancled?
|
||||||
|
end
|
||||||
|
|
||||||
|
@players.delete(context.data[:client_id])
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_ready_to_enter(chunk)
|
def on_ready_to_enter(chunk)
|
||||||
|
|
|
@ -64,6 +64,10 @@ class TeeworldsClient
|
||||||
@hooks[:client_info] = block
|
@hooks[:client_info] = block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def on_client_drop(&block)
|
||||||
|
@hooks[:client_drop] = block
|
||||||
|
end
|
||||||
|
|
||||||
def on_connected(&block)
|
def on_connected(&block)
|
||||||
@hooks[:connected] = block
|
@hooks[:connected] = block
|
||||||
end
|
end
|
||||||
|
@ -265,6 +269,7 @@ class TeeworldsClient
|
||||||
case chunk.msg
|
case chunk.msg
|
||||||
when NETMSGTYPE_SV_READYTOENTER then @game_client.on_ready_to_enter(chunk)
|
when NETMSGTYPE_SV_READYTOENTER then @game_client.on_ready_to_enter(chunk)
|
||||||
when NETMSGTYPE_SV_CLIENTINFO then @game_client.on_client_info(chunk)
|
when NETMSGTYPE_SV_CLIENTINFO then @game_client.on_client_info(chunk)
|
||||||
|
when NETMSGTYPE_SV_CLIENTDROP then @game_client.on_client_drop(chunk)
|
||||||
when NETMSGTYPE_SV_EMOTICON then @game_client.on_emoticon(chunk)
|
when NETMSGTYPE_SV_EMOTICON then @game_client.on_emoticon(chunk)
|
||||||
when NETMSGTYPE_SV_CHAT then @game_client.on_chat(chunk)
|
when NETMSGTYPE_SV_CHAT then @game_client.on_chat(chunk)
|
||||||
else
|
else
|
||||||
|
|
11
sample.rb
11
sample.rb
|
@ -31,6 +31,17 @@ client.on_chat do |msg|
|
||||||
puts "[chat] #{msg}"
|
puts "[chat] #{msg}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
client.on_client_info do |ctx|
|
||||||
|
puts "'#{ctx.data[:player].name}' joined the game#{reason}"
|
||||||
|
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
|
Signal.trap('INT') do
|
||||||
client.disconnect
|
client.disconnect
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue