Created
February 20, 2012 20:51
-
-
Save thomasleveil/1871341 to your computer and use it in GitHub Desktop.
B3 parser for modded iourt 4.1 game server for adding call vote and vote events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# ioUrT callvote Parser for BigBrotherBot(B3) (www.bigbrotherbot.net) | |
# Copyright (C) 2012 courgette@bigbrotherbot.net | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
# | |
# | |
# | |
__author__ = 'Courgette' | |
__version__ = '0.1' | |
from b3.parsers.iourt41 import Iourt41Parser | |
import re | |
import b3 | |
import b3.events | |
class Iourt41callvoteParser(Iourt41Parser): | |
gameName = 'iourt41callvote' | |
_lineFormats = ( | |
#Generated with ioUrbanTerror v4.1: | |
#Callvote: 0 nextmap ut4_swim | |
#Callvote: 0 reload | |
re.compile( | |
r'^(?P<action>Callvote):\s+(?P<data>(?P<cid>[0-9]+)\s+(?P<type>\w+)\s*(?:\s(?P<info>.+))?)$') | |
, | |
#Vote: 1 yes | |
#Vote: 0 no | |
re.compile(r'^(?P<action>Vote):\s+(?P<data>(?P<cid>[0-9]+)\s+(?P<choice>yes|no))$'), | |
) + Iourt41Parser._lineFormats | |
def startup(self): | |
# call parent startup method | |
Iourt41Parser.startup(self) | |
# add our specific events | |
self.Events.createEvent('EVT_CLIENT_CALLVOTE', 'Client call vote') | |
self.Events.createEvent('EVT_CLIENT_VOTE', 'Client vote') | |
def OnCallvote(self, action, data, match=None): | |
self.debug('Client call vote') | |
c = self.clients.getByCID(match.group('cid')) | |
return b3.events.Event(type=b3.events.EVT_CLIENT_CALLVOTE, client=c, | |
data={'type': match.group('type'), 'info': match.group('info')}) | |
def OnVote(self, action, data, match=None): | |
self.debug('Client vote') | |
c = self.clients.getByCID(match.group('cid')) | |
return b3.events.Event(type=b3.events.EVT_CLIENT_VOTE, client=c, data=match.group('choice')) | |
if __name__ == '__main__': | |
# test current parser | |
import unittest | |
import logging | |
from b3.config import XmlConfigParser | |
from b3.fake import FakeConsole, FakeClient | |
from b3.parsers.q3a.abstractParser import AbstractParser | |
class ParserTestCase(unittest.TestCase): | |
""" | |
Test case that is suitable for testing our parser specific features | |
""" | |
@classmethod | |
def setUpClass(cls): | |
# less logging | |
logging.getLogger('output').setLevel(logging.CRITICAL) | |
AbstractParser.__bases__ = (FakeConsole,) | |
# Now parser inheritance hierarchy is : | |
# Iourt41callvoteParser -> Iourt41Parser -> AbstractParser -> FakeConsole -> Parser | |
# so we do not need any read game server or mysql server to run our tests | |
def setUp(self): | |
# create a Iourt41callvoteParser parser | |
self.parser_conf = XmlConfigParser() | |
self.parser_conf.loadFromString(""" | |
<configuration> | |
</configuration> | |
""") | |
self.console = Iourt41callvoteParser(self.parser_conf) | |
self.console.startup() | |
# create a list of parser event to be fired so we can make sure they are fired | |
self.queued_events = [] | |
# replace the parser queueEvent method so it fills our event queue | |
def test_queueEvent(event, expire=10): | |
self.queued_events.append(event) | |
self.console.queueEvent = test_queueEvent | |
# more logging | |
logging.getLogger('output').setLevel(logging.NOTSET) | |
class Test_callvote(ParserTestCase): | |
def test_unknown_cid(self): | |
# empty the event queue | |
self.queued_events = [] | |
# make the parser read a game log line | |
self.console.parseLine("0:00 Callvote: 0 nextmap ut4_swim") | |
# verify that the parser did generate one event | |
self.assertEqual(1, len(self.queued_events)) | |
generated_event = self.queued_events[0] | |
# verify event correctness | |
self.assertEqual(b3.events.EVT_CLIENT_CALLVOTE, generated_event.type) | |
self.assertEqual({'type': 'nextmap', 'info': 'ut4_swim'}, generated_event.data) | |
self.assertEqual(None, generated_event.client) | |
def test_known_cid(self): | |
# create player Bill who is connected on slot 8 | |
bill = FakeClient(self.console, name="Bill", guid="ABABABBA013164654654", groupBits=1, team=-1) | |
bill.connects('8') | |
# empty the event queue | |
self.queued_events = [] | |
# make the parser read a game log line | |
self.console.parseLine("0:00 Callvote: 8 f00 bar") | |
# verify that the parser did generate one event | |
self.assertEqual(1, len(self.queued_events)) | |
generated_event = self.queued_events[0] | |
# verify event correctness | |
self.assertEqual(b3.events.EVT_CLIENT_CALLVOTE, generated_event.type) | |
self.assertEqual({'type': 'f00', 'info': 'bar'}, generated_event.data) | |
self.assertEqual(bill, generated_event.client) | |
def test_without_info(self): | |
# empty the event queue | |
self.queued_events = [] | |
# make the parser read a game log line | |
self.console.parseLine("0:00 Callvote: 12 f00 ") | |
# verify that the parser did generate one event | |
self.assertEqual(1, len(self.queued_events)) | |
generated_event = self.queued_events[0] | |
# verify event correctness | |
self.assertEqual(b3.events.EVT_CLIENT_CALLVOTE, generated_event.type) | |
self.assertEqual({'type': 'f00', 'info': None}, generated_event.data) | |
class Test_vote(ParserTestCase): | |
def test_unknown_cid(self): | |
# empty the event queue | |
self.queued_events = [] | |
# make the parser read a game log line | |
self.console.parseLine("0:00 Vote: 0 yes") | |
# verify that the parser did generate one event | |
self.assertEqual(1, len(self.queued_events)) | |
generated_event = self.queued_events[0] | |
# verify event correctness | |
self.assertEqual(b3.events.EVT_CLIENT_VOTE, generated_event.type) | |
self.assertEqual('yes', generated_event.data) | |
self.assertEqual(None, generated_event.client) | |
def test_known_cid(self): | |
# create player Bill who is connected on slot 8 | |
bill = FakeClient(self.console, name="Bill", guid="ABABABBA013164654654", groupBits=1, team=-1) | |
bill.connects('8') | |
# empty the event queue | |
self.queued_events = [] | |
# make the parser read a game log line | |
self.console.parseLine("0:00 Vote: 8 no") | |
# verify that the parser did generate one event | |
self.assertEqual(1, len(self.queued_events)) | |
generated_event = self.queued_events[0] | |
# verify event correctness | |
self.assertEqual(b3.events.EVT_CLIENT_VOTE, generated_event.type) | |
self.assertEqual('no', generated_event.data) | |
self.assertEqual(bill, generated_event.client) | |
# run all automated tests | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment