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  # We will always return something, either a zero or the highest version number.
11  result = con.execute("SELECT COALESCE(MAX(version), 0) FROM schema").fetchall();
12  curr = result[0][0]
13  if curr < version:
14  Crossfire.Log(Crossfire.LogInfo,
15  "Initializing factions schema %d->%d" % (curr, version))
16  if curr == 0:
17  # schema.sql is already updated to load in the current schema.
18  with open(schema_files[0]) as initfile:
19  con.executescript(initfile.read())
20  else:
21  for f in schema_files:
22  # Compare just the file name
23  if f.split("/").pop() == f"update_schema_{curr}_{curr+1}.sql":
24  with open(f) as updfile:
25  con.executescript(updfile.read())
26  curr += 1
27  con.commit()
28 
30  return os.path.join(Crossfire.DataDirectory(), Crossfire.MapDirectory(),
31  "python/CFReputation/sql", f)
32 
33 def _init_db():
34  # Schema update files must go in order.
35  schema_files = map(_get_sql_path, ["schema.sql", "update_schema_1_2.sql"])
36  init_files = map(_get_sql_path, ["init.sql", "gods.sql"])
37  db_path = os.path.join(Crossfire.LocalDirectory(), "factions.db")
38  con = sqlite3.connect(db_path)
39  _init_schema(con, 2, *schema_files)
40  for f in init_files:
41  with open(f) as initfile:
42  con.executescript(initfile.read())
43  return con
44 
45 def _get_db():
46  return _init_db()
47 
48 def reputation(player, faction=None):
49  """
50  Return tuple with the name and reputation of the player with the given
51  faction. If faction is None, return all known reputations.
52  """
53  con = _get_db()
54  if faction is None:
55  query="""
56 SELECT faction, CAST(ROUND(reputation*100) as integer) as rep
57 FROM reputations
58 WHERE name=? AND ABS(rep) > 0;
59  """
60  result = con.execute(query, (player,)).fetchall()
61  else:
62  query="""
63 SELECT faction, CAST(ROUND(reputation*100) as integer) as rep
64 FROM reputations
65 WHERE name=? AND faction=? AND ABS(rep) > 0;
66  """
67  result = con.execute(query, (player, faction)).fetchall()
68  con.close()
69  return result
70 
71 def record_kill(race, region, player, fraction=0.0001, limit=0.4):
72  con = _get_db()
73  query = """
74 WITH updates AS (
75  SELECT faction, -attitude*? AS change
76  FROM regions
77  NATURAL JOIN relations
78  WHERE race=? AND (region=? OR region='ALL'))
79 REPLACE INTO reputations
80 SELECT ? AS player, updates.faction,
81  COALESCE(reputation, 0) + change AS new_rep
82 FROM updates
83 LEFT JOIN reputations
84  ON updates.faction=reputations.faction AND player=reputations.name
85 WHERE ABS(new_rep) <= ?;
86  """
87  con.execute(query, (fraction, race, region, player, limit))
88  con.commit()
89  con.close()
CFReputation._init_db
def _init_db()
Definition: __init__.py:33
CFBank.open
def open()
Definition: CFBank.py:69
CFReputation._get_sql_path
def _get_sql_path(f)
Definition: __init__.py:29
CFReputation.record_kill
def record_kill(race, region, player, fraction=0.0001, limit=0.4)
Definition: __init__.py:71
disinfect.map
map
Definition: disinfect.py:4
CFReputation._get_db
def _get_db()
Definition: __init__.py:45
CFReputation.reputation
def reputation(player, faction=None)
Definition: __init__.py:48
CFReputation._init_schema
def _init_schema(con, version, *schema_files)
Definition: __init__.py:6