From 6d4c6df6927856433e50cf7b005e29fb226d082a Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Mon, 23 Jul 2018 09:38:58 -0400 Subject: [PATCH] Add UserData ioDevice for setting and getting username from blocks --- src/blocks/scratch3_sensing.js | 5 ++--- src/engine/runtime.js | 2 ++ src/io/userData.js | 24 ++++++++++++++++++++++++ test/unit/blocks_sensing.js | 4 +++- test/unit/io_userData.js | 25 +++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/io/userData.js create mode 100644 test/unit/io_userData.js diff --git a/src/blocks/scratch3_sensing.js b/src/blocks/scratch3_sensing.js index 9e1d37a5..18703d01 100644 --- a/src/blocks/scratch3_sensing.js +++ b/src/blocks/scratch3_sensing.js @@ -308,9 +308,8 @@ class Scratch3SensingBlocks { return 0; } - getUsername () { - // Logged out users get empty string. Return that for now. - return ''; + getUsername (args, util) { + return util.ioQuery('userData', 'getUsername'); } } diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 69b801c0..403768e0 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -21,6 +21,7 @@ const DeviceManager = require('../io/deviceManager'); const Keyboard = require('../io/keyboard'); const Mouse = require('../io/mouse'); const MouseWheel = require('../io/mouseWheel'); +const UserData = require('../io/userData'); const Video = require('../io/video'); const StringUtil = require('../util/string-util'); @@ -259,6 +260,7 @@ class Runtime extends EventEmitter { keyboard: new Keyboard(this), mouse: new Mouse(this), mouseWheel: new MouseWheel(this), + userData: new UserData(), video: new Video(this) }; diff --git a/src/io/userData.js b/src/io/userData.js new file mode 100644 index 00000000..54bd9666 --- /dev/null +++ b/src/io/userData.js @@ -0,0 +1,24 @@ +class UserData { + constructor () { + this._username = ''; + } + + /** + * Handler for updating the username + * @param {object} data Data posted to this ioDevice. + * @property {!string} username The new username. + */ + postData (data) { + this._username = data.username; + } + + /** + * Getter for username. Initially empty string, until set via postData. + * @returns {!string} The current username + */ + getUsername () { + return this._username; + } +} + +module.exports = UserData; diff --git a/test/unit/blocks_sensing.js b/test/unit/blocks_sensing.js index cf607d3b..102cd61a 100644 --- a/test/unit/blocks_sensing.js +++ b/test/unit/blocks_sensing.js @@ -3,6 +3,7 @@ const Sensing = require('../../src/blocks/scratch3_sensing'); const Runtime = require('../../src/engine/runtime'); const Sprite = require('../../src/sprites/sprite'); const RenderedTarget = require('../../src/sprites/rendered-target'); +const BlockUtility = require('../../src/engine/block-utility'); test('getPrimitives', t => { const rt = new Runtime(); @@ -147,7 +148,8 @@ test('loud? boolean', t => { test('username block', t => { const rt = new Runtime(); const sensing = new Sensing(rt); + const util = new BlockUtility(rt.sequencer); - t.equal(sensing.getUsername(), ''); + t.equal(sensing.getUsername({}, util), ''); t.end(); }); diff --git a/test/unit/io_userData.js b/test/unit/io_userData.js new file mode 100644 index 00000000..49dee9f9 --- /dev/null +++ b/test/unit/io_userData.js @@ -0,0 +1,25 @@ +const test = require('tap').test; +const UserData = require('../../src/io/userData'); + +test('spec', t => { + const userData = new UserData(); + + t.type(userData, 'object'); + t.type(userData.postData, 'function'); + t.type(userData.getUsername, 'function'); + t.end(); +}); + +test('getUsername returns empty string initially', t => { + const userData = new UserData(); + + t.strictEquals(userData.getUsername(), ''); + t.end(); +}); + +test('postData sets the username', t => { + const userData = new UserData(); + userData.postData({username: 'TEST'}); + t.strictEquals(userData.getUsername(), 'TEST'); + t.end(); +});