File size: 20,016 Bytes
0bc0d81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LittleBunnyPaws' Magic Co-Pilot</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&family=Mochiy+Pop+One&display=swap" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.7.77/Tone.js"></script>
    <style>
        body { font-family: 'Inter', sans-serif; background-color: #fdf2f8; } /* pink-50 */
        .font-brand { font-family: 'Mochiy Pop One', sans-serif; }
        .hidden { display: none; }
        .tip-bar-progress { transition: width 0.5s ease-in-out; }
        .input-glow { transition: box-shadow 0.3s ease; }
        .input-glow:focus { box-shadow: 0 0 0 3px rgba(236, 72, 153, 0.4); } /* pink-500 with opacity */
    </style>
</head>
<body>

    <!-- =================================================================== -->
    <!--  SECTION 1: INITIAL SETUP (Ask for username)                      -->
    <!-- =================================================================== -->
    <div id="setup-view" class="flex items-center justify-center min-h-screen">
        <div class="w-full max-w-md mx-auto bg-white rounded-2xl shadow-xl p-8 border-4 border-pink-200 text-center">
            <h1 class="font-brand text-4xl text-pink-500 mb-4">🐰 Welcome! 🐰</h1>
            <p class="text-gray-600 mb-6">Let's get your stream co-pilot ready. Please enter your Chaturbate username below.</p>
            <input type="text" id="username-input" placeholder="e.g., littlebunnypaws" class="w-full p-3 text-center border-2 border-gray-300 rounded-lg mb-4 text-lg input-glow focus:outline-none focus:border-pink-400">
            <button id="start-button" class="w-full bg-pink-500 hover:bg-pink-600 text-white font-bold py-3 px-6 rounded-lg shadow-md text-xl">Start Co-Pilot</button>
        </div>
    </div>

    <!-- =================================================================== -->
    <!--  SECTION 2: CONTROL PANEL VIEW (Her private controls)             -->
    <!-- =================================================================== -->
    <div id="control-panel-view" class="hidden p-4">
        <div class="w-full max-w-4xl mx-auto bg-white rounded-2xl shadow-xl p-6 border-4 border-pink-200">
             <header class="text-center mb-4">
                <h1 class="font-brand text-3xl md:text-4xl text-pink-500">🐰 Stream Co-Pilot 🐰</h1>
                <p class="text-gray-600">You are connected to <strong id="display-username" class="text-pink-600"></strong>'s chat.</p>
            </header>
            
            <div class="bg-blue-100 border-l-4 border-blue-500 text-blue-700 p-4 rounded-lg mb-6">
                <h3 class="font-bold">Your OBS Overlay Link</h3>
                <p class="text-sm">Copy this link and paste it into a new Browser Source in OBS. This is what your viewers will see.</p>
                <input type="text" id="obs-link-display" readonly class="w-full bg-blue-50 p-2 mt-2 rounded border border-blue-300 select-all">
            </div>

            <div class="bg-pink-100 rounded-xl p-4 my-6 text-center">
                <div id="group-timer-display" class="font-brand text-7xl md:text-8xl text-pink-600 tracking-wider">00:00</div>
            </div>
            <div id="group-status-message" class="text-center text-lg font-semibold text-gray-600 h-8 mb-4">Set up your stream below!</div>
            
             <div class="grid md:grid-cols-2 gap-6">
                <!-- Live Controls -->
                <div class="bg-gray-50 rounded-xl p-4">
                     <h3 class="font-brand text-xl text-center text-gray-700 mb-4">Live Controls</h3>
                     <div class="flex items-center justify-center gap-4 mb-4">
                        <input type="number" id="group-minutes" value="5" min="1" class="w-24 p-2 text-center border-2 border-gray-300 rounded-lg" placeholder="Mins">
                        <button id="group-start-btn" class="w-full bg-green-500 hover:bg-green-600 text-white font-bold py-2 px-6 rounded-lg shadow-md">Start</button>
                        <button id="group-stop-btn" class="w-full bg-red-500 hover:bg-red-600 text-white font-bold py-2 px-6 rounded-lg shadow-md hidden">Reset</button>
                    </div>
                    <div class="text-center border-t-2 border-gray-200 pt-4 mt-4">
                         <p class="font-semibold text-gray-700 mb-3">🛠️ Manual Tip Override 🛠️</p>
                         <div id="dynamic-tip-buttons-container" class="flex flex-wrap justify-center gap-3"><p class="text-gray-500 text-sm">Create tip rules to add manual buttons.</p></div>
                    </div>
                </div>

                <!-- Setup -->
                <div class="bg-gray-100 rounded-xl p-4">
                     <h3 class="font-brand text-xl text-center text-gray-700 mb-4">Stream Setup</h3>
                     <div class="space-y-4">
                        <div>
                            <label for="tip-goal-input" class="block font-semibold text-gray-700 mb-1">Community Tip Goal</label>
                            <div class="flex items-center gap-2">
                                <input type="number" id="tip-goal-input" value="1000" class="w-full p-2 border-gray-300 rounded-lg">
                                <button id="set-goal-btn" class="bg-pink-500 text-white font-bold py-2 px-4 rounded-lg">Set</button>
                            </div>
                        </div>
                        <div>
                            <label for="sound-select" class="block font-semibold text-gray-700 mb-1">Tip Sound Alert</label>
                            <select id="sound-select" class="w-full p-2 border-gray-300 rounded-lg">
                                <option value="chime">Chime</option><option value="coin">Coin</option><option value="powerup">Power Up</option><option value="boop">Boop</option><option value="none">None</option>
                            </select>
                        </div>
                        <div class="border-t-2 pt-4">
                             <label class="block font-semibold text-gray-700 mb-2 text-center">Tip-to-Time Automation Rules</label>
                             <div class="flex items-center justify-center gap-2">
                                <input type="number" id="tip-token-input" placeholder="Tokens" class="w-24 p-2 text-center border-gray-300 rounded-lg">
                                <input type="number" id="tip-time-input" placeholder="Seconds" class="w-24 p-2 text-center border-gray-300 rounded-lg">
                                <button id="add-tip-button-btn" class="bg-blue-500 text-white font-bold py-2 px-4 rounded-lg">Add Rule</button>
                             </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- =================================================================== -->
    <!--  SECTION 3: ON-STREAM OVERLAY VIEW (What viewers see)             -->
    <!-- =================================================================== -->
    <div id="overlay-view" class="hidden p-4">
         <div class="w-full max-w-2xl mx-auto">
            <div class="bg-black bg-opacity-50 rounded-full p-1 border-2 border-pink-300 shadow-lg">
                <div class="relative w-full h-10 flex items-center justify-center">
                    <div id="tip-bar-progress" class="absolute top-0 left-0 h-full bg-gradient-to-r from-pink-400 to-purple-500 rounded-full tip-bar-progress" style="width: 0%;"></div>
                    <div class="relative z-10 font-brand text-white text-lg tracking-wider text-shadow">
                        <span class="font-bold">BUNNY BONUS:</span>
                        <span id="current-tips-display">0</span> / <span id="tip-goal-display">1000</span> TOKENS
                    </div>
                </div>
            </div>
        </div>
    </div>


    <script>
    // --- APP STATE & CONFIG ---
    const APP_STATE_KEY = 'bunnyCoPilotState';
    const TIP_PATTERN = /"m":"(\w+) has tipped (\d+) tokens!"/; // Updated for common bot format

    // --- DOM Elements ---
    const views = {
        setup: document.getElementById('setup-view'),
        controls: document.getElementById('control-panel-view'),
        overlay: document.getElementById('overlay-view'),
    };
    const elements = {
        usernameInput: document.getElementById('username-input'),
        startButton: document.getElementById('start-button'),
        displayUsername: document.getElementById('display-username'),
        obsLinkDisplay: document.getElementById('obs-link-display'),
        timer: document.getElementById('group-timer-display'),
        status: document.getElementById('group-status-message'),
        minutesInput: document.getElementById('group-minutes'),
        startBtn: document.getElementById('group-start-btn'),
        stopBtn: document.getElementById('group-stop-btn'),
        buttonsContainer: document.getElementById('dynamic-tip-buttons-container'),
        tipGoalInput: document.getElementById('tip-goal-input'),
        setGoalBtn: document.getElementById('set-goal-btn'),
        currentTipsDisplay: document.getElementById('current-tips-display'),
        tipGoalDisplay: document.getElementById('tip-goal-display'),
        tipBarProgress: document.getElementById('tip-bar-progress'),
        soundSelect: document.getElementById('sound-select'),
        addRuleBtn: document.getElementById('add-tip-button-btn'),
        tokenInput: document.getElementById('tip-token-input'),
        timeInput: document.getElementById('tip-time-input'),
    };

    // --- SOUNDS ---
    let sounds = {};
    const initSounds = () => {
        sounds = {
            chime: new Tone.Synth({ oscillator: { type: "sine" }, envelope: { attack: 0.005, decay: 0.1, sustain: 0.3, release: 1 } }).toDestination(),
            coin: new Tone.Synth({ oscillator: { type: "square" }, envelope: { attack: 0.001, decay: 0.1, sustain: 0, release: 0.1 } }).toDestination(),
            powerup: new Tone.Synth({ oscillator: { type: "triangle" }, envelope: { attack: 0.01, decay: 0.2, sustain: 0.1, release: 0.2 } }).toDestination(),
            boop: new Tone.MembraneSynth().toDestination()
        };
    };
    const playSound = (soundName) => {
        if (soundName === 'none' || !sounds[soundName]) return;
        Tone.start().then(() => {
            if (soundName === 'chime') sounds.chime.triggerAttackRelease("C5", "8n");
            if (soundName === 'coin') sounds.coin.triggerAttackRelease("E6", "16n");
            if (soundName === 'powerup') { const now = Tone.now(); sounds.powerup.triggerAttackRelease("C4", "16n", now); sounds.powerup.triggerAttackRelease("G4", "16n", now + 0.1); sounds.powerup.triggerAttackRelease("C5", "16n", now + 0.2); }
            if (soundName === 'boop') sounds.boop.triggerAttackRelease("C2", "8n");
        });
    };

    // --- STATE MANAGEMENT ---
    let state = {
        username: '',
        isTimerRunning: false,
        timeLeft: 0,
        tipGoal: 1000,
        currentTips: 0,
        automationRules: [],
        selectedSound: 'chime',
    };
    let timerInterval = null;

    const saveState = () => localStorage.setItem(APP_STATE_KEY, JSON.stringify(state));
    const loadState = () => Object.assign(state, JSON.parse(localStorage.getItem(APP_STATE_KEY)));
    
    // --- UI RENDERING ---
    const formatTime = (s) => `${Math.floor(s/60)}`.padStart(2,'0')+':'+`${s%60}`.padStart(2,'0');

    function render() {
        // Render for both controls and overlay
        elements.timer.textContent = formatTime(state.timeLeft);
        elements.currentTipsDisplay.textContent = state.currentTips;
        elements.tipGoalDisplay.textContent = state.tipGoal;
        const percentage = Math.min(100, (state.currentTips / state.tipGoal) * 100);
        elements.tipBarProgress.style.width = `${percentage}%`;

        // Render for controls only
        elements.tipGoalInput.value = state.tipGoal;
        elements.soundSelect.value = state.selectedSound;
        elements.startBtn.classList.toggle('hidden', state.isTimerRunning);
        elements.stopBtn.classList.toggle('hidden', !state.isTimerRunning);
        
        elements.buttonsContainer.innerHTML = '';
        if (state.automationRules.length === 0) {
            elements.buttonsContainer.innerHTML = '<p class="text-gray-500 text-sm">Create automation rules.</p>';
        } else {
            state.automationRules.forEach(rule => {
                const button = document.createElement('button');
                button.className = 'bg-gray-600 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded-lg shadow-md text-sm';
                button.textContent = `${rule.tokens}t (+${formatTime(rule.seconds)})`;
                button.onclick = () => processTip(rule.tokens, true); // true for manual override
                elements.buttonsContainer.appendChild(button);
            });
        }
    }

    // --- CORE LOGIC ---
    function processTip(tokens, isManual = false) {
        if (!isManual && !state.isTimerRunning) return; // Don't auto-add time if timer isn't running
        
        const rule = state.automationRules.find(r => r.tokens === tokens);
        if (!rule) return;

        state.timeLeft += rule.seconds;
        state.currentTips += tokens;
        playSound(state.selectedSound);
        saveState();
        render(); // Immediately render for controls, event listener handles overlay
    }

    function startTimer() {
        if (state.isTimerRunning) return;
        const minutes = parseInt(elements.minutesInput.value, 10);
        if (isNaN(minutes) || minutes <= 0) return;

        state.isTimerRunning = true;
        state.timeLeft = minutes * 60;
        elements.status.textContent = "Timer is LIVE!";
        
        clearInterval(timerInterval);
        timerInterval = setInterval(() => {
            state.timeLeft--;
            if (state.timeLeft <= 0) {
                state.timeLeft = 0;
                state.isTimerRunning = false;
                clearInterval(timerInterval);
                elements.status.textContent = "Time's up! Tip to add more!";
            }
            saveState();
            render();
        }, 1000);
        
        saveState();
        render();
    }
    
    function stopTimer() {
        state.isTimerRunning = false;
        state.timeLeft = 0;
        state.currentTips = 0; // Reset progress
        clearInterval(timerInterval);
        elements.status.textContent = "Timer Reset. Ready to go!";
        saveState();
        render();
    }
    
    // --- CHATURBATE CONNECTION ---
    async function watchChat() {
        console.log(`Connecting to ${state.username}'s chat...`);
        try {
            const api_url = `https://chaturbate.com/get_edge_host/?room=${state.username}&type=chat`;
            const response = await fetch(api_url).then(res => res.json());
            if (!response.host) throw new Error("Could not find chat server.");

            const ws_url = `wss://${response.host}/`;
            const socket = new WebSocket(ws_url);
            
            socket.onopen = () => {
                 console.log("Chat connection established.");
                 socket.send(`{"method":"connect","data":{"user":"guest_obs-${Date.now()}","room":"${state.username}","password":""}}`);
                 socket.send(`{"method":"joinroom","data":{"room":"${state.username}"}}`);
                 elements.status.textContent = "Automation connected! Ready to go live.";
            };

            socket.onmessage = (event) => {
                const msg = event.data;
                const match = TIP_PATTERN.exec(msg);
                if (match) {
                    const tokens = parseInt(match[2], 10);
                    console.log(`Tip detected: ${tokens} tokens`);
                    processTip(tokens);
                }
            };

            socket.onclose = () => {
                console.warn("Chat connection closed. Reconnecting in 5 seconds...");
                elements.status.textContent = "Automation lost! Reconnecting...";
                setTimeout(watchChat, 5000);
            };
            socket.onerror = (err) => {
                console.error("Chat socket error:", err);
                socket.close();
            };

        } catch (error) {
            console.error("Failed to connect to chat:", error);
            elements.status.textContent = "Connection failed! Retrying...";
            setTimeout(watchChat, 10000);
        }
    }
    
    // --- INITIALIZATION & ROUTING ---
    function main() {
        const urlParams = new URLSearchParams(window.location.search);
        const view = urlParams.get('view');
        const username = urlParams.get('room');
        
        if (view === 'overlay' && username) {
            // --- OVERLAY VIEW ---
            document.body.style.backgroundColor = 'transparent';
            views.setup.classList.add('hidden');
            views.controls.classList.add('hidden');
            views.overlay.classList.remove('hidden');
            
            if(localStorage.getItem(APP_STATE_KEY)) loadState();
            render();

            window.addEventListener('storage', (event) => {
                if (event.key === APP_STATE_KEY) {
                    loadState();
                    render();
                }
            });

        } else if (username) {
            // --- CONTROL PANEL VIEW ---
            state.username = username;
            views.setup.classList.add('hidden');
            views.overlay.classList.add('hidden');
            views.controls.classList.remove('hidden');

            elements.displayUsername.textContent = state.username;
            const overlayUrl = `${window.location.origin}${window.location.pathname}?view=overlay&room=${state.username}`;
            elements.obsLinkDisplay.value = overlayUrl;
            
            initSounds(); // Initialize audio context on user interaction
            if(localStorage.getItem(APP_STATE_KEY)) loadState(); // Load previous session settings
            render();
            watchChat();

            // Setup event listeners for controls
            elements.startButton.onclick = () => initSounds();
            elements.startBtn.onclick = startTimer;
            elements.stopBtn.onclick = stopTimer;
            elements.setGoalBtn.onclick = () => { state.tipGoal = parseInt(elements.tipGoalInput.value, 10); state.currentTips = 0; saveState(); render(); };
            elements.soundSelect.onchange = (e) => { state.selectedSound = e.target.value; saveState(); };
            elements.addRuleBtn.onclick = () => {
                 const tokens = parseInt(elements.tokenInput.value, 10);
                 const seconds = parseInt(elements.timeInput.value, 10);
                 if (!isNaN(tokens) && !isNaN(seconds) && tokens > 0 && seconds > 0) {
                     state.automationRules.push({ tokens, seconds });
                     state.automationRules.sort((a,b) => a.tokens - b.tokens);
                     elements.tokenInput.value = ''; elements.timeInput.value = '';
                     saveState(); render();
                 }
            };

        } else {
            // --- SETUP VIEW ---
            views.controls.classList.add('hidden');
            views.overlay.classList.add('hidden');
            views.setup.classList.remove('hidden');

            elements.startButton.onclick = () => {
                const enteredUsername = elements.usernameInput.value.trim();
                if (enteredUsername) {
                    window.location.search = `?room=${enteredUsername}`;
                }
            };
        }
    }