Getting Started¶
Introduction¶
DPLib is a Python library that makes you able to write scripts that react on some in-game events
Installation¶
git clone https://github.com/mRokita/DPLib.git
cd DPLib
python3 -m pip install DPLib # Only python 3 is supported
First steps¶
This code is a basic example of how to use DPLib. It uses all the currently available events. You should definitely play around with it. Simply edit the third line to match your server’s config and run it!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | # DPLib - Asynchronous bot framework for Digital Paint: Paintball 2 servers
# Copyright (C) 2017 Michał Rokita
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from dplib.server import Server
s = Server(hostname='127.0.0.1', port=27910, logfile=r'C:\Games\Paintball2\pball\qconsole27910.log', rcon_password='hello')
@s.event
def on_chat(nick, message):
print('Chat message. Nick: {0}, Message: {1}'.format(nick, message))
@s.event
def on_team_switched(nick, old_team, new_team):
print('Team switched. Nick: {0}, Old team: {1}, New team: {2}'.format(nick, old_team, new_team))
@s.event
def on_round_started():
print('Round started...')
@s.event
def on_elim(killer_nick, killer_weapon, victim_nick, victim_weapon):
print('Elimination. Killer\'s nick: {0}, Killer\'s weapon: {1}, Victim\'s nick: {2}, Victim\'s weapon: {3}'
.format(
killer_nick, killer_weapon, victim_nick, victim_weapon
))
@s.event
def on_respawn(team, nick):
print('Respawn. Nick: {0}, Team: {1}'.format(nick, team))
@s.event
def on_entrance(nick, build, addr):
print('Entrance. Nick: {0}, Build: {1}, Address: {2}'.format(
nick, build, addr
))
@s.event
def on_elim_teams_flag(team, nick, points):
print('Points for posession of eliminated teams flag. Team: {0}, Nick: {1}, Points: {2}'.format(
team, nick, points
))
@s.event
def on_flag_captured(team, nick, flag):
print('Flag captured. Team: {0}, Nick: {1}, Flag: {2}'.format(team, nick, flag))
@s.event
def on_game_end(score_blue, score_red, score_yellow, score_purple):
print('Game ended. Blue:{} Red:{} Yellow:{} Purple:{}'.format(
score_blue, score_red, score_yellow, score_purple
))
@s.event
def on_mapchange(mapname):
print('Map changed. Mapname: {}'.format(mapname))
@s.event
def on_namechange(old_nick, new_nick):
print('Name changed. Old nick: {} New nick: {}'.format(old_nick, new_nick))
print(s.get_status())
s.run()
|
Available event handlers¶
dplib.server.Server.on_elim()dplib.server.Server.on_entrance()dplib.server.Server.on_respawn()dplib.server.Server.on_elim_teams_flag()dplib.server.Server.on_team_switched()dplib.server.Server.on_round_started()dplib.server.Server.on_flag_captured()dplib.server.Server.on_message()dplib.server.Server.on_game_end()dplib.server.Server.on_mapchange()dplib.server.Server.on_namechange()
Waiting for future events¶
DPLib uses Python’s asyncio module so you can wait for incoming events without blocking the whole script.
Here’s a script that uses these ‘magic’ coroutines.
It’s a simple spawnkill protection system, it waits for 2 seconds after respawn for elimination event and when the newly respawned player gets killed within these 2 seconds, the spawnkiller gets a warning. After 4 spawnkills she/he gets kicked from the server.
Check out the 10th line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | # DPLib - Asynchronous bot framework for Digital Paint: Paintball 2 servers
# Copyright (C) 2017 Michał Rokita
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from time import time
from dplib.server import Server
s = Server(hostname='127.0.0.1', port=27910, logfile=r'C:\Games\Paintball2\pball\qconsole27910.log', rcon_password='hello')
spawnkills = dict()
spawnkill_last_times = dict()
@s.event
def on_respawn(team, nick):
kill = yield from s.wait_for_elim(victim_nick=nick, timeout=2)
if not kill:
return
if not kill['killer_nick'] in spawnkills or time()-spawnkill_last_times[kill['killer_nick']] > 10:
spawnkills[kill['killer_nick']] = 0
spawnkills[kill['killer_nick']] += 1
spawnkill_last_times[kill['killer_nick']] = time()
s.say('{C}9%s, {C}A{U}STOP SPAWNKILLING{U}' % kill['killer_nick'])
if spawnkills[kill['killer_nick']] > 3:
s.kick(nick=kill['killer_nick'])
s.run()
|
Available coroutines¶
dplib.server.Server.wait_for_elim()dplib.server.Server.wait_for_entrance()dplib.server.Server.wait_for_respawn()dplib.server.Server.wait_for_elim_teams_flag()dplib.server.Server.wait_for_team_switched()dplib.server.Server.wait_for_round_started()dplib.server.Server.wait_for_flag_captured()dplib.server.Server.wait_for_message()dplib.server.Server.wait_for_game_end()dplib.server.Server.wait_for_mapchange()dplib.server.Server.wait_for_namechange()
Examples¶
Map settings¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | # DPLib - Asynchronous bot framework for Digital Paint: Paintball 2 servers
# Copyright (C) 2017 Michał Rokita
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from asyncio import sleep
from dplib.server import Server
s = Server(hostname='127.0.0.1', port=27911, logfile=r'C:\Games\Paintball2\pball\qconsole27911.log', rcon_password='hello')
map_settings = {
'airtime': {
'command': 'set elim 10;set timelimit 10;',
'message': '{C}9Special settings for airtime {C}Aenabled'
},
'shazam33': {
'command': 'set elim 10;set timelimit 10;',
'message': '{C}9Special settings for shazam33 {C}Aenabled'
},
'default_settings': {
'command': 'set elim 20;set timelimit 20;',
'message': '{C}9No special settings for map {I}<mapname>{I}, using defaults'
}
}
@s.event
def on_mapchange(mapname):
if mapname not in map_settings:
settings = map_settings['default_settings']
else:
settings = map_settings[mapname]
command = settings.get('command', None)
message = settings.get('message', None)
if message:
message = mapname.join(message.split('<mapname>'))
if command:
for c in command.split(';'):
s.rcon(c)
if message:
yield from sleep(3)
s.say(message)
s.run()
|
Map elim script¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # DPLib - Asynchronous bot framework for Digital Paint: Paintball 2 servers
# Copyright (C) 2017 Michał Rokita
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from dplib.server import Server
s = Server(hostname='127.0.0.1', port=27910, logfile=r'C:\Games\Paintball2\pball\qconsole27910.log', rcon_password='hello')
elim_active = False
@s.event
def on_chat(nick, message):
global elim_active
if message == '!map elim' and not elim_active:
elim_active = True
maps = ['beta/wobluda_fix', 'beta/daylight_b1', 'airtime']
s.say('{C}AType \'!elim <mapname>\' to eliminate a map.')
while len(maps) > 1:
s.say('{C}9Available maps: ' + ', '.join(maps))
msg = yield from s.wait_for_message(check=lambda n, m: m.startswith('!elim '))
mapname = msg['message'].split('!elim ')[1]
if mapname not in maps:
s.say('{C}9Invalid map.')
s.say('{C}9Available maps: ' + ', '.join(maps))
else:
maps.remove(mapname)
s.rcon('sv newmap '+maps[0])
elim_active = False
s.run()
|
Spawnkill message¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # DPLib - Asynchronous bot framework for Digital Paint: Paintball 2 servers
# Copyright (C) 2017 Michał Rokita
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from dplib.server import Server
s = Server(hostname='127.0.0.1', port=27910, logfile=r'C:\Games\Paintball2\pball\qconsole27910.log', rcon_password='hello')
@s.event
def on_respawn(team, nick):
kill = yield from s.wait_for_elim(victim_nick=nick, timeout=2)
if kill:
print(s.get_ingame_info(kill['killer_nick']).dplogin)
s.say('{C}9%s, {C}A{U}STOP SPAWNKILLING{U}' % kill['killer_nick'])
s.run()
|
Streak¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # DPLib - Asynchronous bot framework for Digital Paint: Paintball 2 servers
# Copyright (C) 2017 Michał Rokita
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import asyncio
from dplib.server import Server
s = Server(hostname='127.0.0.1', port=27910, logfile=r'C:\Games\Paintball2\pball\qconsole27910.log', rcon_password='hello')
@asyncio.coroutine
def streak(killer_nick):
for i in range(1, 3):
print(killer_nick, i)
yield from s.wait_for_elim(killer_nick=killer_nick)
@s.event
def on_elim(killer_nick, killer_weapon, victim_nick, victim_weapon):
try:
yield from asyncio.wait_for(streak(killer_nick), timeout=20)
s.say('{U}%s ZABUJCA!{U}' % killer_nick)
except asyncio.TimeoutError:
print('Timeout for '+ killer_nick)
s.run()
|
Server manager¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # DPLib - Asynchronous bot framework for Digital Paint: Paintball 2 servers
# Copyright (C) 2017 Michał Rokita
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import asyncio
from dplib.server import Server
s = Server(hostname='127.0.0.1', port=27910, logfile=r'C:\Games\Paintball2\pball\qconsole27910.log', rcon_password='hello')
@asyncio.coroutine
def streak(killer_nick):
for i in range(1, 3):
print(killer_nick, i)
yield from s.wait_for_elim(killer_nick=killer_nick)
@s.event
def on_elim(killer_nick, killer_weapon, victim_nick, victim_weapon):
try:
yield from asyncio.wait_for(streak(killer_nick), timeout=20)
s.say('{U}%s ZABUJCA!{U}' % killer_nick)
except asyncio.TimeoutError:
print('Timeout for '+ killer_nick)
s.run()
|