Skip to content
This repository has been archived by the owner on Dec 26, 2018. It is now read-only.

Commit

Permalink
refactoring to read hitlocation and weapon code values from the B3 pa…
Browse files Browse the repository at this point in the history
…rser if it provides them
  • Loading branch information
thomasleveil committed Jul 14, 2013
1 parent 126bfe3 commit e17a25a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 90 deletions.
7 changes: 5 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ Changelog
- change default config file to .ini format (.xml format still works)
- support Urban Terror 4.2 new hitlocation values. See https://github.com/courgette/b3-plugin-headshotsurt/issues/1

1.1 - 14/07/2012 (Fenix)
- changed hitlocaton code for HL_HELMET to support the last UrT release (4.2.013)
1.1 - 14/07/2013 (Fenix)
- changed hitlocation code for HL_HELMET to support the last UrT release (4.2.013)

1.2 - 14/07/2013
- hitlocation and weapon code values are read from the B3 parser if available



Expand Down
65 changes: 45 additions & 20 deletions extplugins/headshotsurt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,70 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
#
__version__ = '1.1'
__version__ = '1.2'
__author__ = 'Courgette'


from b3.plugin import Plugin
from b3.events import EVT_CLIENT_KILL, EVT_GAME_EXIT
from b3 import TEAM_BLUE, TEAM_RED, TEAM_UNKNOWN

### kill modes constants ###
UT_MOD_BLED='23'
UT_MOD_HEGRENADE='25'


class HeadShotsStats:
headshots = 0
kills = 0

#--------------------------------------------------------------------------------------------------

class HeadshotsurtPlugin(Plugin):
_adminPlugin = None
_reset_headshots_stats = False
_min_level_headshots_cmd = 0
_clientvar_name = 'headshots_info'
_show_awards = False

def __init__(self, console, config=None):
Plugin.__init__(self, console, config)
if self.console.gameName not in ('iourt41', 'iourt42'):
self.critical("unsupported game : %s" % self.console.gameName)
raise SystemExit(220)

### kill modes constants ###
try:
self.UT_MOD_BLED = self.console.UT_MOD_BLED
except AttributeError, err:
self.warning("could not get UT_MOD_BLED value from B3 parser. %s" % err)
self.UT_MOD_BLED = '23'
self.debug("UT_MOD_BLED is %s" % self.UT_MOD_BLED)

try:
self.UT_MOD_HEGRENADE = self.console.UT_MOD_HEGRENADE
except AttributeError, err:
self.warning("could not get UT_MOD_HEGRENADE value from B3 parser. %s" % err)
self.UT_MOD_HEGRENADE = '25'
self.debug("UT_MOD_HEGRENADE is %s" % self.UT_MOD_HEGRENADE)

### hit location constants ###
try:
self.HL_HEAD = self.console.HL_HEAD
except AttributeError, err:
self.warning("could not get HL_HEAD value from B3 parser. %s" % err)
if self.console.gameName == 'iourt41':
self.HL_HEAD = '0'
elif self.console.gameName == 'iourt42':
self.HL_HEAD = '1'
self.debug("HL_HEAD is %s" % self.HL_HEAD)

try:
self.HL_HELMET = self.console.HL_HELMET
except AttributeError, err:
self.warning("could not get HL_HELMET value from B3 parser. %s" % err)
if self.console.gameName == 'iourt41':
self.HL_HELMET = '1'
elif self.console.gameName == 'iourt42':
self.HL_HELMET = '2'
self.debug("HL_HELMET is %s" % self.HL_HELMET)



def onLoadConfig(self):

try:
Expand All @@ -69,18 +106,6 @@ def onLoadConfig(self):


def onStartup(self):

### hit location constants ###
if self.console.gameName.startswith('iourt41'):
self.HL_HEAD = '0'
self.HL_HELMET = '1'
elif self.console.gameName.startswith('iourt42'):
self.HL_HEAD = '1'
self.HL_HELMET = '2'
else:
self.critical("unsupported game : %s" % self.console.gameName)
raise SystemExit(220)

self.registerEvent(EVT_CLIENT_KILL)
self.registerEvent(EVT_GAME_EXIT)

Expand Down Expand Up @@ -129,7 +154,7 @@ def handle_kills(self, event):

stats.kills += 1

if weapon not in (UT_MOD_BLED, UT_MOD_HEGRENADE) and self.is_headshot(hitlocation):
if weapon not in (self.UT_MOD_BLED, self.UT_MOD_HEGRENADE) and self.is_headshot(hitlocation):
stats.headshots += 1
self.show_message(client)

Expand Down
26 changes: 26 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import b3.output # do not remove, needed because the module alters some defaults of the logging module
import logging


class logging_disabled(object):
"""
context manager that temporarily disable logging.
USAGE:
with logging_disabled():
# do stuff
"""
DISABLED = False

def __init__(self):
self.nested = logging_disabled.DISABLED

def __enter__(self):
if not self.nested:
logging.getLogger('output').propagate = False
logging_disabled.DISABLED = True

def __exit__(self, exc_type, exc_val, exc_tb):
if not self.nested:
logging.getLogger('output').propagate = True
logging_disabled.DISABLED = False
69 changes: 31 additions & 38 deletions tests/iourt41/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
else:
import unittest
from mockito import when
from tests import logging_disabled
import logging
from b3 import TEAM_UNKNOWN
from b3.config import XmlConfigParser
Expand All @@ -21,50 +22,42 @@ class Iourt41TestCase(unittest.TestCase):

@classmethod
def setUpClass(cls):
# less logging
logging.getLogger('output').setLevel(logging.ERROR)

from b3.parsers.q3a.abstractParser import AbstractParser
from b3.fake import FakeConsole
AbstractParser.__bases__ = (FakeConsole,)
# Now parser inheritance hierarchy is :
# Iourt41Parser -> abstractParser -> FakeConsole -> Parser

with logging_disabled():
from b3.parsers.q3a.abstractParser import AbstractParser
from b3.fake import FakeConsole
AbstractParser.__bases__ = (FakeConsole,)
# Now parser inheritance hierarchy is :
# Iourt41Parser -> abstractParser -> FakeConsole -> Parser

def setUp(self):
# create a Iourt41 parser
self.parser_conf = XmlConfigParser()
self.parser_conf.loadFromString("""<configuration><settings name="server"><set name="game_log"></set></settings></configuration>""")
self.console = Iourt41Parser(self.parser_conf)
self.console.startup()

# load the admin plugin
self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.xml')
self.adminPlugin.onStartup()
with logging_disabled():
# create a Iourt41 parser
self.parser_conf = XmlConfigParser()
self.parser_conf.loadFromString("""<configuration><settings name="server"><set name="game_log"></set></settings></configuration>""")
self.console = Iourt41Parser(self.parser_conf)
self.console.startup()

# make sure the admin plugin obtained by other plugins is our admin plugin
def getPlugin(name):
if name == 'admin':
return self.adminPlugin
else:
return self.console.getPlugin(name)
self.console.getPlugin = getPlugin
# load the admin plugin
self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.xml')
self.adminPlugin.onStartup()

# when starting the PoweradminurtPlugin expects the game server to provide a few cvar values
when(self.console).getCvar('timelimit').thenReturn(Cvar('timelimit', value=20))
when(self.console).getCvar('g_maxGameClients').thenReturn(Cvar('g_maxGameClients', value=16))
when(self.console).getCvar('sv_maxclients').thenReturn(Cvar('sv_maxclients', value=16))
when(self.console).getCvar('sv_privateClients').thenReturn(Cvar('sv_privateClients', value=0))
when(self.console).getCvar('g_allowvote').thenReturn(Cvar('g_allowvote', value=0))
# make sure the admin plugin obtained by other plugins is our admin plugin
when(self.console).getPlugin('admin').thenReturn(self.adminPlugin)

# prepare a few players
self.joe = FakeClient(self.console, name="Joe", exactName="Joe", guid="zaerezarezar", groupBits=1, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.simon = FakeClient(self.console, name="Simon", exactName="Simon", guid="qsdfdsqfdsqf", groupBits=0, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.reg = FakeClient(self.console, name="Reg", exactName="Reg", guid="qsdfdsqfdsqf33", groupBits=4, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.moderator = FakeClient(self.console, name="Moderator", exactName="Moderator", guid="sdf455ezr", groupBits=8, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.admin = FakeClient(self.console, name="Level-40-Admin", exactName="Level-40-Admin", guid="875sasda", groupBits=16, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.superadmin = FakeClient(self.console, name="God", exactName="God", guid="f4qfer654r", groupBits=128, team=TEAM_UNKNOWN, teamId=0, squad=0)
# when starting the PoweradminurtPlugin expects the game server to provide a few cvar values
when(self.console).getCvar('timelimit').thenReturn(Cvar('timelimit', value=20))
when(self.console).getCvar('g_maxGameClients').thenReturn(Cvar('g_maxGameClients', value=16))
when(self.console).getCvar('sv_maxclients').thenReturn(Cvar('sv_maxclients', value=16))
when(self.console).getCvar('sv_privateClients').thenReturn(Cvar('sv_privateClients', value=0))
when(self.console).getCvar('g_allowvote').thenReturn(Cvar('g_allowvote', value=0))

# prepare a few players
self.joe = FakeClient(self.console, name="Joe", exactName="Joe", guid="zaerezarezar", groupBits=1, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.simon = FakeClient(self.console, name="Simon", exactName="Simon", guid="qsdfdsqfdsqf", groupBits=0, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.reg = FakeClient(self.console, name="Reg", exactName="Reg", guid="qsdfdsqfdsqf33", groupBits=4, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.moderator = FakeClient(self.console, name="Moderator", exactName="Moderator", guid="sdf455ezr", groupBits=8, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.admin = FakeClient(self.console, name="Level-40-Admin", exactName="Level-40-Admin", guid="875sasda", groupBits=16, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.superadmin = FakeClient(self.console, name="God", exactName="God", guid="f4qfer654r", groupBits=128, team=TEAM_UNKNOWN, teamId=0, squad=0)

def tearDown(self):
self.console.working = False
Expand Down
56 changes: 26 additions & 30 deletions tests/iourt42/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
else:
import unittest
import logging
from mockito import when
from tests import logging_disabled
from b3 import TEAM_UNKNOWN, __version__ as b3__version__
from b3.config import XmlConfigParser
from b3.fake import FakeClient
Expand All @@ -25,42 +27,36 @@ class Iourt42TestCase(unittest.TestCase):

@classmethod
def setUpClass(cls):
# less logging
logging.getLogger('output').setLevel(logging.ERROR)

from b3.parsers.q3a.abstractParser import AbstractParser
from b3.fake import FakeConsole
AbstractParser.__bases__ = (FakeConsole,)
# Now parser inheritance hierarchy is :
# Iourt41Parser -> abstractParser -> FakeConsole -> Parser
with logging_disabled():
from b3.parsers.q3a.abstractParser import AbstractParser
from b3.fake import FakeConsole
AbstractParser.__bases__ = (FakeConsole,)
# Now parser inheritance hierarchy is :
# Iourt41Parser -> abstractParser -> FakeConsole -> Parser


def setUp(self):
# create a Iourt41 parser
self.parser_conf = XmlConfigParser()
self.parser_conf.loadFromString("""<configuration><settings name="server"><set name="game_log"></set></settings></configuration>""")
self.console = Iourt42Parser(self.parser_conf)
self.console.startup()
with logging_disabled():
# create a Iourt41 parser
self.parser_conf = XmlConfigParser()
self.parser_conf.loadFromString("""<configuration><settings name="server"><set name="game_log"></set></settings></configuration>""")
self.console = Iourt42Parser(self.parser_conf)
self.console.startup()

# load the admin plugin
self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.xml')
self.adminPlugin.onStartup()
# load the admin plugin
self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.xml')
self.adminPlugin.onStartup()

# make sure the admin plugin obtained by other plugins is our admin plugin
def getPlugin(name):
if name == 'admin':
return self.adminPlugin
else:
return self.console.getPlugin(name)
self.console.getPlugin = getPlugin
# make sure the admin plugin obtained by other plugins is our admin plugin
when(self.console).getPlugin('admin').thenReturn(self.adminPlugin)

# prepare a few players
self.joe = FakeClient(self.console, name="Joe", exactName="Joe", guid="zaerezarezar", groupBits=1, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.simon = FakeClient(self.console, name="Simon", exactName="Simon", guid="qsdfdsqfdsqf", groupBits=0, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.reg = FakeClient(self.console, name="Reg", exactName="Reg", guid="qsdfdsqfdsqf33", groupBits=4, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.moderator = FakeClient(self.console, name="Moderator", exactName="Moderator", guid="sdf455ezr", groupBits=8, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.admin = FakeClient(self.console, name="Level-40-Admin", exactName="Level-40-Admin", guid="875sasda", groupBits=16, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.superadmin = FakeClient(self.console, name="God", exactName="God", guid="f4qfer654r", groupBits=128, team=TEAM_UNKNOWN, teamId=0, squad=0)
# prepare a few players
self.joe = FakeClient(self.console, name="Joe", exactName="Joe", guid="zaerezarezar", groupBits=1, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.simon = FakeClient(self.console, name="Simon", exactName="Simon", guid="qsdfdsqfdsqf", groupBits=0, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.reg = FakeClient(self.console, name="Reg", exactName="Reg", guid="qsdfdsqfdsqf33", groupBits=4, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.moderator = FakeClient(self.console, name="Moderator", exactName="Moderator", guid="sdf455ezr", groupBits=8, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.admin = FakeClient(self.console, name="Level-40-Admin", exactName="Level-40-Admin", guid="875sasda", groupBits=16, team=TEAM_UNKNOWN, teamId=0, squad=0)
self.superadmin = FakeClient(self.console, name="God", exactName="God", guid="f4qfer654r", groupBits=128, team=TEAM_UNKNOWN, teamId=0, squad=0)


def tearDown(self):
Expand Down
4 changes: 4 additions & 0 deletions tests/iourt42/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# -*- encoding: utf-8 -*-
import logging
from b3.config import CfgConfigParser
from headshotsurt import HeadshotsurtPlugin
from tests.iourt42 import Iourt42TestCase



class Test_plugin(Iourt42TestCase):
def setUp(self):
super(Test_plugin, self).setUp()
self.conf = CfgConfigParser()
logging.getLogger('output').setLevel(logging.DEBUG)
self.p = HeadshotsurtPlugin(self.console, self.conf)
self.p.onStartup()

Expand Down

0 comments on commit e17a25a

Please sign in to comment.