File: __init__.py

package info (click to toggle)
crossfire-maps 1.75.0%2Bdfsg1-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 275,656 kB
  • sloc: python: 7,711; sql: 92; sh: 73; makefile: 7
file content (82 lines) | stat: -rw-r--r-- 2,644 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os.path
import sqlite3

import Crossfire

_dict_name = 'CFReputationConnection'

def _init_schema(con, version, *schema_files):
    con.execute("PRAGMA journal_mode=WAL;");
    con.execute("PRAGMA synchronous=NORMAL;");
    con.execute("CREATE TABLE IF NOT EXISTS schema(version INT);");
    result = con.execute("SELECT version FROM schema").fetchall();
    curr = len(result)
    if curr < version:
        Crossfire.Log(Crossfire.LogInfo,
                "Initializing factions schema %d->%d" % (curr, version))
        for f in schema_files:
            with open(f) as initfile:
                con.executescript(initfile.read())
        con.commit()

def _get_sql_path(f):
    return os.path.join(Crossfire.DataDirectory(), Crossfire.MapDirectory(),
            "python/CFReputation/sql", f)

def _init_db():
    schema_files = map(_get_sql_path, ["schema.sql"])
    init_files = map(_get_sql_path, ["init.sql", "gods.sql"])
    db_path = os.path.join(Crossfire.LocalDirectory(), "factions.db")
    con = sqlite3.connect(':memory:')
    _init_schema(con, 1, *schema_files)
    for f in init_files:
        with open(f) as initfile:
            con.executescript(initfile.read())
    Crossfire.GetSharedDictionary()[_dict_name] = con

def _get_db():
    d = Crossfire.GetSharedDictionary()
    if _dict_name not in d:
        _init_db()
    return d[_dict_name]

def reputation(player, faction=None):
    """
    Return tuple with the name and reputation of the player with the given
    faction. If faction is None, return all known reputations.
    """
    con = _get_db()
    if faction is None:
        query="""
SELECT faction, CAST(ROUND(reputation*100) as integer) as rep
FROM reputations
WHERE name=? AND ABS(rep) > 0;
        """
        result = con.execute(query, (player,)).fetchall()
    else:
        query="""
SELECT faction, CAST(ROUND(reputation*100) as integer) as rep
FROM reputations
WHERE name=? AND faction=? AND ABS(rep) > 0;
        """
        result = con.execute(query, (player, faction)).fetchall()
    return result

def record_kill(race, region, player, fraction=0.0001, limit=0.4):
    con = _get_db()
    query = """
WITH updates AS (
    SELECT faction, -attitude*? AS change
    FROM regions
    NATURAL JOIN relations
    WHERE race=? AND (region=? OR region='ALL'))
REPLACE INTO reputations
SELECT ? AS player, updates.faction,
    COALESCE(reputation, 0) + change AS new_rep
FROM updates
LEFT JOIN reputations
    ON updates.faction=reputations.faction AND player=reputations.name
WHERE ABS(new_rep) <= ?;
    """
    con.execute(query, (fraction, race, region, player, limit))
    con.commit()