[CSGO] Damage Print

Release your plugins here!
silviogreuel
Junior Member
Posts: 6
Joined: Sat Jun 20, 2015 5:21 pm

[CSGO] Damage Print

Postby silviogreuel » Wed Dec 28, 2016 11:07 pm

Prints received and given damages

Usage
Type in chat:
!dmg
or
.dmg

Install

Create a file in:
addons/source-python/plugins/damageprint/damageprint.py

Copy paste script to damageprint.py

Load plugin adding sp plugin load damageprint or type manualy on server console

Syntax: Select all

from commands.say import SayCommand
from players.helpers import playerinfo_from_index
from players.helpers import playerinfo_from_userid
from players.helpers import index_from_userid
from messages import SayText2
from events import Event

from cvars.public import PublicConVar
from cvars import ConVarFlags
from plugins.info import PluginInfo

info = PluginInfo()
info.name = "Damage print"
info.author = "Silvio Greuel <silviogreuel@gmail.com>"
info.version = "0.0.1"
info.basename = "damageprint"
info.variable = "{}_version".format(info.basename)
info.convar = PublicConVar(info.variable, info.version, "{} Version".format(info.name), ConVarFlags.NONE)

class DamageManager:
def __init__(self):
self.damages = dict()
self.hits = dict()
self.kills = dict()
self.lock = True

def add(self, victim, attacker, damage):
self._add_hit(victim, attacker)
self._add_dmg(victim, attacker, damage)

def _add_hit(self, victim, attacker):
if victim not in self.hits:
self.hits[victim] = dict()

if attacker not in self.hits:
self.hits[attacker] = dict()

if attacker not in self.hits[victim]:
self.hits[victim][attacker] = 0

if victim not in self.hits[attacker]:
self.hits[attacker][victim] = 0

self.hits[victim][attacker] += 1

def _add_dmg(self, victim, attacker, damage):
if victim not in self.damages:
self.damages[victim] = dict()

if attacker not in self.damages:
self.damages[attacker] = dict()

if attacker not in self.damages[victim]:
self.damages[victim][attacker] = 0

if victim not in self.damages[attacker]:
self.damages[attacker][victim] = 0

self.damages[victim][attacker] += damage


def kill(self, victim, attacker):
self.kills[victim] = attacker

def get_damages(self, player):
if player not in self.damages:
return

return self.damages[player]

def get_attacks(self, player):
attacks = dict()

for key in self.damages:
if player in self.damages[key]:
attacks[key] = self.damages[key][player]

return attacks

def get_killer(self, userid):
if userid not in self.kills:
return None
return self.kills[userid]

def clear(self):
self.damages = dict()
self.hits = dict()
self.kills = dict()
self.lock = True

def unlock(self):
self.lock = False


dm = DamageManager()
round_started = True

@SayCommand(['!cor'])
def color_command(command, playerindex, teamonly):
text = ' \x01a\x02a\x03a\x04a\x05a\x06a\x06a\x07a\x08a\x09a\x0Aa\x0Ba\x0Ca\x0Da\x0Ea\x0Fa\x10a'
message = SayText2(message = text)
message.index = 0
message.send(playerindex)
message.index = 1
message.send(playerindex)
message.index = 2
message.send(playerindex)
message.index = 3
message.send(playerindex)


@SayCommand(['!dmg', '.dmg'])
def dmg_print_command(command, playerindex, teamonly):
player = playerinfo_from_index(playerindex)
print(round_started)
if dm.lock:
return

player = player.userid
damages = dm.get_damages(player)
attacks = dm.get_attacks(player)
killer = dm.get_killer(player)
dmg_print(dm.hits, damages, attacks, killer, playerindex, player)


def dmg_print(hits, damages, attacks, killer, player, userid):
for attacker in damages:
attackerName = playerinfo_from_userid(attacker).name
attackerDmg = get_attacker_dmg(attacker, attacks)
attackerHits = hits[attacker][userid]

yourDmg = damages[attacker]
yourHits = hits[userid][attacker]

attackerKiller = dm.get_killer(attacker)
happyface = '\x04=D' if attackerKiller == userid else '\x10=|'
deadface = '\x0E=X' if killer == attacker else '\x02=('


text = ' {}\x01 -> {} dmg in {} -> {} -> {} dmg in {} -> {}'.format(happyface, attackerDmg, attackerHits, attackerName, yourDmg, yourHits, deadface)

tell(player, text)

def get_attacker_dmg(attacker, attacks):
if attacker in attacks:
return attacks[attacker]
return 0

def tell(player, text):
message = SayText2(message=text)
message.index = 2
message.send(player)

@Event('player_hurt')
def player_hurt(event):
victim = event.get_int('userid')
attacker = event.get_int('attacker')
damage = event.get_int('dmg_health')
dm.add(victim, attacker, damage)

@Event('player_death')
def player_death(event):
victim = event.get_int('userid')
attacker = event.get_int('attacker')
dm.kill(victim, attacker)


@Event('round_freeze_end')
def round_freeze_end(event):
round_started = True
dm.clear()

@Event('round_end')
def round_end(event):
print('round terminou')
dm.unlock()
Last edited by silviogreuel on Wed Dec 28, 2016 11:35 pm, edited 1 time in total.
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: [CSGO] Damage Print

Postby Ayuto » Wed Dec 28, 2016 11:14 pm

The index attribute of the SayText2 class has to be a player index and not a static integer. It's used for the color \3. Depending on the value the color changes:
  • index = 0: light green
  • index = index of a T player: red
  • index = index of a CT player: blue
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: [CSGO] Damage Print

Postby satoon101 » Wed Dec 28, 2016 11:38 pm

To add to Ayuto's post, some games also show the 'spectator' color if you use index -1.

Also, the first line of the plugin will cause an error, since you don't include an info.py here. Since you include the info stuff in the plugin itself, you can simply remove that line from above. Although, if you want to include the info.py separately, just zip up the contents with all files, and post that as an attachment instead of actually posting the code directly on the forums.

And, you are still mixing tabs and spaces. Your get_attacker_dmg function certainly does that. Which text editor are you using? Most of them, and all 'good' ones, have an option to replace tabs with a specified number of spaces. I would highly recommend looking into that. I personally use Pycharm almost exclusively now. But, I have also used Notepad2 (very basic and doesn't support working on projects), Notepad++, SublimeText 2 and 3, and Eclipse (with the PyDev plugin).
Image
silviogreuel
Junior Member
Posts: 6
Joined: Sat Jun 20, 2015 5:21 pm

Re: [CSGO] Damage Print

Postby silviogreuel » Thu Dec 29, 2016 1:24 am

I'm using vim+tmux,
The tab/spaces isue is because I'm developing/committing to github from my notebook(archlinux without a WM, and where my server lives).
To make a post, I copy/paste from github to notepad++ and edit it to make a one file plugin (where the isue starts)

Return to “Plugin Releases”

Who is online

Users browsing this forum: Google [Bot] and 21 guests