MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | // MorgenGrauen MUDlib |
| 2 | /** \file /file.c |
| 3 | * Kurzbeschreibung. |
| 4 | * Langbeschreibung... |
| 5 | * \author <Autor> |
| 6 | * \date <date> |
| 7 | * \version $Id$ |
| 8 | */ |
| 9 | /* Changelog: |
| 10 | */ |
| 11 | #pragma strong_types,save_types,rtt_checks |
| 12 | #pragma no_clone,no_inherit,no_shadow |
| 13 | #pragma pedantic, range_check |
| 14 | |
| 15 | #include <defines.h> |
| 16 | #include <events.h> |
| 17 | #include <wizlevels.h> |
| 18 | #include <player/base.h> |
| 19 | #include <userinfo.h> |
| 20 | |
| 21 | #define HOME(x) (__PATH__(0)+x) |
| 22 | #define ZDEBUG(x) tell_room("/players/zesstra/workroom",\ |
| 23 | sprintf("second: %O\n",x)); |
| 24 | |
| 25 | protected void create() |
| 26 | { |
| 27 | seteuid(getuid()); |
| 28 | if (sl_open(HOME("ARCH/second.sqlite")) != 1) |
| 29 | { |
| 30 | raise_error("Datenbank konnte nicht geoeffnet werden.\n"); |
| 31 | } |
| 32 | // Tabellen und Indices anlegen, falls die nicht existieren. |
| 33 | sl_exec("CREATE TABLE IF NOT EXISTS zweities(uuid TEXT PRIMARY KEY ASC, " |
| 34 | "name TEXT NOT NULL, erstieuuid TEXT NOT NULL, " |
| 35 | "erstie TEXT NOT NULL);"); |
| 36 | sl_exec("CREATE TABLE IF NOT EXISTS testies(name TEXT PRIMARY KEY ASC, " |
| 37 | "magier TEXT NOT NULL, " |
| 38 | "lastlogin DATETIME DEFAULT current_timestamp);"); |
| 39 | sl_exec("CREATE TABLE IF NOT EXISTS familien(" |
| 40 | "erstieuuid TEXT PRIMARY KEY ASC, familie TEXT NOT NULL);"); |
| 41 | sl_exec("CREATE INDEX IF NOT EXISTS idx_erstie ON zweities(erstie);"); |
| 42 | sl_exec("CREATE INDEX IF NOT EXISTS idx_name ON zweities(name);"); |
| 43 | sl_exec("CREATE INDEX IF NOT EXISTS idx_magiername ON testies(magier);"); |
| 44 | sl_exec("CREATE INDEX IF NOT EXISTS idx_familie ON familien(familie);"); |
| 45 | |
| 46 | // Login-Event abonnieren |
| 47 | if (EVENTD->RegisterEvent(EVT_LIB_LOGIN, |
| 48 | "listen", this_object()) <= 0) |
| 49 | { |
| 50 | raise_error("Loginevent konnte nicht abonniert werden.\n"); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public void listen(string eid, object trigob, mixed data) |
| 55 | { |
| 56 | if (previous_object() != find_object(EVENTD) |
| 57 | || !trigob |
| 58 | || !query_once_interactive(trigob) |
| 59 | || IS_LEARNER(trigob)) |
| 60 | return; |
| 61 | // wenn testie, wird der Char als Testie behandelt und P_SECOND ignoriert. |
| 62 | mixed testie=trigob->QueryProp(P_TESTPLAYER); |
| 63 | if (stringp(testie) |
| 64 | && strstr(testie,"Gilde")==-1) |
| 65 | { |
| 66 | mixed plinfo = master()->get_userinfo(testie); |
| 67 | if (pointerp(plinfo)) |
| 68 | { |
| 69 | sl_exec("INSERT OR REPLACE INTO testies(name, magier, lastlogin) " |
| 70 | "VALUES(?1,?2,?3);", |
| 71 | trigob->query_real_name(), |
| 72 | testie, time()); |
| 73 | return; // zweitie jetzt auf jeden Fall ignorieren. |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | mixed erstie=trigob->QueryProp(P_SECOND); |
| 78 | if (stringp(erstie)) |
| 79 | { |
| 80 | mixed plinfo = master()->get_userinfo(erstie); |
| 81 | if (pointerp(plinfo)) |
| 82 | { |
| 83 | sl_exec("INSERT OR REPLACE INTO zweities(uuid, name, erstieuuid, erstie) " |
| 84 | "VALUES(?1,?2,?3,?4);", |
| 85 | getuuid(trigob), |
| 86 | trigob->query_real_name(), |
| 87 | erstie + "_" + plinfo[USER_CREATION_DATE+1], |
| 88 | erstie); |
| 89 | } |
| 90 | //ZDEBUG(sprintf("%O, %O, %O\n",eid,trigob,data)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | varargs int remove(int silent) |
| 95 | { |
| 96 | EVENTD->UnregisterEvent(EVT_LIB_LOGIN, this_object()); |
| 97 | sl_close(); |
| 98 | destruct(ME); |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | public mixed sql_query(string query) |
| 103 | { |
| 104 | if (ARCH_SECURITY) |
| 105 | return sl_exec(query); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | private string get_erstie_data(string datum, object zweitie) |
| 110 | { |
| 111 | if (!zweitie) return 0; |
| 112 | mixed res = sl_exec("SELECT " + datum + " FROM zweities WHERE uuid=?1", |
| 113 | getuuid(zweitie)); |
| 114 | if (sizeof(res)) |
| 115 | return res[0][0]; |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | public varargs string QueryErstieName(object zweitie) |
| 121 | { |
| 122 | return get_erstie_data("erstie", zweitie || previous_object()); |
| 123 | } |
| 124 | |
| 125 | public varargs string QueryErstieUUID(object zweitie) |
| 126 | { |
| 127 | return get_erstie_data("erstieuuid", zweitie || previous_object()); |
| 128 | } |
| 129 | |
| 130 | private string* get_zweitie_data(string datum, object erstie) |
| 131 | { |
| 132 | if (!erstie) return 0; |
| 133 | mixed tmp = sl_exec("SELECT " + datum + " FROM zweities WHERE erstieuuid=?1", |
| 134 | getuuid(erstie)); |
| 135 | if (sizeof(tmp)) |
| 136 | { |
| 137 | string *res=({}); |
| 138 | foreach(string *row: tmp) |
| 139 | res+=({row[0]}); |
| 140 | return res; |
| 141 | } |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | public varargs string* QueryZweities(object erstie) |
| 146 | { |
| 147 | return get_zweitie_data("name", erstie || previous_object()); |
| 148 | } |
| 149 | |
| 150 | public varargs string QueryFamilie(object pl) |
| 151 | { |
| 152 | string erstie = get_erstie_data("erstieuuid", |
| 153 | pl || previous_object()); |
| 154 | // Wenn !erstie, dann ist pl kein Zweitie, also ist er selber erstie |
| 155 | erstie ||= getuuid(pl); |
| 156 | // jetzt noch gucken, ob ne explizite Familie fuer den erstie erfasst ist. |
| 157 | mixed tmp = sl_exec("SELECT familie FROM familien WHERE " |
| 158 | "erstieuuid=?1",erstie); |
| 159 | if (sizeof(tmp)) |
| 160 | return tmp[0][0]; |
| 161 | // wenn nicht, dann ist die Familie die Zweitieuuid |
| 162 | return erstie; |
| 163 | } |
| 164 | |