-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
522 lines (447 loc) · 14.5 KB
/
server.js
File metadata and controls
522 lines (447 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import { Server } from "socket.io";
import { createServer } from "http";
import express from "express";
import * as dotenv from "dotenv";
dotenv.config();
import axios from "axios";
import cors from "cors";
import path from "path";
import { fileURLToPath } from "url";
import crypto from "crypto";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(cors()); // Enable CORS for all routes
app.use(express.json()); // Add middleware to parse JSON bodies
// API Routes
app.post("/invoice", async (req, res) => {
try {
console.log("Creating invoice");
if (PAYMENT_PROCESSOR === 'voltage_payments') {
// Generate a UUID for the payment
const paymentId = crypto.randomUUID();
// Create payment request
const response = await axios.post(
`${VOLTAGE_API_URL}/v1/organizations/${VOLTAGE_ORGANIZATION_ID}/environments/${VOLTAGE_ENVIRONMENT_ID}/payments`,
{
id: paymentId,
wallet_id: VOLTAGE_WALLET_ID,
currency: "btc",
amount_msats: 1000000,
payment_kind: "bolt11",
description: "BitDoom Player Joins"
},
{
headers: {
"x-api-key": VOLTAGE_API_KEY,
"Content-Type": "application/json"
}
}
);
if (response.status === 202) {
return res.json({
payment_request: null, // Will be populated by polling
invoice_id: paymentId,
payment_processor: 'voltage_payments'
});
}
}
// Default to LNBits
const response = await axios.post(
`${LNBITS_API_URL}/api/v1/payments`,
{ out: false, amount: 1000, memo: "player join" },
{
headers: {
"X-Api-Key": LNBITS_ADMIN_KEY,
"Content-Type": "application/json",
},
}
);
console.debug("returning invoice!");
return res.json({
payment_request: response.data.payment_request,
invoice_id: response.data.payment_hash,
payment_processor: 'lnbits'
});
} catch (error) {
console.error(
"Error creating invoice:",
error.response?.data || error.message
);
res.status(500).json({ error: "Failed to create invoice" });
}
});
app.get("/invoice", async (req, res) => {
try {
console.log("Checking invoice status");
const { id, processor } = req.query;
if (!id) {
return res.status(400).json({ error: "Invoice ID is required" });
}
if (processor === 'voltage_payments') {
const response = await axios.get(
`${VOLTAGE_API_URL}/v1/organizations/${VOLTAGE_ORGANIZATION_ID}/environments/${VOLTAGE_ENVIRONMENT_ID}/payments/${id}`,
{
headers: {
"x-api-key": VOLTAGE_API_KEY
}
}
);
const payment = response.data;
// If the payment is still being created, return PENDING
if (payment.status === 'receiving' && !payment.data?.payment_request) {
return res.json({ status: "PENDING" });
}
// If we have the payment request but it's not paid yet
if (payment.status === 'receiving' && payment.data?.payment_request) {
return res.json({
status: "PENDING",
payment_request: payment.data.payment_request
});
}
// If the payment is completed
if (payment.status === 'completed') {
return res.json({ status: "PAID" });
}
// If there was an error
if (payment.error) {
return res.status(500).json({ error: payment.error });
}
return res.json({ status: "PENDING" });
}
// Default to LNBits
const response = await axios.get(
`${LNBITS_API_URL}/api/v1/payments/${id}`,
{ headers: { "X-Api-Key": LNBITS_ADMIN_KEY } }
);
return res.json({ status: response.data.paid ? "PAID" : "PENDING" });
} catch (error) {
console.error(
"Error checking invoice status:",
error.response?.data || error.message
);
res.status(500).json({ error: "Failed to fetch invoice status" });
}
});
app.post("/withdraw", async (req, res) => {
try {
console.log("Creating withdrawal invoice");
const { lightningAddress, amount } = req.body;
if (!lightningAddress || !amount || amount <= 0) {
return res.status(400).json({ error: "Invalid withdrawal request" });
}
if (PAYMENT_PROCESSOR === 'voltage_payments') {
// Generate a UUID for the payment
const paymentId = crypto.randomUUID();
// Create payment request
const response = await axios.post(
`${VOLTAGE_API_URL}/v1/organizations/${VOLTAGE_ORGANIZATION_ID}/environments/${VOLTAGE_ENVIRONMENT_ID}/payments`,
{
id: paymentId,
wallet_id: VOLTAGE_WALLET_ID,
currency: "btc",
amount_msats: amount * 1000, // Convert sats to msats
payment_kind: "bolt11",
description: "BitDoom Withdrawal",
destination: lightningAddress
},
{
headers: {
"x-api-key": VOLTAGE_API_KEY,
"Content-Type": "application/json"
}
}
);
if (response.status === 202) {
return res.json({
payment_request: null, // Will be populated by polling
invoice_id: paymentId,
payment_processor: 'voltage_payments'
});
}
}
// Default to LNBits
const response = await axios.post(
`${LNBITS_API_URL}/api/v1/payments`,
{
out: true,
amount: amount,
memo: "BitDoom Withdrawal",
lnurl: lightningAddress
},
{
headers: {
"X-Api-Key": LNBITS_ADMIN_KEY,
"Content-Type": "application/json",
},
}
);
return res.json({
payment_request: response.data.payment_request,
invoice_id: response.data.payment_hash,
payment_processor: 'lnbits'
});
} catch (error) {
console.error(
"Error creating withdrawal invoice:",
error.response?.data || error.message
);
res.status(500).json({ error: "Failed to create withdrawal invoice" });
}
});
app.post("/pay", async (req, res) => {
try {
const { payment_request, amount } = req.body;
if (!payment_request || !amount || amount <= 0) {
return res.status(400).json({ error: "Invalid payment request" });
}
// Generate a UUID for the payment
const paymentId = crypto.randomUUID();
// Create payment request
const response = await axios.post(
`${VOLTAGE_API_URL}/v1/organizations/${VOLTAGE_ORGANIZATION_ID}/environments/${VOLTAGE_ENVIRONMENT_ID}/payments`,
{
id: paymentId,
wallet_id: VOLTAGE_WALLET_ID,
currency: "btc",
type: "bolt11",
data: {
amount_msats: amount * 1000, // Convert sats to msats
max_fee_msats: Math.floor(amount * 1000 * 0.1), // 10% of amount
payment_request: payment_request
}
},
{
headers: {
"x-api-key": VOLTAGE_API_KEY,
"Content-Type": "application/json"
}
}
);
if (response.status === 202) {
return res.json({
payment_id: paymentId,
status: "pending"
});
}
throw new Error("Failed to initiate payment");
} catch (error) {
console.error(
"Error processing payment:",
error.response?.data || error.message
);
res.status(500).json({ error: "Failed to process payment" });
}
});
app.get("/payment-status/:id", async (req, res) => {
try {
const { id } = req.params;
if (!id) {
return res.status(400).json({ error: "Payment ID is required" });
}
const response = await axios.get(
`${VOLTAGE_API_URL}/v1/organizations/${VOLTAGE_ORGANIZATION_ID}/environments/${VOLTAGE_ENVIRONMENT_ID}/payments/${id}`,
{
headers: {
"x-api-key": VOLTAGE_API_KEY
}
}
);
const payment = response.data;
if (payment.status === "completed") {
return res.json({ status: "completed" });
} else if (payment.error) {
return res.json({ status: "error", error: payment.error });
} else {
return res.json({ status: "pending" });
}
} catch (error) {
console.error(
"Error checking payment status:",
error.response?.data || error.message
);
res.status(500).json({ error: "Failed to check payment status" });
}
});
// Serve static files from the React app build directory
app.use(express.static(path.join(__dirname, "dist")));
// Handle React routing, return all requests to React app
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "index.html"));
});
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
const LNBITS_API_URL = process.env.LNBITS_API_URL;
const LNBITS_ADMIN_KEY = process.env.LNBITS_ADMIN_KEY;
const PAYMENT_PROCESSOR = process.env.PAYMENT_PROCESSOR || 'lnbits';
// Voltage Payments configuration
const VOLTAGE_API_URL = process.env.VOLTAGE_API_URL;
const VOLTAGE_API_KEY = process.env.VOLTAGE_API_KEY;
const VOLTAGE_ORGANIZATION_ID = process.env.VOLTAGE_ORGANIZATION_ID;
const VOLTAGE_ENVIRONMENT_ID = process.env.VOLTAGE_ENVIRONMENT_ID;
const VOLTAGE_WALLET_ID = process.env.VOLTAGE_WALLET_ID;
// Store all connected players
const players = {};
const DAMAGE = 5; // 5% damage per hit
const INITIAL_SATS = 1000; // Starting bitcoin amount
// Portal timer constants
const PORTAL_COUNTDOWN = 60;
const PORTAL_DURATION = 10;
let portalTimer = PORTAL_COUNTDOWN;
let portalOpen = false;
let timerInterval = null;
// Start the portal timer when the server starts
function startPortalTimer() {
if (timerInterval) clearInterval(timerInterval);
timerInterval = setInterval(() => {
if (!portalOpen) {
// Countdown phase
portalTimer--;
if (portalTimer <= 0) {
// Open portal
portalOpen = true;
portalTimer = PORTAL_DURATION;
io.emit('portalTimerSync', { countdown: portalTimer, isOpen: true });
// Set timeout to close portal
setTimeout(() => {
portalOpen = false;
portalTimer = PORTAL_COUNTDOWN;
io.emit('portalTimerSync', { countdown: portalTimer, isOpen: false });
}, PORTAL_DURATION * 1000);
} else {
// Regular countdown update
io.emit('portalTimerSync', { countdown: portalTimer, isOpen: false });
}
}
}, 1000);
}
// Start the timer when server starts
startPortalTimer();
io.on('connection', (socket) => {
console.log('New connection established');
let playerId = null;
// Send current portal state to new connections
socket.emit('portalTimerSync', { countdown: portalTimer, isOpen: portalOpen });
socket.on('join', (data) => {
playerId = data.id;
// Join a room with the player's ID
socket.join(playerId);
console.log(`Player ${playerId} joined room ${playerId}`);
// Update or create player
players[data.id] = {
id: data.id,
color: data.color,
position: { x: 0, y: 2, z: 0 },
rotation: { x: 0, y: 0, z: 0 },
health: data.health || 100,
sats: INITIAL_SATS, // Always start with 1000 sats
name: data.name // Store the player's name
};
// Send current players to new player
socket.emit("players", { players });
// Notify other players
socket.broadcast.emit("playerJoined", {
player: players[data.id],
});
});
socket.on('playerEscaped', (data) => {
const player = players[data.id];
if (player && portalOpen) {
// Handle player escape - they keep their sats
io.to(data.id).emit('escaped', { sats: player.sats });
delete players[data.id];
socket.broadcast.emit('playerLeft', { id: data.id });
}
});
socket.on('update', (data) => {
// Update player position and rotation
if (players[data.id]) {
players[data.id].position = data.position;
players[data.id].rotation = data.rotation;
// Broadcast update to all other players
socket.broadcast.emit("players", { players });
}
});
socket.on("hit", (data) => {
// Handle player being hit
const targetPlayer = players[data.targetId];
const attackingPlayer = players[data.id];
if (targetPlayer && targetPlayer.health > 0 && attackingPlayer) {
// Apply damage
targetPlayer.health = Math.max(0, targetPlayer.health - DAMAGE);
// If player dies, spawn coins instead of direct transfer
if (targetPlayer.health <= 0) {
const coinsToSpawn = Math.floor(targetPlayer.sats / 100);
targetPlayer.sats = 0;
console.log(
`Player ${data.id} killed Player ${data.targetId} and spawned ${coinsToSpawn} coins`
);
// Notify all players about the coins spawn
io.emit("coinsSpawned", {
targetId: data.targetId,
position: targetPlayer.position,
coins: coinsToSpawn,
totalSats: coinsToSpawn * 100
});
// Notify the killer about the kill
io.to(data.id).emit("playerKilled", {
targetId: data.targetId,
newHealth: attackingPlayer.health,
newSats: attackingPlayer.sats
});
}
console.log(
`Player ${data.targetId} hit, new health: ${targetPlayer.health}`
);
// Notify the hit player through their room
io.to(data.targetId).emit("playerHit", {
targetId: data.targetId,
newHealth: targetPlayer.health,
newSats: targetPlayer.sats,
});
// Update all players about the new state
io.emit("players", { players });
}
});
// Handle coin collection
socket.on("collectCoin", (data) => {
const player = players[data.id];
if (player) {
player.sats += 100; // Each coin is worth 100 sats
// Broadcast coin collection to all players
io.emit("coinCollected", {
playerId: data.id,
newSats: player.sats,
coinId: data.coinId // Make sure we pass the coin ID
});
}
});
socket.on("respawn", (data) => {
if (players[data.id]) {
players[data.id].health = 100;
players[data.id].position = { x: 0, y: 2, z: 0 };
// Notify all players
io.emit("players", { players });
}
});
socket.on("disconnect", () => {
if (playerId) {
console.log(`Player ${playerId} disconnected`);
delete players[playerId];
// Leave the room
socket.leave(playerId);
// Notify other players
socket.broadcast.emit("playerLeft", {
id: playerId,
});
}
});
});
httpServer.listen(8080, () => {
console.log("Server running on port 8080");
});