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