· 4 years ago · Oct 15, 2020, 10:44 PM
1config.js
2//=============================================================================
3// ** Config
4//=============================================================================
5
6const mjsoulUrl = 'wss://mjusgs.mahjongsoul.com:9443';
7
8const tournamentId = {ID};
9const userId = '';
10const oauthToken = {OAUTH token};
11
12const discordAuthToken = {Discord token};
13const discordChannelId = {Discord channel ID};
14
15//=============================================================================
16// ** Exports
17//=============================================================================
18
19module.exports = {
20 mjsoulUrl: mjsoulUrl,
21 tournamentId: tournamentId,
22 userId: userId,
23 oauthToken: oauthToken,
24 discordAuthToken: discordAuthToken,
25 discordChannelId: discordChannelId,
26}
27
28
29
30mjsoul.js:
31//============================================================================
32// ** Imports
33//============================================================================
34
35const fs = require("fs");
36const MJSoul = require("mjsoul");
37const shuffle = require("shuffle-array");
38const config = require("./config");
39const DiscordClient = require("./discord");
40
41//=============================================================================
42// ** MJsoulClient
43//=============================================================================
44
45class MJSoulClient {
46
47 //--------------------------------------------------------------------------
48 // * Initialization Methods
49 //--------------------------------------------------------------------------
50
51 constructor(url) {
52 this.discordClient = new DiscordClient(this);
53 this.mjsoul = new MJSoul.DHS({ 'url': url });
54
55 this.mjsoul.on(
56 'NotifyCustomContestSystemMsg',
57 this.handleNotifyContestSystemMsg.bind(this),
58 );
59
60 setInterval(this.sendPing.bind(this), 15000);
61 }
62
63 run() {
64 this.mjsoul.open(this.onConnect.bind(this));
65 }
66
67 send(name, payload, callback) {
68 this.mjsoul.send(name, payload, callback);
69 }
70
71 //--------------------------------------------------------------------------
72 // * Login and Authentication Logic
73 //--------------------------------------------------------------------------
74
75 onConnect(data) {
76
77 console.log("Login Attempt");
78
79 this.send(
80 'oauth2LoginContestManager',
81 {
82 type: 10,
83 access_token: config.oauthToken,
84 reconnect: false,
85 },
86 this.onLogin.bind(this),
87 );
88 }
89
90 onLogin(data) {
91 console.log(data);
92
93 this.send('loginBeat', { contract: '0.6.252' }, () => {
94 this.send('heatbeat', { no_operation_counter: 0 }, () => {
95
96 this.send(
97 'manageContest',
98 { unique_id: config.tournamentId },
99 (data) => {
100 console.log(data);
101 });
102 });
103 });
104 }
105
106 handleNotifyContestSystemMsg(data) {
107 console.log(data);
108
109 if (data.type !== 2) {
110 return;
111 }
112 }
113
114 //--------------------------------------------------------------------------
115 // * Healthcheck
116 //--------------------------------------------------------------------------
117
118 sendPing() {
119 this.send(
120 'heatbeat',
121 { no_operation_counter: 0 },
122 () => { console.log("^- %i -v", ++this.beatNum); }
123 );
124 }
125}
126
127//=============================================================================
128// ** Exports
129//=============================================================================
130
131module.exports = MJSoulClient;