SFP commited on
Commit
d8d884a
1 Parent(s): 504afaa

Create SpaceFace.py

Browse files
Files changed (1) hide show
  1. SpaceFace.py +129 -0
SpaceFace.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import coding
2
+ import scratchattach as scratch3
3
+ import threading
4
+ import time
5
+ import random
6
+ global conn
7
+ global projectid
8
+ global endpoint
9
+ global users
10
+ users = {}
11
+ with open("password.txt") as f:
12
+ session = scratch3.login("PolyPenguin", f.read())
13
+ class User:
14
+ def __init__(self, name, id):
15
+ self.name = name
16
+ self.id = id
17
+ class Responder:
18
+ def __init__(self, projectid, channel, can_respond, can_stream, value):
19
+ self.projectid = projectid
20
+ self.can_respond = can_respond
21
+ self.can_stream = can_stream
22
+ self.channel = channel
23
+ self.cooldown = time.time()
24
+ self.wrote = False
25
+ self.last_value = value
26
+ def poll(self):
27
+ value = scratch3.get_var(self.projectid, "channel "+str(self.channel))
28
+ if value != self.last_value and value != None:
29
+ self.last_value = value
30
+ binary = coding.decimal_to_binary(int(value))
31
+ if binary[1:3]=="01":
32
+ value = str(coding.binary_to_decimal(binary[3:]))
33
+ if binary[1:3]=="10":
34
+ value = str("true" if binary[3:] == 1 else "false")
35
+ if binary[1:3]=="11":
36
+ value = coding.convert_to_text(binary[3:])
37
+
38
+ return value
39
+ def close(self):
40
+ if self.can_stream or not self.can_respond:
41
+ conn.set_var("channel "+str(self.channel), "0")
42
+ else:
43
+ while str(scratch3.get_var(self.projectid, "channel "+str(self.channel))) !="0":
44
+ pass
45
+ def respond(self, response):
46
+ global conn
47
+ if self.wrote and not self.can_stream:
48
+ raise Exception("Can't stream to this as a response")
49
+ if not (self.can_respond or self.can_stream):
50
+ raise Exception("Can't respond to this")
51
+ while time.time() - self.cooldown < 0.5:
52
+ time.sleep(0.5 - (time.time() - self.cooldown))
53
+ if self.can_respond or self.can_stream:
54
+ payload = "1"
55
+ if type(response) is int:
56
+ payload+="01"+coding.decimal_to_binary(response)
57
+ elif type(response) is bool:
58
+ payload+="10"+"1" if response else "0"
59
+ elif type(response) is str:
60
+ payload+="11"+coding.convert_to_binary(response)
61
+ self.last_value = str(coding.binary_to_decimal(payload))
62
+ conn.set_var("channel "+str(self.channel), str(coding.binary_to_decimal(payload)))
63
+ t= time.time()
64
+ times=0.2
65
+ while scratch3.get_var(self.projectid, "channel "+str(self.channel)) !=str(coding.binary_to_decimal(payload)):
66
+ if time.time()-t>=times:
67
+ print("Message not sent, retrying")
68
+ times+=0.1
69
+ conn.set_var("channel "+str(self.channel), str(coding.binary_to_decimal(payload)))
70
+ t=time.time()
71
+ self.wrote = True
72
+ class ConnectionEndpoint:
73
+ def __init__(self):
74
+ pass
75
+ def receivedMessage(self, message, user, responder):
76
+ global users
77
+ r=random.randrange(1, 2047)
78
+ while r in users:
79
+ r=random.randrange(1, 2047)
80
+ users[r] = User(message, r)
81
+ responder.respond(r)
82
+ responder.close()
83
+
84
+ def thread(n):
85
+ global users
86
+ global conn
87
+ global projectid
88
+ global endpoint
89
+ conn.set_var("channel "+str(n), "0")
90
+ while True:
91
+
92
+ value = scratch3.get_var(projectid, "channel "+str(n))
93
+ if str(value) != "0" and value != None:
94
+ binary = coding.decimal_to_binary(int(value))
95
+ reqendpoint = coding.binary_to_decimal(binary[1:6])
96
+ header = coding.binary_to_decimal(binary[6:17])
97
+ staticpayload = binary[17] == '1'
98
+ streamingpayload = binary[18] == '1'
99
+ acceptstaticpayload = binary[19] == '1'
100
+ acceptstreamingpayload = binary[20] == '1'
101
+ payload = None
102
+ if staticpayload and not streamingpayload:
103
+ payloadformat = binary[21:23]
104
+ if payloadformat == "01":
105
+ payload = str(coding.binary_to_decimal(binary[23:]))
106
+ if payloadformat == "10":
107
+ payload = "true" if binary[23:]=='1' else "false"
108
+ if payloadformat == "11":
109
+ payload = coding.convert_to_text(binary[23:])
110
+ respond = Responder(projectid, n, acceptstaticpayload, acceptstreamingpayload, value)
111
+ if header in users:
112
+ user = users[header]
113
+ else:
114
+ user = None
115
+ endpoint[reqendpoint].receivedMessage(payload, user, respond)
116
+
117
+
118
+ def start_server(endpoints, project_id):
119
+ global projectid
120
+ global conn
121
+ global endpoint
122
+ endpoints.insert(0, ConnectionEndpoint())
123
+ endpoint = endpoints
124
+ projectid = project_id
125
+ conn = session.connect_cloud(project_id)
126
+ threads = [threading.Thread(target=thread, args=(i+1,)) for i in range(10)]
127
+ for t in threads:
128
+ t.start()
129
+ return t