Skip to content

Commit

Permalink
Add yopen
Browse files Browse the repository at this point in the history
  • Loading branch information
pgDora56 committed Aug 1, 2024
1 parent 4519051 commit 1a92044
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 608 deletions.
17 changes: 16 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import talkingbox
import markov
from tamagame import TamaGame
from openroom import NagayaOpener
from openroom import NagayaOpener, YOpener
from trans import TransMocho

# config.jsonの読み込み
Expand Down Expand Up @@ -178,6 +178,21 @@ async def on_message(message):
await write("Roomopen Error:" + str(e))
else:
await message.channel.send("部屋名とパスワードは10文字以内で指定してください。")
elif msg.startswith("yopen"):
command = msg.split()
print(command)
if len(command) == 2:
randlst = [random.choice(string.ascii_letters + string.digits) for _ in range(10)]
command.append("".join(randlst))
if len(command) == 3:
if 0 < len(command[1]) <= 20 and \
0 < len(command[2]) <= 20:
try:
YOpener(command[1], command[2], message.channel).run_forever()
except:
pass # 握りつぶして良い
else:
await message.channel.send("部屋名とパスワードは20文字以内で指定してください。")
else:
e = ExecPy()
if(not await e.execution(message)):
Expand Down
119 changes: 118 additions & 1 deletion openroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import urllib
import time
import json
import websocket
try:
import thread
except ImportError:
import _thread as thread
import time

with open("config.json", "r") as f:
conf = json.load(f)
Expand Down Expand Up @@ -36,6 +42,117 @@ def openroom(user, pw):
time.sleep(1)
return roomno

class YOpener:
def __init__(self, roomname, pw, dchannel):
YQUI_WS_URI = "ws://yqui.net/ws"

websocket.enableTrace(False)

self.roomname = roomname
self.pw = pw
self.channel = dchannel
self.rooms = []
self.ws = websocket.WebSocketApp(YQUI_WS_URI,
on_message = lambda ws, msg: self.on_message(ws, msg),
on_error = lambda ws, msg: self.on_error(ws, msg),
on_close = lambda ws: self.on_close(ws))
self.ws.on_open = lambda ws: self.on_open(ws)

self.tryCount = 0
self.joinedRoom = -1
self.status = "lobby" # lobby, waiting

def send_to_channel(self, msg):
if self.channel == None:
print("Send: " + msg)
else:
self.channel.send(msg)

def on_message(self, ws, message):
msg = json.loads(message)
print(msg)
if "type" in msg:
if msg["type"] == "rooms":
self.rooms = msg["content"]
if msg["type"] == "joined":
announce = "部屋を開きました。\n\n" + \
f"Yqui Room{msg['content']} {self.roomname}\n" + \
f"パスワードは {self.pw} です。\n" + \
f"http://yqui.net/room/{msg['content']}"
self.send_to_channel(announce)
self.joinedRoom = int(msg["content"]) - 1
self.status = "waiting"
if msg["type"] == "sound":
if self.rooms[self.joinedRoom]["numUsers"] > 1:
# 自分以外にいるっぽいので自分は抜ける
self.ws.close()


# エラー時に呼ばれる関数
def on_error(self, ws, error):
self.send_to_channel("Error: " + error)
# self.ws.close()

# サーバーから切断時に呼ばれる関数
def on_close(self, ws):
# print("### closed ###")
pass

# サーバーから接続時に呼ばれる関数
def on_open(self, ws):
thread.start_new_thread(self.run, ())

# サーバーから接続時にスレッドで起動する関数
def run(self, *args):
time.sleep(5)
while True:
self.try_to_create_room()
time.sleep(30)

self.ws.close()
print("thread terminating...")

# websocketクライアント起動
def run_forever(self):
self.ws.run_forever()

def try_to_create_room(self):
if self.status != "lobby":
return None
tryOpen = -1
for room in self.rooms:
if room["numUsers"] == 0:
tryOpen = room["no"]
break
if tryOpen == -1:
self.send_to_channel("No room available")
return None
self.tryCount += 1

req = {
"c": "join",
"a": {
"name": "Momo",
"observer": True,
"chatAnswer": False,
"color": {
"index": 0,
"custom": "#FFC0CB"
},
"roomNo": tryOpen,
"first": True,
"tag": {
"title": self.roomname,
"password": self.pw
},
"scoreBackup": None
}
}
self.ws.send(json.dumps(req))




if __name__ == '__main__':
NagayaOpener.openroom("Test", "test")
# NagayaOpener.openroom("Test", "test")
YOpener("QAS", "qas", None).run_forever()
Loading

0 comments on commit 1a92044

Please sign in to comment.