]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Basic message passing
authorTimePath <andrew.hardaker1995@gmail.com>
Wed, 26 Aug 2015 01:06:56 +0000 (11:06 +1000)
committerTimePath <andrew.hardaker1995@gmail.com>
Wed, 26 Aug 2015 01:06:56 +0000 (11:06 +1000)
qcsrc/common/mutators/all.inc
qcsrc/common/mutators/mutator/social.qc [new file with mode: 0644]

index 0471dff7cd700623d9c28941e4495791643e7216..a874e98107d007110580385bbce7e343165f1837 100644 (file)
@@ -1,3 +1,4 @@
 #include "mutator/damagetext.qc"
 #include "mutator/itemstime.qc"
+#include "mutator/social.qc"
 #include "mutator/waypointsprites.qc"
diff --git a/qcsrc/common/mutators/mutator/social.qc b/qcsrc/common/mutators/mutator/social.qc
new file mode 100644 (file)
index 0000000..7ee332d
--- /dev/null
@@ -0,0 +1,76 @@
+#ifdef MENUQC
+REGISTER_MUTATOR(social, true);
+
+void Social_parse(entity fh, entity pass, int status)
+{
+       switch (status) {
+               case URL_READY_CANWRITE: {
+                   string msg = pass.message;
+                       url_fputs(fh, msg);
+                       url_fputs(fh, "\n");
+                       url_fclose(fh);
+                       break;
+               }
+               case URL_READY_CANREAD: {
+                   string first = url_fgets(fh);
+                   if (first == "") return;
+                   int id = stoi(first);
+                   printf("from player %d:\n", id);
+                       for (string input; (input = url_fgets(fh)); ) {
+                               print(input, "\n");
+                       }
+                       url_fclose(fh);
+                       break;
+               }
+               case URL_READY_CLOSED: break;
+               case URL_READY_ERROR:
+               default:
+               {
+                       printf("Social_parse(): %s\n", ftos(status));
+                       break;
+               }
+       }
+}
+
+string autocvar_g_social_uri = "http://localhost:5000";
+
+void Social_send(int to, string msg)
+{
+    entity e = spawn(); e.message = msg;
+    if (crypto_getmyidstatus(0) > 0) {
+        url_single_fopen(
+            sprintf("%s/send?to=%d", autocvar_g_social_uri, to),
+            FILE_APPEND,
+            Social_parse,
+            e
+        );
+    }
+}
+
+void Social_recv()
+{
+    if (crypto_getmyidstatus(0) > 0) {
+        url_single_fopen(
+            sprintf("%s/recv", autocvar_g_social_uri),
+            FILE_APPEND,
+            Social_parse,
+            NULL
+        );
+    }
+}
+
+MUTATOR_HOOKFUNCTION(social, GameCommand) {
+    if (MUTATOR_RETURNVALUE) return false; // command was already handled
+    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));
+        Social_send(to, msg);
+        return true;
+    }
+    if (cmd_name == "srecv") {
+        Social_recv();
+        return true;
+    }
+    return false;
+}
+#endif