From 9aab8def083f1ac2137dd33722f3a2d35fe03c59 Mon Sep 17 00:00:00 2001 From: TimePath Date: Wed, 26 Aug 2015 18:55:06 +1000 Subject: [PATCH] Alias management --- qcsrc/common/mutators/mutator/social.qc | 36 +++++++++++++++++++++++-- qcsrc/common/util.qc | 28 +++++++++---------- qcsrc/common/util.qh | 19 +++++++++++++ 3 files changed, 66 insertions(+), 17 deletions(-) diff --git a/qcsrc/common/mutators/mutator/social.qc b/qcsrc/common/mutators/mutator/social.qc index c9b462213..52dd90a05 100644 --- a/qcsrc/common/mutators/mutator/social.qc +++ b/qcsrc/common/mutators/mutator/social.qc @@ -5,6 +5,19 @@ void Social_send(int to, string msg); string Social_invite_accept; +int Social_db = -1; + +STATIC_INIT(Social) { + Social_db = db_load("social.db"); +} + +string Social_get(int id) +{ + string s = db_get(Social_db, itos(id)); + if (s == "") s = sprintf("Player %d", id); + return s; +} + void Social_parse(entity fh, entity pass, int status) { switch (status) { @@ -25,7 +38,8 @@ void Social_parse(entity fh, entity pass, int status) case "ping": Social_send(id, "pong"); case "pong": - printf("Player %d is online\n", id); + string who = Social_get(id); + printf("%s is online\n", who); break; case "msg": for (string input; (input = url_fgets(fh)); ) { @@ -34,7 +48,8 @@ void Social_parse(entity fh, entity pass, int status) break; case "invite": { string s = url_fgets(fh); - printf("Player %d has invited you to %s; `menu_cmd saccept` to join\n", id, s); + string who = Social_get(id); + printf("%s has invited you to %s; `menu_cmd saccept` to join\n", who, s); Social_invite_accept = sprintf("\nconnect %s\n", s); break; } @@ -82,6 +97,23 @@ void Social_recv() MUTATOR_HOOKFUNCTION(social, GameCommand) { if (MUTATOR_RETURNVALUE) return false; // command was already handled + if (cmd_name == "sadd" && cmd_argc >= 3) { + int id = stoi(argv(1)); + string nick = substring(cmd_string, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)); + db_put(Social_db, itos(id), nick); + return true; + } + if (cmd_name == "sremove" && cmd_argc == 2) { + int id = stoi(argv(1)); + db_delete(Social_db, itos(id)); + return true; + } + if (cmd_name == "slist") { + FOREACH_DB(Social_db, LAMBDA( + printf("%s: %s\n", k, v); + )); + return true; + } if (cmd_name == "ssend" && cmd_argc >= 3) { int to = stoi(argv(1)); string msg = substring(cmd_string, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)); diff --git a/qcsrc/common/util.qc b/qcsrc/common/util.qc index 5479698ac..6e14a7f5a 100644 --- a/qcsrc/common/util.qc +++ b/qcsrc/common/util.qc @@ -343,20 +343,15 @@ int db_load(string pFilename) return db; } -void db_dump(float db, string pFilename) +void db_dump(int db, string pFilename) { - float fh, i, j, n, m; - fh = fopen(pFilename, FILE_WRITE); - if(fh < 0) + int fh = fopen(pFilename, FILE_WRITE); + if (fh < 0) error(strcat("Can't dump DB to ", pFilename)); - n = buf_getsize(db); fputs(fh, "0\n"); - for(i = 0; i < n; ++i) - { - m = tokenizebyseparator(bufstr_get(db, i), "\\"); - for(j = 2; j < m; j += 2) - fputs(fh, strcat("\\", argv(j-1), "\\", argv(j), "\n")); - } + FOREACH_DB(db, LAMBDA( + fputs(fh, strcat("\\", k, "\\", v, "\n")); + )); fclose(fh); } @@ -367,18 +362,21 @@ void db_close(float db) string db_get(float db, string pKey) { - float h; - h = crc16(false, pKey) % DB_BUCKETS; + int h = crc16(false, pKey) % DB_BUCKETS; return uri_unescape(infoget(bufstr_get(db, h), pKey)); } void db_put(float db, string pKey, string pValue) { - float h; - h = crc16(false, pKey) % DB_BUCKETS; + int h = crc16(false, pKey) % DB_BUCKETS; bufstr_set(db, h, infoadd(bufstr_get(db, h), pKey, uri_escape(pValue))); } +void db_delete(float db, string pKey) +{ + db_put(db, pKey, string_null); +} + void db_test() { float db, i; diff --git a/qcsrc/common/util.qh b/qcsrc/common/util.qh index 2e41ce76c..b4aa5b305 100644 --- a/qcsrc/common/util.qh +++ b/qcsrc/common/util.qh @@ -89,6 +89,25 @@ int db_load(string filename); void db_close(int db); string db_get(int db, string key); void db_put(int db, string key, string value); +void db_delete(float db, string pKey); +#define FOREACH_INFO(s, f) do { \ + string __s = s; \ + int __m = tokenizebyseparator(__s, "\\"); \ + for (int __j = 2; __j < __m; __j += 2) { \ + string k = argv(__j - 1), v = argv(__j); \ + f; \ + } \ +} while (0) +#define FOREACH_BUF(db, f) do { \ + int __db = db; \ + int __n = buf_getsize(__db); \ + for (int __i = 0; __i < __n; ++__i) { \ + f; \ + } \ +} while (0) +#define FOREACH_DB(db, f) do { \ + FOREACH_BUF(db, FOREACH_INFO(bufstr_get(__db, __i), f)); \ +} while (0) // stringbuffer loading/saving int buf_load(string filename); -- 2.39.2