-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.js
More file actions
96 lines (92 loc) · 4.7 KB
/
Copy pathsystem.js
File metadata and controls
96 lines (92 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
This is an example of how to read and write cloud variables in Scratch using Node.js.
You are free to use any of the following code with proper credit to the respective owners.
This Node.js scripts requires the "scratch-api", and the "node-fetch" packages.
*/
var Scratch = require('scratch-api');
const fetch = require('node-fetch');
Scratch.UserSession.create("Scratch_username", "Scratch_password", function (err, user) { //Enter your Scratch account credentials
user.cloudSession(123456789, function (err, cloud) { //Replace the number with the project id of your desired project
let code = ["", "", "", "", "", "", "", "", "", "", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", ".", " ", "_"]
var encoded,
idx,
letter_num,
value
function decode(encoded) {
letter_num = 0
value = ""
do {
idx = encoded.charAt(letter_num) + encoded.charAt(letter_num + 1)
letter_num = letter_num + 2
if (Number(idx) < 1) { //evaluates it as a number rather than a string
break
}
value = value + code[Number(idx)]
} while (Number(idx) >= 1)
return value
}
function encode(val) {
temp = "" //so it concatenates strings rather than adding numbers
letter_num = 1
for (letter_num = 0; letter_num < val.length; letter_num++) {
temp += code.indexOf(val.charAt(letter_num))
}
temp += "00" //otherwise it would evaluate to 0
return temp
}
var idle_counter = 0
//Please note that the "☁" symbol is required in variable names!
function timeout() {
/*
This is the code that my "☁ Live Message Counter" project (https://scratch.mit.edu/projects/419697811/) uses. Modify the code below to match your needs.
Use cloud.get('☁ myVariable, value) to read the value of a cloud variable.
Use cloud.set('☁ myVariable, value) to write a value to a cloud variable.
Full documentation avaible here: https://github.com/trumank/scratch-api
*/
setTimeout(function () {
var cloud_check = cloud.get('☁ checkMessage')
if (cloud_check > 0){
if (cloud_check == 1){
cloud.set('☁ checkMessage', 2)
} else if (cloud_check == 2){
idle_counter++
if (idle_counter > 5){
cloud.set('☁ checkMessage', 0)
idle_counter = 0
}
} else if (cloud_check == 3){
if (cloud.get('☁ message') != 0){
var username = decode(cloud.get('☁ message'))
fetch('https://api.scratch.mit.edu/users/' + username + '/messages/count')
.then(res => res.json())
.then(data => {
var message = ""
if (data.code == null) {
message = encode("success." + data.count)
console.log("SUCCESS - Successfully retrieved the message count (" + data.count + ") for the user " + username + "!")
} else {
message = encode("error.user not found")
console.log("ERROR - The user " + username + " could not be found!")
}
//console.log(data.code)
//console.log(data.count)
//console.log(message)
cloud.set('☁ message', message)
cloud.set('☁ checkMessage', 4)
idle_counter = 0
})
}
} else {
idle_counter++
if (idle_counter > 5){
cloud.set('☁ checkMessage', 0)
idle_counter = 0
}
}
}
timeout();
}, 2000 ); //Time in milliseconds to check for an update, never go below 10 requests per second (100ms) as the Scratch API might block you.
}
timeout() //The timeout loop
});
});