Merge branch 'master' of github.com:espruino/BangleApps

pull/1639/head
Gordon Williams 2022-03-30 11:10:33 +01:00
commit c3dc1ed885
15 changed files with 125 additions and 95 deletions

View File

@ -4,3 +4,4 @@
0.04: Bug fix score reset after Game Over, new icon 0.04: Bug fix score reset after Game Over, new icon
0.05: Chevron marker on the randomly added square 0.05: Chevron marker on the randomly added square
0.06: Fixed issue 1609 added a message popup state handler to control unwanted screen redraw 0.06: Fixed issue 1609 added a message popup state handler to control unwanted screen redraw
0.07: Optimized the mover algorithm for efficiency (work in progress)

View File

@ -164,16 +164,16 @@ const buttons = {
const mover = { const mover = {
direction: { direction: {
up: {name: 'up', step: 1, innerBegin: 0, innerEnd: rows-1, outerBegin: 0, outerEnd: cols-1, iter: rows -1, up: {name: 'up', step: 1, innerBegin: 0, innerEnd: rows-1, outerBegin: 0, outerEnd: cols-1, iter: rows -1,
sqIndex: function (m,n) {return m*(cols) + n;}, sqNextIndex: function (m,n) {return m < rows -1 ? (m+1)*(cols) + n : -1;} sqIndex: function (i,o) {return i*(cols) + o;}, sqNextIndex: function (i,o) {return i < rows -1 ? (i+1)*(cols) + o : -1;}
}, },
down: {name: 'down', step:-1, innerBegin: rows-1, innerEnd: 0, outerBegin: cols-1, outerEnd: 0, iter: rows -1, down: {name: 'down', step:-1, innerBegin: rows-1, innerEnd: 0, outerBegin: cols-1, outerEnd: 0, iter: rows -1,
sqIndex: function (m,n) {return m*(cols) + n;}, sqNextIndex: function (m,n) {return m > 0 ? (m-1)*(cols) + n : -1;} sqIndex: function (i,o) {return i*(cols) + o;}, sqNextIndex: function (i,o) {return i > 0 ? (i-1)*(cols) + o : -1;}
}, },
left: {name: 'left', step: 1, innerBegin: 0, innerEnd: cols-1, outerBegin: 0, outerEnd: rows-1, iter: cols -1, left: {name: 'left', step: 1, innerBegin: 0, innerEnd: cols-1, outerBegin: 0, outerEnd: rows-1, iter: cols -1,
sqIndex: function (m,n) {return n*(rows) + m;}, sqNextIndex: function (m,n) {return m < cols -1 ? n*(rows) + m +1 : -1;} sqIndex: function (i,o) {return o*(rows) + i;}, sqNextIndex: function (i,o) {return i < cols -1 ? o*(rows) + i +1 : -1;}
}, },
right: {name: 'right', step:-1, innerBegin: cols-1, innerEnd: 0, outerBegin: rows-1, outerEnd: 0, iter: cols -1, right: {name: 'right', step:-1, innerBegin: cols-1, innerEnd: 0, outerBegin: rows-1, outerEnd: 0, iter: cols -1,
sqIndex: function (m,n) {return n*(rows) + m;}, sqNextIndex: function (m,n) {return m > 0 ? n*(rows) + m -1: -1;} sqIndex: function (i,o) {return o*(rows) + i;}, sqNextIndex: function (i,o) {return i > 0 ? o*(rows) + i -1: -1;}
} }
}, },
anyLeft: function() { anyLeft: function() {
@ -207,49 +207,39 @@ const mover = {
}); });
return canContinue; return canContinue;
}, },
nonEmptyCells: function (dir) { moveAndMerge: function (dir) {
debug(() => console.log("Move: ", dir.name));
const step = dir.step; const step = dir.step;
// outer loop for all colums/rows // outer loop for all colums/rows
for (let n = dir.outerBegin; step*n <= step*dir.outerEnd; n=n+step) { for (let o = dir.outerBegin; step*o <= step*dir.outerEnd; o=o+step) {
// let rowStr = '| ';
// Move a number of iteration with the squares to move them all to one side let allVals = allSquares.map(sq=>{return sq.getExpVal()});
for (let iter = 0; iter < dir.iter; iter++) { let allLineVals = [];
// lets move squares one position in a row or column, counting backwards starting from the and where the squares will end up
for (let m = dir.innerBegin; step*m <= step*dir.innerEnd; m=m+step) { for (let m = dir.innerBegin; step*m <= step*dir.innerEnd; m=m+step) {
// get the array of squares index for current cell allLineVals.push(allVals[dir.sqIndex(m,o)]);
const idx = dir.sqIndex(m,n); }
const nextIdx = dir.sqNextIndex(m,n);
if (allSquares[idx].expVal == 0 && nextIdx >= 0) { let sortedLineVals = allLineVals.filter((val)=>{return val>0;});
allSquares[idx].setExpVal(allSquares[nextIdx].expVal); let zeroLineVals = allLineVals.filter((val)=>{return val==0;});
allSquares[nextIdx].setExpVal(0); // merge the equally valued adjacent cells
let r=0;
while (r<sortedLineVals.length-1) {
if (sortedLineVals[r] == sortedLineVals[r+1]) {
++sortedLineVals[r];
addToScore(sortedLineVals[r]);
sortedLineVals[++r] = 0;
} }
r++;
} }
} let mergedLineVals = sortedLineVals.filter((val)=>{return val>0;});
} sortedLineVals.filter((val)=>{return val==0;}).forEach((zero)=>{mergedLineVals.push(zero);});
}, zeroLineVals.forEach((zero)=>{mergedLineVals.push(zero);});
// add up the conjacent squares with identical values en set next square to empty in the process
mergeEqlCells: function(dir) { let i = 0;
const step = dir.step;
// outer loop for all colums/rows
for (let n = dir.outerBegin; step*n <= step*dir.outerEnd; n=n+step) {
// lets move squares one position in a row or column, counting backwards starting from the and where the squares will end up
for (let m = dir.innerBegin; step*m <= step*dir.innerEnd; m=m+step) { for (let m = dir.innerBegin; step*m <= step*dir.innerEnd; m=m+step) {
const idx = dir.sqIndex(m,n); let idx = dir.sqIndex(m,o);
const nextIdx = dir.sqNextIndex(m,n); allSquares[idx].setExpVal(mergedLineVals[i++]);
if ((allSquares[idx].expVal > 0) && nextIdx >= 0) {
if (allSquares[idx].expVal == allSquares[nextIdx].expVal) {
let expVal = allSquares[idx].expVal;
allSquares[idx].setExpVal(++expVal);
allSquares[idx].addToScore();
allSquares[nextIdx].setExpVal(0);
}
}
} }
debug(()=>console.log("new allSquares values:", allSquares.map(sq=>{return sq.expVal})));
} }
} }
}; };
@ -301,7 +291,7 @@ class Button {
} }
class Cell { class Cell {
constructor(x0, y0, width, idx, cb) { constructor(x0, y0, width, idx) {
this.x0 = x0; this.x0 = x0;
this.y0 = y0; this.y0 = y0;
this.x1 = x0 + width; this.x1 = x0 + width;
@ -309,7 +299,7 @@ class Cell {
this.expVal = 0; this.expVal = 0;
this.previousExpVals=[]; this.previousExpVals=[];
this.idx = idx; this.idx = idx;
this.cb = cb; // this.cb = cb;
this.isRndm = false; this.isRndm = false;
this.ax = x0; this.ax = x0;
this.ay = Math.floor(0.2*width+y0); this.ay = Math.floor(0.2*width+y0);
@ -345,6 +335,9 @@ class Cell {
.drawString(char, strX, strY); .drawString(char, strX, strY);
} }
} }
getExpVal() {
return this.expVal;
}
setExpVal(val) { setExpVal(val) {
this.expVal = val; this.expVal = val;
} }
@ -364,10 +357,6 @@ class Cell {
removeUndo() { removeUndo() {
this.previousExpVals=[0]; this.previousExpVals=[0];
} }
addToScore() {if (typeof this.cb === 'function') {
this.cb(this.expVal);
}
}
setRndmFalse() { setRndmFalse() {
this.isRndm = false; this.isRndm = false;
} }
@ -416,7 +405,7 @@ function createGrid () {
for (let c = 0; c < cols; c++) { for (let c = 0; c < cols; c++) {
let x0 = borderWidth + c*(borderWidth + sqWidth) - (rows/2)*(2*borderWidth + sqWidth) + middle.x + Math.floor(sqWidth/3); let x0 = borderWidth + c*(borderWidth + sqWidth) - (rows/2)*(2*borderWidth + sqWidth) + middle.x + Math.floor(sqWidth/3);
let y0 = borderWidth + r*(borderWidth + sqWidth) - (cols/2)*(2*borderWidth + sqWidth) + middle.y + Math.floor(sqWidth/3); let y0 = borderWidth + r*(borderWidth + sqWidth) - (cols/2)*(2*borderWidth + sqWidth) + middle.y + Math.floor(sqWidth/3);
let cell = new Cell(x0, y0, sqWidth, c + r*cols, addToScore); let cell = new Cell(x0, y0, sqWidth, c + r*cols);
allSquares.push(cell); allSquares.push(cell);
} }
} }
@ -651,9 +640,7 @@ dragger.attach();
function runGame(dir){ function runGame(dir){
addToUndo(); addToUndo();
updUndoLvlIndex(); updUndoLvlIndex();
mover.nonEmptyCells(dir); mover.moveAndMerge(dir);
mover.mergeEqlCells(dir);
mover.nonEmptyCells(dir);
allSquares.forEach(sq => {sq.setRndmFalse();}); allSquares.forEach(sq => {sq.setRndmFalse();});
addRandomNumber(); addRandomNumber();
drawGrid(); drawGrid();

View File

@ -1,7 +1,7 @@
{ "id": "game1024", { "id": "game1024",
"name": "1024 Game", "name": "1024 Game",
"shortName" : "1024 Game", "shortName" : "1024 Game",
"version": "0.06", "version": "0.07",
"icon": "game1024.png", "icon": "game1024.png",
"screenshots": [ {"url":"screenshot.png" } ], "screenshots": [ {"url":"screenshot.png" } ],
"readme":"README.md", "readme":"README.md",

View File

@ -1 +1,2 @@
0.01: New App! 0.01: New App!
0.02: Add sunrise/sunset. Fix timer bugs.

View File

@ -11,6 +11,7 @@ A clock based on the Anton Clock with stopwatches, timers and alarms based on th
* alarms * alarms
* multiple stopwatches, timers and alarms * multiple stopwatches, timers and alarms
* stopwatches and timers keep running in the background * stopwatches and timers keep running in the background
* optional time of sunrise/sunset using the My Location app - hidden by default
## Images ## Images

View File

@ -1 +0,0 @@
{"id":"timerclk","name":"tclk Alarm","src":"timerclk.alarm.js","icon":"timerclk.img","version":"0.01","tags":"","files":"","sortorder":10}

File diff suppressed because one or more lines are too long

View File

@ -1,26 +1,17 @@
var timerclkTimerTimeout; var timerclkTimerTimeout;
var timerclkAlarmTimeout; var timerclkAlarmTimeout;
function timerclkCheckTimers() { function timerclkCheckTimers() {
var expiresIn=require("timerclk.lib.js").timerExpiresIn;
if (timerclkTimerTimeout) clearTimeout(timerclkTimerTimeout); if (timerclkTimerTimeout) clearTimeout(timerclkTimerTimeout);
var timers = require('Storage').readJSON('timerclk.timer.json',1)||[]; var timers = require('Storage').readJSON('timerclk.timer.json',1)||[];
timers = timers.filter(e=>e.start); timers = timers.filter(e=>e.start);
if (timers.length) { if (timers.length) {
timers = timers.sort((a,b)=>{ timers = timers.sort((a,b)=>expiresIn(a)-expiresIn(b));
var at = a.timeAdd;
if (a.start) at += Date.now()-a.start;
at = a.period-at;
var bt = b.timeAdd;
if (b.start) bt += Date.now()-b.start;
bt = b.period-bt;
return at-bt;
});
if (!require('Storage').read("timerclk.timer.alert.js")) { if (!require('Storage').read("timerclk.timer.alert.js")) {
console.log("No timer app!"); console.log("No timer app!");
} else { } else {
var time = timers[0].timeAdd; var time = expiresIn(timers[0]);
if (timers[0].start) time += Date.now()-timers[0].start; if (time<1000) time=1000;
time = timers[0].time - time;
if (time<1000) t=1000;
if (timerclkTimerTimeout) clearTimeout(timerclkTimerTimeout); if (timerclkTimerTimeout) clearTimeout(timerclkTimerTimeout);
timerclkTimerTimeout = setTimeout(() => load("timerclk.timer.alert.js"),time); timerclkTimerTimeout = setTimeout(() => load("timerclk.timer.alert.js"),time);
} }
@ -38,7 +29,7 @@ function timerclkCheckAlarms() {
} else { } else {
var time = alarms[0].time-currentTime; var time = alarms[0].time-currentTime;
if (alarms[0].last == new Date().getDate() || time < 0) time += 86400000; if (alarms[0].last == new Date().getDate() || time < 0) time += 86400000;
if (time<1000) t=1000; if (time<1000) time=1000;
if (timerclkAlarmTimeout) clearTimeout(timerclkAlarmTimeout); if (timerclkAlarmTimeout) clearTimeout(timerclkAlarmTimeout);
timerclkAlarmTimeout = setTimeout(() => load("timerclk.alarm.alert.js"),time); timerclkAlarmTimeout = setTimeout(() => load("timerclk.alarm.alert.js"),time);
} }

View File

@ -125,3 +125,5 @@ exports.registerControls = function(o) {
}); });
} }
}; };
exports.timerExpiresIn=t=>t.time-(Date.now()-t.start);

View File

@ -2,7 +2,7 @@
"id": "timerclk", "id": "timerclk",
"name": "Timer Clock", "name": "Timer Clock",
"shortName":"Timer Clock", "shortName":"Timer Clock",
"version":"0.01", "version":"0.02",
"description": "A clock with stopwatches, timers and alarms build in.", "description": "A clock with stopwatches, timers and alarms build in.",
"icon": "app-icon.png", "icon": "app-icon.png",
"type": "clock", "type": "clock",
@ -28,10 +28,7 @@
{"name":"timerclk.timer.js","url":"timer.js"}, {"name":"timerclk.timer.js","url":"timer.js"},
{"name":"timerclk.timer.alert.js","url":"timer.alert.js"}, {"name":"timerclk.timer.alert.js","url":"timer.alert.js"},
{"name":"timerclk.alarm.js","url":"alarm.js"}, {"name":"timerclk.alarm.js","url":"alarm.js"},
{"name":"timerclk.alarm.alert.js","url":"alarm.alert.js"}, {"name":"timerclk.alarm.alert.js","url":"alarm.alert.js"}
{"name":"timerclk.stopwatch.info","url":"stopwatch.info"},
{"name":"timerclk.timer.info","url":"timer.info"},
{"name":"timerclk.alarm.info","url":"alarm.info"}
], ],
"data": [{"name":"timerclk.json"},{"name":"timerclk.stopwatch.json"},{"name":"timerclk.timer.json"},{"name":"timerclk.alarm.json"}], "data": [{"name":"timerclk.json"},{"name":"timerclk.stopwatch.json"},{"name":"timerclk.timer.json"},{"name":"timerclk.alarm.json"}],
"sortorder": 0 "sortorder": 0

View File

@ -12,9 +12,12 @@
"dowFontSize":2, "dowFontSize":2,
"specialFont":"6x8", "specialFont":"6x8",
"specialFontSize":2, "specialFontSize":2,
"srssFont":"6x8",
"srssFontSize":2,
"shortDate":true, "shortDate":true,
"showStopwatches":true, "showStopwatches":true,
"showTimers":true, "showTimers":true,
"showSrss":false,
}, settings.clock||{}); }, settings.clock||{});
settings.stopwatch = Object.assign({ settings.stopwatch = Object.assign({
"font":"Vector", "font":"Vector",
@ -108,6 +111,23 @@
writeSettings(); writeSettings();
} }
}, },
"sun font":{
value: 0|g.getFonts().indexOf(settings.clock.srssFont),
format: v => g.getFonts()[v],
min: 0, max: g.getFonts().length-1,
onchange: v => {
settings.clock.srssFont = g.getFonts()[v];
writeSettings();
}
},
"sun size":{
value: 0|settings.clock.srssFontSize,
min: 0,
onchange: v => {
settings.clock.srssFontSize = v;
writeSettings();
}
},
"short date": { "short date": {
value: !!settings.clock.shortDate, value: !!settings.clock.shortDate,
format: BOOL_FORMAT, format: BOOL_FORMAT,
@ -132,6 +152,14 @@
writeSettings(); writeSettings();
} }
}, },
"sun times": {
value: !!settings.clock.showSrss,
format: v=>v?/*LANG*/"Show":/*LANG*/"Hide",
onchange: v => {
settings.clock.showSrss = v;
writeSettings();
}
},
}; };
var stopwatchMenu = { var stopwatchMenu = {

View File

@ -1 +0,0 @@
{"id":"timerclk","name":"tclk Stopwatch","src":"timerclk.stopwatch.js","icon":"timerclk.img","version":"0.01","tags":"","files":"","sortorder":10}

View File

@ -14,10 +14,7 @@ function showTimer(timer) {
buttons : {/*LANG*/"Ok":true} buttons : {/*LANG*/"Ok":true}
}).then(function(ok) { }).then(function(ok) {
buzzCount = 0; buzzCount = 0;
if (ok) {
timer.time += Date.now() - timer.start;
timer.start = null; timer.start = null;
}
require("Storage").write("timerclk.timer.json",JSON.stringify(timers)); require("Storage").write("timerclk.timer.json",JSON.stringify(timers));
load(); load();
}); });
@ -45,16 +42,8 @@ console.log("checking for timers...");
var timers = require("Storage").readJSON("timerclk.timer.json",1)||[]; var timers = require("Storage").readJSON("timerclk.timer.json",1)||[];
var active = timers.filter(e=>e.start); var active = timers.filter(e=>e.start);
if (active.length) { if (active.length) {
// if there's an timer, show it // if there's an active timer, show it
active = active.sort((a,b)=>{ active = active.sort((a,b)=>timerclk.timerExpiresIn(a)-timerclk.timerExpiresIn(b));
var at = a.time;
if (a.start) at += Date.now()-a.start;
at = a.period-at;
var bt = b.time;
if (b.start) bt += Date.now()-b.start;
bt = b.period-bt;
return at-bt;
});
showTimer(active[0]); showTimer(active[0]);
} else { } else {
// otherwise just go back to default app // otherwise just go back to default app

View File

@ -1 +0,0 @@
{"id":"timerclk","name":"tclk Timer","src":"timerclk.timer.js","icon":"timerclk.img","version":"0.01","tags":"","files":"","sortorder":10}

View File

@ -34,18 +34,20 @@ function update() {
} }
function play() { function play() {
if (all[current].start) { // running if (all[current].start) { // running
all[current].timeAdd += Date.now() - all[current].start; all[current].timeAdd = Date.now() - all[current].start;
all[current].start = null; all[current].start = null;
update(); update();
} else { // paused } else { // paused
all[current].start = Date.now(); all[current].start = Date.now() - all[current].timeAdd;
all[current].timeAdd = 0;
update(); update();
} }
require("Storage").write("timerclk.timer.json",JSON.stringify(all)); require("Storage").write("timerclk.timer.json",JSON.stringify(all));
timerclkCheckTimers(); timerclkCheckTimers();
} }
function reset() { function reset() {
all[current] = defaultElement.clone(); all[current].start = null;
all[current].timeAdd = 0;
update(); update();
require("Storage").write("timerclk.timer.json",JSON.stringify(all)); require("Storage").write("timerclk.timer.json",JSON.stringify(all));
timerclkCheckTimers(); timerclkCheckTimers();