termbluff 1.0.0 → 1.1.0
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.
- package/README.md +7 -5
- package/bin/termbluff.js +32 -15
- package/package.json +29 -8
- package/src/client.js +8 -4
- package/src/online.js +111 -0
- package/src/server.js +5 -3
package/README.md
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
**Liar's Dice** in your terminal: a bluffing and probability game for 2-6 players over LAN or the internet. No dependencies, nothing to install except Node.js 16+.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
|
-
npx termbluff host # host a
|
|
7
|
-
npx termbluff join
|
|
6
|
+
npx termbluff host # host; shows a ROOM CODE like A4VQ66
|
|
7
|
+
npx termbluff join A4VQ66 # friend joins with just the code (internet)
|
|
8
|
+
npx termbluff join 192.168.1.5 # or direct IP[:port] on a LAN
|
|
8
9
|
npx termbluff # interactive menu
|
|
9
|
-
npx termbluff serve # headless server (VPS)
|
|
10
10
|
```
|
|
11
11
|
|
|
12
12
|
## How to play
|
|
@@ -20,8 +20,10 @@ Whoever loses a die starts the next round. At 0 dice you're out. Last player sta
|
|
|
20
20
|
|
|
21
21
|
Controls: up/down = quantity, left/right = face, Enter = bid, `L` = call liar, `q` = quit. Terminal at least 60x22.
|
|
22
22
|
|
|
23
|
-
## Over the internet
|
|
24
|
-
Host
|
|
23
|
+
## Over the internet (office-friendly)
|
|
24
|
+
Host and joiners both connect **outbound** to a free public MQTT relay over WSS (port 8884/8084/8081), so nobody needs a public IP, port-forwarding, VPN, account or signup. The host picks the first relay reachable from their network and encodes it in the room code.
|
|
25
|
+
|
|
26
|
+
Caveats: the relay is shared infrastructure, so treat the room code as a secret and do not expect privacy beyond that. Game traffic is not encrypted end to end. If an office firewall blocks every relay port the host sees "Relay unreachable"; use a phone hotspot or `serve` on a VPS as a fallback.
|
|
25
27
|
|
|
26
28
|
## Publish
|
|
27
29
|
`npm login && npm publish`
|
package/bin/termbluff.js
CHANGED
|
@@ -4,16 +4,21 @@ const os = require('os');
|
|
|
4
4
|
const readline = require('readline');
|
|
5
5
|
const { createServer } = require('../src/server');
|
|
6
6
|
const { run } = require('../src/client');
|
|
7
|
+
const { hostRelay, onlineSocket, isRoomCode } = require('../src/online');
|
|
7
8
|
|
|
8
9
|
const DEFAULT_PORT = 7778;
|
|
9
10
|
|
|
10
11
|
function usage() {
|
|
11
|
-
console.log(`termbluff - Liar's Dice for 2-6 players in your terminal
|
|
12
|
+
console.log(`termbluff - Liar's Dice for 2-6 players in your terminal
|
|
12
13
|
|
|
13
|
-
npx termbluff
|
|
14
|
-
npx termbluff
|
|
15
|
-
npx termbluff join
|
|
16
|
-
npx termbluff serve [--port N] headless dedicated server
|
|
14
|
+
npx termbluff host [--name NAME] [--offline] host a table; prints a ROOM CODE
|
|
15
|
+
npx termbluff join ROOMCODE [--name NAME] join over the internet (no IP/VPN needed)
|
|
16
|
+
npx termbluff join HOST[:PORT] join directly over LAN / known IP
|
|
17
|
+
npx termbluff serve [--port N] headless dedicated server (TCP)
|
|
18
|
+
npx termbluff interactive menu
|
|
19
|
+
|
|
20
|
+
Internet play connects both sides OUT to a free public relay: no signup, no
|
|
21
|
+
port-forwarding, works through typical office firewalls (outbound HTTPS/WSS).
|
|
17
22
|
`);
|
|
18
23
|
}
|
|
19
24
|
|
|
@@ -35,8 +40,8 @@ function ask(q) {
|
|
|
35
40
|
return new Promise((res) => rl.question(q, (a) => { rl.close(); res(a.trim()); }));
|
|
36
41
|
}
|
|
37
42
|
|
|
38
|
-
function startServer(port) {
|
|
39
|
-
const srv = createServer({ port });
|
|
43
|
+
function startServer(port, host) {
|
|
44
|
+
const srv = createServer({ port, host });
|
|
40
45
|
srv.server.on('error', (e) => {
|
|
41
46
|
console.error(e.code === 'EADDRINUSE' ? `Port ${port} is already in use (use --port).` : `Server error: ${e.message}`);
|
|
42
47
|
process.exit(1);
|
|
@@ -49,28 +54,40 @@ async function main() {
|
|
|
49
54
|
let cmd = args[0], target = args[1];
|
|
50
55
|
const name = opt(args, '--name', os.userInfo().username);
|
|
51
56
|
const port = Number(opt(args, '--port', DEFAULT_PORT));
|
|
57
|
+
const offline = args.includes('--offline');
|
|
52
58
|
|
|
53
59
|
if (!cmd) {
|
|
54
60
|
console.log("TERMBLUFF - Liar's Dice\n 1) Host a table\n 2) Join a table\n 3) Quit");
|
|
55
61
|
const c = await ask('> ');
|
|
56
62
|
if (c === '1') cmd = 'host';
|
|
57
|
-
else if (c === '2') { cmd = 'join'; target = await ask('
|
|
63
|
+
else if (c === '2') { cmd = 'join'; target = await ask('Room code (or host address): '); }
|
|
58
64
|
else return;
|
|
59
65
|
}
|
|
60
66
|
|
|
61
67
|
if (cmd === 'serve') {
|
|
62
|
-
startServer(port).server.on('listening', () =>
|
|
68
|
+
startServer(port, '0.0.0.0').server.on('listening', () =>
|
|
63
69
|
console.log(`termbluff server on port ${port}. LAN: ${lanIps().map((i) => `${i}:${port}`).join(', ') || 'n/a'}`));
|
|
64
70
|
} else if (cmd === 'host') {
|
|
65
|
-
|
|
71
|
+
// Local player talks to a loopback-only server; remote players arrive via the
|
|
72
|
+
// online relay (outbound only) and, with --offline, optionally over LAN.
|
|
73
|
+
const srv = startServer(port, offline ? '0.0.0.0' : '127.0.0.1');
|
|
74
|
+
let relay = null, status = offline ? `LAN: join ${lanIps()[0] || '<ip>'}:${port}` : 'Connecting to relay...';
|
|
66
75
|
srv.server.on('listening', () => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
76
|
+
if (!offline) {
|
|
77
|
+
hostRelay(srv.attach)
|
|
78
|
+
.then((r) => { relay = r; status = r ? `ROOM CODE: ${r.code} (friend runs: npx termbluff join ${r.code})` : 'Relay unreachable - this network blocks it'; })
|
|
79
|
+
.catch((e) => { status = `Relay error: ${e.message}`; });
|
|
80
|
+
}
|
|
81
|
+
run({ host: '127.0.0.1', port, name, info: () => status,
|
|
82
|
+
onExit: (c) => { if (relay) relay.close(); srv.close(); process.exit(c); } });
|
|
70
83
|
});
|
|
71
84
|
} else if (cmd === 'join' && target) {
|
|
72
|
-
|
|
73
|
-
|
|
85
|
+
if (isRoomCode(target) && !offline) {
|
|
86
|
+
run({ name, info: `Room ${target.toUpperCase()}`, connect: () => onlineSocket(target), onExit: process.exit });
|
|
87
|
+
} else {
|
|
88
|
+
const t = parseTarget(target);
|
|
89
|
+
run({ host: t.host, port: t.port, name, info: `Connected to ${t.host}:${t.port}`, onExit: process.exit });
|
|
90
|
+
}
|
|
74
91
|
} else {
|
|
75
92
|
usage();
|
|
76
93
|
process.exit(cmd === 'help' || cmd === '--help' ? 0 : 1);
|
package/package.json
CHANGED
|
@@ -1,11 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "termbluff",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Liar's Dice -
|
|
5
|
-
"bin": {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Liar's Dice - multiplayer bluffing game in your terminal. Play over the internet with a room code: no signup, no IP, no VPN.",
|
|
5
|
+
"bin": {
|
|
6
|
+
"termbluff": "bin/termbluff.js"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"bin",
|
|
10
|
+
"src",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "node test/smoke.js"
|
|
15
|
+
},
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=16"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"game",
|
|
21
|
+
"terminal",
|
|
22
|
+
"multiplayer",
|
|
23
|
+
"liars-dice",
|
|
24
|
+
"bluff",
|
|
25
|
+
"cli",
|
|
26
|
+
"lan"
|
|
27
|
+
],
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"mqtt": "^5.16.1"
|
|
31
|
+
}
|
|
11
32
|
}
|
package/src/client.js
CHANGED
|
@@ -56,18 +56,22 @@ function renderFrame(s, sel, info, msg, now = 0) {
|
|
|
56
56
|
return L.join('\x1b[K\r\n') + '\x1b[K';
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
// `connect` optionally supplies a socket-like stream (used for online rooms);
|
|
60
|
+
// `info` may be a string or a function returning one.
|
|
61
|
+
function run({ host, port, name, info, connect, onExit = () => {} }) {
|
|
60
62
|
const out = process.stdout, inp = process.stdin;
|
|
61
63
|
if (!inp.isTTY || !out.isTTY) { console.error('termbluff needs an interactive terminal.'); return onExit(1); }
|
|
62
64
|
let state = null, sel = { q: 1, f: 1 }, msg = '', done = false, recvAt = 0, lastKey = '';
|
|
63
65
|
|
|
64
|
-
const sock = net.connect({ host, port });
|
|
66
|
+
const sock = connect ? connect() : net.connect({ host, port });
|
|
65
67
|
sock.setNoDelay(true);
|
|
68
|
+
const noReply = setTimeout(() => { if (!state) finish(1, 'No response - wrong room code, or the host is not running.'); }, 15000);
|
|
66
69
|
const send = (o) => { if (!done) sock.write(JSON.stringify(o) + '\n'); };
|
|
67
70
|
const finish = (code, text) => {
|
|
68
71
|
if (done) return;
|
|
69
72
|
done = true;
|
|
70
73
|
clearInterval(tick);
|
|
74
|
+
clearTimeout(noReply);
|
|
71
75
|
try { inp.setRawMode(false); } catch {}
|
|
72
76
|
inp.pause();
|
|
73
77
|
out.write('\x1b[?25h\x1b[?1049l');
|
|
@@ -80,14 +84,14 @@ function run({ host, port, name, info, onExit = () => {} }) {
|
|
|
80
84
|
const cols = out.columns || 80, rows = out.rows || 24;
|
|
81
85
|
if (cols < 60 || rows < 22) { out.write('\x1b[2J\x1b[HTerminal too small (need 60x22). Resize.'); return; }
|
|
82
86
|
// state.ms is time left when sent; rebase to local clock for the countdown
|
|
83
|
-
out.write('\x1b[H' + renderFrame({ ...state, ms: state.ms }, sel, info, msg, Date.now() - recvAt) + '\x1b[J');
|
|
87
|
+
out.write('\x1b[H' + renderFrame({ ...state, ms: state.ms }, sel, typeof info === 'function' ? info() : info, msg, Date.now() - recvAt) + '\x1b[J');
|
|
84
88
|
};
|
|
85
89
|
const tick = setInterval(draw, 500);
|
|
86
90
|
|
|
87
91
|
inp.setRawMode(true);
|
|
88
92
|
inp.resume();
|
|
89
93
|
out.write('\x1b[?1049h\x1b[?25l\x1b[2J');
|
|
90
|
-
out.write(`Connecting to ${host}:${port} ...`);
|
|
94
|
+
out.write(connect ? 'Connecting to online room ...' : `Connecting to ${host}:${port} ...`);
|
|
91
95
|
|
|
92
96
|
const resetSel = () => {
|
|
93
97
|
const b = state.bid;
|
package/src/online.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Internet play without public IPs, VPNs or port-forwarding: host and joiners both
|
|
3
|
+
// connect OUT to a public MQTT broker over WSS, and the game's normal JSON lines
|
|
4
|
+
// travel through it. Room code = broker letter + 5 random chars, e.g. "A7K3QZ".
|
|
5
|
+
const { Duplex } = require('stream');
|
|
6
|
+
const crypto = require('crypto');
|
|
7
|
+
|
|
8
|
+
const BROKERS = [
|
|
9
|
+
'wss://broker.hivemq.com:8884/mqtt',
|
|
10
|
+
'wss://broker.emqx.io:8084/mqtt',
|
|
11
|
+
'wss://test.mosquitto.org:8081',
|
|
12
|
+
];
|
|
13
|
+
const ALPHA = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
|
|
14
|
+
const CODE_RE = /^[A-C][A-HJ-NP-Z2-9]{5}$/;
|
|
15
|
+
const PING_MS = 5000;
|
|
16
|
+
const IDLE_MS = 20000;
|
|
17
|
+
const BYE = '{"t":"bye"}\n';
|
|
18
|
+
const PING = '{"t":"ping"}\n';
|
|
19
|
+
|
|
20
|
+
const isRoomCode = (s) => CODE_RE.test(String(s || '').toUpperCase());
|
|
21
|
+
const stub = (sock) => { sock.setNoDelay = () => {}; sock.setKeepAlive = () => {}; return sock; };
|
|
22
|
+
|
|
23
|
+
function connectBroker(url) {
|
|
24
|
+
const mqtt = require('mqtt');
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
const c = mqtt.connect(url, { protocolVersion: 4, connectTimeout: 8000, reconnectPeriod: 0, clientId: 'tb_' + crypto.randomBytes(6).toString('hex') });
|
|
27
|
+
c.once('connect', () => { c.removeAllListeners('error'); resolve(c); });
|
|
28
|
+
c.once('error', (e) => { c.end(true); reject(e); });
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
const subscribe = (c, topic) => new Promise((res, rej) => c.subscribe(topic, { qos: 1 }, (e) => (e ? rej(e) : res())));
|
|
32
|
+
|
|
33
|
+
// Host side: every remote player becomes a virtual socket handed to the game server.
|
|
34
|
+
async function hostRelay(attach) {
|
|
35
|
+
for (let i = 0; i < BROKERS.length; i++) {
|
|
36
|
+
let c;
|
|
37
|
+
try { c = await connectBroker(BROKERS[i]); } catch { continue; }
|
|
38
|
+
const code = String.fromCharCode(65 + i) + Array.from(crypto.randomBytes(5), (b) => ALPHA[b % ALPHA.length]).join('');
|
|
39
|
+
const base = `termbluff/v1/${code}`;
|
|
40
|
+
try { await subscribe(c, `${base}/up/+`); } catch { c.end(true); continue; }
|
|
41
|
+
const conns = new Map();
|
|
42
|
+
|
|
43
|
+
c.on('message', (topic, payload) => {
|
|
44
|
+
const cid = topic.split('/').pop();
|
|
45
|
+
let e = conns.get(cid);
|
|
46
|
+
const text = payload.toString();
|
|
47
|
+
if (!e) {
|
|
48
|
+
if (text.includes('"bye"')) return;
|
|
49
|
+
const sock = stub(new Duplex({
|
|
50
|
+
read() {},
|
|
51
|
+
write(chunk, _enc, cb) { c.publish(`${base}/down/${cid}`, chunk.toString(), { qos: 1 }, () => cb()); },
|
|
52
|
+
final(cb) { cb(); setImmediate(() => sock.destroy()); },
|
|
53
|
+
}));
|
|
54
|
+
e = { sock, seen: Date.now() };
|
|
55
|
+
conns.set(cid, e);
|
|
56
|
+
sock.on('close', () => conns.delete(cid));
|
|
57
|
+
sock.on('error', () => {});
|
|
58
|
+
attach(sock);
|
|
59
|
+
}
|
|
60
|
+
e.seen = Date.now();
|
|
61
|
+
if (text.includes('"bye"')) return e.sock.destroy();
|
|
62
|
+
e.sock.push(text);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const sweep = setInterval(() => {
|
|
66
|
+
for (const e of conns.values()) if (Date.now() - e.seen > IDLE_MS) e.sock.destroy();
|
|
67
|
+
}, PING_MS);
|
|
68
|
+
c.on('close', () => clearInterval(sweep));
|
|
69
|
+
return { code, close() { clearInterval(sweep); for (const e of conns.values()) e.sock.destroy(); c.end(true); } };
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Joiner side: a socket-like Duplex that emits 'connect' once it is wired to the room.
|
|
75
|
+
function onlineSocket(rawCode) {
|
|
76
|
+
const code = rawCode.toUpperCase();
|
|
77
|
+
const url = BROKERS[code.charCodeAt(0) - 65];
|
|
78
|
+
const cid = crypto.randomBytes(6).toString('hex');
|
|
79
|
+
const base = `termbluff/v1/${code}`;
|
|
80
|
+
let c = null, pingT = null;
|
|
81
|
+
const sock = stub(new Duplex({
|
|
82
|
+
read() {},
|
|
83
|
+
write(chunk, _enc, cb) {
|
|
84
|
+
if (!c || !c.connected) return cb();
|
|
85
|
+
c.publish(`${base}/up/${cid}`, chunk.toString(), { qos: 1 }, () => cb());
|
|
86
|
+
},
|
|
87
|
+
destroy(err, cb) {
|
|
88
|
+
clearInterval(pingT);
|
|
89
|
+
const client = c; c = null;
|
|
90
|
+
if (client) {
|
|
91
|
+
const end = () => client.end(true);
|
|
92
|
+
client.connected ? client.publish(`${base}/up/${cid}`, BYE, { qos: 1 }, end) : end();
|
|
93
|
+
setTimeout(end, 1500).unref();
|
|
94
|
+
}
|
|
95
|
+
cb(err);
|
|
96
|
+
},
|
|
97
|
+
}));
|
|
98
|
+
connectBroker(url)
|
|
99
|
+
.then(async (client) => {
|
|
100
|
+
c = client;
|
|
101
|
+
client.on('message', (_t, p) => sock.push(p.toString()));
|
|
102
|
+
client.on('close', () => sock.destroy());
|
|
103
|
+
await subscribe(client, `${base}/down/${cid}`);
|
|
104
|
+
pingT = setInterval(() => client.connected && client.publish(`${base}/up/${cid}`, PING, { qos: 0 }), PING_MS);
|
|
105
|
+
sock.emit('connect');
|
|
106
|
+
})
|
|
107
|
+
.catch((e) => sock.destroy(e));
|
|
108
|
+
return sock;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
module.exports = { hostRelay, onlineSocket, isRoomCode, BROKERS };
|
package/src/server.js
CHANGED
|
@@ -138,7 +138,8 @@ function createServer({ port = 7778, host = '0.0.0.0', turnMs = 40000, revealMs
|
|
|
138
138
|
broadcast();
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
|
|
141
|
+
// onConn handles any duplex stream: real TCP sockets and the online relay's virtual ones.
|
|
142
|
+
const onConn = (sock) => {
|
|
142
143
|
sock.setNoDelay(true);
|
|
143
144
|
sock.setKeepAlive(true, 15000);
|
|
144
145
|
let me = null, buf = '';
|
|
@@ -179,9 +180,10 @@ function createServer({ port = 7778, host = '0.0.0.0', turnMs = 40000, revealMs
|
|
|
179
180
|
});
|
|
180
181
|
sock.on('close', () => { clearTimeout(joinTimer); if (me) leave(me); });
|
|
181
182
|
sock.on('error', () => sock.destroy());
|
|
182
|
-
}
|
|
183
|
+
};
|
|
184
|
+
const server = net.createServer(onConn);
|
|
183
185
|
server.listen(port, host);
|
|
184
|
-
return { server, close() { clearTimeout(timer); for (const p of players.values()) p.sock.destroy(); server.close(); } };
|
|
186
|
+
return { server, attach: onConn, close() { clearTimeout(timer); for (const p of players.values()) p.sock.destroy(); server.close(); } };
|
|
185
187
|
}
|
|
186
188
|
|
|
187
189
|
module.exports = { createServer, validRaise, cleanName };
|