B Thibault 9 سال پیش
والد
کامیت
c14cf329ed
4فایلهای تغییر یافته به همراه216 افزوده شده و 1 حذف شده
  1. 2 1
      crosswords.js
  2. 55 0
      src/Grid.js
  3. 84 0
      src/GridManager.js
  4. 75 0
      test.js

+ 2 - 1
crosswords.js

@@ -5,5 +5,6 @@ process.on("uncaughtException", function (err) {
     console.error('err uncaught Exception  : ', err);
 });
 
-require("./src/httpServer.js").serve(config);
+//require("./src/httpServer.js").serve(config);
+var g = new (require('./src/Grid.js').Grid)(require('./test.js'));
 

+ 55 - 0
src/Grid.js

@@ -0,0 +1,55 @@
+
+function GridCell(x, y) {
+    this.px = x;
+    this.py = y;
+    this.version = 0;
+}
+
+function LetterCell(x, y, letter) {
+    GridCell.call(this, x, y);
+    this.letter = letter;
+    this.found = false;
+}
+LetterCell.prototype = Object.create(GridCell);
+LetterCell.prototype.constructor = LetterCell;
+
+var debugFound = {};
+
+function DefinitionCell(x, y, letter, definitionArray, currentDefinition) {
+    if (debugFound[letter]) debugFound[letter].push({x:x,y:y}); else debugFound[letter] = [{x:x,y:y}];
+    GridCell.call(this, x, y);
+    this.definitions = [];
+}
+DefinitionCell.prototype = Object.create(GridCell);
+DefinitionCell.prototype.constructor = DefinitionCell;
+
+function parseGrid(grid, definitions, w, h) {
+    var currentDefinition = 0
+        ,resultGrid = [];
+
+    for (var i =0; i < h; i++) {
+        for (var j =0; j < w; j++) {
+            var c = grid[i][j];
+            if (c == c.toUpperCase()) {
+                resultGrid.push(new LetterCell(i, j, c));
+            } else {
+                var cell = new DefinitionCell(i, j, c, definitions, currentDefinition);
+                currentDefinition += cell.definitions.length;
+                resultGrid.push(cell);
+            }
+        }
+    }
+    console.log(debugFound);
+    return resultGrid;
+}
+
+function Grid(data) {
+    this.title = data["titre"];
+    this.difficulty = data["force"];
+    this.width = data["nbcaseslargeur"];
+    this.height = data["nbcaseshauteur"];
+    this.grid = parseGrid(data["grille"], data["definitions"], this.width, this.height);
+};
+
+module.exports.Grid = Grid;
+

+ 84 - 0
src/GridManager.js

@@ -0,0 +1,84 @@
+
+const http = require('http')
+    ,util = require('util')
+    ,Grid = require('./Grid.js').Grid;
+
+const RCILIST = 'http://rcijeux.fr/drupal_game/20minutes/menu/js/jeux_mfleches.js?date=1495635261516'
+    ,RCIGETGRID = 'http://rcijeux.fr/drupal_game/20minutes/grids/%s.mfj'
+    ,DEBUG_QUERY = true
+    ,CACHE_EXPIRACY = 86400000; // 1 day
+
+const GRID_CACHE = {
+    data: {},
+    expireAt: 0
+};
+
+function extractVar(data) {
+    var first = data.indexOf('{')
+        ,last = data.indexOf('}');
+
+    if (first == -1 || last == -1 || last < first)
+        return null;
+    try {
+        var dataObj;
+        console.log(data.substr(first, last));
+        eval("dataObj="+data.substr(first, last));
+        return dataObj;
+    } catch (e) {
+        return null;
+    }
+    return null; // unreachable
+}
+
+function getVar(url, cb) {
+    http.get(url, (res) => {
+        let chunks = null;
+        res.on('data', (chunk) => {
+            if (chunks)
+                chunks = Buffer.concat([chunks, chunk], chunks.length +chunk.length);
+            else
+                chunks = Buffer.from(chunk);
+        });
+        res.once('end', () => {
+            cb(extractVar(chunks.toString("utf8")));
+        });
+    });
+}
+
+module.exports.listGrids = function(cb) {
+    if (DEBUG_QUERY) {
+        console.warn("[gridManager] debug skip list grids");
+        cb({"240517":{ level: '1' }});
+    } else {
+        const now = Date.now();
+
+        if (GRID_CACHE.expireAt < now) {
+            console.log("[gridManager] list grids");
+            getVar(RCILIST, (grids) => {
+                if (grids) {
+                    let gridObjects = {};
+                    for (let gridId in grids) {
+                        gridObjects[gridId] = {
+                            "level": grids[gridId][1]
+                        };
+                    }
+                    GRID_CACHE.data = gridObjects;
+                    GRID_CACHE.expireAt = now +CACHE_EXPIRACY;
+                    cb(gridObjects);
+                } else {
+                    cb(null);
+                }
+            });
+        } else {
+            cb(GRID_CACHE.data);
+        }
+    }
+};
+
+module.exports.getGrid = function(gridId, cb) {
+    console.log("[gridManager] get grid " +gridId);
+    getVar(util.format(RCIGETGRID, gridId), (data) => {
+        cb(data ? new Grid(data) : null);
+    });
+};
+

+ 75 - 0
test.js

@@ -0,0 +1,75 @@
+module.exports = {
+"titre":"F2-Mfleches035",
+"legende":"",
+"force":"2",
+"nbcaseslargeur":12,
+"nbcaseshauteur":15,
+"grille":[
+"xLnAcDmOlAlV",
+"PENDfINSURGE",
+"rVERROUILLER",
+"DINEURaEVENT",
+"qTETAaGRESEE",
+"CETzzzzzzrPB",
+"dbTzzzzzzKIR",
+"ZOEzzzzzzdbE",
+"rNSzzzzzzVOS",
+"OEbnPmTcBbNb",
+"rREPARATRICE",
+"HESITERiAXEL",
+"rUTERINESzzz",
+"HSaTONaUEzzz",
+"aEDENaREEzzz"],
+definitions:[
+["S'ÉLÈVE","AU-","DESSUS","DU SOL"],
+["RETOMBE"],
+["SOUS LE","SOLEIL,","EXACTE–","MENT..."],
+["POUPÉES"],
+["COUTU–","RIER","FRANÇAIS"],
+["IL DOIT SE","PLIER AU","TRAVAIL"],
+["NON","COUVERT"],
+["VILLE","D'ARÈNES"],
+["ALGUE","VERTE"],
+["L'ATLAS","ET L'AXIS"],
+["PLANTE","ALPINE"],
+["REBELLE"],
+["LEVA","L'ARRIÈ–","RETRAIN"],
+["BLOQUER","LES","ISSUES"],
+["IL MANGE","LE SOIR"],
+["NARINE","DE","BALEINE"],
+["BUT DU","PETIT LAIT"],
+["DÉSIGNE","L'OBJET"],
+["PONCÉE À","LA MEULE"],
+["SYMBOLE","D'UN","LOURD"],
+["APÉRITIF","ROYAL"],
+["PRÉNOM","FÉMININ"],
+["QUI","IMPOSE","DES FRAIS"],
+["À","VOUS"],
+["ELLE VAUT","SON","PESANT","D'OR"],
+["AVANT","JÉSUS-","CHRIST"],
+["AXE DU","VENT"],
+["C'EST LE","MATIN","QU'ON LE","VOIT","ROUGIR"],
+["CHEF DE","SERVICE","HOSPI–","TALIER"],
+["FERVEUR"],
+["AFFLUENT","DE LA","GARONNE"],
+["BASE DE","CALCULS"],
+["SOUDÉE"],
+["NEUF À","ROME"],
+["ARTICLE","ESPAGNOL"],
+["QUI","REDONNE","DU PEP"],
+["NAGER OU","FLOTTER"],
+["SIMPLE OU","TRIPLE,","MAIS SUR","GLACE"],
+["COINCÉE"],
+["PAS","CONSAN–","GUINES"],
+["C'EST","CASSÉ"],
+["IL TOURNE","À L'AIGRE","EN CAS DE","DISPUTE"],
+["MOSAÏQUE","DE PAYS"],
+["JARDIN","DU","PARADIS"],
+["CRIE","COMME","UNE BÊTE"]
+],
+spountzV:[[2,9],[1,10],[1,14],[8,14]],
+spountzH:[],
+photos:[[6,4,9,9],[13,10,15,12]],
+concours:[],
+enonceconcours:""
+}