Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TimeKeeper to extension list #159

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,14 @@ export default [
creator: "WAYLIVES",
isGitHub: false,
},
{
name: "TimeKeeper",
description: "Adds extra blocks to the editor that are related to the timer reporter.",
code: "SparkyCreations/TimeKeeper.js",
banner: "SparkyCreations/TimeKeeper.js",
creator: "SparkyCreations",
isGithub: true,
},
// Does not follow the main guideline of "Your extension should be created for a specific purpose." Description is misleading.
// {
// name: "Randomly Blocks",
Expand Down
211 changes: 211 additions & 0 deletions static/extensions/SparkyCreations/TimeKeeper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
// Name: TimeKeeper
// ID: TimeSparkyCreations
// Description: Extra blocks to control the timer reporter.
// By: SparkyCreations

// Version 1.0.0

(function(Scratch) {
class PauseResumeExtension {
constructor(runtime) {
this.runtime = runtime;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused variable

this.timers = {};
this.countUpValues = {};
this.intervals = {};
}
getInfo() {
return {
id: "TimeSparkyCreations",
name: "TimeKeeper",
color1: "#5CB1D6",
blocks: [
{
opcode: "pauseClock",
blockType: Scratch.BlockType.COMMAND,
text: "Pause Clock"
},
{
opcode: "resumeClock",
blockType: Scratch.BlockType.COMMAND,
text: "Resume Clock"
},
{
opcode: "reportTimerInSeconds",
blockType: Scratch.BlockType.REPORTER,
text: "report timer in seconds"
},
{
opcode: "reportTimerInMinutes",
blockType: Scratch.BlockType.REPORTER,
text: "report timer in minutes"
},
{
opcode: "reportTimerInHours",
blockType: Scratch.BlockType.REPORTER,
text: "report timer in hours"
},
{
opcode: "reportTimerInInputSeconds",
blockType: Scratch.BlockType.REPORTER,
text: "report timer in [SECONDS] seconds",
arguments: {
SECONDS: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 1
},
},
},
{
opcode: "timePaused",
blockType: Scratch.BlockType.REPORTER,
text: "Time Paused"
},
{
opcode: "createTimerNamed",
blockType: Scratch.BlockType.COMMAND,
text: "create timer named [TIMERNAME] count up by [COUNTUP]",
arguments: {
TIMERNAME: {
type: Scratch.ArgumentType.STRING,
defaultValue: "Timer"
},
COUNTUP: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 1
},
},
},
{
opcode: "reportTimers",
blockType: Scratch.BlockType.REPORTER,
text: "report timers"
},
{
opcode: "setTimer",
blockType: Scratch.BlockType.COMMAND,
text: "set timer [NAME] to [VALUE]",
arguments: {
NAME: {
type: Scratch.ArgumentType.STRING,
defaultValue: "Timer"
},
VALUE: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 0
},
},
},
{
opcode: "deleteAllTimers",
blockType: Scratch.BlockType.COMMAND,
text: "delete all timers"
},
{
opcode: "deleteTimerNamed",
blockType: Scratch.BlockType.COMMAND,
text: "delete timer [TIMERNAME]",
arguments: {
TIMERNAME: {
type: Scratch.ArgumentType.STRING,
defaultValue: "Timer"
},
},
},
],
};
}

pauseClock() {
if (Scratch.vm.runtime.ioDevices.clock && typeof Scratch.vm.runtime.ioDevices.clock.pause === "function") {
Scratch.vm.runtime.ioDevices.clock.pause();
}
}

resumeClock() {
if (Scratch.vm.runtime.ioDevices.clock && typeof Scratch.vm.runtime.ioDevices.clock.resume === "function") {
Scratch.vm.runtime.ioDevices.clock.resume();
}
}

reportTimerInSeconds() {
if (Scratch.vm.runtime.ioDevices.clock && typeof Scratch.vm.runtime.ioDevices.clock.projectTimer === "function") {
const timer = Scratch.vm.runtime.ioDevices.clock.projectTimer();
return timer / 1000;
}
return 0;
}

reportTimerInMinutes() {
if (Scratch.vm.runtime.ioDevices.clock && typeof Scratch.vm.runtime.ioDevices.clock.projectTimer === "function") {
const timer = Scratch.vm.runtime.ioDevices.clock.projectTimer();
return timer / (1000 * 60);
}
return 0;
}

reportTimerInHours() {
if (Scratch.vm.runtime.ioDevices.clock && typeof Scratch.vm.runtime.ioDevices.clock.projectTimer === "function") {
const timer = Scratch.vm.runtime.ioDevices.clock.projectTimer();
return timer / (1000 * 60 * 60);
}
return 0;
}

async reportTimerInInputSeconds(args) {
const seconds = args.SECONDS;
await new Promise(resolve => setTimeout(resolve, seconds * 1000));
if (Scratch.vm.runtime.ioDevices.clock && typeof Scratch.vm.runtime.ioDevices.clock.projectTimer === "function") {
return Scratch.vm.runtime.ioDevices.clock.projectTimer();
}
return 0;
}

timePaused() {
if (Scratch.vm.runtime.ioDevices.clock && Scratch.vm.runtime.ioDevices.clock._projectTimer) {
return Scratch.vm.runtime.ioDevices.clock._pausedTime
}
return 0;
}

createTimerNamed(args) {
const timerName = args.TIMERNAME;
const countUpValue = Scratch.Cast.toNumber(args.COUNTUP);
if (!this.timers.hasOwnProperty(timerName)) {
this.timers[timerName] = 0;
this.countUpValues[timerName] = countUpValue;
this.intervals[timerName] = setInterval(() => {
this.timers[timerName] += this.countUpValues[timerName];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't do this, as many timers all doing this at once will cause lag.

You should set up a separate function that updates all values at once instead of individually

}, 1);
}
}

reportTimers() { return JSON.stringify(this.timers) }

setTimer(args) {
const timerName = args.NAME;
const value = Scratch.Cast.toNumber(args.VALUE);
if (this.timers.hasOwnProperty(timerName)) this.timers[timerName] = value;
}

deleteAllTimers() {
this.timers = {};
this.countUpValues = {};
for (const timerName in this.intervals) {
clearInterval(this.intervals[timerName]);
}
this.intervals = {};
}

deleteTimerNamed(args) {
const timerName = args.TIMERNAME;
delete this.timers[timerName];
delete this.countUpValues[timerName];
if (this.intervals.hasOwnProperty(timerName)) {
clearInterval(this.intervals[timerName]);
delete this.intervals[timerName];
}
}
}

Scratch.extensions.register(new PauseResumeExtension());
})(Scratch);
1 change: 1 addition & 0 deletions static/images/SparkyCreations/TimeKeeper.svg
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thumbnail should be 600x300. Otherwise it will be stretched

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.