Page 1 of 2

[SCRIPT] Player Logger

Posted: Tue Nov 12, 2013 2:03 am
by gamemaster77
This script will log every logon and disconnect to the server to find who is on between a time range or at a specific time. Useful for build servers or any other servers where someone did something wrong at a certain time and you need to find who was online. The command /findusers will return all of the players online paired with their ips during the interval you specify. The logs are also saved at server exit and loaded at server start. You must CTRL-C exit the server for the log to save at the end, however. I might also update it during the future to include who a player is logged in as if they're logged in (only at a specific point though. It cant work during intervals because people can login, leave, come back and login multiple times during a range)
Code: Select all
#-------------------------------------------------------------------------------
# Name:        Player Logger
# Purpose:     Logs when players are on and off the server. Can find what
#              players were on the server during a specific time interval by
#              using /findusers timeinterval1 timeinterval2 day month year
#              Each time interval should be in the form hour:minute:second
#              If the intervals are the same it finds the users logged on at
#              that point in time. If day, month, or year is left out it will
#              use the current day, month, or year at the time the command is
#              used. Logs are saved on disconnect to the file specified at the
#              beginning of this script.
#
#              WARNING: YOU MUST CTRL-C EXIT THE SERVER FOR THE LOG TO SAVE. IT
#              WILL NOT SAVE IF YOU JUST EXIT THE WINDOW
#
# Author:      Gamemaster77
#-------------------------------------------------------------------------------
Here's an example (defaults to current, day, month, and year):
/findusers 10:00:00 10:10:00
(jacksmack => 45.83.74.140) , (dan => 123.56.11.100) were logged on

Re: [SCRIPT] Player Logger

Posted: Tue Nov 12, 2013 6:37 am
by Bux Xray
Game...nice
do you use it at Build4yourlife?

Re: [SCRIPT] Player Logger

Posted: Wed Nov 13, 2013 10:12 am
by learn_more
maybe a simple database would be better for storing this stuff?

Re: [SCRIPT] Player Logger

Posted: Thu Nov 14, 2013 4:42 am
by Kuma
I think search by name and ip should be added.

Re: [SCRIPT] Player Logger

Posted: Thu Nov 14, 2013 4:46 am
by gamemaster77
learn_more wrote:
maybe a simple database would be better for storing this stuff?
Well I tried looking into shelve to write these to a file as they go along. It said that it is sometimes tricky to use with mutable python objects, and I store them in one big nested dictionary right now. So I just figured I'd just save it at the end because I haven't done much with other databases.
Kuma wrote:
I think search by name and ip should be added.
I should be able to add that pretty easily. It cycles through names and ips already when it searches.

Re: [SCRIPT] Player Logger

Posted: Sat Nov 16, 2013 8:03 am
by thepolm3
For a *simple* file database
Code: Select all
def save_database(di):
    f=open("file.txt","w")
    f.write(str(di) )
    f.close() 

def load_database():
    f=open("file.txt","r")
    di=f.read() 
    F.close()
    try:
        return eval(di)
    except Exception(error):
        print error

Re: [SCRIPT] Player Logger

Posted: Sat Nov 16, 2013 1:30 pm
by learn_more
bad idea to use eval on user input (usernames = user input)

Re: [SCRIPT] Player Logger

Posted: Sat Nov 16, 2013 9:22 pm
by thepolm3
He said it is in dictionary format; what I am suggesting is opening a dictionary from a file and then change it, then write it back to the file. Villa, stored dictionary. At no point do you Eval any usernames

Re: [SCRIPT] Player Logger

Posted: Mon Nov 18, 2013 11:29 pm
by learn_more
>>return eval(di)

Re: [SCRIPT] Player Logger

Posted: Tue Nov 19, 2013 6:30 pm
by thepolm3
di should be a string of a dictionary

Re: [SCRIPT] Player Logger

Posted: Thu Nov 21, 2013 3:14 pm
by learn_more

Re: [SCRIPT] Player Logger

Posted: Mon Nov 25, 2013 6:39 pm
by rakiru
I'd suggest SQLite or similar too, but if you want to just save the dict to a flat file and load it again, use json or pickle, definitely not eval.

Re: [SCRIPT] Player Logger

Posted: Tue Nov 26, 2013 6:31 am
by thepolm3
*simple*
It's obviously not the best method at all. I'd agree that using sql or json would be better

Re: [SCRIPT] Player Logger

Posted: Tue Nov 26, 2013 11:15 am
by learn_more
thepolm3 wrote:
*simple*
It's obviously not the best method at all. I'd agree that using sql or json would be better
in this case i would prefer not over the simple method.

Re: [SCRIPT] Player Logger

Posted: Mon Dec 02, 2013 7:44 pm
by rakiru
json/pickle would be just as simple though:
Code: Select all
import json

def save_database(db):
    with open("db.json", "w") as fp:
        json.dump(db, fp)

def load_database():
    with open("db.json", "r") as fp:
        return json.load(fp)
Code: Select all
import cPickle as pickle

def save_database(db):
    with open("db.pickle", "wb") as fp:
        pickle.dump(db, fp)

def load_database():
    with open("db.pickle", "rb") as fp:
        return pickle.load(fp)
Going a little further than the pickle module, you could use shelve. After that, SQLite would be the thing to use.