username, nickname, track, car, lap_num, sp1, sp2, sp3, time
# lap_csv.py
import plugin
import datetime
import os
CURRENT_DIR = os.path.dirname(__file__)
# uname, pname, track, car, lap_num, sp1, sp2, sp3, time
class LapInfo(object):
def __init__(self):
self.splits = [0,0,0]
self.last_split = 0
class LapCSV(plugin.Plugin):
def __init__(self):
plugin.Plugin.__init__(self)
self.bind(plugin.ISP_RST, self.race_start)
self.bind(plugin.ISP_TINY, self.tiny)
self.bind(plugin.ISP_SPX, self.split)
self.bind(plugin.ISP_LAP, self.lap)
self.bind(plugin.ISP_MSO, self.message_out)
def connected(self, host):
host.send(plugin.ISP_TINY, ReqI=255, SubT=plugin.TINY_RST)
def disconnected(self, host):
if hasattr(host, 'logging') and host.logging:
host.file.close()
host.logging = False
def create_name(self, track):
now = datetime.datetime.now()
return '%s_%s.csv' % (track, now.strftime('%d_%m_%Y_%H_%M_%S'))
def race_start(self, host, rst):
if hasattr(host, 'logging') and host.logging:
host.file.close()
host.logging = False
name = self.create_name(rst.Track)
path = os.path.join(CURRENT_DIR, name)
host.file = open(path, 'w')
host.logging = True
def tiny(self, host, tiny):
if tiny.SubT == plugin.TINY_REN:
if hasattr(host, 'logging') and host.logging:
host.file.close()
host.logging = False
def split(self, host, spx):
npl = host.players[spx.PLID]
if not hasattr(npl, 'lap_info'):
npl.lap_info = LapInfo()
npl.lap_info.splits[spx.Split-1] = (spx.STime - npl.lap_info.last_split)
npl.lap_info.last_split = spx.STime
def lap(self, host, lap):
if hasattr(host, 'logging') and host.logging:
npl = host.players[lap.PLID]
if not hasattr(npl, 'lap_info'):
return
ncn = host.conns[npl.UCID]
sp1 = npl.lap_info.splits[0]
sp2 = npl.lap_info.splits[1]
sp3 = lap.LTime - npl.lap_info.last_split
host.file.write('%s,%s,%s,%s,%d,%s,%s,%s,%s\n' % (ncn.UName,
ncn.PName,
host.state.Track,
npl.CName,
lap.LapsDone,
plugin.timestr(sp1),
plugin.timestr(sp2),
plugin.timestr(sp3),
plugin.timestr(lap.LTime),))
npl.lap_info = LapInfo()
def message_out(self, host, mso):
if mso.UserType == plugin.MSO_PREFIX:
args = mso.Msg[mso.TextStart:].split()
if args:
cmd = args[0].lower()
if cmd == '!start':
if hasattr(host, 'logging'):
host.logging = True
host.sendm('^3| ^7Started logging', mso.UCID)
elif cmd == '!stop':
if hasattr(host, 'logging'):
host.logging = False
host.sendm('^3| ^7Stopped logging', mso.UCID)
# !/usr/bin/env python
# swear_filter.py
import plugin
import re
# List of swearwords, in lower-case.
SWEAR_WORDS = ['boobs', 'bum', 'fart', 'knockers', 'poo']
MAX_WARNINGS = 3 # 0 for no warning.
SPLIT_REGEX = re.compile('\W+')
def get_swearwords(msg):
"""Return a list of swearwords in the message."""
words = SPLIT_REGEX.split(msg.lower())
return filter(lambda w: w in SWEAR_WORDS, words)
def contains_swearword(msg):
"""Return true if a message contains a swearword."""
words = SPLIT_REGEX.split(msg.lower())
for word in words:
if word in SWEAR_WORDS:
return True
return False
class SwearFilter(plugin.Plugin):
"""Plugin to handle a swear-filter."""
def __init__(self):
plugin.Plugin.__init__(self)
self.warnings = {} # Store warnings in a dict with the uname as the key.
self.bind(plugin.ISP_MSO, self.message_out)
self.bind(plugin.ISP_CNL, self.connection_left)
def message_out(self, host, mso):
"""Handle MSO message packet."""
if mso.UserType == plugin.MSO_USER:
msg = mso.Msg[mso.TextStart:]
if contains_swearword(msg):
ncn = host.conns[mso.UCID]
self.update_warnings(ncn)
self.check_warnings(host, ncn)
def connection_left(self, host, cnl):
"""Handle CNL packet, delete any warnings which exist for the user."""
ncn = host.conns(cnl.UCID)
if ncn.UName in self.warnings:
del self.warnings[ncn.UName]
def update_warnings(self, ncn):
"""Increment a user's warnings."""
if ncn.UName in self.warnings:
self.warnings[ncn.UName] += 1
else:
self.warnings[ncn.UName] = 1 # Add new warning to dict.
def check_warnings(self, host, ncn):
"""Check if the user has exceeded their warning quota, otherwise send warning messge."""
if self.warnings[ncn.UName] >= MAX_WARNINGS:
host.sendm('^7| %s ^3kicked for swearing' % ncn.PName)
host.sendm('/kick %s' % ncn.UName)
else:
warnings = self.warnings[ncn.UName]
host.sendm('^7| ^3Swear warning (%d of %d)' % (warnings, MAX_WARNINGS), ncn.UCID)
# hello.py
import plugin
class Hello(plugin.Plugin):
def __init__(self):
plugin.Plugin.__init__(self)
def connected(self, host):
host.send(plugin.ISP_MST, Msg='Hello, %s' % host.name)
# speeding.py
import plugin
SPEED_LIMIT = 80 # Kph
class Speeding(plugin.Plugin):
def __init__(self):
plugin.Plugin.__init__(self)
self.bind(plugin.ISP_MCI, self.car_update)
def car_update(self, host, mci):
for car in mci.Info:
if plugin.kph(car.Speed) > SPEED_LIMIT:
npl = host.players[car.PLID]
host.sendm('/spec %s' % npl.PName)
host.sendm('%s ^7spectated for speeding!' % npl.PName)
host.sendm('Do not go above %d Kph!' % SPEED_LIMIT, ucid=npl.UCID)
$this->sendPacket(new IS_MTC(Msg='Hello, world!', UCID=0));
isi->Prefix = ord('!');