aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <holden.rohrer@gmail.com>2019-12-27 14:13:48 -0500
committerHolden Rohrer <holden.rohrer@gmail.com>2019-12-27 14:13:48 -0500
commit790a01a32aa37684a5c39e5af6cb17d892212e79 (patch)
treebd413e7ada66ed2554b4d119ec33df6fcc2a5fd6
parent639ec59f1913a903dee3bb9068108b78c193d616 (diff)
added proto-.loc functionality to space.js
-rw-r--r--examples/jarvis.js2
-rw-r--r--space.js25
-rw-r--r--tests/space_fromfetch.js3
3 files changed, 18 insertions, 12 deletions
diff --git a/examples/jarvis.js b/examples/jarvis.js
index 9d4bb91..a78024b 100644
--- a/examples/jarvis.js
+++ b/examples/jarvis.js
@@ -43,7 +43,7 @@ function identity(sender){
let loc = locs[i];
if (read.has(loc)) clearTimeout(expire[loc]);
let locspace = new Space();
- locspace.fromfetch(tiles, [loc[0][0], loc[0][1], loc[1][0], loc[1][1]], conform=false);
+ locspace.fromfetch(tiles, [loc, loc], conform=false);
let results = read.add(locspace);
if (! results) respond(results);
expire[loc] = setTimeout(()=>{read.del(loc)}, 30000);
diff --git a/space.js b/space.js
index 50ae16c..c0cce60 100644
--- a/space.js
+++ b/space.js
@@ -3,6 +3,7 @@
// It provides combination between Spaces. [DONE]
// It also gives a search utility and a utility to grab an arbitrary section [DONE] [DONE]
const fs = require('fs')
+const vec = require('./utils/vec');
function chop(string, n){ // chops a string into n-sized chunks. Assumed to be perfect multiple
let arr = [];
@@ -11,6 +12,7 @@ function chop(string, n){ // chops a string into n-sized chunks. Assumed to be p
}
return arr;
}
+
function replace(text, old, repl){ //replaces, in an array `text`, the instances of `old` with `repl`
for (let i=0; i<text.length; i++){
if (text[i] == old) text[i] = repl;
@@ -18,16 +20,17 @@ function replace(text, old, repl){ //replaces, in an array `text`, the instances
return text;
}
-function Space(){ // CLASS
- this.data = []; //
- self = this;
- this.fromfetch = function(tiles, dimension, conform=true){ //tiles is straight from fetch/tileUpdate function, dimension is a quadruplet; conform is false for tileUpdate because the cell_props don't actually mean anything; all data is still included
- for (let y=dimension[0]; y<=dimension[2]; y++){
+function Space(){
+ this.data = [];
+ this.loc = []; // Coordinates to represent topleft location of spaces, and aid with .comb
+ this.fromfetch = function(tiles, dimension, conform=true){ //tiles is straight from fetch/tileUpdate function, dimension is a min/max pair of y/x coordinates; conform is false for tileUpdate because the cell_props don't actually mean anything; all data is still included
+ this.loc = vec.tileToChar(dimension[0]);
+ for (let y=dimension[0][0]; y<=dimension[1][0]; y++){
for (let line=0; line<8; line++)
this.data.push([]); // Adds lines
- for (let x=dimension[1]; x<=dimension[3]; x++){
- if (! tiles[[y,x]]) tilein({"content":' '.repeat(16*8),"properties":{"cell_props":{}}}); // Insert a null tile
- else tilein(tiles[[y,x]],y-dimension[0]);
+ for (let x=dimension[0][1]; x<=dimension[1][1]; x++){
+ if (! tiles[[y,x]]) tilein({"content":' '.repeat(16*8),"properties":{"cell_props":{}}}); // Insert a null tile if server sends null
+ else tilein.call(this, tiles[[y,x]], y-dimension[0][0]);
}
}
@@ -38,9 +41,9 @@ function Space(){ // CLASS
for (let line=0; line<8; line++){
curline = line+8*tilerow;
if (conform && incl.includes(line)){
- for (let i=0; i<16; i++) self.data[curline].push(' ');
+ for (let i=0; i<16; i++) this.data[curline].push(' ');
} else {
- Array.prototype.push.apply(self.data[curline],cont[read].split(''));
+ Array.prototype.push.apply(this.data[curline],cont[read].split(''));
read++;
}
}
@@ -54,12 +57,14 @@ function Space(){ // CLASS
}
return writes;
}
+
this.tofile = function(filename){
fs.writeFileSync(filename, this.print());
};
this.fromfile = function(filename){ //Reads an external file into internal data
this.adhoc(fs.readFileSync(filename,'utf8'));
}
+
this.adhoc = function(text){
text = text.split('\n')
text = text.map(row => row.split(''));
diff --git a/tests/space_fromfetch.js b/tests/space_fromfetch.js
index 67b41cc..e6c7846 100644
--- a/tests/space_fromfetch.js
+++ b/tests/space_fromfetch.js
@@ -3,7 +3,8 @@ const Space = require('../space')
newspace = new Space()
-newspace.fromfetch({"4,2": {"content": " hhhhhhhh k hone ns ", "properties": {"writability": null, "cell_props": {}}}},[4,2,4,2],conform=false);
+newspace.fromfetch({"4,2": {"content": " hhhhhhhh k hone ns ", "properties": {"writability": null, "cell_props": {}}}},[[4,2],[4,2]],conform=false);
// Straight from a real tileUpdate
+console.log(newspace.loc);
console.log(newspace.data); //Should be an array of arrays of chars, representing the content of "content" above.