Skip to content

Commit 32bca55

Browse files
committed
docs: fix the copying
1 parent 008c5d6 commit 32bca55

File tree

8 files changed

+1773
-1
lines changed

8 files changed

+1773
-1
lines changed

docs/SA-Wiki/Cloud-Requests.md

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
Cloud Requests Framework (inspired by di*****.py) that allows Scratch projects and Python to interact
2+
3+
New in v2.0: Cloud Requests now use threading.Event which massively lowers CPU usage.
4+
5+
# Basic usage
6+
7+
**Add Cloud Requests to your Scratch project:**
8+
9+
Download this project file to your computer (click the link to download it): https://github.com/TimMcCool/scratchattach/raw/main/assets/CloudRequests_Template.sb3
10+
11+
Then, go to the Scratch website, create a new project and upload the project file from above.
12+
13+
**How to use with Scratch:**
14+
15+
Copy this code to your Python editor. [How to get your session id](https://github.com/TimMcCool/scratchattach/wiki/Get-your-session-id)
16+
17+
```py
18+
import scratchattach as sa
19+
20+
session = sa.login_by_id("session_id", username="username") #replace with your session_id and username
21+
cloud = session.connect_cloud("project_id") #replace with your project id
22+
client = cloud.requests()
23+
24+
@client.request
25+
def ping(): #called when client receives request
26+
print("Ping request received")
27+
return "pong" #sends back 'pong' to the Scratch project
28+
29+
@client.event
30+
def on_ready():
31+
print("Request handler is running")
32+
33+
client.start(thread=True) # thread=True is an optional argument. It makes the cloud requests handler run in a thread
34+
```
35+
36+
Replace "session_id" and "username" with your data and "project_id" with the id of the project you created on Scratch.
37+
Then, run the code.
38+
39+
Now go to the Scratch project. In the `Cloud Requests` sprite, you will find this block:
40+
41+
![image](https://github.com/TimMcCool/scratchattach/blob/main/wiki/images/cr_tut_block.png)
42+
43+
When active, it sends a "ping" request to the Python client. This will call the `ping()` function. The data returned by the function will be sent back to the project.
44+
Try it out by clicking the block!
45+
46+
![image](https://github.com/TimMcCool/scratchattach/blob/main/wiki/images/cr_tut_restult.png)
47+
48+
**How to use with TurboWarp:**
49+
50+
```python
51+
import scratchattach as sa
52+
53+
cloud = sa.get_tw_cloud("project_id") #replace with your project id
54+
client = cloud.requests()
55+
56+
...
57+
```
58+
59+
# Examples
60+
61+
**Example 1: Script that loads your message count**
62+
63+
Scratch code:
64+
65+
![image](https://github.com/TimMcCool/scratchattach/blob/main/wiki/images/cr_tut_example1.png)
66+
67+
```python
68+
@client.request
69+
def message_count(argument1):
70+
print(f"Message count requested for user {argument1}")
71+
user = sa.get_user(argument1)
72+
return user.message_count()
73+
```
74+
75+
The arguments you specify in the Scratch code are given to the Python function.
76+
77+
# Basic features
78+
79+
- TCP-like packet loss prevention (new in v2.0)
80+
- **No length limitation** for the request or the returned data! (If it is too long for one cloud variable, it will be split into multiple cloud variables)
81+
- Cloud Requests can handle **multiple requests sent at the same time**
82+
- Requests can also **return lists,** these will be decoded as list in the Scratch project
83+
- You can freely choose the name of your requests
84+
- Requests that only return numbers won't be encoded (-> 50% faster!)
85+
86+
# Advanced features
87+
88+
**Send data to the Scratch project** (new in v2.0)
89+
90+
It's now possible to send data to the Scratch project anytime without a priorly received request:
91+
92+
```py
93+
client.send("message to send")
94+
```
95+
96+
**Get the request metadata:**
97+
98+
In your requests, you can use these functions (since v2.0, they also work for requests running in threads):
99+
```py
100+
client.get_requester() #Returns the name of the user who sent the request
101+
client.get_timestamp() #Returns the timestamp of when the request was sent (in milliseconds since 1970)
102+
client.get_exact_timestamp() #Returns the exact timestamp of when the request was sent (fetches it from the clouddata logs). New in v1.2.6
103+
```
104+
105+
**Change the used cloud variables:**
106+
```py
107+
client = cloud.requests(used_cloud_vars=["1", "2", "3", "4", "5", "6", "7", "8", "9"])
108+
```
109+
**Enable no-packet-loss:**
110+
111+
No packet loss will make cloud requests reconnect before every sent back request. This eliminates packet loss.
112+
```py
113+
client = cloud.requests(no_packet_loss=True)
114+
```
115+
**Send more than two arguments:**
116+
117+
The seperator used to join the different arguments is "&". To send more than three arguments from Scratch, join them using "&".
118+
119+
# Advanced requests
120+
(new in v1.0.0, updated in v2.0.0)
121+
122+
**Add arguments to the decorator:**
123+
124+
Above requests, you put the decorator `@client.request`.
125+
You can use this decorator to customize your requests!
126+
127+
*Modify the response priority*
128+
It is possible to change the way sending back request responses is being prioritised. There are three options to choose from:
129+
130+
1. _Respond in receive order (default):_ Requests received first will be sent back to the Scratch project first
131+
132+
```py
133+
client = cloud.requests(respond_order="receive")
134+
```
135+
136+
2. _Respond in finish order:_ Requests that finished first will be sent back to the Scratch project first
137+
138+
```py
139+
client = cloud.requests(respond_order="finish")
140+
```
141+
142+
3. _Respond based on individual priority scores:_ The lower the request's priorit score, the sooner it will be sent back to the Scratch project
143+
144+
```py
145+
client = cloud.requests(respond_order="priority")
146+
```
147+
Use this code to assign priorities to the individual requests:
148+
```py
149+
@client.request(response_priority=1)
150+
```
151+
152+
*Disable running request in thread*
153+
By default, requests are ran in threads in v2.0. However, you may want to disable this if your client is getting lots of requests that can be finished within less than 0.1 seconds.
154+
155+
```py
156+
@client.request(thread=False)
157+
```
158+
159+
*Disable request:*
160+
Put this decorator above a request to disable it:
161+
```py
162+
@client.request(enabled=False)
163+
```
164+
165+
*Overwrite the request name:*
166+
Put this decorator above a request to overwrite its name with something else:
167+
```py
168+
@client.request(name="new_name")
169+
```
170+
171+
**Manually add, edit and remove requests:**
172+
173+
*Add requests:*
174+
To add requests, you can also use this method:
175+
176+
```py
177+
client.add_request(function, thread=False, enabled=True, name="request_name")
178+
```
179+
180+
*Edit / Remove requests:*
181+
```py
182+
client.edit_request("request_name", thread=False, enabled=True, new_name="request_name", new_function=new_function) #The keyword arguments are optional and can be removed if they are not needed
183+
client.remove_request("request_name")
184+
```
185+
186+
# Advanced events
187+
188+
Events will be called when specific things happen.
189+
There are more events than just `on_ready`!
190+
191+
*Called after the client connected to the cloud:*
192+
```py
193+
@client.event
194+
def on_ready():
195+
print("Request handler is ready")
196+
```
197+
198+
*Called every time the client receives any request:*
199+
```py
200+
@client.event
201+
def on_request(request):
202+
print("Received request", request.name, request.requester, request.arguments, request.timestamp, request.request_id)
203+
```
204+
205+
*Called when the client receives an unknown / undefined request:*
206+
```py
207+
@client.event
208+
def on_unknown_request(request):
209+
print("Received unknown request", request.name, request.requester, request.arguments, request.timestamp, request.request_id)
210+
```
211+
212+
*Called when an error occurs in a request:*
213+
```py
214+
@client.event
215+
def on_error(request, e):
216+
print("Request: ", request.name, request.requester, request.arguments, request.timestamp, request.request_id)
217+
print("Error that occured: ", e)
218+
```
219+
220+
*Called when the client receives a disabled request:*
221+
```py
222+
@client.event
223+
def on_disabled_request(request):
224+
print("Received disabled request", request.name, request.requester, request.arguments, request.timestamp, request.request_id)
225+
```

docs/SA-Wiki/Cloud-Storage.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Cloud Storage Framework that makes it easy to store project data by sending it over cloud variables
2+
3+
# Basic usage
4+
5+
**Add a Cloud Storage to your Scratch project:**
6+
7+
Download this project file to your computer (click the link to download it): https://github.com/TimMcCool/scratchattach/raw/main/assets/CloudStorage_Template.sb3
8+
9+
Then, go to the Scratch website, create a new project and upload the project file from above.
10+
11+
## Create a database in your Python code
12+
13+
A database is a simple key-value storage. It automatically saves the data to a JSON file on your local device. If the specified JSON file does not exist, scratchattach will create an empty one for storing the data, else it will load the existing data from the JSON file.
14+
15+
```py
16+
import scratchattach as sa
17+
from scratchattach import Database
18+
19+
db1 = Database("db_name", json_file_path="path/to/json_file.json", save_interval=10) # save_interval specifies the wait time between the automated saves to the JSON file
20+
21+
...
22+
```
23+
24+
## Start the cloud storage
25+
26+
A cloud storage consists of at least one database.
27+
28+
```py
29+
...
30+
31+
session = sa.login("username", "password")
32+
cloud = session.connect_cloud("project_id")
33+
storage = cloud.storage()
34+
35+
storage.add_database(db1)
36+
# you can add more databases here
37+
38+
storage.start()
39+
```
40+
41+
In your Scratch project, you will find blocks that you can use to get values from the cloud storage or set keys on the cloud storage. They work while the Python backend is running. As you've probably already noticed, cloud storages are based on cloud requests.
42+
43+
# Advanced
44+
45+
**Methods of `sa.Database`:**
46+
```py
47+
db.save_to_json() #saves the data to the JSON file
48+
db.keys() #returns the keys of the key-value db
49+
db.get(key) #gets a value from the db
50+
db.set(key, value)
51+
```
52+
53+
**Database events:**
54+
55+
Can be used to automatically call functions when specific conditions occur.
56+
57+
```py
58+
@db.event
59+
def on_save():
60+
print("The data was saved")
61+
62+
@db.event
63+
def on_set(key, value):
64+
print("Key", key, "was set to value", value)
65+
```
66+
67+
**Methods of `sa.CloudStorage`:**
68+
69+
```py
70+
storage.get(db_name, key)
71+
storage.set(db_name, key, value)
72+
storage.keys(db_name)
73+
storage.databases()
74+
storage.database_names()
75+
storage.add_database(database)
76+
storage.get_database(db_name)
77+
storage.save()
78+
```

0 commit comments

Comments
 (0)