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> |
Zesstra | 3c09351 | 2019-01-07 22:55:26 +0100 | [diff] [blame] | 20 | #include <config.h> |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 21 | |
Zesstra | 3c09351 | 2019-01-07 22:55:26 +0100 | [diff] [blame] | 22 | #define DBPATH "/"LIBDATADIR"/"SECUREDIR"/ARCH/second.sqlite" |
| 23 | |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 24 | #define ZDEBUG(x) tell_room("/players/zesstra/workroom",\ |
| 25 | sprintf("second: %O\n",x)); |
| 26 | |
| 27 | protected void create() |
| 28 | { |
| 29 | seteuid(getuid()); |
Zesstra | 3c09351 | 2019-01-07 22:55:26 +0100 | [diff] [blame] | 30 | if (sl_open(DBPATH) != 1) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 31 | { |
| 32 | raise_error("Datenbank konnte nicht geoeffnet werden.\n"); |
| 33 | } |
| 34 | // Tabellen und Indices anlegen, falls die nicht existieren. |
| 35 | sl_exec("CREATE TABLE IF NOT EXISTS zweities(uuid TEXT PRIMARY KEY ASC, " |
| 36 | "name TEXT NOT NULL, erstieuuid TEXT NOT NULL, " |
| 37 | "erstie TEXT NOT NULL);"); |
| 38 | sl_exec("CREATE TABLE IF NOT EXISTS testies(name TEXT PRIMARY KEY ASC, " |
| 39 | "magier TEXT NOT NULL, " |
| 40 | "lastlogin DATETIME DEFAULT current_timestamp);"); |
| 41 | sl_exec("CREATE TABLE IF NOT EXISTS familien(" |
| 42 | "erstieuuid TEXT PRIMARY KEY ASC, familie TEXT NOT NULL);"); |
| 43 | sl_exec("CREATE INDEX IF NOT EXISTS idx_erstie ON zweities(erstie);"); |
| 44 | sl_exec("CREATE INDEX IF NOT EXISTS idx_name ON zweities(name);"); |
| 45 | sl_exec("CREATE INDEX IF NOT EXISTS idx_magiername ON testies(magier);"); |
| 46 | sl_exec("CREATE INDEX IF NOT EXISTS idx_familie ON familien(familie);"); |
| 47 | |
| 48 | // Login-Event abonnieren |
| 49 | if (EVENTD->RegisterEvent(EVT_LIB_LOGIN, |
| 50 | "listen", this_object()) <= 0) |
| 51 | { |
| 52 | raise_error("Loginevent konnte nicht abonniert werden.\n"); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public void listen(string eid, object trigob, mixed data) |
| 57 | { |
| 58 | if (previous_object() != find_object(EVENTD) |
| 59 | || !trigob |
| 60 | || !query_once_interactive(trigob) |
| 61 | || IS_LEARNER(trigob)) |
| 62 | return; |
| 63 | // wenn testie, wird der Char als Testie behandelt und P_SECOND ignoriert. |
| 64 | mixed testie=trigob->QueryProp(P_TESTPLAYER); |
| 65 | if (stringp(testie) |
| 66 | && strstr(testie,"Gilde")==-1) |
| 67 | { |
Zesstra | c5cbd08 | 2019-01-07 23:06:13 +0100 | [diff] [blame] | 68 | testie=lower_case(testie); |
| 69 | mixed plinfo = master()->get_userinfo(testie); |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 70 | if (pointerp(plinfo)) |
| 71 | { |
| 72 | sl_exec("INSERT OR REPLACE INTO testies(name, magier, lastlogin) " |
| 73 | "VALUES(?1,?2,?3);", |
| 74 | trigob->query_real_name(), |
| 75 | testie, time()); |
| 76 | return; // zweitie jetzt auf jeden Fall ignorieren. |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | mixed erstie=trigob->QueryProp(P_SECOND); |
| 81 | if (stringp(erstie)) |
| 82 | { |
Zesstra | c5cbd08 | 2019-01-07 23:06:13 +0100 | [diff] [blame] | 83 | erstie=lower_case(erstie); |
| 84 | mixed plinfo = master()->get_userinfo(erstie); |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 85 | if (pointerp(plinfo)) |
| 86 | { |
| 87 | sl_exec("INSERT OR REPLACE INTO zweities(uuid, name, erstieuuid, erstie) " |
| 88 | "VALUES(?1,?2,?3,?4);", |
| 89 | getuuid(trigob), |
| 90 | trigob->query_real_name(), |
| 91 | erstie + "_" + plinfo[USER_CREATION_DATE+1], |
| 92 | erstie); |
| 93 | } |
| 94 | //ZDEBUG(sprintf("%O, %O, %O\n",eid,trigob,data)); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | varargs int remove(int silent) |
| 99 | { |
| 100 | EVENTD->UnregisterEvent(EVT_LIB_LOGIN, this_object()); |
| 101 | sl_close(); |
| 102 | destruct(ME); |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | public mixed sql_query(string query) |
| 107 | { |
| 108 | if (ARCH_SECURITY) |
| 109 | return sl_exec(query); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | private string get_erstie_data(string datum, object zweitie) |
| 114 | { |
| 115 | if (!zweitie) return 0; |
| 116 | mixed res = sl_exec("SELECT " + datum + " FROM zweities WHERE uuid=?1", |
| 117 | getuuid(zweitie)); |
| 118 | if (sizeof(res)) |
| 119 | return res[0][0]; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | public varargs string QueryErstieName(object zweitie) |
| 125 | { |
| 126 | return get_erstie_data("erstie", zweitie || previous_object()); |
| 127 | } |
| 128 | |
| 129 | public varargs string QueryErstieUUID(object zweitie) |
| 130 | { |
| 131 | return get_erstie_data("erstieuuid", zweitie || previous_object()); |
| 132 | } |
| 133 | |
| 134 | private string* get_zweitie_data(string datum, object erstie) |
| 135 | { |
| 136 | if (!erstie) return 0; |
| 137 | mixed tmp = sl_exec("SELECT " + datum + " FROM zweities WHERE erstieuuid=?1", |
| 138 | getuuid(erstie)); |
| 139 | if (sizeof(tmp)) |
| 140 | { |
| 141 | string *res=({}); |
| 142 | foreach(string *row: tmp) |
| 143 | res+=({row[0]}); |
| 144 | return res; |
| 145 | } |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | public varargs string* QueryZweities(object erstie) |
| 150 | { |
| 151 | return get_zweitie_data("name", erstie || previous_object()); |
| 152 | } |
| 153 | |
| 154 | public varargs string QueryFamilie(object pl) |
| 155 | { |
| 156 | string erstie = get_erstie_data("erstieuuid", |
| 157 | pl || previous_object()); |
| 158 | // Wenn !erstie, dann ist pl kein Zweitie, also ist er selber erstie |
| 159 | erstie ||= getuuid(pl); |
| 160 | // jetzt noch gucken, ob ne explizite Familie fuer den erstie erfasst ist. |
| 161 | mixed tmp = sl_exec("SELECT familie FROM familien WHERE " |
| 162 | "erstieuuid=?1",erstie); |
| 163 | if (sizeof(tmp)) |
| 164 | return tmp[0][0]; |
| 165 | // wenn nicht, dann ist die Familie die Zweitieuuid |
| 166 | return erstie; |
| 167 | } |
| 168 | |
Zesstra | 60ec497 | 2019-01-08 21:07:16 +0100 | [diff] [blame] | 169 | public string SetFamilie(object|string pl, string familie) |
| 170 | { |
| 171 | if (!ARCH_SECURITY) |
| 172 | return 0; |
| 173 | |
| 174 | // Wenn Spielerobjekt, UUID ermitteln |
| 175 | if (objectp(pl) && query_once_interactive(pl)) |
| 176 | pl = getuuid(pl); |
| 177 | |
| 178 | sl_exec("INSERT OR REPLACE INTO familien(erstieuuid, familie) " |
| 179 | "VALUES(?1, ?2);", pl, familie); |
| 180 | |
| 181 | mixed tmp = sl_exec("SELECT familie FROM familien WHERE " |
| 182 | "erstieuuid=?1", pl); |
| 183 | if (sizeof(tmp)) |
| 184 | return tmp[0][0]; |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | public int DeleteFamilie(object|string pl) |
| 190 | { |
| 191 | if (!ARCH_SECURITY) |
| 192 | return 0; |
| 193 | |
| 194 | // Wenn Spielerobjekt, UUID ermitteln |
| 195 | if (objectp(pl) && query_once_interactive(pl)) |
| 196 | pl = getuuid(pl); |
| 197 | |
| 198 | sl_exec("DELETE FROM familien WHERE erstieuuid=?1;", |
| 199 | pl); |
| 200 | |
| 201 | mixed tmp = sl_exec("SELECT familie FROM familien WHERE " |
| 202 | "erstieuuid=?1", pl); |
| 203 | if (sizeof(tmp)) |
| 204 | return -1; |
| 205 | |
| 206 | return 1; |
| 207 | } |
| 208 | |
Zesstra | b9dc01d | 2019-01-08 21:22:21 +0100 | [diff] [blame] | 209 | public int DeleteZweitie(object|string pl) |
| 210 | { |
| 211 | if (!ARCH_SECURITY) |
| 212 | return 0; |
| 213 | |
| 214 | // Wenn Spielerobjekt, UUID ermitteln |
| 215 | if (objectp(pl) && query_once_interactive(pl)) |
| 216 | pl = getuuid(pl); |
| 217 | |
| 218 | sl_exec("DELETE FROM zweities WHERE uuid=?1;", pl); |
| 219 | |
| 220 | mixed tmp = sl_exec("SELECT name FROM zweities WHERE " |
| 221 | "uuid=?1", pl); |
| 222 | if (sizeof(tmp)) |
| 223 | return -1; |
| 224 | |
| 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | public int DeleteTestie(object|string pl) |
| 229 | { |
| 230 | if (!ARCH_SECURITY) |
| 231 | return 0; |
| 232 | |
| 233 | // Wenn Spielerobjekt, UID ermitteln |
| 234 | if (objectp(pl) && query_once_interactive(pl)) |
| 235 | pl = getuid(pl); |
| 236 | |
| 237 | sl_exec("DELETE FROM testies WHERE name=?1;", pl); |
| 238 | |
| 239 | mixed tmp = sl_exec("SELECT magier FROM testies WHERE " |
| 240 | "name=?1", pl); |
| 241 | if (sizeof(tmp)) |
| 242 | return -1; |
| 243 | |
| 244 | return 1; |
| 245 | } |
| 246 | |