Skip to content

Commit

Permalink
Implement cloud variables (#14)
Browse files Browse the repository at this point in the history
Implement cloud variables and cloud lists
  • Loading branch information
NoMod-Programming authored Aug 14, 2016
1 parent b2f237b commit 5d8dc86
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 3 deletions.
Binary file modified bin/Scratch.swf
Binary file not shown.
Binary file modified bin/ScratchFor10.2.swf
Binary file not shown.
19 changes: 18 additions & 1 deletion src/Specs.as
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public class Specs {
[15, "Program", 0x026f2e],
[16, "Strings", 0x949400],
[17, "Websockets", 0x39bf93],
[18, "Color", 0x2e2e2e]
[18, "Color", 0x2e2e2e],
[29, "Data", variableColor],
[32, "List", listColor],
];

public static function blockColor(categoryID:int):int {
Expand Down Expand Up @@ -436,6 +438,11 @@ public class Specs {
["-"],
["@cookie set cookie variable %s to %s", " ", 9, "cookieSetVariable"],
["@cookie change cookie variable %s by %n", " ", 9, "cookieChangeVariable"],
["--"],
["☁ %s", "r", 29, "getCloud"],
["-"],
["set ☁ variable %s to %s", " ", 29, "cloudSet"],
["change ☁ variable %s by %n", " ", 29, "cloudChange"],

// lists
["add %s to %m.list", " ", 12, "append:toList:"],
Expand All @@ -451,6 +458,16 @@ public class Specs {
["show list %m.list", " ", 12, "showList:"],
["hide list %m.list", " ", 12, "hideList:"],
["set %m.list color to %c", " ", 12, "listSet:colorTo:"],
["--"],
["add %s to ☁ list %s", " ", 32, "cloudAdd"],
["-"],
["delete %d.listDeleteItem of ☁ list %s", " ", 32, "cloudDelete"],
["insert %s at %d.listItem of ☁ list %s", " ", 32, "cloudInsert"],
["replace item %d.listItem of ☁ list %s with %s", " ", 32, "cloudReplace"],
["-"],
["item %d.listItem of ☁ list %s", "r", 32, "cloudGetItem"],
["length of ☁ list %s", "r", 32, "cloudLength"],
["☁ list %s contains %s", "b", 32, "cloudContains"],

//System 13
["save %s to file %s", " ", 13, "save:toFile:", "hello world", "file.txt"],
Expand Down
192 changes: 192 additions & 0 deletions src/interpreter/Interpreter.as
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ import extensions.ExtensionManager;
import flash.geom.Point;
import flash.utils.Dictionary;
import flash.utils.getTimer;
import flash.net.*;
import flash.events.Event;

import primitives.*;

Expand Down Expand Up @@ -105,6 +107,8 @@ public class Interpreter {
static private var returnBlock:Block = new Block('', '', 0, 'doReturn', [0]);
static private var report0Block:Block = new Block('%s', '', 0, 'report', [0]);

private var cloudServerUrl:String = "http://blueapi.gwiddle.co.uk/cloud.php/";

public function Interpreter(app:Scratch) {
this.app = app;
initPrims();
Expand Down Expand Up @@ -547,6 +551,18 @@ public class Interpreter {
primTable["getDefinedVars"] = primGetDefinedVars;
primTable["setDefinedVars"] = primSetDefinedVars;
// primTable["varSet:colorTo:"] = primVarSetColor;
specialTable["getCloud"] = primGetCloud;
specialTable["cloudSet"] = primSetCloud;
specialTable["cloudChange"] = primChangeCloud;

// cloud lists
specialTable["cloudAdd"] = primCloudAdd;
specialTable["cloudDelete"] = primCloudDelete;
specialTable["cloudInsert"] = primCloudInsert;
specialTable["cloudReplace"] = primCloudReplace;
specialTable["cloudGetItem"] = primCloudGetItem;
specialTable["cloudLength"] = primCloudLength;
specialTable["cloudContains"] = primCloudContains;

// edge-trigger hat blocks
primTable["whenDistanceLessThan"] = primNoop;
Expand Down Expand Up @@ -898,4 +914,180 @@ public class Interpreter {
}
}

private function primGetCloud(b:Array):void {
if (activeThread.firstTime) {
activeThread.firstTime = false;
var request:URLRequest = new URLRequest((cloudServerUrl + "get/" + escape(encodeURIComponent((b[0]).toString()))));
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, dataGet);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
function dataGet(event:Event):void {
activeThread.popState();
activeThread.values.push(event.target.data.toString());
activeThread.firstTime = true;
}
}
doYield();
}

private function primSetCloud(b:Array):void {
if (activeThread.firstTime) {
var request:URLRequest = new URLRequest((cloudServerUrl + "set/" + escape(encodeURIComponent((b[0]).toString())) + "/" + escape(encodeURIComponent((b[1].toString())))));
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, dataGet);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
activeThread.firstTime = false;
function dataGet(event:Event):void {
var block = activeThread.block;
activeThread.popState();
if (block.nextBlock) activeThread.pushStateForBlock(block.nextBlock);
activeThread.firstTime = true;
}
}
doYield();
}

private function primChangeCloud(b:Array):void {
if (activeThread.firstTime) {
var request:URLRequest = new URLRequest((cloudServerUrl + "change/" + escape(encodeURIComponent((b[0]).toString())) + "/" + escape(encodeURIComponent((b[1].toString())))));
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, dataGet);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
activeThread.firstTime = false;
function dataGet(event:Event):void {
var block = activeThread.block;
activeThread.popState();
if (block.nextBlock) activeThread.pushStateForBlock(block.nextBlock);
activeThread.firstTime = true;
}
}
doYield();
}

private function primCloudAdd(b:Array):void {
if (activeThread.firstTime) {
var request:URLRequest = new URLRequest((cloudServerUrl + "listadd/" + escape(encodeURIComponent((b[0]).toString())) + "/" + escape(encodeURIComponent((b[1]).toString()))));
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, dataGet);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
activeThread.firstTime = false;
function dataGet(event:Event):void {
var block = activeThread.block;
activeThread.popState();
if (block.nextBlock) activeThread.pushStateForBlock(block.nextBlock);
activeThread.firstTime = true;
}
}
doYield();
}

private function primCloudDelete(b:Array):void {
if (activeThread.firstTime) {
var request:URLRequest = new URLRequest((cloudServerUrl + "listdelete/" + escape(encodeURIComponent((b[0]).toString())) + "/" + escape(encodeURIComponent((b[1]).toString()))));
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, dataGet);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
activeThread.firstTime = false;
function dataGet(event:Event):void {
var block = activeThread.block;
activeThread.popState();
if (block.nextBlock) activeThread.pushStateForBlock(block.nextBlock);
activeThread.firstTime = true;
}
}
doYield();
}


private function primCloudInsert(b:Array):void {
if (activeThread.firstTime) {
var request:URLRequest = new URLRequest((cloudServerUrl + "listinsert/" + escape(encodeURIComponent((b[0]).toString())) + "/" + escape(encodeURIComponent((b[1]).toString())) + "/" + (escape(encodeURIComponent((b[2]).toString())))));
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, dataGet);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
activeThread.firstTime = false;
function dataGet(event:Event):void {
var block = activeThread.block;
activeThread.popState();
if (block.nextBlock) activeThread.pushStateForBlock(block.nextBlock);
activeThread.firstTime = true;
}
}
doYield();
}

private function primCloudReplace(b:Array):void {
if (activeThread.firstTime) {
var request:URLRequest = new URLRequest((cloudServerUrl + "listreplace/" + escape(encodeURIComponent((b[0]).toString())) + "/" + escape(encodeURIComponent((b[1]).toString())) + "/" + escape(encodeURIComponent((b[2]).toString()))));
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, dataGet);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
activeThread.firstTime = false;
function dataGet(event:Event):void {
var block = activeThread.block;
activeThread.popState();
if (block.nextBlock) activeThread.pushStateForBlock(block.nextBlock);
activeThread.firstTime = true;
}
}
doYield();
}

private function primCloudGetItem(b:Array):void {
if (activeThread.firstTime) {
var request:URLRequest = new URLRequest((cloudServerUrl + "listgetitem/" + escape(encodeURIComponent((b[0]).toString())) + "/" + escape(encodeURIComponent((b[1]).toString()))));
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, dataGet);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
activeThread.firstTime = false;
function dataGet(event:Event):void {
activeThread.popState();
activeThread.values.push(event.target.data.toString());
activeThread.firstTime = true;
}
}
doYield();
}

private function primCloudLength(b:Array):void {
if (activeThread.firstTime) {
var request:URLRequest = new URLRequest((cloudServerUrl + "listlength/" + escape(encodeURIComponent((b[0]).toString()))));
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, dataGet);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
activeThread.firstTime = false;
function dataGet(event:Event):void {
activeThread.popState();
activeThread.values.push(event.target.data.toString());
activeThread.firstTime = true;
}
}
doYield();
}

private function primCloudContains(b:Array):void {
if (activeThread.firstTime) {
var request:URLRequest = new URLRequest((cloudServerUrl + "listcontains/" + escape(encodeURIComponent((b[0]).toString())) + "/" + escape(encodeURIComponent((b[1]).toString()))));
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, dataGet);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
activeThread.firstTime = false;
function dataGet(event:Event):void {
activeThread.popState();
activeThread.values.push(event.target.data.toString() == '1');
activeThread.firstTime = true;
}
}
doYield();
}
}}
5 changes: 3 additions & 2 deletions src/scratch/PaletteBuilder.as
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public class PaletteBuilder {
addBlocksForCategory(Specs.dataCategory, catColor);
nextY += 15;
}
addBlocksForCategory(29, catColor);

// lists
catColor = Specs.listColor;
Expand All @@ -191,6 +192,7 @@ public class PaletteBuilder {
nextY += 10;
addBlocksForCategory(Specs.listCategory, catColor);
}
addBlocksForCategory(32, catColor);
updateCheckboxes();
}

Expand Down Expand Up @@ -221,7 +223,6 @@ public class PaletteBuilder {
d.addTitle('New Variable');
d.addField('Variable name', 150);
d.addWidget(varSettings);
d.addText('Cloud data coming soon');
d.addAcceptCancelButtons('OK');
d.showOnStage(app.stage);
}
Expand All @@ -239,7 +240,7 @@ public class PaletteBuilder {
d.addTitle('New List');
d.addField('List name', 150);
d.addWidget(varSettings);
d.addText('Cloud data coming soon');
d.addText('Cloud lists coming soon');
d.addAcceptCancelButtons('OK');
d.showOnStage(app.stage);
}
Expand Down

0 comments on commit 5d8dc86

Please sign in to comment.