🎂 Houdini 150   🚚 FREE FEDEX SHIPPING from 397 USD   🌎 WORLDWIDE SHIPPING for 25 USD

Time Practice

Introduction

You should practice using the remote, so you can enter any time confidently without watching. The Time Practice mini-game is here to help you.

The app generates a random time, and you have to enter it correctly. When connected to a browser, it will also tell you the time with an Audio Assistant, and give you a feedback if you were successful or not.

Here's the MagiScript code, which is using some of the latest language features:

function main() {
  ps.connect('*');
  ps.print('Practice');
  events.send('Are...You...Ready?', 'voice');
  generateTime();
}

let time = '';  // the time entered
let targetTime; // the time you have to enter

// prints the target time and the time input
function printFormattedTime() {
    ps.print(`${targetTime.text}\n<${time}>`);
}

function generateTime() {
    targetTime = parseTime(rand(12), rand(60));
    events.send(targetTime.text, 'voice');
    printFormattedTime();
}

let timeout = 0; // will contain a timeout ID
let mapping = [ // button layour
  '1', '2', '3',
  '4', '5', '6',
  '7', '8', '9',
  'g', '0', 'x'
];

function check() {
  clearTimeout(timeout); // don't trigger send
  if (time === '') return; // nothing to set

  let enteredTime = parseTime(time);

  // is it correct?
  if (targetTime.text === enteredTime.text) {
      ps.print('CORRECT');
      events.send('THAT IS CORRECT', 'voice');
      atom.vibrate('.');
      setTimeout(() => {
          time = '';
          generateTime();
      }, 200);
  } else {
      ps.print('NOPE');
      events.send('NOT...CORRECT...', 'voice');
      atom.vibrate('=');
      setTimeout(() => {
          printFormattedTime();
      }, 200);
  }
}

function onButtonClick(buttonId) {
  let key = mapping[buttonId];
  
  // generate a new time
  if (key === 'g') {
    time = '';
    generateTime();
    return;
  }
  
  // delete the last digit or add a new
  if (key === 'x') {
    if (strLen(time) > 0) time = strSub(time, 0, strLen(time) - 1);
  } else {
    time = time + key;
  }

  // delete the previous timer, and start a new with 4s
  clearTimeout(timeout);
  timeout = setTimeout(check, 2000);

  // display the times
  printFormattedTime();
}

// there are events coming from the SB Watch and the buttons of Atom
function onEvent(e) {
  console.log(e.value, e.type, e.source);
  if (e.type === 'connected' && e.source === 'ps:ble') {
      atom.vibrate('\'');
  }
  if (e.type === 'click' && e.source === 'atom:button') {
    onButtonClick(parseInt(e.value));
  }
}
cross