izuemon commited on
Commit
73638d4
·
verified ·
1 Parent(s): 102d1f8

Update turbowarp-server/qr-converter.py

Browse files
Files changed (1) hide show
  1. turbowarp-server/qr-converter.py +102 -65
turbowarp-server/qr-converter.py CHANGED
@@ -2,6 +2,7 @@ import scratchcommunication
2
  import time
3
  import requests
4
  import string
 
5
 
6
  PROJECT_ID = "1293030706"
7
  USERNAME = "server"
@@ -13,10 +14,6 @@ tw = scratchcommunication.TwCloudConnection(
13
  contact_info=CONTACT
14
  )
15
 
16
- # ---------------------
17
- # 文字<->数字マッピング
18
- # 0-9: 0-9, a-z: 10-35
19
- # ---------------------
20
  CHARS = list(string.digits + string.ascii_lowercase)
21
  CHAR_TO_NUM = {c: i for i, c in enumerate(CHARS)}
22
  NUM_TO_CHAR = {i: c for i, c in enumerate(CHARS)}
@@ -27,100 +24,140 @@ NUM_TO_CHAR = {i: c for i, c in enumerate(CHARS)}
27
  def get_var(name):
28
  try:
29
  return tw.get_variable(name=name, name_literal=False)
30
- except Exception:
 
 
31
  return None
32
 
33
  def set_var(name, value):
34
  try:
35
  return tw.set_variable(name=name, value=value, name_literal=False)
36
- except Exception:
 
 
37
  return None
38
 
39
  # ---------------------
40
- # n0管理(使用中フラグ)
41
  # ---------------------
42
  def n0_is_free():
43
- v = get_var("n0")
44
- return v == "0" or v is None
 
 
 
 
 
45
 
46
  def set_n0(value):
47
- set_var("n0", str(value))
 
 
 
 
48
 
49
  # ---------------------
50
  # IDエンコード / デコード
51
  # ---------------------
52
  def encode_id(sid):
53
- """a-z0-9を00-35に変換して結合"""
54
- out = ""
55
- for c in sid:
56
- num = CHAR_TO_NUM[c]
57
- out += f"{num:02d}"
58
- return out
 
 
 
 
59
 
60
  def decode_id(data):
61
- out = ""
62
- for i in range(0, len(data), 2):
63
- num = int(data[i:i+2])
64
- out += NUM_TO_CHAR[num]
65
- return out
 
 
 
 
 
66
 
67
  # ---------------------
68
  # n1の監視・送受信
69
  # ---------------------
70
  def monitor_n1():
71
- v = get_var("n1")
72
- if not v or len(v) < 3:
 
 
 
 
 
 
 
 
 
 
 
73
  return None
74
 
75
- status = v[0] # 0: ID送信, 1: レスポンス返送
76
- read_flag = v[1] # 0:未読, 1:既読
77
- content = v[2:]
78
-
79
- return {"status": status, "read": read_flag, "content": content}
80
-
81
  def mark_n1_read():
82
- v = get_var("n1")
83
- if v and len(v) >= 2:
84
- newv = v[0] + "1" + v[2:]
85
- set_var("n1", newv)
 
 
 
 
86
 
87
  def send_response(encoded_id):
88
- # 先頭1は1 (レスポンス送信), 2文字目は0 (未読)
89
- payload = "1" + "0" + encoded_id
90
- set_var("n1", payload)
 
 
 
91
 
92
  # ---------------------
93
  # メインループ
94
  # ---------------------
95
  while True:
96
- n1_data = monitor_n1()
97
- if n1_data:
98
- # ID受信(送信元Scratchから)
99
- if n1_data["status"] == "0" and n1_data["read"] == "0" and n0_is_free():
100
- # 使用中フラグ立てる
101
- set_n0(1)
102
-
103
- received_id = n1_data["content"]
104
- mark_n1_read() # 既読にする
105
-
106
- # サーバーにID送信してレスポンス取得
107
- try:
108
- url = f"https://20.rf.gd/set.php?project_id={received_id}"
109
- r = requests.get(url, timeout=10, cookies={"__test": "872039d283902860e895efb6018d2632"},)
110
- r.raise_for_status()
111
- response_json = r.json()
112
- short_id = response_json.get("short_id", "0") # short_idがなければ"0"
113
- encoded_short = encode_id(short_id)
114
-
115
- # ScratchにレスポンスID送信
116
- send_response(encoded_short)
117
-
118
- except Exception:
119
- # エラー時は0で返す
120
- send_response(encode_id("0"))
121
-
122
- # レスポンス既読を検知
123
- elif n1_data["status"] == "1" and n1_data["read"] == "1":
124
- set_n0(0) # 使用中フラグを下ろす
 
 
 
 
 
 
125
 
126
  time.sleep(0.2)
 
2
  import time
3
  import requests
4
  import string
5
+ import traceback
6
 
7
  PROJECT_ID = "1293030706"
8
  USERNAME = "server"
 
14
  contact_info=CONTACT
15
  )
16
 
 
 
 
 
17
  CHARS = list(string.digits + string.ascii_lowercase)
18
  CHAR_TO_NUM = {c: i for i, c in enumerate(CHARS)}
19
  NUM_TO_CHAR = {i: c for i, c in enumerate(CHARS)}
 
24
  def get_var(name):
25
  try:
26
  return tw.get_variable(name=name, name_literal=False)
27
+ except Exception as e:
28
+ print(f"[ERROR] get_var('{name}') failed: {e}")
29
+ traceback.print_exc()
30
  return None
31
 
32
  def set_var(name, value):
33
  try:
34
  return tw.set_variable(name=name, value=value, name_literal=False)
35
+ except Exception as e:
36
+ print(f"[ERROR] set_var('{name}', '{value}') failed: {e}")
37
+ traceback.print_exc()
38
  return None
39
 
40
  # ---------------------
41
+ # n0管理
42
  # ---------------------
43
  def n0_is_free():
44
+ try:
45
+ v = get_var("n0")
46
+ return v == "0" or v is None
47
+ except Exception as e:
48
+ print(f"[ERROR] n0_is_free() check failed: {e}")
49
+ traceback.print_exc()
50
+ return False
51
 
52
  def set_n0(value):
53
+ try:
54
+ set_var("n0", str(value))
55
+ except Exception as e:
56
+ print(f"[ERROR] set_n0({value}) failed: {e}")
57
+ traceback.print_exc()
58
 
59
  # ---------------------
60
  # IDエンコード / デコード
61
  # ---------------------
62
  def encode_id(sid):
63
+ try:
64
+ out = ""
65
+ for c in sid:
66
+ num = CHAR_TO_NUM[c]
67
+ out += f"{num:02d}"
68
+ return out
69
+ except Exception as e:
70
+ print(f"[ERROR] encode_id('{sid}') failed: {e}")
71
+ traceback.print_exc()
72
+ return "00" # デフォルトの無効値
73
 
74
  def decode_id(data):
75
+ try:
76
+ out = ""
77
+ for i in range(0, len(data), 2):
78
+ num = int(data[i:i+2])
79
+ out += NUM_TO_CHAR[num]
80
+ return out
81
+ except Exception as e:
82
+ print(f"[ERROR] decode_id('{data}') failed: {e}")
83
+ traceback.print_exc()
84
+ return "0"
85
 
86
  # ---------------------
87
  # n1の監視・送受信
88
  # ---------------------
89
  def monitor_n1():
90
+ try:
91
+ v = get_var("n1")
92
+ if not v or len(v) < 3:
93
+ return None
94
+
95
+ status = v[0]
96
+ read_flag = v[1]
97
+ content = v[2:]
98
+
99
+ return {"status": status, "read": read_flag, "content": content}
100
+ except Exception as e:
101
+ print(f"[ERROR] monitor_n1() failed: {e}")
102
+ traceback.print_exc()
103
  return None
104
 
 
 
 
 
 
 
105
  def mark_n1_read():
106
+ try:
107
+ v = get_var("n1")
108
+ if v and len(v) >= 2:
109
+ newv = v[0] + "1" + v[2:]
110
+ set_var("n1", newv)
111
+ except Exception as e:
112
+ print(f"[ERROR] mark_n1_read() failed: {e}")
113
+ traceback.print_exc()
114
 
115
  def send_response(encoded_id):
116
+ try:
117
+ payload = "1" + "0" + encoded_id
118
+ set_var("n1", payload)
119
+ except Exception as e:
120
+ print(f"[ERROR] send_response('{encoded_id}') failed: {e}")
121
+ traceback.print_exc()
122
 
123
  # ---------------------
124
  # メインループ
125
  # ---------------------
126
  while True:
127
+ try:
128
+ n1_data = monitor_n1()
129
+ if n1_data:
130
+ # ID受信
131
+ if n1_data["status"] == "0" and n1_data["read"] == "0" and n0_is_free():
132
+ set_n0(1)
133
+ received_id = n1_data["content"]
134
+ mark_n1_read()
135
+
136
+ try:
137
+ url = f"https://20.rf.gd/set.php?project_id={received_id}"
138
+ r = requests.get(
139
+ url,
140
+ timeout=10,
141
+ cookies={"__test": "872039d283902860e895efb6018d2632"},
142
+ )
143
+ r.raise_for_status()
144
+ response_json = r.json()
145
+ short_id = response_json.get("short_id", "0")
146
+ encoded_short = encode_id(short_id)
147
+
148
+ send_response(encoded_short)
149
+
150
+ except Exception as e:
151
+ print(f"[ERROR] Server request for ID '{received_id}' failed: {e}")
152
+ traceback.print_exc()
153
+ send_response(encode_id("0"))
154
+
155
+ # レスポンス既読
156
+ elif n1_data["status"] == "1" and n1_data["read"] == "1":
157
+ set_n0(0)
158
+
159
+ except Exception as e:
160
+ print(f"[ERROR] Main loop iteration failed: {e}")
161
+ traceback.print_exc()
162
 
163
  time.sleep(0.2)