py2hackCraft2 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,136 @@
1
+ import websocket
2
+ import threading
3
+ import time
4
+ import json
5
+
6
+ class WebSocketClient:
7
+ def __init__(self, url):
8
+ self.url = url
9
+ self.ws = websocket.WebSocketApp(url,
10
+ on_message=self.on_message,
11
+ on_error=self.on_error,
12
+ on_close=self.on_close)
13
+ self.ws.on_open = self.on_open
14
+ self.thread = threading.Thread(target=self.ws.run_forever)
15
+ self.thread.daemon = True
16
+ self.lock = threading.Lock()
17
+ self.connected = False
18
+ self.response_event = threading.Event() # イベントオブジェクトを追加
19
+ self.last_message = None # 最後に受信したメッセージを保持
20
+
21
+ def on_message(self, ws, message):
22
+ print("Received '%s'" % message)
23
+ self.last_message = message # 最後に受信したメッセージを更新
24
+ self.response_event.set() # イベントをセットして、メッセージの受信を通知
25
+
26
+ def on_error(self, ws, error):
27
+ print("Error '%s'" % error)
28
+
29
+ def on_close(self, ws, close_status_code, close_msg):
30
+ print("### closed ###")
31
+ self.connected = False
32
+
33
+ def on_open(self, ws):
34
+ print("Opened connection")
35
+ self.connected = True
36
+
37
+ def run_forever(self):
38
+ self.thread.start()
39
+
40
+ def wait_for_connection(self):
41
+ while not self.connected:
42
+ time.sleep(0.1) # Wait for connection to be established
43
+
44
+ def send(self, message):
45
+ self.wait_for_connection()
46
+ with self.lock:
47
+ self.response_event.clear() # イベントをクリアして新しいレスポンスの準備をする
48
+ self.ws.send(message)
49
+ self.response_event.wait() # サーバーからのレスポンスを待つ
50
+ return self.last_message # 最後に受信したメッセージを返す
51
+
52
+ def close(self):
53
+ self.ws.close()
54
+ self.thread.join()
55
+
56
+
57
+ class Entity:
58
+ def __init__(self, url, player, entity):
59
+ self.client = None # 初期化時にはWebSocketClientはNone
60
+ self.url = url
61
+ self.player = player
62
+ self.entity = entity
63
+
64
+ def connect(self):
65
+ print("Connecting to %s %s %s" %( self.url, self.player, self.entity))
66
+ if not self.client:
67
+ self.client = WebSocketClient(self.url) # 遅延初期化
68
+ self.client.run_forever()
69
+ self.client.send(json.dumps({
70
+ "type": "connect2",
71
+ "data": {
72
+ "player": self.player,
73
+ "entity": self.entity,
74
+ }
75
+ }))
76
+
77
+ def disconnect(self):
78
+ if self.client:
79
+ self.client.close()
80
+ self.client = None
81
+ self.player = None
82
+ self.entity = None
83
+
84
+ def forward(self):
85
+ message = {
86
+ "type": "call",
87
+ "data": {
88
+ "name": "forward"
89
+ }
90
+ }
91
+ self.client.send(json.dumps(message))
92
+
93
+ def back(self):
94
+ message = {
95
+ "type": "call",
96
+ "data": {
97
+ "name": "back"
98
+ }
99
+ }
100
+ self.client.send(json.dumps(message))
101
+
102
+ def up(self):
103
+ message = {
104
+ "type": "call",
105
+ "data": {
106
+ "name": "up"
107
+ }
108
+ }
109
+ self.client.send(json.dumps(message))
110
+
111
+ def down(self):
112
+ message = {
113
+ "type": "call",
114
+ "data": {
115
+ "name": "down"
116
+ }
117
+ }
118
+ self.client.send(json.dumps(message))
119
+
120
+ def turnLeft(self):
121
+ message = {
122
+ "type": "call",
123
+ "data": {
124
+ "name": "turnLeft"
125
+ }
126
+ }
127
+ self.client.send(json.dumps(message))
128
+
129
+ def turnRight(self):
130
+ message = {
131
+ "type": "call",
132
+ "data": {
133
+ "name": "turnRight"
134
+ }
135
+ }
136
+ self.client.send(json.dumps(message))
@@ -0,0 +1,11 @@
1
+ Metadata-Version: 2.1
2
+ Name: py2hackCraft2
3
+ Version: 0.0.1
4
+ Summary: These are APIs that connect to the hackCraft2 server from Python to manipulate pets.
5
+ Home-page: http://example.com/HelloWorld/
6
+ Author: Masafumi Terazono
7
+ Author-email: masafumi_t@0x48lab.com
8
+ License: MIT
9
+ Keywords: hackCraft2
10
+ Requires-Dist: websocket-client
11
+
@@ -0,0 +1,6 @@
1
+ py2hackCraft/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ py2hackCraft/modules.py,sha256=SAl7SqcHTFGNnlrw0WMjJMc9_Kg1YakKv27qF5_4s7Q,4053
3
+ py2hackCraft2-0.0.1.dist-info/METADATA,sha256=MuxWKSfoPHc6nLHCzBpG2nHRNYV0dTwnu_TmCMByjK8,323
4
+ py2hackCraft2-0.0.1.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
5
+ py2hackCraft2-0.0.1.dist-info/top_level.txt,sha256=ni8nQFDCOPl1sT1h9p0iGlHmntnuEBQdqFZnAcRsJjA,13
6
+ py2hackCraft2-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.40.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ py2hackCraft