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

Update turbowarp-server/qr-converter.py

Browse files
Files changed (1) hide show
  1. turbowarp-server/qr-converter.py +88 -87
turbowarp-server/qr-converter.py CHANGED
@@ -14,146 +14,147 @@ tw = scratchcommunication.TwCloudConnection(
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)}
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  # ---------------------
22
  # クラウド変数操作
23
  # ---------------------
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:
 
14
  contact_info=CONTACT
15
  )
16
 
17
+ # ---------------------
18
+ # 文字テーブル
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)}
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
  def set_var(name, value):
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
+ total = int(v[1])
 
 
 
 
112
  except Exception as e:
113
+ print(f"[ERROR] Invalid total in slot '{slot}': {v}")
114
  traceback.print_exc()
115
+ return None
116
 
117
+ data = v[3:]
118
+ newv = v[:2] + "1" + v[3:]
119
+ set_var(slot, newv)
 
 
 
 
 
120
 
121
+ if slot not in buffers:
122
+ buffers[slot] = []
 
123
 
124
+ buffers[slot].append(data)
 
 
 
 
125
 
126
+ if len(buffers[slot]) < total:
127
+ return None
 
 
 
 
 
 
 
128
 
129
+ joined = "".join(buffers[slot])
130
+ buffers[slot] = []
131
+ return decode(joined)
 
 
 
 
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
  except Exception as e: