Page 1 of 1

How to check if victim (who killed) wasn't AFK

PostPosted: Tue Feb 28, 2012 6:56 am
by Neka
Hello, I'm writing my custom addon. How can i check if were kill player, who was AFK, or not? In player_death() function/

Thanks!

PostPosted: Tue Feb 28, 2012 2:27 pm
by satoon101
What is this custom addon going to do?

Here is how we check for AFK in player_death to not allow leveling up:
../addons/eventscripts/gungame51/modules/gameevents.py wrote:
def player_death(event_var):
    '''Called every time a player dies'''

'''
   More code here
'
''


    # Don't continue if the victim is AFK
    if not int(gg_allow_afk_levels):

        # Make sure the victim is not a bot
        if not es.isbot(userid):

            # Is AFK ?
            if ggVictim.afk():

                # Is their weapon an hegrenade
                # and do we allow AFK leveling?
                if (ggAttacker.weapon == 'hegrenade' and
                  int(gg_allow_afk_levels_nade)):

                    # Pass if we are allowing AFK leveling on nade level
                    pass

                # Is their weapon a knife and do we allow AFK leveling?
                elif (ggAttacker.weapon == 'knife' and
                  int(gg_allow_afk_levels_knife)):

                    # Pass if we are allowing AFK leveling on knife level
                    pass

                # None of the above checks apply --- continue with hudhint
                else:

                    # Make sure the attacker is not a bot
                    if es.isbot(attacker):
                        return

                    # Tell the attacker they victim was AFK
                    ggAttacker.hudhint(
                        'PlayerAFK', {'player': event_var['es_username']})
                    return
The "return" statements are where it stops the player from leveling as it exits the player_death function.

gg_allow_afk_levels, gg_allow_afk_levels_nade, and gg_allow_afk_levels_knife each equals a es.ServerVar instance of the specific variable.

ggVictim and ggAttacker are Player instances (from gungame51.core.players.shortcuts) of the userid (or victim) and attacker respectively.

Satoon

Re: How to check if victim (who killed) wasn't AFK

PostPosted: Tue Feb 28, 2012 3:27 pm
by Neka
My addon keep knife statistic in sqlite database.
Total kills with knife, total death by knife and lose. "!kme" calls the popup with this stat. There is the code. I know, it ugliest, but i quite new into python.
# ../scripts/custom/gg_kme/gg_kme.py

# ==================================================
# IMPORTS
# ==================================================
# Python Imports
import sqlite3

# Eventscripts Imports
import es
import popuplib
from cmdlib import registerSayCommand
from cmdlib import unregisterSayCommand

# GunGame Imports
from gungame51.core import get_game_dir
from gungame51.core.players.shortcuts import Player
from gungame51.core.addons.shortcuts import AddonInfo

# ==================================================
# ADDON REGISTRATION/INFORMATION
# ==================================================
info = AddonInfo()
info.name = 'gg_kme'
info.title = 'GG Khife Statistic'
info.author = 'Neka'
info.version = '1.0'

# ==================================================
# LOAD / UNLOAD
# ==================================================
def load():
    # Delete the popup if it exists
    if popuplib.exists('ggKmeMenu'):
        popuplib.unsendname('ggKmeMenu', getUseridList('#human'))
        popuplib.delete('ggKmeMenu')

    # Let's create the "gungameKmeMenu" popup
    ggKmeMenu = popuplib.create('ggKmeMenu')

    # Create empty instance of the popup
    ggKmeMenu.addline('     Статистика ножей    ')
    ggKmeMenu.addline('                         ')
    ggKmeMenu.addline(' Зарезал(а):             ')
    ggKmeMenu.addline('  ---------------------  ')
    ggKmeMenu.addline(' Тебя зарезали :         ')
    ggKmeMenu.addline(' На тебе выиграли:       ')
    ggKmeMenu.prepuser = prep_kme_menu
    ggKmeMenu.timeout('send', 10)
    ggKmeMenu.timeout('view', 10)

    con = sqlite3.connect(get_game_dir('cfg/gungame51/database/gg_kme.db'))
    cur = con.cursor()
    cur.execute('CREATE TABLE IF NOT EXISTS gg_kme (' +
                'uniqueid VARCHAR(20),' +
                'total_kills INTEGER,' +
                'total_death INTEGER,' +
                'lose INTEGER,' +
                'PRIMARY KEY(uniqueid DESC)' +
                ')')
    cur.execute('PRAGMA auto_vacuum = 1')
    con.commit()
    con.close()
    registerSayCommand("!kme", kme_menu_cmd, "Displays knife statistic")

def unload():
    # Delete the popup if it exists
    if popuplib.exists('ggKmeMenu'):
        popuplib.unsendname('ggKmeMenu', getUseridList('#human'))
        popuplib.delete('ggKmeMenu')

    # Unregister commands
    unregisterSayCommand('!kme')

# ==================================================
# GAME EVENTS
# ==================================================

def player_death(event_var):
   
    attackerid = int(event_var['attacker'])
    victimid = int(event_var['userid'])

    # Make sure it wasn't a suicid  e
    if attackerid == 0:
        return

    # Make sure it wasn't a teamkill
    if event_var['es_attackerteam'] == event_var['es_userteam']:
        return

    # Make sure the player kill with a knife
    if event_var['weapon'] != 'knife':
        return

    attackerSid = es.getplayersteamid(attackerid)
    victimSid = es.getplayersteamid(victimid)

    if (attackerSid == 'BOT') or (victimSid == 'BOT'):
        return

    con = sqlite3.connect(get_game_dir('cfg/gungame51/database/gg_kme.db'))
    cur = con.cursor()

    # Killer update
    cur.execute('SELECT COUNT(*) FROM gg_kme WHERE uniqueid="' + str(attackerSid) + '"')

    if cur.fetchone()[0] > 0:
        cur.execute('UPDATE gg_kme SET total_kills=total_kills+1 WHERE uniqueid="' + str(attackerSid) + '"')
    else:
        cur.execute('INSERT INTO gg_kme(uniqueid, total_kills, total_death, lose) VALUES("' + str(attackerSid) + '", 1, 0, 0)')

    # Victim Update
    cur.execute('SELECT COUNT(*) FROM gg_kme WHERE uniqueid="' + str(victimSid) + '"')

    if cur.fetchone()[0] > 0:
        cur.execute('UPDATE gg_kme SET total_death=total_death+1 WHERE uniqueid="' + str(victimSid) + '"')
    else:
        cur.execute('INSERT INTO gg_kme(uniqueid, total_kills, total_death, lose) VALUES("' + str(victimSid) + '", 0, 1,  0)')
    con.commit()
    con.close()

def gg_win(ev):
   
    loserSid = es.getplayersteamid(ev['loser'])
   
    con = sqlite3.connect(get_game_dir('cfg/gungame51/database/gg_kme.db'))
    cur = con.cursor()

    cur.execute('UPDATE gg_kme SET lose=lose+1 WHERE uniqueid="' + str(loserSid) + '"')
   
    con.commit()
    con.close()

# ==================================================
# MENU COMMANDS
# ==================================================
def kme_menu_cmd(userid, args):
    # Make sure player exists
    if not es.exists('userid', userid) and userid != 0:
        return

    if len(args):
        return
    else:
        # Send menu
        popuplib.send('ggKmeMenu', userid)

def prep_kme_menu(userid, popupid):
    # Make sure the popup exists
    if not popuplib.exists('ggKmeMenu'):
        return

    usersteamid = es.getplayersteamid(userid)

    ggKmeMenu = popuplib.find('ggKmeMenu')
   
    con = sqlite3.connect(get_game_dir('cfg/gungame51/database/gg_kme.db'))
    cur = con.cursor()
   
    cur.execute('SELECT * FROM gg_kme WHERE uniqueid="' + usersteamid + '"')
    row = cur.fetchone()

    if len(row):
        total_kills = row[1]
        lotal_death = row[2]
        lose = row[3]
    else:
        lotal_kills = total_death = lose= 0

    ggKmeMenu.modline(3, ' Зарезал(а): %s раз' % total_kills)
    ggKmeMenu.modline(5, ' Тебя зарезали: %s раз' % total_death)
    ggKmeMenu.modline(6, ' На тебе выиграли: %s раз' % lose)

I started to write popup functionality, but it didn't work with non-latin chars, how to fix it?

Re: How to check if victim (who killed) wasn't AFK

PostPosted: Tue Feb 28, 2012 3:42 pm
by Neka
My addon keep knife statistic in sqlite database.
Total kills with knife, total death by knife and lose. "!kme" calls the popup with this stat. There is the code. I know, it ugliest, but i quite new into python.
I started to write popup functionality, but it didn't work with non-latin chars, how to fix it?
# ../scripts/custom/gg_kme/gg_kme.py

    # Create empty instance of the popup
    ggKmeMenu.addline('     Статистика ножей    ')
    ggKmeMenu.addline('                         ')
    ggKmeMenu.addline(' Зарезал(а):             ')
    ggKmeMenu.addline('  ---------------------  ')
    ggKmeMenu.addline(' Тебя зарезали :         ')
    ggKmeMenu.addline(' На тебе выиграли:       ')
    ggKmeMenu.prepuser = prep_kme_menu
    ggKmeMenu.timeout('send', 10)
    ggKmeMenu.timeout('view', 10)
 

And whole code of addon attached
gg_kme.py
(5.38 KiB) Downloaded 450 times

PostPosted: Tue Feb 28, 2012 3:43 pm
by satoon101
Ok, question then. Do you want it to store them based off of Knife Pro and/or Knife Level, or do you want it regardless of whether or not they leveled because of the knifing? If you want to store only when they achieve something, you can just use gg_levelup as the event and check the "reason" (to see if it is "steal") and the player's previous weapon (by getting their level, minus 1, and check to see if the level's weapon is "knife").

Satoon

PostPosted: Tue Feb 28, 2012 3:55 pm
by Neka
I want to store every knifing except AFK and BOT

PostPosted: Tue Feb 28, 2012 3:59 pm
by satoon101
Is that if the "attacker" is a Bot, the "victim" is a Bot, or both?

Satoon

Re: How to check if victim (who killed) wasn't AFK

PostPosted: Tue Feb 28, 2012 4:03 pm
by Neka
if (attackerSid == 'BOT') or (victimSid == 'BOT'):
        return

That is all in my code)) It was work fine, before i start to writing popups :)

Re: How to check if victim (who killed) wasn't AFK

PostPosted: Tue Feb 28, 2012 4:23 pm
by Neka
con = sqlite3.connect(get_game_dir('cfg/gungame51/database/gg_kme.db'))
    cur = con.cursor()
   
    cur.execute('SELECT * FROM gg_kme WHERE uniqueid="' + usersteamid + '"')
    row = cur.fetchone()

    if len(row):
        total_kills = row[1]
        lotal_death = row[2]
        lose = row[3]
    else:
        lotal_kills = 0
        total_death = 0
        lose= 0

    ggKmeMenu.modline(3, ' You knife: %s times' % total_kills)
    ggKmeMenu.modline(5, ' You knifed: %s times' % total_death)
    ggKmeMenu.modline(6, ' You lose: %s times' % lose)

File "../eventscripts/gungame51/scripts/custom/gg_kme/gg_kme.py", line 175, in prep_kme_menu
ggKmeMenu.modline(5, ' You knifed: %s times' % total_death)
UnboundLocalError: local variable 'total_death' referenced before assignment
# ================================================

Where i was wrong?

PostPosted: Tue Feb 28, 2012 4:30 pm
by satoon101
Ok, simple enough:
def player_death(event_var):
    # Is the weapon a knife?
    if event_var['weapon'] != 'knife':

        # If not, do nothing
        return

    # Store the victim's SteamID
    victim_steamid = event_var['es_steamid']

    # Store the attacker's SteamID
    attacker_steamid = event_var['es_attackersteamid']

    # Was the attacker or the victim a Bot?
    if 'BOT' in (victim_steamid, attacker_steamid):

        # If so, do nothing
        return

    # Store the attacker's userid
    attacker = event_var['attacker']

    # Store the victim's userid
    userid = event_var['userid']

    # Was this a suicide?
    if attacker in (userid, '0'):

        # If so, do nothing
        return

    # Was this a Team Kill?
    if event_var['es_userteam'] == event_var['es_attackerteam']:

        # If so, do nothing
        return

    # Get the victim's Player instance
    ggVictim = Player(userid)

    # Was the player AFK?
    if ggVictim.afk():

        # If so, do nothing
        return

    '''
        The rest of you code follows
    '
''
Satoon

Re: How to check if victim (who killed) wasn't AFK

PostPosted: Tue Feb 28, 2012 4:32 pm
by satoon101
Neka wrote:
    if len(row):
        total_kills = row[1]
        lotal_death = row[2]
        lose = row[3]
    else:
        lotal_kills = 0
        total_death = 0
        lose= 0

File "../eventscripts/gungame51/scripts/custom/gg_kme/gg_kme.py", line 175, in prep_kme_menu
ggKmeMenu.modline(5, ' You knifed: %s times' % total_death)
UnboundLocalError: local variable 'total_death' referenced before assignment
# ================================================

Where i was wrong?
You misspelled total 2 times.

Satoon

PostPosted: Tue Feb 28, 2012 4:57 pm
by Neka
Ha-ha, "l" very similar to "t". Thanks!

But what about non-latin symbols in addon? File is in utf8

PostPosted: Tue Feb 28, 2012 5:07 pm
by satoon101
Not sure. Have you tried using GunGame's built-in translation system? Use an .ini file within the addon's directory and assign the file by using:
# ==================================================
# ADDON REGISTRATION/INFORMATION
# ==================================================
info = AddonInfo()
info.name = 'gg_kme'
info.title = 'GG Khife Statistic'
info.author = 'Neka'
info.version = '1.0'
info.translations = ['gg_kme']
Satoon