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

class dplib.server.ListenerType[source]

Bases: enum.Enum

An enumeration.

PERMANENT = 0
TRIGGER_ONCE = 1
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
class dplib.server.Server(hostname, port=27910, logfile=None, rcon_password=None)[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
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')
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
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

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_elim(killer_nick, killer_weapon, victim_nick, victim_weapon)[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_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_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)[source]

Execute a console command using RCON.

Parameters:command – Command
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)[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:
../_images/say_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)[source]

Main loop.

Parameters:
  • scan_old (bool) – Scan present logfile data
  • realtime (bool) – Wait for incoming logfile data
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_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_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
ELIM = 2
ELIM_TEAMS_FLAG = 9
ENTRANCE = 7
FLAG_CAPTURED = 8
MAPCHANGE = 4
NAMECHANGE = 6
RESPAWN = 3
ROUND_STARTED = 10
TEAM_SWITCHED = 11
TIMEOUT = 0

Module contents