Browse Source

initial commit

Jean-Baptiste Aubort 5 months ago
commit
35794fbbd8
8 changed files with 3899 additions and 0 deletions
  1. 11 0
      Makefile
  2. 26 0
      README
  3. BIN
      __pycache__/messages_pb2.cpython-38.pyc
  4. BIN
      __pycache__/messages_pb2.cpython-39.pyc
  5. 119 0
      bot.py
  6. 398 0
      messages.proto
  7. 3338 0
      messages_pb2.py
  8. 7 0
      requirements.txt

+ 11 - 0
Makefile

@@ -0,0 +1,11 @@
+deps: requirements.txt
+	pip3 install -r requirements.txt
+
+messages.proto:
+	wget https://raw.githubusercontent.com/thecodingmachine/workadventure/develop/messages/protos/messages.proto
+
+messages_pb2.py: messages.proto
+	protoc --python_out=. messages.proto
+
+run: bot.py
+	while true; do ./bot.py ; sleep 10; done

+ 26 - 0
README

@@ -0,0 +1,26 @@
+Small bot
+========
+
+Run
+---
+
+```
+make deps
+make run
+```
+
+
+JS info
+-------
+
+this.localStorage:
+    characterLayers: "["color_24","eyes_18","hair_1","clothes_15","hats_1","accessory_1"]"
+    customCursorPosition: "{"activeRow":0,"selectedLayers":[23,17,0,15,0,0]}"
+    gameQuality: "60"
+    length: 8
+    localUser: "{"uuid":"197dc5c8-32b9-4fb1-add6-eeae1d30d380","jwtToken":"ASD","textures":[]}"
+    playerName: "JB"
+    selectedPlayer: "-1"
+    showJoystick: "false"
+    volume: "1"
+

BIN
__pycache__/messages_pb2.cpython-38.pyc


BIN
__pycache__/messages_pb2.cpython-39.pyc


+ 119 - 0
bot.py

@@ -0,0 +1,119 @@
+#!/usr/bin/env python3
+
+import requests
+import asyncio
+import websockets
+import logging
+import time
+from notify_run import Notify
+
+import messages_pb2
+
+# Test
+PUSHER = "world-test.fixme.ch"
+ROOM = "_%2Fglobal%2Fworld-test.fixme.ch%2Fmaps%2Ffixme.json"
+
+# Prod
+#PUSHER = "pusher.world.fixme.ch"
+#ROOM = "_%2Fglobal%2Fmaps.world.fixme.ch%2Ffixme%2Ffixme.json" 
+
+
+# Bot info
+name = "FIXMYBOT"
+character = "Female4"
+character = ["color_28","eyes_23","hair_1","clothes_50","hats_1","accessory_23"]
+bot_x = 382
+bot_y = 305
+
+logging.basicConfig(level=logging.INFO)
+
+s = requests.Session()
+r = s.post("https://{}/anonymLogin".format(PUSHER))
+
+authToken = r.json()["authToken"]
+userUuid = r.json()["userUuid"]
+
+logging.debug("authToken: {}".format(authToken))
+logging.debug("userUuid: {}".format(userUuid))
+
+
+uri = "wss://{}/room?roomId={}&token={}&name={}&{}&x={}&y={}&top=0&bottom=358&left=0&right=640".format(PUSHER, ROOM, authToken, name, 'characterLayers='+'&characterLayers='.join(character), bot_x, bot_y)
+
+def lerp(a, b, t):
+    return int(a + (b-a) * t)
+
+async def Follow(ws, position, speed=10):
+    global bot_x, bot_y
+    user_x = position.x
+    user_y = position.y
+    user_prox = 30
+    print('===initial var')
+    print('bot position=', bot_x, bot_y)
+    print('user position=', user_x, user_y)
+    print('---moving start')
+    for i in range(11):
+        t = i / speed
+        bot_x = lerp(bot_x, user_x-user_prox, t)
+        bot_y = lerp(bot_y, user_y-user_prox, t)
+        print(bot_x, bot_y, t)
+
+        message = messages_pb2.ClientToServerMessage(
+                userMovesMessage=messages_pb2.UserMovesMessage(
+                    position = messages_pb2.PositionMessage(x=bot_x, y=bot_y, direction=position.direction, moving=True,),
+                    viewport = messages_pb2.ViewportMessage(left=0, top=0, right=1000, bottom=1000),
+                ),
+        )
+        await ws.send(message.SerializeToString())
+
+        time.sleep(0.1)
+    print('---moving end')
+
+    # Stop moving at last position
+    message = messages_pb2.ClientToServerMessage(
+            userMovesMessage=messages_pb2.UserMovesMessage(
+                position = messages_pb2.PositionMessage(x=bot_x, y=bot_y, direction='DOWN', moving=False,),
+                viewport = messages_pb2.ViewportMessage(left=0, top=0, right=1000, bottom=1000),
+            ),
+    )
+    await ws.send(message.SerializeToString())
+
+async def SendMessage(ws, name):
+    message = messages_pb2.ClientToServerMessage(
+            playGlobalMessage=messages_pb2.PlayGlobalMessage(
+                id='1', type='message', message='Hello {}, <br/><br/> Welcome to VirtualFIXME !'.format(name)
+            ),
+            #stopGlobalMessage=messages_pb2.StopGlobalMessage(id='1')
+    )
+    await ws.send(message.SerializeToString())
+    logging.info("message sent: {}".format(message.SerializeToString()))
+
+def getMessages(message):
+    if message.WhichOneof("message") == "batchMessage":
+        for msg in message.batchMessage.payload:
+            yield msg
+    else:
+        return [message]
+
+async def bot():
+    follow_task = None
+
+    async with websockets.connect(uri) as websocket:
+
+        while True:
+            data = await websocket.recv()
+            message = messages_pb2.ServerToClientMessage()
+            message.ParseFromString(data)
+
+            for msg in getMessages(message):
+                print(msg)
+
+                if msg.WhichOneof("message") == "userMovedMessage":
+                    #await Follow(websocket, x, y, msg.userMovedMessage.position.x, msg.userMovedMessage.position.y)
+                    if follow_task != None:
+                        follow_task.cancel()
+                    follow_task = asyncio.create_task(Follow(websocket, msg.userMovedMessage.position))
+                if msg.WhichOneof("message") == "userJoinedMessage":
+                    #await SendMessage(websocket, msg.userJoinedMessage.name)
+                    pass
+
+asyncio.get_event_loop().run_until_complete(bot())

+ 398 - 0
messages.proto

@@ -0,0 +1,398 @@
+syntax = "proto3";
+
+/*********** PARTIAL MESSAGES **************/
+
+message PositionMessage {
+  int32 x = 1;
+  int32 y = 2;
+  enum Direction {
+    UP = 0;
+    RIGHT = 1;
+    DOWN = 2;
+    LEFT = 3;
+  }
+  Direction direction = 3;
+  bool moving = 4;
+}
+
+message PointMessage {
+  int32 x = 1;
+  int32 y = 2;
+}
+
+message ViewportMessage {
+  int32 left = 1;
+  int32 top = 2;
+  int32 right = 3;
+  int32 bottom = 4;
+}
+
+message SilentMessage {
+  bool silent = 1;
+}
+
+message CharacterLayerMessage {
+  string url = 1;
+  string name = 2;
+}
+
+/*********** CLIENT TO SERVER MESSAGES *************/
+
+message PingMessage {
+
+}
+
+message SetPlayerDetailsMessage {
+  string name = 1;
+  repeated string characterLayers = 2;
+}
+
+message UserMovesMessage {
+  PositionMessage position = 1;
+  ViewportMessage viewport = 2;
+}
+
+message WebRtcSignalToServerMessage {
+  int32 receiverId = 1;
+  string signal = 2;
+}
+
+message ReportPlayerMessage {
+  int32 reportedUserId = 1;
+  string reportComment = 2;
+}
+
+message QueryJitsiJwtMessage {
+  string jitsiRoom = 1;
+  string tag = 2; // FIXME: rather than reading the tag from the query, we should read it from the current map!
+}
+
+message ClientToServerMessage {
+  oneof message {
+    UserMovesMessage userMovesMessage = 2;
+    SilentMessage silentMessage = 3;
+    ViewportMessage viewportMessage = 4;
+    ItemEventMessage itemEventMessage = 5;
+    SetPlayerDetailsMessage setPlayerDetailsMessage = 6;
+    WebRtcSignalToServerMessage webRtcSignalToServerMessage = 7;
+    WebRtcSignalToServerMessage webRtcScreenSharingSignalToServerMessage = 8;
+    PlayGlobalMessage playGlobalMessage = 9;
+    StopGlobalMessage stopGlobalMessage = 10;
+    ReportPlayerMessage reportPlayerMessage = 11;
+    QueryJitsiJwtMessage queryJitsiJwtMessage = 12;
+  }
+}
+
+
+/************ BI-DIRECTIONAL MESSAGES **************/
+
+message ItemEventMessage {
+  int32 itemId = 1;
+  string event = 2;
+  string stateJson = 3;
+  string parametersJson = 4;
+}
+
+message PlayGlobalMessage {
+  string id = 1;
+  string type = 2;
+  string message = 3;
+}
+
+message StopGlobalMessage {
+  string id = 1;
+}
+
+/*********** SERVER TO CLIENT MESSAGES *************/
+
+message UserMovedMessage {
+  int32 userId = 1;
+  PositionMessage position = 2;
+}
+
+message SubMessage {
+  oneof message {
+    UserMovedMessage userMovedMessage = 1;
+    GroupUpdateMessage groupUpdateMessage = 2;
+    GroupDeleteMessage groupDeleteMessage = 3;
+    UserJoinedMessage userJoinedMessage = 4;
+    UserLeftMessage userLeftMessage = 5;
+    ItemEventMessage itemEventMessage = 6;
+  }
+}
+
+message BatchMessage {
+  string event = 1;
+  repeated SubMessage payload = 2;
+}
+
+message GroupUpdateMessage {
+  int32 groupId = 1;
+  PointMessage position = 2;
+  int32 groupSize = 3;
+}
+
+message GroupDeleteMessage {
+  int32 groupId = 1;
+}
+
+message UserJoinedMessage {
+  int32 userId = 1;
+  string name = 2;
+  repeated CharacterLayerMessage characterLayers = 3;
+  PositionMessage position = 4;
+}
+
+message UserLeftMessage {
+  int32 userId = 1;
+}
+
+message ErrorMessage {
+  string message = 1;
+}
+
+message ItemStateMessage {
+  int32 itemId = 1;
+  string stateJson = 2;
+}
+
+message RoomJoinedMessage {
+  //repeated UserJoinedMessage user = 1;
+  //repeated GroupUpdateMessage group = 2;
+  repeated ItemStateMessage item = 3;
+  int32 currentUserId = 4;
+  repeated string tag = 5;
+}
+
+message WebRtcStartMessage {
+  int32 userId = 1;
+  string name = 2;
+  bool initiator = 3;
+  string webrtcUserName = 4;
+  string webrtcPassword = 5;
+}
+
+message WebRtcDisconnectMessage {
+  int32 userId = 1;
+}
+
+message WebRtcSignalToClientMessage {
+  int32 userId = 1;
+  string signal = 2;
+  string webrtcUserName = 4;
+  string webrtcPassword = 5;
+}
+
+message TeleportMessageMessage{
+  string map = 1;
+}
+
+message SendJitsiJwtMessage {
+  string jitsiRoom = 1;
+  string jwt = 2;
+}
+
+message SendUserMessage{
+  string type = 1;
+  string message = 2;
+}
+
+message WorldFullWarningMessage{
+}
+message WorldFullWarningToRoomMessage{
+    string roomId = 1;
+}
+
+message WorldFullMessage{
+}
+
+message BanUserMessage{
+  string type = 1;
+  string message = 2;
+}
+
+message ServerToClientMessage {
+  oneof message {
+    BatchMessage batchMessage = 1;
+    ErrorMessage errorMessage = 2;
+    RoomJoinedMessage roomJoinedMessage = 3;
+    WebRtcStartMessage webRtcStartMessage = 4;
+    WebRtcSignalToClientMessage webRtcSignalToClientMessage = 5;
+    WebRtcSignalToClientMessage webRtcScreenSharingSignalToClientMessage = 6;
+    WebRtcDisconnectMessage webRtcDisconnectMessage = 7;
+    PlayGlobalMessage playGlobalMessage = 8;
+    StopGlobalMessage stopGlobalMessage = 9;
+    TeleportMessageMessage teleportMessageMessage = 10;
+    SendJitsiJwtMessage sendJitsiJwtMessage = 11;
+    SendUserMessage sendUserMessage = 12;
+    BanUserMessage banUserMessage = 13;
+    AdminRoomMessage adminRoomMessage = 14;
+    WorldFullWarningMessage worldFullWarningMessage = 15;
+    WorldFullMessage worldFullMessage = 16;
+  }
+}
+
+
+/************************** SERVICES **************************/
+
+message JoinRoomMessage {
+  PositionMessage positionMessage = 1;
+  string name = 2;
+  repeated CharacterLayerMessage characterLayer = 3;
+  string userUuid = 4;
+  string roomId = 5;
+  repeated string tag = 6;
+  string IPAddress = 7;
+}
+
+message UserJoinedZoneMessage {
+  int32 userId = 1;
+  string name = 2;
+  repeated CharacterLayerMessage characterLayers = 3;
+  PositionMessage position = 4;
+  Zone fromZone = 5;
+}
+
+message UserLeftZoneMessage {
+  int32 userId = 1;
+  Zone toZone = 2;
+}
+
+message GroupUpdateZoneMessage {
+  int32 groupId = 1;
+  PointMessage position = 2;
+  int32 groupSize = 3;
+  Zone fromZone = 4;
+}
+
+message GroupLeftZoneMessage {
+  int32 groupId = 1;
+  Zone toZone = 2;
+}
+
+
+message Zone {
+  int32 x = 1;
+  int32 y = 2;
+}
+
+message ZoneMessage {
+  string roomId = 1;
+  int32 x = 2;
+  int32 y = 3;
+}
+
+message PusherToBackMessage {
+  oneof message {
+    JoinRoomMessage joinRoomMessage = 1;
+    UserMovesMessage userMovesMessage = 2;
+    SilentMessage silentMessage = 3;
+    ItemEventMessage itemEventMessage = 4;
+    SetPlayerDetailsMessage setPlayerDetailsMessage = 5;
+    WebRtcSignalToServerMessage webRtcSignalToServerMessage = 6;
+    WebRtcSignalToServerMessage webRtcScreenSharingSignalToServerMessage = 7;
+    PlayGlobalMessage playGlobalMessage = 8;
+    StopGlobalMessage stopGlobalMessage = 9;
+    ReportPlayerMessage reportPlayerMessage = 10;
+    QueryJitsiJwtMessage queryJitsiJwtMessage = 11;
+    SendUserMessage sendUserMessage = 12;
+    BanUserMessage banUserMessage = 13;
+  }
+}
+
+message BatchToPusherMessage {
+  repeated SubToPusherMessage payload = 2;
+}
+
+message SubToPusherMessage {
+  oneof message {
+    UserJoinedZoneMessage userJoinedZoneMessage = 1;
+    GroupUpdateZoneMessage groupUpdateZoneMessage = 2;
+    UserMovedMessage userMovedMessage = 3;
+    GroupLeftZoneMessage groupLeftZoneMessage = 4;
+    UserLeftZoneMessage userLeftZoneMessage = 5;
+    ItemEventMessage itemEventMessage = 6;
+    SendUserMessage sendUserMessage = 7;
+    BanUserMessage banUserMessage = 8;
+  }
+}
+
+/*message BatchToAdminPusherMessage {
+  repeated SubToAdminPusherMessage payload = 2;
+}*/
+
+/*message SubToAdminPusherMessage {
+  oneof message {
+    string userUuidJoinedRoom = 1;
+    string userUuidLeftRoom = 2;
+  }
+}
+
+message ServerToAdminClientMessage {
+  repeated SubToAdminPusherMessage payload = 2;
+}*/
+
+message UserJoinedRoomMessage {
+  string uuid = 1;
+  string ipAddress = 2;
+  string name = 3;
+}
+
+message UserLeftRoomMessage {
+  string uuid = 1;
+}
+
+message ServerToAdminClientMessage {
+  oneof message {
+    UserJoinedRoomMessage userJoinedRoom = 1;
+    UserLeftRoomMessage userLeftRoom = 2;
+  }
+}
+
+message AdminPusherToBackMessage {
+  oneof message {
+    string subscribeToRoom = 1;
+    // TODO ban, unban
+  }
+}
+
+// A message sent by an administrator to a recipient
+message AdminMessage {
+  string message = 1;
+  string recipientUuid = 2;
+  string roomId = 3;
+  string type = 4;
+}
+
+// A message sent by an administrator to everyone in a specific room
+message AdminRoomMessage {
+  string message = 1;
+  string roomId = 2;
+}
+
+// A message sent by an administrator to absolutely everybody
+message AdminGlobalMessage {
+  string message = 1;
+}
+
+message BanMessage {
+  string recipientUuid = 1;
+  string roomId = 2;
+  string type = 3;
+  string message = 4;
+}
+
+message EmptyMessage {
+
+}
+
+service RoomManager {
+  rpc joinRoom(stream PusherToBackMessage) returns (stream ServerToClientMessage);
+  rpc listenZone(ZoneMessage) returns (stream BatchToPusherMessage);
+  rpc adminRoom(stream AdminPusherToBackMessage) returns (stream ServerToAdminClientMessage);
+  rpc sendAdminMessage(AdminMessage) returns (EmptyMessage);
+  rpc sendGlobalAdminMessage(AdminGlobalMessage) returns (EmptyMessage);
+  rpc ban(BanMessage) returns (EmptyMessage);
+  rpc sendAdminMessageToRoom(AdminRoomMessage) returns (EmptyMessage);
+  rpc sendWorldFullWarningToRoom(WorldFullWarningToRoomMessage) returns (EmptyMessage);
+}

File diff suppressed because it is too large
+ 3338 - 0
messages_pb2.py


+ 7 - 0
requirements.txt

@@ -0,0 +1,7 @@
+websockets
+requests
+black
+protobuf
+# grpcio
+# grpcio-tools
+notify-run