This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#276 2012-05-20 12:21:49

dreamod
New Scratcher
Registered: 2012-01-22
Posts: 100+

Re: Block Files

I think I got the args to work, but then I realized that I had no internet. :p

Offline

 

#277 2012-05-20 12:35:01

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Block Files

This seems to work!

Code:

|t1 t2 |
t1_ WeakKeyAssociation new value: #('This is a test.').
t1 key: 'test'.
t2 _ #() asOrderedCollection.
t2 add: t1.
^ HTTPSocket httpPostDocument: 'www.blocks.scratchr.org/postPing.php' args: t2

This code assigns the string "This is a test." to the arg "test" and appears to successfully pass the data.   big_smile   big_smile   big_smile

Offline

 

#278 2012-05-20 12:45:57

dreamod
New Scratcher
Registered: 2012-01-22
Posts: 100+

Re: Block Files

Greenatic wrote:

This seems to work!

Code:

|t1 t2 |
t1_ WeakKeyAssociation new value: #('This is a test.').
t1 key: 'test'.
t2 _ #() asOrderedCollection.
t2 add: t1.
^ HTTPSocket httpPostDocument: 'www.blocks.scratchr.org/postPing.php' args: t2

This code assigns the string "This is a test." to the arg "test" and appears to successfully pass the data.   big_smile   big_smile   big_smile

Thank you SO much Greenatic. You are amazing. I modified the code a little for 4 arguments and it works. Now sparks and I can start making the upload feature, right sparks?

Offline

 

#279 2012-05-20 13:15:56

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Block Files

dreamod wrote:

Greenatic wrote:

This seems to work!

Code:

|t1 t2 |
t1_ WeakKeyAssociation new value: #('This is a test.').
t1 key: 'test'.
t2 _ #() asOrderedCollection.
t2 add: t1.
^ HTTPSocket httpPostDocument: 'www.blocks.scratchr.org/postPing.php' args: t2

This code assigns the string "This is a test." to the arg "test" and appears to successfully pass the data.   big_smile   big_smile   big_smile

Thank you SO much Greenatic. You are amazing. I modified the code a little for 4 arguments and it works. Now sparks and I can start making the upload feature, right sparks?

No problem.  wink

I specifically made t2 an OrderedCollection so that you could add as many args as you needed.  If you're going to pass the same number of args each time, using a simple Array should work as well.

Offline

 

#280 2012-05-20 13:18:08

dreamod
New Scratcher
Registered: 2012-01-22
Posts: 100+

Re: Block Files

Sparks, I'm pretty much done with my part. Here is the result of POSTing the 'move () steps'

Code:

 'Array
(
    [username] => guest
    [password] => guest
    [comment] => my comment
    [block] => ''From MIT Squeak 0.9.4 (June 1, 2003) [No updates present.] on 20 May 2012 at 7:14:17 pm''!

!ScratchSpriteMorph methodsFor: ''motion ops'' stamp: ''jens 11/21/2008 09:49''!
forward: distance
    "Move the object forward (i.e., the direction of its heading) by the given distance.
    Avoid infinite or NaN coordinates"

    | radians deltaP newPos newX newY |
    radians _ rotationDegrees degreesToRadians.
    deltaP _ ((radians cos)@(radians sin)) * distance.
    newPos _ self position + deltaP.
    newX _ newPos x.
    newY _ newPos y.
    newX isNaN ifTrue: [newX _ 0].
    newX isInf ifTrue: [newX _ newX sign * 10000].
    newY isNaN ifTrue: [newY _ 0].
    newY isInf ifTrue: [newY _ newY sign * 10000].
    self position: newX @ newY.
    self keepOnScreen.
! !
!CustomBlockSpecsDictionary class methodsFor: ''blocks''!
getNewestBlock
    ^ #(''Sprites'' ''motion'' (''move $Number$ steps'' #- #forward:))! !
)
'

so 4 fields:
username
password
comment
block

username has the username.
password has the password.
comment has a comment describing what the block does.
block includes the contents of the block file.

Offline

 

#281 2012-05-20 13:22:43

dreamod
New Scratcher
Registered: 2012-01-22
Posts: 100+

Re: Block Files

Greenatic wrote:

dreamod wrote:

Greenatic wrote:

This seems to work!

Code:

|t1 t2 |
t1_ WeakKeyAssociation new value: #('This is a test.').
t1 key: 'test'.
t2 _ #() asOrderedCollection.
t2 add: t1.
^ HTTPSocket httpPostDocument: 'www.blocks.scratchr.org/postPing.php' args: t2

This code assigns the string "This is a test." to the arg "test" and appears to successfully pass the data.   big_smile   big_smile   big_smile

Thank you SO much Greenatic. You are amazing. I modified the code a little for 4 arguments and it works. Now sparks and I can start making the upload feature, right sparks?

No problem.  wink

I specifically made t2 an OrderedCollection so that you could add as many args as you needed.  If you're going to pass the same number of args each time, using a simple Array should work as well.

Yeah, I understood that. I'm mentioning this in the credits.
"Credit to Greenatic for making uploading blocks possible and beta testing."

Offline

 

#282 2012-05-20 13:38:35

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Block Files

dreamod wrote:

Greenatic wrote:

dreamod wrote:


Thank you SO much Greenatic. You are amazing. I modified the code a little for 4 arguments and it works. Now sparks and I can start making the upload feature, right sparks?

No problem.  wink

I specifically made t2 an OrderedCollection so that you could add as many args as you needed.  If you're going to pass the same number of args each time, using a simple Array should work as well.

Yeah, I understood that. I'm mentioning this in the credits.
"Credit to Greenatic for making uploading blocks possible and beta testing."

Thanks!   smile    Now let's see what sparks can come up with.

Offline

 

#283 2012-05-20 16:24:52

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Block Files

Hey guys! I've been ill most of this week and am still pretty under the weather and I have a programming exam on Thursday but I'll do my best to add the upload feature for this!

Since we'll be storing block files on the site as SQL database entries, I'm really worried the whole SQL system is going to cause problems. I think that dreamod, your code should allow for error messages to be sent back from the site should a problem occur so you can display a message to the user. Here are some I can think of:

wrongUsernameOrPass
blockAlreadyExists
SQLConnectionError

For the last one, I suggest entering a loop that tries four or five more times, and if it fails then, display a message along the lines of "The block could not be uploaded to the library right now. Please try again later."

I'll have a stab at the upload code now  smile


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#284 2012-05-20 17:08:57

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Block Files

sparks wrote:

Hey guys! I've been ill most of this week and am still pretty under the weather and I have a programming exam on Thursday but I'll do my best to add the upload feature for this!

Since we'll be storing block files on the site as SQL database entries, I'm really worried the whole SQL system is going to cause problems. I think that dreamod, your code should allow for error messages to be sent back from the site should a problem occur so you can display a message to the user. Here are some I can think of:

wrongUsernameOrPass
blockAlreadyExists
SQLConnectionError

For the last one, I suggest entering a loop that tries four or five more times, and if it fails then, display a message along the lines of "The block could not be uploaded to the library right now. Please try again later."

I'll have a stab at the upload code now  smile

How would we get blockAlreadyEsists?  We would have to be able to read from the database.  Instead, your code could return the errors itself, and our code could just read the echo.

Offline

 

#285 2012-05-20 17:35:39

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Block Files

@Greenatic: yes, the error names I suggested would be worked out by my code and echoed back to the smalltalk  smile  The code is nearly done. Oddly enough, the thing that took the longest was parsing spec, type and category from the block file  tongue


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#286 2012-05-20 17:46:24

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Block Files

Well I finished the code. I get an SQLConnectionError error echoed every time I try it though!

Code:

<?php
//remoteBlockUploader.php
//date created: 20 May 2012
//function:
//    Allows Dreamod's block mod to connect and upload blocks

include("connect.php"); //connect to the SQL database
if(isset($_POST['username']) && isset ($_POST['password']) && isset($_POST['comment']) && isset($_POST['block'])){     //check to make sure that all required variables are being received.
    $usernameStatus = trim(file_get_contents("http://scratch.mit.edu/api/authenticateuser?username=" . $_POST['username'] . "&password=" . $_POST['password'])); //use the Scratch API to determine the username and password status.
    if($usernameStatus  != 'false'){ //check to make sure that the username exists and the password is correct.
    $username = $_POST['username'];
    $comment = $_POST['comment'];
        ###############################################################
        //PARSE DATA FROM BLOCK FILE
        $code = $_POST['block'];
        $startPos = strpos($code, "getNewestBlock") + 22; //locate the spec within the block file
        $loopCount = 0;
        while ($loopCount < 4){
            $startPos = strpos ($code, "'", $startPos + 1);
            $loopCount ++;
        }
        $category = substr($code, $startPos + 1, strlen($code) - $startPos + 1);
        $carryOnCode = $category;
        $endPos = strpos($category, "'");
        $category = substr($category, 0, $endPos);
        #######category gained##############
        $loopCount = 0;
        while ($loopCount < 4){
            $startPos = strpos ($code, "'", $startPos + 1);
            $loopCount ++;
        }
        $blockSpec = substr($code, $startPos + 1, strlen($code) - $startPos + 1);
        $carryOnCode = $blockSpec;
        $endPos = strpos($blockSpec, "'");
        $blockSpec = substr($blockSpec, 0, $endPos);
        #####blockspec gained.#################
        $startPos = strpos($carryOnCode, "#") + 1;
        $type = substr($carryOnCode, $startPos, 1);
        ####type gained#####################
        
        ##############################################################
        //check whether the blockspec already exists
        $exists = false; //missing until found
        $result = mysql_query("SELECT * FROM `blocks`");
        while($row = mysql_fetch_array($result)){
            if($row['name'] == $blockSpec){
                $exists = true;
            }
        }
        if($exists){ //if the block already exists
            echo "blockAlreadyExists"; //echo a "block already exists" message.
        }
        if(!$exists){ //if the block doesn't exists yet, add it.
            $query = "INSERT INTO `blocks` VALUES ('', '$blockSpec', '$type', 'category', '$code', '$username', '$username', '$comment', 'unapproved', '', 'Scratch')"; //insert block values for id, spec, type, category, code, creator, sharer, comment, status, application.
        }
        //due to the terrible SQL connection, the code now checks the database to see if the block has appeared.
        $exists = false; //missing until found
        $result = mysql_query("SELECT * FROM blocks");
        while($row = mysql_fetch_array($result)){
            if($row['name'] == $blockSpec){
                $exists = true;
            }
        }
        if($exists){ //the block was added
         echo "success"; //echo a success message;
        }
        if(!$exists){ //if the block can't be found,
            echo "SQLConnectionError"; //echo an SQL connection error,
        }
        
    }
    if($usernameStatus == 'false'){ //there's a problem with the username or password!
        echo "wrongUsernameOrPass"; //echo a "wrong username or password" message.
    }
}
else{ //one or more required variables are missing from the action!
    echo "missingArgs"; //echo a "missing arguments" message.
}
?>

http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#287 2012-05-20 17:51:34

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Block Files

Then again, I'm not getting an SQL error every time I try the button on the home page of the library site so maybe there's a problem with that code. I'll take a look tomorrow. It's 10:50 here and I'm ill  tongue


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#288 2012-05-20 18:24:06

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Block Files

sparks wrote:

Then again, I'm not getting an SQL error every time I try the button on the home page of the library site so maybe there's a problem with that code. I'll take a look tomorrow. It's 10:50 here and I'm ill  tongue

Hope you get better soon!

You might want to add that PHP upload page to the list of pages librarians can edit.  (And where do the block files go, and should we be able to edit them manually?)

And maybe you should add the image uploader to the log book.

Last edited by Greenatic (2012-05-20 18:32:23)

Offline

 

#289 2012-05-21 00:39:40

dreamod
New Scratcher
Registered: 2012-01-22
Posts: 100+

Re: Block Files

sparks wrote:

Hey guys! I've been ill most of this week and am still pretty under the weather and I have a programming exam on Thursday but I'll do my best to add the upload feature for this!

Since we'll be storing block files on the site as SQL database entries, I'm really worried the whole SQL system is going to cause problems. I think that dreamod, your code should allow for error messages to be sent back from the site should a problem occur so you can display a message to the user. Here are some I can think of:

wrongUsernameOrPass
blockAlreadyExists
SQLConnectionError

For the last one, I suggest entering a loop that tries four or five more times, and if it fails then, display a message along the lines of "The block could not be uploaded to the library right now. Please try again later."

I'll have a stab at the upload code now  smile

Get well soon. I'll fix that. how about if there is an error on uploading the block, the webpage displays: 'error: message informing what went wrong'. That way the block importer would read the 'error:' line and then display a dialog with the title error and the message after 'error: '.

Offline

 

#290 2012-05-21 05:34:26

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Block Files

@Dreamod: Okay, cool. I'll create more informative errors then  smile

Although really, things like missingArgs are things that YOUR code should respond to (or never get by checking all variables are set to something)... you do have to send a comment, even if the comment is blank.

Another thing, what if say, a librarian or you are uploading a load of blocks from the library to the site? It's not really fair that we then get credited for the creation of that block, which is why my table has a "creator" and a "sharer" field. It might be nice if your upload process could give the option for a user to credit another with the block's creation.

@Greenatic:
"block files" will actually be entries to a database, so yes, a manual viewer and editor could be built. When someone wants to download a block file, the file will be created using the database information as and when it is needed, then deleted when the download is complete. I'll add the remoteBlockUploader.php to the Librarian editor list, and get the image uploader up again, although remember we're not uploading images of blocks now.


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#291 2012-05-21 10:12:28

dreamod
New Scratcher
Registered: 2012-01-22
Posts: 100+

Re: Block Files

sparks wrote:

@Dreamod: Okay, cool. I'll create more informative errors then  smile

Although really, things like missingArgs are things that YOUR code should respond to (or never get by checking all variables are set to something)... you do have to send a comment, even if the comment is blank.

Another thing, what if say, a librarian or you are uploading a load of blocks from the library to the site? It's not really fair that we then get credited for the creation of that block, which is why my table has a "creator" and a "sharer" field. It might be nice if your upload process could give the option for a user to credit another with the block's creation.

@Greenatic:
"block files" will actually be entries to a database, so yes, a manual viewer and editor could be built. When someone wants to download a block file, the file will be created using the database information as and when it is needed, then deleted when the download is complete. I'll add the remoteBlockUploader.php to the Librarian editor list, and get the image uploader up again, although remember we're not uploading images of blocks now.

I'll add a credit field. But concerning the current blocks I think we should upload them using a 'false ID'. Basically skip the password field until all the blocks are up. I'm not sure how we can do that safely, though.

Offline

 

#292 2012-05-21 10:40:12

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Block Files

dreamod wrote:

I'll add a credit field. But concerning the current blocks I think we should upload them using a 'false ID'. Basically skip the password field until all the blocks are up. I'm not sure how we can do that safely, though.

Hmm, yeah, that or we upload them with the creator's name in the comments. A Librarian will later be able to edit the creator field properly using some editor I'll build.


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#293 2012-05-21 10:49:48

dreamod
New Scratcher
Registered: 2012-01-22
Posts: 100+

Re: Block Files

sparks wrote:

dreamod wrote:

I'll add a credit field. But concerning the current blocks I think we should upload them using a 'false ID'. Basically skip the password field until all the blocks are up. I'm not sure how we can do that safely, though.

Hmm, yeah, that or we upload them with the creator's name in the comments. A Librarian will later be able to edit the creator field properly using some editor I'll build.

ok. By the way, about the comment field. If it is left blank then it is treated as an empty string basically "". but it's still a field and included. I'm going to see if I can make block files of the library again. All I have to do is import them and upload them when that works.

Offline

 

#294 2012-05-21 11:31:47

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Block Files

Okay. I think I'll take away the comment field from the missingArgs message.

Still no idea why I always get the SQLConnectionError message  sad


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#295 2012-05-21 11:35:00

dreamod
New Scratcher
Registered: 2012-01-22
Posts: 100+

Re: Block Files

sparks wrote:

Okay. I think I'll take away the comment field from the missingArgs message.

Still no idea why I always get the SQLConnectionError message  sad

Keep it, you never know if someone will hack the block importer and remove a field.

Offline

 

#296 2012-05-21 11:38:55

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Block Files

Well in that case you should make sure that the comment field contains a space or something if it's empty. (That sentence makes little sense, I mean send a space instead of nothing to the uploader.)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#297 2012-05-21 11:45:34

dreamod
New Scratcher
Registered: 2012-01-22
Posts: 100+

Re: Block Files

sparks wrote:

Well in that case you should make sure that the comment field contains a space or something if it's empty. (That sentence makes little sense, I mean send a space instead of nothing to the uploader.)

Well, the comment field is automatically initialized to an empty string, and no space is required, because it's still a string object even if it's empty. (does that make any sense?) But if there are fields that have been hacked away then that's when the error would act.
Any ideas for the block uploading?

Offline

 

#298 2012-05-21 17:53:55

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Block Files

sparks wrote:

@Greenatic:
"block files" will actually be entries to a database, so yes, a manual viewer and editor could be built. When someone wants to download a block file, the file will be created using the database information as and when it is needed, then deleted when the download is complete. I'll add the remoteBlockUploader.php to the Librarian editor list, and get the image uploader up again, although remember we're not uploading images of blocks now.

Great!  I was thinking about using the image uploader for improving the "Share a Block" page.

Offline

 

#299 2012-05-22 01:29:02

dreamod
New Scratcher
Registered: 2012-01-22
Posts: 100+

Re: Block Files

sparks wrote:

Well I finished the code. I get an SQLConnectionError error echoed every time I try it though!

Code:

<?php
//remoteBlockUploader.php
//date created: 20 May 2012
//function:
//    Allows Dreamod's block mod to connect and upload blocks

include("connect.php"); //connect to the SQL database
if(isset($_POST['username']) && isset ($_POST['password']) && isset($_POST['comment']) && isset($_POST['block'])){     //check to make sure that all required variables are being received.
    $usernameStatus = trim(file_get_contents("http://scratch.mit.edu/api/authenticateuser?username=" . $_POST['username'] . "&password=" . $_POST['password'])); //use the Scratch API to determine the username and password status.
    if($usernameStatus  != 'false'){ //check to make sure that the username exists and the password is correct.
    $username = $_POST['username'];
    $comment = $_POST['comment'];
        ###############################################################
        //PARSE DATA FROM BLOCK FILE
        $code = $_POST['block'];
        $startPos = strpos($code, "getNewestBlock") + 22; //locate the spec within the block file
        $loopCount = 0;
        while ($loopCount < 4){
            $startPos = strpos ($code, "'", $startPos + 1);
            $loopCount ++;
        }
        $category = substr($code, $startPos + 1, strlen($code) - $startPos + 1);
        $carryOnCode = $category;
        $endPos = strpos($category, "'");
        $category = substr($category, 0, $endPos);
        #######category gained##############
        $loopCount = 0;
        while ($loopCount < 4){
            $startPos = strpos ($code, "'", $startPos + 1);
            $loopCount ++;
        }
        $blockSpec = substr($code, $startPos + 1, strlen($code) - $startPos + 1);
        $carryOnCode = $blockSpec;
        $endPos = strpos($blockSpec, "'");
        $blockSpec = substr($blockSpec, 0, $endPos);
        #####blockspec gained.#################
        $startPos = strpos($carryOnCode, "#") + 1;
        $type = substr($carryOnCode, $startPos, 1);
        ####type gained#####################
        
        ##############################################################
        //check whether the blockspec already exists
        $exists = false; //missing until found
        $result = mysql_query("SELECT * FROM `blocks`");
        while($row = mysql_fetch_array($result)){
            if($row['name'] == $blockSpec){
                $exists = true;
            }
        }
        if($exists){ //if the block already exists
            echo "blockAlreadyExists"; //echo a "block already exists" message.
        }
        if(!$exists){ //if the block doesn't exists yet, add it.
            $query = "INSERT INTO `blocks` VALUES ('', '$blockSpec', '$type', 'category', '$code', '$username', '$username', '$comment', 'unapproved', '', 'Scratch')"; //insert block values for id, spec, type, category, code, creator, sharer, comment, status, application.
        }
        //due to the terrible SQL connection, the code now checks the database to see if the block has appeared.
        $exists = false; //missing until found
        $result = mysql_query("SELECT * FROM blocks");
        while($row = mysql_fetch_array($result)){
            if($row['name'] == $blockSpec){
                $exists = true;
            }
        }
        if($exists){ //the block was added
         echo "success"; //echo a success message;
        }
        if(!$exists){ //if the block can't be found,
            echo "SQLConnectionError"; //echo an SQL connection error,
        }
        
    }
    if($usernameStatus == 'false'){ //there's a problem with the username or password!
        echo "wrongUsernameOrPass"; //echo a "wrong username or password" message.
    }
}
else{ //one or more required variables are missing from the action!
    echo "missingArgs"; //echo a "missing arguments" message.
}
?>

Should I change the upload url to 'http://blocks.scratchr.org/remoteBlockUploader.php'?

Offline

 

#300 2012-05-22 05:15:55

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Block Files

Yup!


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

Board footer