This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rustsdb.py
66 lines (56 loc) · 2.61 KB
/
rustsdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Rustsdb.py Store information related to
online players for the GamingOnLinux server.
Should be compatible with any source server/game"""
import valve.source.a2s
import psycopg2
import os
import datetime
SERVER = "89.34.99.19"
PORT = 28015
# assumes players table has been already created:
# create table players (id serial, name varchar, login timestamp,
# logout timestamp, last_seen timestamp, duration int);
# expects DBNAME and DBUSER to be set on environment:
# DBNAME=rust_gol DBUSER=user PYTHONPATH=~/git/python-valve/ python3 rusts.py dbstore
def store_to_db():
"""Store players info to a postgres database
table columns: currenttime, name, duration"""
try:
players = valve.source.a2s.ServerQuerier((SERVER, PORT)).players()['players']
except Exception as e:
print("Can't connect to server")
print(e)
else:
try:
# potser check de duration al primer run, per veure si esta connectat des de l'ultim
dbname = os.environ['DBNAME']
dbuser = os.environ['DBUSER']
players_loggedin = ()
conn = psycopg2.connect("dbname="+dbname+" user="+dbuser)
cur = conn.cursor()
for player in players:
cur.execute("SELECT id FROM players WHERE logout IS NULL AND name = %s", (player['name'],))
record = cur.fetchone()
if record is None:
login = datetime.datetime.now() - datetime.timedelta(seconds=player['duration'])
query = ("INSERT INTO players (name, login, last_seen, duration) VALUES (%s, %s, %s, %s) RETURNING id")
cur.execute(query, (player['name'], login, datetime.datetime.now(), player['duration']))
conn.commit()
players_loggedin += (cur.fetchone()[0],)
else:
cur.execute("UPDATE players SET last_seen = now() WHERE id = %s", (record[0],))
conn.commit()
players_loggedin += (record[0],)
if len(players_loggedin) > 0:
cur.execute("UPDATE players SET logout = last_seen WHERE logout IS NULL AND id NOT IN %s", (players_loggedin,))
conn.commit()
else:
cur.execute("UPDATE players SET logout = last_seen WHERE logout IS NULL")
conn.commit()
except psycopg2.OperationalError as e:
print("Uh oh, can't connect. Invalid dbname, user or password?")
print(e)
if __name__ == "__main__":
store_to_db()