Spaces:
Running
Running
Update turbowarp-server/qr-converter.py
Browse files- turbowarp-server/qr-converter.py +63 -144
turbowarp-server/qr-converter.py
CHANGED
|
@@ -1,164 +1,83 @@
|
|
| 1 |
-
import scratchcommunication
|
| 2 |
import time
|
| 3 |
import requests
|
| 4 |
-
import
|
| 5 |
-
import traceback
|
| 6 |
|
| 7 |
-
PROJECT_ID =
|
| 8 |
-
USERNAME = "server"
|
| 9 |
-
CONTACT = "contact"
|
| 10 |
|
| 11 |
tw = scratchcommunication.TwCloudConnection(
|
| 12 |
project_id=PROJECT_ID,
|
| 13 |
-
username=
|
| 14 |
-
contact_info=
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# ---
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# 文字列エンコード / デコード
|
| 26 |
-
# ---------------------
|
| 27 |
-
def encode(text):
|
| 28 |
-
try:
|
| 29 |
-
out = ""
|
| 30 |
-
for c in text:
|
| 31 |
-
out += f"{CHAR_TO_NUM[c]:02d}"
|
| 32 |
-
return out
|
| 33 |
-
except Exception as e:
|
| 34 |
-
print(f"[ERROR] encode('{text}') failed: {e}")
|
| 35 |
-
traceback.print_exc()
|
| 36 |
-
return "00"
|
| 37 |
|
| 38 |
-
def decode(data):
|
| 39 |
-
try:
|
| 40 |
-
out = ""
|
| 41 |
-
for i in range(0, len(data), 2):
|
| 42 |
-
num = int(data[i:i+2])
|
| 43 |
-
out += NUM_TO_CHAR.get(num, "0")
|
| 44 |
-
return out
|
| 45 |
-
except Exception as e:
|
| 46 |
-
print(f"[ERROR] decode('{data}') failed: {e}")
|
| 47 |
-
traceback.print_exc()
|
| 48 |
-
return "0"
|
| 49 |
-
|
| 50 |
-
# ---------------------
|
| 51 |
-
# クラウド変数操作
|
| 52 |
-
# ---------------------
|
| 53 |
-
def get_var(name):
|
| 54 |
-
try:
|
| 55 |
-
return tw.get_variable(name=name, name_literal=False)
|
| 56 |
-
except Exception:
|
| 57 |
-
return None
|
| 58 |
|
| 59 |
-
|
| 60 |
-
try:
|
| 61 |
-
tw.set_variable(name=name, value=value, name_literal=False)
|
| 62 |
-
except Exception as e:
|
| 63 |
-
print(f"[ERROR] set_var('{name}', '{value}') failed: {e}")
|
| 64 |
-
traceback.print_exc()
|
| 65 |
-
|
| 66 |
-
# ---------------------
|
| 67 |
-
# n0管理
|
| 68 |
-
# ---------------------
|
| 69 |
-
def n0_is_free():
|
| 70 |
-
v = get_var("n0")
|
| 71 |
-
return v in [None, "0"]
|
| 72 |
-
|
| 73 |
-
def set_n0(value):
|
| 74 |
-
set_var("n0", str(value))
|
| 75 |
-
|
| 76 |
-
# ---------------------
|
| 77 |
-
# データ送受信
|
| 78 |
-
# ---------------------
|
| 79 |
-
SLOT = "n1"
|
| 80 |
-
buffers = {}
|
| 81 |
-
|
| 82 |
-
def send(slot, text):
|
| 83 |
-
encoded = encode(text)
|
| 84 |
-
size = 99996
|
| 85 |
-
packets = [encoded[i:i+size] for i in range(0, len(encoded), size)]
|
| 86 |
-
total = len(packets)
|
| 87 |
-
|
| 88 |
-
for p in packets:
|
| 89 |
-
packet = f"1{total}0{p}"
|
| 90 |
-
start = time.time()
|
| 91 |
-
set_var(slot, packet)
|
| 92 |
-
|
| 93 |
-
while True:
|
| 94 |
-
v = get_var(slot)
|
| 95 |
-
if v and len(v) > 2 and v[2] == "1":
|
| 96 |
-
break
|
| 97 |
-
if time.time() - start > 10:
|
| 98 |
-
print(f"[WARN] send timeout for slot '{slot}'")
|
| 99 |
-
break
|
| 100 |
-
time.sleep(0.1)
|
| 101 |
-
|
| 102 |
-
def receive(slot):
|
| 103 |
-
v = get_var(slot)
|
| 104 |
-
if not v or len(v) < 3:
|
| 105 |
-
return None
|
| 106 |
-
|
| 107 |
-
if v[0] != "0" or v[2] != "0":
|
| 108 |
-
return None
|
| 109 |
|
|
|
|
| 110 |
try:
|
| 111 |
-
|
| 112 |
-
except Exception as e:
|
| 113 |
-
print(f"[ERROR] Invalid total in slot '{slot}': {v}")
|
| 114 |
-
traceback.print_exc()
|
| 115 |
-
return None
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
-
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
|
| 133 |
-
#
|
| 134 |
-
|
| 135 |
-
# ---------------------
|
| 136 |
-
while True:
|
| 137 |
-
try:
|
| 138 |
-
if n0_is_free():
|
| 139 |
-
received_id = receive(SLOT)
|
| 140 |
-
if received_id:
|
| 141 |
-
print(f"[INFO] Received ID: {received_id}")
|
| 142 |
-
set_n0(1)
|
| 143 |
-
|
| 144 |
-
try:
|
| 145 |
-
url = f"https://20.rf.gd/set.php?project_id={received_id}"
|
| 146 |
-
r = requests.get(url, timeout=10)
|
| 147 |
-
r.raise_for_status()
|
| 148 |
-
response_json = r.json()
|
| 149 |
-
short_id = response_json.get("short_id", "0")
|
| 150 |
-
print(f"[INFO] Server response short_id: {short_id}")
|
| 151 |
-
send(SLOT, short_id)
|
| 152 |
-
|
| 153 |
-
except Exception as e:
|
| 154 |
-
print(f"[ERROR] Server request failed for ID '{received_id}': {e}")
|
| 155 |
-
traceback.print_exc()
|
| 156 |
-
send(SLOT, "0")
|
| 157 |
-
|
| 158 |
-
set_n0(0)
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import time
|
| 2 |
import requests
|
| 3 |
+
import scratchcommunication
|
|
|
|
| 4 |
|
| 5 |
+
PROJECT_ID = 1293030706
|
|
|
|
|
|
|
| 6 |
|
| 7 |
tw = scratchcommunication.TwCloudConnection(
|
| 8 |
project_id=PROJECT_ID,
|
| 9 |
+
username="server",
|
| 10 |
+
contact_info="contact"
|
| 11 |
)
|
| 12 |
|
| 13 |
+
# 文字→数値変換(a-z0-9 → 00-35)
|
| 14 |
+
def encode_short_id(short_id):
|
| 15 |
+
chars = "abcdefghijklmnopqrstuvwxyz0123456789"
|
| 16 |
+
result = ""
|
| 17 |
+
for c in short_id:
|
| 18 |
+
index = chars.index(c)
|
| 19 |
+
result += f"{index:02d}"
|
| 20 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
last_n1 = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
while True:
|
| 26 |
try:
|
| 27 |
+
n1 = str(tw.get_variable("n1"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# 変化検知
|
| 30 |
+
if n1 != last_n1:
|
| 31 |
+
last_n1 = n1
|
| 32 |
|
| 33 |
+
# 最低3桁必要
|
| 34 |
+
if len(n1) < 3:
|
| 35 |
+
continue
|
| 36 |
|
| 37 |
+
mode = n1[0] # 0 or 1
|
| 38 |
+
read_flag = n1[1]
|
| 39 |
|
| 40 |
+
# ---------------------------
|
| 41 |
+
# 📥 Scratch → サーバー
|
| 42 |
+
# ---------------------------
|
| 43 |
+
if mode == "0" and read_flag == "0":
|
| 44 |
+
# 使用中フラグ
|
| 45 |
+
tw.set_variable("n0", 1)
|
| 46 |
|
| 47 |
+
# ID取得
|
| 48 |
+
recv_id = n1[2:]
|
| 49 |
+
print("受信ID:", recv_id)
|
| 50 |
|
| 51 |
+
# 既読に変更
|
| 52 |
+
tw.set_variable("n1", "0" + "1" + recv_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# APIアクセス
|
| 55 |
+
url = f"https://20.rf.gd/set.php?project_id={recv_id}"
|
| 56 |
+
res = requests.get(url)
|
| 57 |
+
data = res.json()
|
| 58 |
+
|
| 59 |
+
short_id = data.get("short_id", "")
|
| 60 |
+
print("short_id:", short_id)
|
| 61 |
|
| 62 |
+
# エンコード
|
| 63 |
+
encoded = encode_short_id(short_id)
|
| 64 |
+
|
| 65 |
+
# ---------------------------
|
| 66 |
+
# 📤 サーバー → Scratch
|
| 67 |
+
# ---------------------------
|
| 68 |
+
# 未読で送信
|
| 69 |
+
tw.set_variable("n1", "1" + "0" + encoded)
|
| 70 |
+
|
| 71 |
+
# ---------------------------
|
| 72 |
+
# 📬 既読検知(Scratch側)
|
| 73 |
+
# ---------------------------
|
| 74 |
+
elif mode == "1" and read_flag == "1":
|
| 75 |
+
# 使用終了
|
| 76 |
+
tw.set_variable("n0", 0)
|
| 77 |
+
print("通信終了")
|
| 78 |
+
|
| 79 |
+
time.sleep(0.2)
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print("エラー:", e)
|
| 83 |
+
time.sleep(1)
|