dplib package

Submodules

dplib.dplogin module

DPLogin - a module for managing DPLogin accounts.

class dplib.dplogin.DPLogin(username=None, password=None, pw_hash=None, pw_session_hash=None, session_id=None)[source]

Bases: object

A class that represents a DPLogin session.

Parameters
  • username

  • password

  • pw_hash

  • pw_session_hash

  • session_id

Raises

TypeError – Wrong password or hash

add_name(name)[source]

Add a name to the account.

Parameters

name – name to be deleted

Returns

HTTP response

Return type

str

cancel_invite(clanid, playerid)[source]

Cancel invite to clan for some player.

Parameters
  • clanid – id of a clan

  • playerid – id of a player

Returns

HTTP response

Return type

str

cancel_join_request(clanid)[source]

Cancel a clan join request.

Parameters

clanid – id of a clan

Returns

HTTP response

Return type

str

create_clan(name, tag)[source]

Create a new clan.

Parameters
  • name – Name of the new clan

  • tag – Tag of the new clan

Returns

HTTP response

Return type

str

del_name(name)[source]

Delete a name from the account.

Parameters

name – name to be deleted

Returns

HTTP response

Return type

str

get_clan_members(clanid)[source]

Get members of a clan with id of clanid.

Parameters

clanid – id of the clan

Returns

A list of dict objects with ids, names and ranks.

Return type

list

get_clans()[source]

Get clans of the user.

Returns

A list of dicts with clan info

Return type

list

get_profile_data()[source]

Get profile data.

Returns

dict({“field”: “value”})

Return type

dict

invite_member(clanid, playerid=None, name=None)[source]

Invite a member to a clan.

Parameters
  • clanid – id of the clan

  • playerid – id of the player to invite

  • name – name of the player to invite

Returns

HTTP response

Return type

str

join_clan(clan_id=None, clan_name=None)[source]

Join a clan.

Parameters
  • clan_id – id of a clan

  • clan_name – name of a clan

Returns

HTTP Response

Return type

str

kick_from_clan(clanid, playerid)[source]

Kick a player with id of playerid from a clan with id of clanid.

Parameters
  • clanid – id of a clan

  • playerid – id of a player

Returns

HTTP response

Return type

str

leave_clan(clan_id)[source]

Leave clan with clan_id. :param clan_id: id of the clan to leave

Returns

HTTP Response

Return type

str

leave_current_clan()[source]

Leave the current clan.

Returns

HTTP Response

Return type

str

make_leader(clanid, playerid)[source]

Make player with id of playerid a leader of a clan with clanid.

Parameters
  • clanid – id of a clan

  • playerid – id of a player

Returns

HTTP response

Return type

str

reject_join_request(clanid, playerid)[source]

Reject a clan join request.

Parameters
  • clanid – id of a clan

  • playerid – id of a player

Returns

HTTP response

Return type

str

remove_clan_leader(clanid, playerid)[source]

Remove a leader with id of playerid from a clan with id of clanid.

Parameters
  • clanid – id of a clan

  • playerid – id of a player

Returns

HTTP response

Return type

str

set_sessid(session_id)[source]

Set session id to session_id. Used to “fake” session_id, pretty useful for hijacking ;)

Parameters

session_id – session id to set

Returns

None

Return type

NoneType

update_profile(newpassword=None, email=None, realname=None, birthdate=None, location=None, displayemail=None, forumname=None, aim=None, icq=None, msn=None, yim=None, website=None, bio=None)[source]

Update DPLogin profile.

Parameters
  • newpassword

  • email

  • realname

  • birthdate

  • location

  • displayemail

  • forumname

  • aim

  • icq

  • msn

  • yim

  • website

  • bio

Returns

HTTP response

Return type

str

dplib.dplogin.get_new_password_hash(password, user_id)[source]

Hash the password at password change.

Parameters
  • password

  • user_id

Returns

Hashed password and user id

Return type

str

dplib.dplogin.get_password_hash(password, user_id, session_id)[source]

Hash plain password a few times. Used at log in when a plaintext password is specified. :param password: :param user_id: :param session_id:

Returns

Hashed password

Return type

str

dplib.dplogin.get_session_hash(pwhash, session_id)[source]

Hash password again, use session_id as a seed. Used at log in when no plaintext password is specified :param pwhash: :param session_id:

Returns

Session hash

Rtype str

dplib.dplogin.hex_md5(string)[source]

Hash a string using md5

Parameters

string – String to hash

Returns

MD5 hash of string

Return type

str

dplib.parse module

A module for parsing DP data

dplib.parse.decode_ingame_text(text)[source]

Removes special chars from ingame messages/nicks

Parameters

text – Text to decode

Returns

Decoded text

dplib.parse.escape_braces(string)[source]

Escapes braces, use for user-input in render_text()

Parameters

string – string to escape

Returns

escaped string

dplib.parse.render_text(text)[source]

Renders some text with formatting to a DP message. Replaces {C} with color char (ASCII 136), {U} with underline (ASCII 134) and {I} with italic (ASCII 135)

Parameters

text (str) – Text to render

Returns

DP message

Return type

str

dplib.server module

exception dplib.server.BadRconPasswordError[source]

Bases: Exception

class dplib.server.GameMode[source]

Bases: enum.Enum

An enumeration.

CTF = 'CTF'
DEATHMATCH = 'DM'
ELIMINATION = 'Elim'
KOTH = 'KOTH'
ONE_FLAG = '1Flag'
PONG = 'Pong'
SIEGE = 'Siege'
TDM = 'TDM'
class dplib.server.ListenerType[source]

Bases: enum.Enum

An enumeration.

PERMANENT = 0
TRIGGER_ONCE = 1
exception dplib.server.MapNotFoundError[source]

Bases: Exception

class dplib.server.Player(server, id, dplogin, nick, build)[source]

Bases: object

Player info from sv players command

Attributes
  • dplogin - dplogin.com account id, None when Player has no account

  • nick - nickname:

  • build - game build

  • server - an instance of Server

exception dplib.server.SecurityCheckError[source]

Bases: Exception

class dplib.server.Server(hostname, port=27910, logfile=None, rcon_password=None, pty_master=None, init_vars=True)[source]

Bases: object

Represents a DP:PB2 server

Parameters
  • hostname (str) – Server hostname, for example ‘127.0.0.1’

  • port (int) – Server port, default 27910

  • logfile – Path to logfile

  • rcon_password – rcon password

  • pty_master (int) – Master of the dp2 process (Linux only, useful only if you want to run the server from your Python script). Go to the getting started section for details.

  • init_vars – Send come commands used for security

cprint(message)[source]

Cprints a message.

Parameters

message – Text, can contain {C} - color char {U} - underline char {I} italic.

Remember to escape user input using :func:`dplib.parse.escape_brac

Returns

Rcon response

Return type

str

event(func)[source]

Decorator, used for event registration.

Parameters

func – function to register

Return type

builtin_function_or_method

Example

1
2
3
4
5
6
7
8
>>> from dplib.server import Server
>>> s = Server(hostname='127.0.0.1', port=27910, logfile=r'qconsole27910.log', rcon_password='hello')
>>> @s.event
... def on_chat(nick, message):
...     print((nick, message))
...
>>> s.run()
('mRokita', 'Hi')
gamemode(gamemode)[source]

Onround end, can be overridden using the Server.event() decorator.

Parameters

gamemode (str) – map’s gamemode

get_cvar(var)[source]

Gets cvar value

Parameters

var (str) – Variable name

Returns

Cvar value

Return type

str

get_event_handler(event_type)[source]
get_ingame_info(nick)[source]

Get ingame info about a player with nickname

Parameters

nick – Nick

Returns

An instance of Player

get_players()[source]

Gets playerlist.

Returns

List of Player instances

Return type

list

get_simple_playerlist()[source]

Get a list of player names

Returns

List of nicks

Return type

list

get_status()[source]

Gets server status

Example

1
2
3
>>> s = Server(hostname='127.0.0.1', port=27910, logfile=r'C:\Games\Paintball2\pball\qconsole27910.log', rcon_password='hello')
>>> s.get_status()
{'players': [{'score': '0', 'ping': '13', 'name': 'mRokita'}], 'sv_certificated': '1', 'mapname': 'beta/wobluda_fix', 'TimeLeft': '20:00', '_scores': 'Red:0 Blue:0 ', 'gamename': 'Digital Paint Paintball 2 v1.930(186)', 'gameversion': 'DPPB2 v1.930(186)', 'sv_login': '1', 'needpass': '0', 'gamedate': 'Aug 10 2015', 'protocol': '34', 'version': '2.00 x86 Aug 10 2015 Win32 RELEASE (41)', 'hostname': 'asdfgh', 'elim': 'airtime', 'fraglimit': '50', 'timelimit': '20', 'gamedir': 'pball', 'game': 'pball', 'maxclients': '8'}
Returns

status dict

Return type

dict

is_listening()[source]

Check if the main loop is running.

Return type

bool

kick(id=None, nick=None)[source]

Kicks a player with id using rcon.

Parameters
  • id – Player’s id

  • nick – Player’s nick

Returns

Rcon response

Return type

str

make_secure(timeout=10)[source]

This function fixes some compatibility and security issues on DP server side - Adds “mapchange” to sv_blockednames - Sets sl_logging to 1 All variables are set using the rcon protocol, use this function if you want to wait for the server to start.

Parameters

timeout – Timeout in seconds

new_map(map_name, gamemode=None)[source]

Changes the map using sv newmap <mapname> <gamemode> :param map_name: map name, without .bsp :param gamemode: Game mode :type gamemode: GameMode :return: Rcon response :raises MapNotFoundError: When map is not found on the server :rtype: str

nicks_valid(*nicks)[source]
on_chat(nick, message)[source]

On chat, can be overridden using the Server.event() decorator.

Parameters
  • nick (str) – Player’s nick.

  • message (str) – Message.

on_disconnect(nick)[source]

On disconnect, can be overridden using the :func:`.Server.event`decorator.

Parameters

nick (str) – Disconnected player’s nick

on_elim(killer_nick, killer_weapon, victim_nick, victim_weapon, suicide)[source]

On elim can be overridden using the Server.event() decorator.

Parameters
  • killer_nick (str) – Killer’s nick

  • killer_weapon (str) – Killer’s weapon

  • victim_nick (str) – Victim’s nick

  • victim_weapon (str) – Victim’s weapon

on_elim_teams_flag(team, nick, points)[source]

On scored points for possession of eliminated teams flag, can be overridden using the Server.event() decorator.

Parameters
  • team (str) – Player’s team.

  • nick (str) – Player’s nick.

  • points (int) – Points earned.

on_entrance(nick, build, addr)[source]

On entrance, can be overriden using the Server.event() decorator.

Parameters
  • nick (str) – Player’s nick

  • build (str) – Player’s game version (‘build 41’ for example

  • addr (str) – Player’s address, IP:PORT (‘127.0.0.1:23414’ for example)

on_flag_captured(team, nick, flag)[source]

On flag captured, can be overridden using the Server.event() decorator.

Parameters
  • team (str) – Player’s team.

  • nick (str) – Player’s nick.

  • flag (str) – Captured flag (Blue|Red|Yellow|Purple|White)

on_flag_drop(nick)[source]

On flag grab, can be overridden using the Server.event() decorator.

Parameters
  • nick (str) – Player’s nick

  • team (str) – Flag color (Blue|Red|Yellow|Purple)

on_flag_grab(nick, flag)[source]

On flag grab, can be overridden using the Server.event() decorator.

Parameters
  • nick (str) – Player’s nick

  • team (str) – Flag color (Blue|Red|Yellow|Purple)

on_game_end(score_blue, score_red, score_yellow, score_purple)[source]

On game end, can be overriden using the Server.event() decorator.

Parameters
  • score_blue – Blue’s score - None if there was no Blue team.

  • score_red – Red’s score - None if there was no Red team.

  • score_yellow – Yellow’s score - None if there was no Yellow team.

  • score_purple – Purple’s score - None if there was no Purple team.

on_mapchange(mapname)[source]

On mapcange, can be overridden using the Server.event() decorator.

Parameters

mapname (str) – Mapname

on_namechange(old_nick, new_nick)[source]

On name change, can be overridden using the Server.event() decorator.

Parameters
  • old_nick (str) – Old nick

  • new_nick (str) – Old nick

on_respawn(team, nick)[source]

On respawn, can be overridden using the Server.event() decorator.

Parameters
  • team (str) – Player’s team (Blue|Red|Yellow|Purple)

  • nick (str) – Player’s nick

on_round_end()[source]

Onround end, can be overridden using the Server.event() decorator.

on_round_started()[source]

On round started, can be overridden using the Server.event() decorator.

on_team_switched(nick, old_team, new_team)[source]

On team switched, can be overridden using the Server.event() decorator.

Parameters
  • nick (str) – Player’s nick

  • old_team (str) – Old team (Blue|Red|Yellow|Purple|Observer)

  • new_team (str) – New team (Blue|Red|Yellow|Purple|Observer)

permaban(ip=None)[source]

Bans IP address or range of adresses and saves ban list to disk.

Parameters

ip – IP address to ban

Returns

Rcon response

Return type

str

rcon(command, socket_timeout=3)[source]

Execute a console command using RCON.

Parameters
  • command – Command

  • socket_timeout – Timeout for the UDP socket.

Returns

Response from server

Return type

str

Example

1
2
3
4
>>> from dplib.server import Server
>>> s = Server(hostname='127.0.0.1', port=27910, logfile=r'qconsole27910.log', rcon_password='hello')
>>> s.rcon('sv listuserip')
'ÿÿÿÿprint\n mRokita [127.0.0.1:9419]\nadmin is listing IP for mRokita [127.0.0.1:9419]\n'
remove_permaban(ip=None)[source]

Removes ban on IP address and saves ban list to disk.

Parameters

ip – IP address to unban

Returns

Rcon response

Return type

str

remove_tempobans()[source]

Removes all temporary bans

Returns

Rcon response

Return type

str

run(scan_old=False, realtime=True, debug=False, make_secure=True)[source]

Runs the main loop using asyncio.

Parameters
  • scan_old (bool) – Scan present logfile data

  • realtime (bool) – Wait for incoming logfile data

say(message)[source]

Say a message

Parameters

message – Text, can contain {C} - color char {U} - underline char {I} italic.

Remember to escape user input using dplib.parse.escape_braces().

Return type

str

Returns

Rcon response

Example

1
2
3
4
5
>>> from dplib.server import Server
>>> s = Server(hostname='127.0.0.1', port=27910, logfile=r'qconsole27910.log', rcon_password='hello')
>>> s.say('{C}ARed text')
>>> s.say('{U}Underline{U}')
>>> s.say('{I}Italic{I}')
Ingame result

source\....docimagessay_test.png
set_cvar(var, value)[source]

Set a server cvar

Parameters
  • var – cvar name

  • value – value to set

Returns

Rcon response

Return type

str

start(scan_old=False, realtime=True, debug=False)[source]

Main loop.

Parameters
  • scan_old (bool) – Scan present logfile data

  • realtime (bool) – Wait for incoming logfile data

status()[source]

Execute status query.

Returns

Status string

Return type

str

stop_listening()[source]

Stop the main loop

tempoban(id=None, nick=None, duration=3)[source]

Temporarily bans a player with specified id using rcon

Parameters
  • id – Player’s id

  • nick – Player’s nick

  • duration – Ban duration in minutes (defaults to 3)

Returns

Rcon response

Return type

str

wait_for_elim(timeout=None, killer_nick=None, killer_weapon=None, victim_nick=None, victim_weapon=None, check=None)[source]

Waits for elimination event.

Parameters
  • timeout – Time to wait for elimination event, if exceeded, returns None.

  • killer_nick – Killer’s nick to match, ignored if None.

  • killer_weapon – Killer’s weapon to match, ignored if None.

  • victim_nick – Victim’s nick to match, ignored if None.

  • victim_weapon – Victim’s weapon to match, ignored if None.

  • check – Check function, ignored if None.

Returns

Returns message info dict keys: (‘killer_nick’, ‘killer_weapon’, ‘victim_nick’, ‘victim_weapon’)

Return type

dict

wait_for_elim_teams_flag(timeout=None, team=None, nick=None, points=None, check=None)[source]

Waits for elim teams flag event.

Parameters
  • timeout – Time to wait for event, if exceeded, returns None.

  • team – Player’s team.

  • nick – Player’s nick.

  • points (int) – Points scored.

  • check – Check function, ignored if none.

Returns

Returns message info dict keys: (‘team’, ‘nick’, ‘points’).

Return type

dict

wait_for_entrance(timeout=None, nick=None, build=None, addr=None, check=None)[source]

Waits for entrance.

Parameters
  • timeout – Time to wait for entrance event, if exceeded, returns None.

  • nick – Player’s nick.

  • build – Player’s build.

  • addr – Player’s address (IP:PORT)

Returns

wait_for_flag_captured(timeout=None, team=None, nick=None, flag=None, check=None)[source]

Waits for flag capture.

Parameters
  • timeout – Time to wait for event, if exceeded, returns None.

  • team – Player’s team.

  • nick – Player’s nick.

  • flag – Captured flag.

  • check – Check function, ignored if none.

Returns

Returns an empty dict.

Return type

dict

wait_for_flag_drop(timeout=None, nick=None, check=None)[source]

Waits for flag drop.

Parameters
  • timeout – Time to wait for event, if exceeded, returns None.

  • nick – Player’s nick.

  • flag – dropped flag.

  • check – Check function, ignored if none.

Returns

Returns an empty dict.

Return type

dict

wait_for_game_end(timeout=None, score_blue=None, score_red=None, score_yellow=None, score_purple=None, check=None)[source]

Waits for game end.

Parameters
  • timeout – Time to wait for event, if exceeded, returns None.

  • score_blue – Blue score

  • score_red – Red score.

  • score_yellow – Yellow score.

  • score_purple – Purple score.

  • check – Check function, ignored if none.

Returns

Returns an empty dict.

Return type

dict

wait_for_mapchange(timeout=None, mapname=None, check=None)[source]

Waits for mapchange.

Parameters
  • timeout – Time to wait for elimination event, if exceeded, returns None.

  • mapname – Killer’s nick to match, ignored if None.

  • check – Check function, ignored if None.

Returns

Returns message info dict keys: (‘killer_nick’, ‘killer_weapon’, ‘victim_nick’, ‘victim_weapon’)

Return type

dict

wait_for_message(timeout=None, nick=None, message=None, check=None)[source]

Waits for a message.

Parameters
  • timeout – Time to wait for message, if exceeded, returns None.

  • nick (str) – Player’s nick to match, ignored if None

  • message (str) – Message text to match, ignored if None

  • check – Check function, ignored if None

Returns

Returns message info dict keys: (‘nick’, ‘message’)

Return type

dict

Example

1
2
3
4
5
@s.event
def on_chat(nick, message):
    if message == '!start' and not elim_active:
        msg = yield from s.wait_for_message(check=lambda n, m: m.startswith('!hi '))
        s.say('Hi ' + msg['message'].split('!hi ')[1] + '!')
wait_for_namechange(timeout=None, old_nick=None, new_nick=None, check=None)[source]

Waits for mapchange.

Parameters
  • timeout – Time to wait for elimination event, if exceeded, returns None.

  • mapname – Killer’s nick to match, ignored if None.

  • check – Check function, ignored if None.

Returns

Returns message info dict keys: (‘killer_nick’, ‘killer_weapon’, ‘victim_nick’, ‘victim_weapon’)

Return type

dict

wait_for_respawn(timeout=None, team=None, nick=None, check=None)[source]

Waits for respawn event.

Parameters
  • timeout – Time to wait for respawn event, if exceeded, returns None.

  • team – Player’s team.

  • nick – Player’s nick.

  • check – Check function, ignored if none.

Returns

Returns message info dict keys: (‘team’, ‘nick’).

Return type

dict

wait_for_round_started(timeout=None, check=None)[source]

Waits for round start.

Parameters
  • timeout – Time to wait for event, if exceeded, returns None.

  • check – Check function, ignored if none.

Returns

Returns an empty dict.

Return type

dict

wait_for_team_switched(timeout=None, nick=None, old_team=None, new_team=None, check=None)[source]

Waits for team switch event.

Parameters
  • timeout – Time to wait for event, if exceeded, returns None.

  • old_team – Player’s old team.

  • new_team – Player’s new team.

  • nick – Player’s nick.

  • check – Check function, ignored if none.

Returns

Returns message info dict keys: (‘nick’, ‘old_team’, ‘new_nick’).

Return type

dict

class dplib.server.ServerEvent[source]

Bases: enum.Enum

An enumeration.

CHAT = 1
DATE = 5
DISCONNECT = 12
ELIM = 2
ELIM_TEAMS_FLAG = 9
ENTRANCE = 7
FLAG_CAPTURED = 8
FLAG_DROP = 14
FLAG_GRAB = 13
GAMEMODE = 16
GAME_END = 17
MAPCHANGE = 4
NAMECHANGE = 6
RESPAWN = 3
ROUND_END = 15
ROUND_STARTED = 10
TEAM_SWITCHED = 11
TIMEOUT = 0

Module contents