MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | #pragma strict_types |
| 2 | #pragma no_clone |
| 3 | #pragma no_shadow |
| 4 | #pragma no_inherit |
| 5 | #pragma verbose_errors |
| 6 | #pragma warn_deprecated |
| 7 | |
| 8 | #include <reputation.h> |
| 9 | #include <wizlevels.h> |
| 10 | |
| 11 | /* Datenstruktur reputations: |
| 12 | * (["id": (["uids": ({ "uid1", "uid2" }), "name":"", "changemsg":"", |
| 13 | * "flags":x|x|x |
| 14 | * ]) |
| 15 | * ]) |
| 16 | * Bsp: |
| 17 | * (["schaedlspalta": (["uids": ({"d.wald.nibel", "d.wald.kessa"}), |
| 18 | * "name": "Schaedlspalta-Klan", "active": 1, |
| 19 | * "changemsg":"beim Schaedlspalta-Klan" |
| 20 | * ]) |
| 21 | * ]) |
| 22 | */ |
| 23 | |
| 24 | private mapping reputations; |
| 25 | |
| 26 | protected void create() { |
| 27 | seteuid(getuid(this_object())); |
| 28 | |
| 29 | if(!restore_object(REP_SAVEFILE) || !mappingp(reputations)) |
| 30 | reputations = ([ ]); |
| 31 | } |
| 32 | |
| 33 | // Zugriff auf Handlingmethoden fuer ROOT, EMs+ |
| 34 | private int Sec() { |
| 35 | if(process_call()) return 0; |
| 36 | if(previous_object() && geteuid(previous_object()) == ROOTID) return 1; |
| 37 | return objectp(previous_object()) && ARCH_SECURITY; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | /* Argumente: |
| 42 | * repid = id im mapping reputations |
| 43 | * validuids = UIDs welche die Reputation veraendern duerfen, String |
| 44 | * oder String-Array |
| 45 | * name = Name der Reputationsfraktion |
| 46 | * changemsg = Text der in die Default-Changemsg eingefuegt wird |
| 47 | * (Dein Ansehen %s hat sich ... verbessert.) |
| 48 | * |
| 49 | * Returns: |
| 50 | * 1 = Reputation erfolgreich hinzugefuegt |
| 51 | * -1 = Keine Zugriffsrechte |
| 52 | * -2 = Falsche Argumente |
| 53 | * -3 = Reputation bereits vorhanden |
| 54 | */ |
| 55 | public int AddReputation(string repid, mixed validuids, string name, |
| 56 | string changemsg) { |
| 57 | if(!Sec()) |
| 58 | return -1; |
| 59 | if(stringp(validuids)) validuids = ({ validuids }); |
| 60 | if(!pointerp(validuids)) validuids = ({ }); |
| 61 | // validuids + changemsgs kann auch erstmal leer bleiben |
| 62 | if(!stringp(repid) || !sizeof(repid) || !stringp(name) || !sizeof(name)) |
| 63 | return -2; |
| 64 | if(member(reputations, repid)) |
| 65 | return -3; |
| 66 | reputations += ([ repid : ([ |
| 67 | "uids": validuids, |
| 68 | "name": name, |
| 69 | "flags": REP_FLAG_ACTIVE, |
| 70 | "changemsg": changemsg |
| 71 | ]) ]); |
| 72 | save_object(REP_SAVEFILE); |
| 73 | tell_object(this_interactive(), sprintf("Reputation eingetragen:\n" |
| 74 | "Name: %s\nValid UIDs: %O\nFlags: REP_FLAG_ACTIVE\nChangemsg: %s\n", |
| 75 | reputations[repid]["name"], reputations[repid]["uids"], |
| 76 | reputations[repid]["changemsg"])); |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | /* Argumente: |
| 81 | * repid = id im mapping reputations |
| 82 | * |
| 83 | * Returns: |
| 84 | * 1 = wurde erfolgreich deaktiviert |
| 85 | * -1 = Keine Zugriffsrechte |
| 86 | * -2 = Falsche Argumente |
| 87 | * -3 = Rep nicht vorhanden |
| 88 | * -4 = Ist bereits deaktiviert |
| 89 | */ |
| 90 | public int DeactivateReputation(string repid) { |
| 91 | if(!Sec()) |
| 92 | return -1; |
| 93 | if(!stringp(repid) || !sizeof(repid)) |
| 94 | return -2; |
| 95 | if(!member(reputations, repid)) |
| 96 | return -3; |
| 97 | if(!(reputations[repid]["flags"] & REP_FLAG_ACTIVE)) |
| 98 | return -4; |
| 99 | reputations[repid]["flags"] |= REP_FLAG_ACTIVE; |
| 100 | save_object(REP_SAVEFILE); |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | /* Argumente: |
| 105 | * repid = id im mapping reputations |
| 106 | * |
| 107 | * Returns: |
| 108 | * 1 = wurde erfolgreich aktiviert |
| 109 | * -1 = Keine Zugriffsrechte |
| 110 | * -2 = Falsche Argumente |
| 111 | * -3 = Rep nicht vorhanden |
| 112 | * -4 = Ist bereits aktiviert |
| 113 | */ |
| 114 | public int ActivateReputation(string repid) { |
| 115 | if(!Sec()) |
| 116 | return -1; |
| 117 | if(!stringp(repid) || !sizeof(repid)) |
| 118 | return -2; |
| 119 | if(!member(reputations, repid)) |
| 120 | return -3; |
| 121 | if(reputations[repid]["flags"] & REP_FLAG_ACTIVE) |
| 122 | return -4; |
| 123 | reputations[repid]["flags"] ^= REP_FLAG_ACTIVE; |
| 124 | save_object(REP_SAVEFILE); |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | /* Argumente: |
| 129 | * repid = id im mapping reputations |
| 130 | * uid = UID welche die Reputation veraendern darf |
| 131 | * |
| 132 | * Returns: |
| 133 | * 1 = UID erfolgreich hinzugefuegt |
| 134 | * -1 = Keine Zugriffsrechte |
| 135 | * -2 = Falsche Argumente |
| 136 | * -3 = Rep nicht vorhanden |
| 137 | * -4 = UID ist bereits vorhanden |
| 138 | */ |
| 139 | public int AddReputationUid(string repid, string uid) { |
| 140 | if(!Sec()) |
| 141 | return -1; |
| 142 | if(!stringp(repid) || !sizeof(repid) || !stringp(uid) || !sizeof(uid)) |
| 143 | return -2; |
| 144 | if(!member(reputations, repid)) |
| 145 | return -3; |
| 146 | if(member(reputations[repid]["uids"], uid) != -1) |
| 147 | return -4; |
| 148 | reputations[repid]["uids"] += ({ uid }); |
| 149 | save_object(REP_SAVEFILE); |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | /* Argumente: |
| 154 | * repid = id im mapping reputations |
| 155 | * uid = UID welche die Reputation nicht mehr veraendern darf |
| 156 | * |
| 157 | * Returns: |
| 158 | * 1 = UID erfolgreich geloescht |
| 159 | * -1 = Keine Zugriffsrechte |
| 160 | * -2 = Falsche Argumente |
| 161 | * -3 = Rep nicht vorhanden |
| 162 | * -4 = UID nicht vorhanden |
| 163 | */ |
| 164 | public int RemoveReputationUid(string repid, string uid) { |
| 165 | if(!Sec()) |
| 166 | return -1; |
| 167 | if(!stringp(repid) || !sizeof(repid) || !stringp(uid) || !sizeof(uid)) |
| 168 | return -2; |
| 169 | if(!member(reputations, repid)) |
| 170 | return -3; |
| 171 | if(member(reputations[repid]["uids"], uid) == -1) |
| 172 | return -4; |
| 173 | reputations[repid]["uids"] -= ({ uid }); |
| 174 | save_object(REP_SAVEFILE); |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | /* Argumente: |
| 179 | * repid = id im mapping reputations |
| 180 | * name = Neuer Name der Rep |
| 181 | * |
| 182 | * Returns: |
| 183 | * 1 = Neuer Name erfolgreich gesetzt |
| 184 | * -1 = Keine Zugriffsrechte |
| 185 | * -2 = Falsche Argumente |
| 186 | * -3 = Rep nicht vorhanden |
| 187 | * -4 = Name entspricht dem derzeitigen |
| 188 | */ |
| 189 | public int ChangeReputationName(string repid, string name) { |
| 190 | if(!Sec()) |
| 191 | return -1; |
| 192 | if(!stringp(repid) || !sizeof(repid) || !stringp(name) || !sizeof(name)) |
| 193 | return -2; |
| 194 | if(!member(reputations, repid)) |
| 195 | return -3; |
| 196 | if(reputations[repid]["name"] == name) |
| 197 | return -4; |
| 198 | reputations[repid]["name"] = name; |
| 199 | save_object(REP_SAVEFILE); |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | /* Argumente: |
| 204 | * repid = id im mapping reputations |
| 205 | * msg = Neuer String in der Default-Changemsg |
| 206 | * |
| 207 | * Returns: |
| 208 | * 1 = Neue Msg erfolgreich gesetzt |
| 209 | * -1 = Keine Zugriffsrechte |
| 210 | * -2 = Falsche Argumente |
| 211 | * -3 = Rep nicht vorhanden |
| 212 | * -4 = Msg entspricht der derzeitigen |
| 213 | */ |
| 214 | public int ChangeReputationMsg(string repid, string msg) { |
| 215 | if(!Sec()) |
| 216 | return -1; |
| 217 | if(!stringp(repid) || !sizeof(repid) || !stringp(msg) || !sizeof(msg)) |
| 218 | return -2; |
| 219 | if(!member(reputations, repid)) |
| 220 | return -3; |
| 221 | if(reputations[repid]["changemsg"] == msg) |
| 222 | return -4; |
| 223 | reputations[repid]["changemsg"] = msg; |
| 224 | save_object(REP_SAVEFILE); |
| 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | /* Argumente: |
| 229 | * repid = id(s) im mapping reputations, ohne Argument werden alle aufgelistet, |
| 230 | * String oder String-array |
| 231 | * |
| 232 | * Returns: |
| 233 | * 1 = Liste ausgegeben |
| 234 | * -1 = Falsche Argumente |
| 235 | * -2 = Rep nicht vorhanden |
| 236 | */ |
| 237 | public varargs int ViewReputationData(mixed repids) { |
| 238 | // Fuer alle sichtbar, damit man ggf. weiss, wo man nachfragen muss |
| 239 | if(!repids) repids = m_indices(reputations); |
| 240 | else if(stringp(repids)) repids = ({ repids }); |
| 241 | if(!pointerp(repids) || !sizeof(repids)) |
| 242 | return -1; |
| 243 | // Textausgabe fuer alle richtigen IDs |
| 244 | foreach(string repid : repids) { |
| 245 | if(!member(reputations, repid)) continue; |
| 246 | tell_object(this_interactive(), sprintf("%s %s\n Name: %s\n Changemsg: " |
| 247 | "%s\n ValidUIDS: %O\n\n", repid, |
| 248 | (reputations[repid]["flags"] & REP_FLAG_ACTIVE ? "" : |
| 249 | "(Inaktiv)"), reputations[repid]["name"], |
| 250 | reputations[repid]["changemsg"], reputations[repid]["uids"])); |
| 251 | } |
| 252 | // Min. 1 nicht vorhanden: return -2, sonst 1 |
| 253 | return (sizeof(repids - m_indices(reputations)) ? -2 : 1); |
| 254 | } |
| 255 | |
| 256 | /* Argumente: |
| 257 | * repid = id im mapping reputations |
| 258 | * |
| 259 | * Returns: |
| 260 | * mapping = Daten der Reputation |
| 261 | * 0 = Falsches Argument / Reputation nicht vorhanden |
| 262 | */ |
| 263 | public mapping GetReputationData(string repid) { |
| 264 | if(!stringp(repid) || !sizeof(repid) || !member(reputations, repid)) |
| 265 | return 0; |
| 266 | return deep_copy(reputations[repid]); |
| 267 | } |
| 268 | |
| 269 | /* Argumente: |
| 270 | * repid = id im mapping reputations |
| 271 | * value = Wert um den die Rep veraendert wird (+ / -) |
| 272 | * |
| 273 | * Returns: |
| 274 | * string = "Dein Ansehen %s hat sich ... .\n" |
| 275 | * 0 = Falsche Argumente / Rep nicht vorhanden |
| 276 | */ |
| 277 | public string GetDefaultChangeMsg(string repid, int value) { |
| 278 | string strength; |
| 279 | if(!stringp(repid) || !sizeof(repid) || !intp(value) || !value || |
| 280 | !member(reputations, repid)) |
| 281 | return 0; |
| 282 | switch(abs(value)) { |
| 283 | case 0..25 : strength = "kaum merklich"; break; |
| 284 | case 26..50 : strength = "leicht"; break; |
| 285 | case 51..100 : strength = "deutlich"; break; |
| 286 | case 101..250 : strength = "erheblich"; break; |
| 287 | case 251..1000: strength = "sehr stark"; break; |
| 288 | default: strength = "ausserordentlich stark"; break; |
| 289 | } |
| 290 | return sprintf("Dein Ansehen %s hat sich%s%s.\n", |
| 291 | reputations[repid]["changemsg"], (!strength ? " " : " "+ strength +" "), |
| 292 | (value < 0 ? "verschlechtert" : "verbessert")); |
| 293 | } |
| 294 | |
| 295 | /* Argumente: |
| 296 | * repid = id im mapping reputations |
| 297 | * ob = Objekt, dessen UID geprueft wird, ohne Argument -> prev_ob() |
| 298 | * |
| 299 | * Returns: |
| 300 | * 1 = UID valid |
| 301 | * 0 = UID invalid |
| 302 | * -1 = Falsche Argumente |
| 303 | * -2 = Rep nicht vorhanden |
| 304 | */ |
| 305 | public int CheckValidUid(string repid, object ob) { |
| 306 | if(!stringp(repid) || !sizeof(repid) || !objectp(ob)) |
| 307 | return -1; |
| 308 | if(!member(reputations, repid)) |
| 309 | return -2; |
| 310 | return (member(reputations[repid]["uids"], getuid(ob)) != -1); |
| 311 | } |
| 312 | |
| 313 | /* Argumente: |
| 314 | * repid = id im mapping reputations |
| 315 | * |
| 316 | * Returns: |
| 317 | * 1 = Rep aktiv |
| 318 | * 0 = Rep inaktiv |
| 319 | * -1 = Falsche Argumente |
| 320 | * -2 = Rep nicht vorhanden |
| 321 | */ |
| 322 | public int CheckRepActive(string repid) { |
| 323 | if(!stringp(repid) || !sizeof(repid)) |
| 324 | return -1; |
| 325 | if(!member(reputations, repid)) |
| 326 | return -2; |
| 327 | // Verboolung |
| 328 | return !!(reputations[repid]["flags"] & REP_FLAG_ACTIVE); |
| 329 | } |