MahjongRepository / tenhou-python-bot

Bot for tenhou.net riichi mahjong server written in Python
MIT License
200 stars 52 forks source link

[Question/Need help] How to watch a game based on python-socket communication with tenhou.net #165

Closed ZJChe closed 1 year ago

ZJChe commented 1 year ago

Hi developer @Nihisil , I'm very interested in your AI developed. I'm mostly interested in the socket communication part that allows the AI to play game automatically through sending text messages. So I'm now currently learning your code and developing a program to watch a tenhou game and print out the results. But I meet some problems.

By clicking into a tenhou game link obtained from tenhou.net/0/wg in my browser, and press F12, I saw the message sent in order to start watching a game: image So after I connected by socket and got authenticated (this part is same as your code), I sent <WG id="8DAC0C1A" tw=0 />, but I got no reply at all. At this stage if a sent a message requesting for a game (copied from your code), I could get reply and start a game. So I'm doubting whether I sent the correct message.

Later on I compared other messages my browser sent with your code's, and I found some differences: The Login message: image image Your code has an extra tid="f0"

The alive message when hanging around in lobby: image image The numbers within the messages are different.

Since both messages work for sure, I wonder how you found out the correct message to send, and why you did not choose the same message as the browser version. (Note, most other messages sent are the same)

I'm a beginner to socket, and I'm purely coding for fun and learning. I would appreciate it very much if you could give me some hints and guidance!

Nihisil commented 1 year ago

I'm sending messages that the Flash client sent a while ago, so they are based on real client-server communication. It looks like tenhou has now adjusted the protocol a bit, but old messages are still working fine.

To watch the game you can try this code: https://github.com/MahjongRepository/phoenix-logs/blob/18351052c10080564ef4e848e1709ce17067d9a5/live_games/watcher.py#L12

It connects to the ongoing game and stores all messages in array.

ZJChe commented 1 year ago

Thanks! That code inspired me a lot. I'm amazed at so many codes about Riichi Mahjong that you wrote😄

I still have some questions:

  1. How did you find out the Host ip and Port to connect to tenhou.net? It seems that the host and port in your 2 codes are different.
  2. If I intend to watch several games in several threads at the same time, connecting to tenhou.net through the same host and port will cause unpredictable and corrupted results. Do you have any solutions or suggestions?
ZJChe commented 1 year ago

I need to watch an uncertain number of games at the same time, possibly up to 10.

ZJChe commented 1 year ago

@Nihisil

Nihisil commented 1 year ago
  1. You can open any live game https://tenhou.net/0/wg/ and get the port IP from the browser network tab. I think tenhou has some IPs and ports.
  2. I think I can't help with that one
ZJChe commented 1 year ago

Is it supposed to be here😥 I don't see anything related to IP or PORT here. I can see in the "message" section that messages are receiving and sending. Screenshot 2022-11-14 172741

Nihisil commented 1 year ago

In FF it shows IP address, and default SSL port. Probably you can try just use host b-wc.mjv.jp and 443 port for to connection (I didn't try that):

image

I got IPs and ports for my code from Wireshark when sniffed traffic from old Flash version.