[SCRIPT] Statistics in a flat-file db.
Posted: Mon Aug 05, 2013 12:31 pm
So, i just made a script, which stores all players, who ever visited your server, and their amount of kills. I know flat-file db is ugly, and it also may have tons of bugs. You can parse this file to your website to make a real-time top100,500,15,10,etc.
So, here is the script itself, report bugs in thread please(and yeah, you need to create empty file data.txt in your server folder manually):
upd. Oh guys sorry, script seems to work improperly :< Ill fix it soon
db.py
So, here is the script itself, report bugs in thread please(and yeah, you need to create empty file data.txt in your server folder manually):
upd. Oh guys sorry, script seems to work improperly :< Ill fix it soon
db.py
Code: Select all
from pyspades.constants import *
def apply_script(protocol, connection, config):
class DbConnection(connection):
def on_login(self, name):
name_exist = 0
lnum = 0
nick = name.split()
if len(nick) > 1:
nick = '_'.join(nick)
print nick
else:
nick = ''.join(nick)
with open('data.txt', 'r') as db:
for line in db:
lnum += 1
if lnum > 1:
namesplitted = line.split()
truename = namesplitted[0]
if truename == str(nick):
name_exist = 1
print str(name).upper() + ' logged in. Name exist is database.'
with open('data.txt', 'a') as db:
if not name_exist:
db.write('\n'+str(nick)+' 0')
print 'New player '+ str(nick).upper() + ' added to databse.'
connection.on_login(self,name)
def on_kill(self,killer,type, grenade):
if killer is not None and self.team is not killer.team and self != killer:
lnum2 = 0
knick = killer.name.split()
if len(knick) > 1:
knick = '_'.join(knick)
else:
knick = ''.join(knick)
if killer is not None and self.team is not killer.team and self != killer:
with open('data.txt', 'r') as db:
for line in db:
lnum2 += 1
if lnum2 > 1:
namesplitted = line.split()
nick = namesplitted[0]
kills = namesplitted[1]
i = int(kills)
if nick == str(nick):
del namesplitted[1]
i += 1
namesplitted.insert(1,str(i))
print namesplitted
x = ' '.join(namesplitted)
print x
break
with open('data.txt', 'r') as db:
old = db.read()
print knick+' '+kills
new = old.replace(knick+' '+kills, x)
print new
with open('data.txt', 'w') as db:
db.write(new)
connection.on_kill(self,killer,type,grenade)
return protocol, DbConnection