Commits

mathematicalcoffee  committed 573421b

Added Unicode language-->exemplar characters dictionary (https://sites.google.com/site/cldr/) so now toons can give random speeches from character sets that are consistent within language. TODO: problems with null terminated strings and bad utf8 to JS conversion, should clean the dictionary to remove these ones. Also, many that don't render that should be filtered out on a per-user basis.

  • Participants
  • Parent commits 85b9dbf
  • Branches gnome3.6

Comments (0)

Files changed (3)

File xpenguins@mathematical.coffee.gmail.com/language_dictionary.json

+../../CHAR_CODES/common/main/language_dictionary.json

File xpenguins@mathematical.coffee.gmail.com/talk.js

  * - On email: show an email symbol (and so on...)
  * - read speeches from theme?
  * - generate random unicode speeches (but all from the same code block/language)
+ * - use a BoxPointer for the speech bbble?
  */
 const Clutter = imports.gi.Clutter;
 const Lang = imports.lang;
 const Mainloop = imports.mainloop;
 const St = imports.gi.St;
+const GLib = imports.gi.GLib;
 
-var Me;
-try {
-    Me = imports.ui.extensionSystem.extensions['xpenguins@mathematical.coffee.gmail.com'];
-} catch (err) {
-    Me = imports.misc.extensionUtils.getCurrentExtension().imports;
-}
-const Toon = Me.toon;
-const XPUtil = Me.util;
+const Me = imports.misc.extensionUtils.getCurrentExtension();
+const Toon = Me.imports.toon;
+const XPUtil = Me.imports.util;
 
 const SPEECHBUBBLE_TIMEOUT = 1000;
 
 Arrows[Toon.DOWNLEFT] = '\u2199';
 Arrows[Toon.DOWNRIGHT] = '\u2198';
 
+let LanguageDictionary, Languages = [], NumLanguages = 0;
+function init() {
+    let data = GLib.file_get_contents(
+        GLib.build_filenamev([Me.path, 'language_dictionary.json'])
+    )[1];
+
+    // Failed to convert UTF-8 string to JS string: Invalid byte sequence in conversion input
+    LanguageDictionary = JSON.parse(data);
+    Languages = Object.keys(LanguageDictionary);
+    NumLanguages = Languages.length;
+}
+if (!LanguageDictionary) {
+    init();
+}
+// TODO: avoid speeches in the locale of the user?
 const RandomSpeeches = [
-    'º(•♠•)º',
-    '╿︡O͟-O︠╿',
+// can't get some of these to render.
+//    'º(•♠•)º',
+//    '╿︡O͟-O︠╿',
+//    //TODO: these ones appear too often.
     '\u2af7\u00b0\u29ed\u00b0\u2af8', // ⫷ °⧭°⫸
     '\u2603', // ☃
     '\u2665', // ♥
     '\u2699', // ⚘ 
     '\u266B\u266A', // music
-    function (toon) { // TODO: check renderable...
-        let length = XPUtil.RandInt(3) + 1,
+    // random speech from a language's character set, 3-7 characters long.
+    function () {
+        let length = XPUtil.RandInt(5) + 3,
+            language = Languages[XPUtil.RandInt(NumLanguages)],
+            characters = LanguageDictionary[language],
+            n = characters.length,
+            speech = '';
+        if (n === null) {
+            log('nULL N: ' + language + ', ' + characters);
+        }
+        while (length--) {
+            speech += String.fromCharCode.apply('', characters[XPUtil.RandInt(n)]);
+            //speech += characters[XPUtil.RandInt(n)];
+        }
+        return speech;
+    },
+    // random speech from any unicode point, 1-2 characters long
+    function () {
+        let length = XPUtil.RandInt(2) + 1,
             speech = [];
-        while (length--) {
+        while (length--) { 
             speech.push(XPUtil.RandIntRange(32, 65535));
         }
-        speech = String.fromCharCode.apply('', speech);
-        // log(speech);
-        // BIG TODO: utf8 character can't be converted -- avoid these ones.
-        return speech;
+        return String.fromCharCode.apply('', speech);
     }
 ];
+
 const Speeches = {
     walker: {
         initial: function (toon) { return Arrows[toon.direction] }
                     return '';
                     break;
             }
+            return '';
         }
     },
     

File xpenguins@mathematical.coffee.gmail.com/xpenguins.js

                 } // whether sqashed
                 /* there is a small chance a toon will say something, if it
                  * isn't already saying something */
-                // little under 20%
                 if (!XPUtil.RandInt(100) && !toon.isSpeaking()) {
                     let speeches = Talk.Speeches[toon.type] ? Talk.Speeches[toon.type].during : null,
-                        speech = null;
-                    if (!speeches || !speeches.length) {
-                        speeches = Talk.RandomSpeeches;
-                    }
-                    speech = speeches[XPUtil.RandInt(speeches.length)];
+                        speech = null,
+                        numS = speeches ? speeches.length : 0;
+                    // you can always do a random speech.
+                    let speechN = XPUtil.RandInt(numS + Talk.RandomSpeeches.length);
+                    speech = speeches && speeches[speechN] || Talk.RandomSpeeches[speechN - numS];
                     if (typeof speech === 'function') {
                         speech = speech(toon);
                     }
-                    toon.speak(speech);
+                    try {
+                    if (speech) {
+                        toon.speak(speech);
+                    }
+                    } catch (e) {
+                        // TODO: when toons hdie so should their speech bubbles.
+                        // TODO: Embedded NULs
+                        // (I should probably loop through the dictionary and clean it).
+                        // TODO: failed to convert utf8 to JS
+                        log("HAD TROUBLE GIVING SPEECH: " + e.message);
+                    }
                 }
 
             } // toon state