Crossfire Server, Trunk
__init__.py
Go to the documentation of this file.
1 import os.path
2 import sqlite3
3 
4 import Crossfire
5 
6 def _init_schema(con, version, *schema_files):
7  con.execute("PRAGMA journal_mode=WAL;");
8  con.execute("PRAGMA synchronous=NORMAL;");
9  con.execute("CREATE TABLE IF NOT EXISTS schema(version INT);");
10  result = con.execute("SELECT version FROM schema").fetchall();
11  curr = len(result)
12  if curr < version:
13  Crossfire.Log(Crossfire.LogInfo,
14  "Initializing factions schema %d->%d" % (curr, version))
15  for f in schema_files:
16  with open(f) as initfile:
17  con.executescript(initfile.read())
18  con.commit()
19 
21  return os.path.join(Crossfire.DataDirectory(), Crossfire.MapDirectory(),
22  "python/CFReputation/sql", f)
23 
24 def _init_db():
25  schema_files = map(_get_sql_path, ["schema.sql"])
26  init_files = map(_get_sql_path, ["init.sql", "gods.sql"])
27  db_path = os.path.join(Crossfire.LocalDirectory(), "factions.db")
28  con = sqlite3.connect(db_path)
29  _init_schema(con, 1, *schema_files)
30  for f in init_files:
31  with open(f) as initfile:
32  con.executescript(initfile.read())
33  return con
34 
35 def _get_db():
36  return _init_db()
37 
38 def reputation(player, faction=None):
39  """
40  Return tuple with the name and reputation of the player with the given
41  faction. If faction is None, return all known reputations.
42  """
43  con = _get_db()
44  if faction is None:
45  query="""
46 SELECT faction, CAST(ROUND(reputation*100) as integer) as rep
47 FROM reputations
48 WHERE name=? AND ABS(rep) > 0;
49  """
50  result = con.execute(query, (player,)).fetchall()
51  else:
52  query="""
53 SELECT faction, CAST(ROUND(reputation*100) as integer) as rep
54 FROM reputations
55 WHERE name=? AND faction=? AND ABS(rep) > 0;
56  """
57  result = con.execute(query, (player, faction)).fetchall()
58  con.close()
59  return result
60 
61 def record_kill(race, region, player, fraction=0.0001, limit=0.4):
62  con = _get_db()
63  query = """
64 WITH updates AS (
65  SELECT faction, -attitude*? AS change
66  FROM regions
67  NATURAL JOIN relations
68  WHERE race=? AND (region=? OR region='ALL'))
69 REPLACE INTO reputations
70 SELECT ? AS player, updates.faction,
71  COALESCE(reputation, 0) + change AS new_rep
72 FROM updates
73 LEFT JOIN reputations
74  ON updates.faction=reputations.faction AND player=reputations.name
75 WHERE ABS(new_rep) <= ?;
76  """
77  con.execute(query, (fraction, race, region, player, limit))
78  con.commit()
79  con.close()
CFReputation._init_db
def _init_db()
Definition: __init__.py:24
CFBank.open
def open()
Definition: CFBank.py:70
CFReputation._get_sql_path
def _get_sql_path(f)
Definition: __init__.py:20
CFReputation.record_kill
def record_kill(race, region, player, fraction=0.0001, limit=0.4)
Definition: __init__.py:61
disinfect.map
map
Definition: disinfect.py:4
CFReputation._get_db
def _get_db()
Definition: __init__.py:35
CFReputation.reputation
def reputation(player, faction=None)
Definition: __init__.py:38
CFReputation._init_schema
def _init_schema(con, version, *schema_files)
Definition: __init__.py:6