Repository navigation
Expand file tree
/
Copy pathcloud_functions_missing.js
More file actions
101 lines (79 loc) · 3.61 KB
/
Copy pathcloud_functions_missing.js
File metadata and controls
101 lines (79 loc) · 3.61 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
// ===============================
// 📡 broadcastPKPoints.js (JOYTOX - Real-time PK Points Broadcast)
// ===============================
Parse.Cloud.define("broadcastPKPoints", async (request) => {
try {
const { currentRoomID, opponentRoomID, my_points, his_points, battlePoints } = request.params;
console.log(`📡 broadcastPKPoints | Current: ${currentRoomID} | Opponent: ${opponentRoomID}`);
if (!currentRoomID) {
throw new Error("❌ currentRoomID is required");
}
// If called from send_gift with battlePoints, we need to update database first
if (battlePoints && battlePoints > 0) {
console.log(`🎯 Updating PK points in database: +${battlePoints}`);
const Streaming = Parse.Object.extend("Streaming");
// Find current stream
const currentQuery = new Parse.Query(Streaming);
currentQuery.equalTo("streaming_channel", currentRoomID);
const currentStream = await currentQuery.first({ useMasterKey: true });
if (!currentStream) {
throw new Error(`❌ Stream not found for channel: ${currentRoomID}`);
}
// Update my_points in current stream
const currentMyPoints = currentStream.get("my_points") || 0;
const newMyPoints = currentMyPoints + battlePoints;
currentStream.set("my_points", newMyPoints);
// If opponent exists, sync his_points
if (opponentRoomID && opponentRoomID !== currentRoomID) {
const opponentQuery = new Parse.Query(Streaming);
opponentQuery.equalTo("streaming_channel", opponentRoomID);
const opponentStream = await opponentQuery.first({ useMasterKey: true });
if (opponentStream) {
// Update opponent's his_points to match my new points
opponentStream.set("his_points", newMyPoints);
// Also get opponent's my_points to sync back
const opponentMyPoints = opponentStream.get("my_points") || 0;
currentStream.set("his_points", opponentMyPoints);
// Save both atomically
await Parse.Object.saveAll([currentStream, opponentStream], { useMasterKey: true });
console.log(`✅ PK Points updated | Current: ${newMyPoints}, Opponent: ${opponentMyPoints}`);
return {
success: true,
my_points: newMyPoints,
his_points: opponentMyPoints,
message: "PK points broadcasted successfully"
};
}
}
// Save just current stream if no opponent
await currentStream.save(null, { useMasterKey: true });
return {
success: true,
my_points: newMyPoints,
his_points: currentStream.get("his_points") || 0,
message: "PK points updated (no opponent)"
};
}
// If called with explicit points values (from save_hisBattle_points)
if (my_points !== undefined && his_points !== undefined) {
console.log(`✅ Broadcasting explicit points | My: ${my_points}, His: ${his_points}`);
return {
success: true,
my_points: my_points,
his_points: his_points,
message: "Points synced from save_hisBattle_points"
};
}
throw new Error("❌ Either battlePoints or (my_points + his_points) must be provided");
} catch (error) {
console.error("❌ broadcastPKPoints error:", error);
throw new Parse.Error(
Parse.Error.INTERNAL_SERVER_ERROR,
error.message || "Failed to broadcast PK points"
);
}
});
console.log("✅ broadcastPKPoints.js loaded successfully (JOYTOX)");
// ===============================
// End of broadcastPKPoints.js
// ===============================