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
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.run()

Available event handlers:

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
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: