···22<script lang="ts">
33 import { onMount } from 'svelte';
44 import { initializeApp } from 'firebase/app';
55-65 import { getDatabase, ref, update, get, onValue } from 'firebase/database';
7688- // See: https://firebase.google.com/docs/web/learn-more#config-object
99- const firebaseConfig = {
1010- databaseURL: process.env.FIREBASE_DATABASE_URL!,
1111- type: 'service_account',
1212- project_id: process.env.FIREBASE_PROJECT_ID!,
1313- private_key_id: process.env.FIREBASE_PRIVATE_KEY_ID!,
1414- private_key: process.env.FIREBASE_PRIVATE_KEY!.replace(/\\n/g, '\n'),
1515- client_email: process.env.FIREBASE_CLIENT_EMAIL!,
1616- client_id: process.env.FIREBASE_CLIENT_ID!,
1717- auth_uri: 'https://accounts.google.com/o/oauth2/auth',
1818- token_uri: 'https://oauth2.googleapis.com/token',
1919- auth_provider_x509_cert_url: 'https://www.googleapis.com/oauth2/v1/certs',
2020- client_x509_cert_url: process.env.FIREBASE_CLIENT_CERT_URL!,
2121- universe_domain: 'googleapis.com'
2222- };
77+ import type { PageData } from './$types';
88+99+ import { page } from '$app/stores';
23102411 // Initialize Firebase
2525- const app = initializeApp(firebaseConfig);
2626-1212+ const app = initializeApp($page.data.firebaseConfig);
2713 // Initialize Realtime Database and get a reference to the service
2814 const db = getDatabase(app);
29151616+ // Set up important game variables.
3017 let currentHand = '';
3118 let otherHand = '';
3232-3319 let gameTimer = 0;
34203521 onMount(async () => {
2222+ // Set the game state to the current state in the database.
3623 let gameState = (await get(ref(db, '/'))).val();
37243838- console.log('lastactive', gameState.leftHand.sessionLastActive);
3939- console.log(new Date(Date.now() - 5000));
2525+ // Set the current hand to the one that was not last active, and the other hand to the one that was last active.
4026 currentHand =
4127 new Date(gameState.leftHand.sessionLastActive) > new Date(Date.now() - 5000)
4228 ? 'rightHand'
4329 : 'leftHand';
4430 otherHand = currentHand == 'leftHand' ? 'rightHand' : 'leftHand';
45313232+ // Add event listeners to the current hand.
4633 window.addEventListener('mousemove', (e) => {
3434+ // Get the mouse position.
4735 const x = e.clientX;
4836 const y = e.clientY;
49373838+ // Update the database with the new mouse position.
5039 update(ref(db, currentHand), {
5140 x: x,
5241 y: y
5342 });
5443 });
55444545+ // Move the current hand to the position in the database.
5646 onValue(ref(db, currentHand), (snapshot) => {
5747 const data = snapshot.val();
5848···6252 hand!.style.top = data.y - 50 + 'px';
6353 });
64545555+ // Move the other hand to the position in the database.
6556 onValue(ref(db, otherHand), (snapshot) => {
6657 const data = snapshot.val();
6758···7162 hand!.style.top = data.y - 50 + 'px';
7263 });
73646565+ // Update the current hand in the database with sessionLastActive every 500ms.
7466 setInterval(() => {
7567 update(ref(db, currentHand), {
7668 sessionLastActive: new Date()
7769 });
7870 }, 500);
79717272+ // Update the game timer in the database based on if the hands are touching (checks every 500ms).
8073 setInterval(() => {
8174 // check if hands are touching
8275 let leftHand = document.getElementById('leftHand');
···110103</script>
111104112105<div class="box">
113113- <h1>Hello world</h1>
106106+ <h1 class="title">Welcome to the Harvest Hackathon Game!</h1>
107107+ <p>
108108+ Link your hand with the hand of the other player, then dodge objects together for as long as
109109+ possible!
110110+ </p>
114111115112 <p>Timer {gameTimer}</p>
116113</div>
117117-118114<!-- LEFT -->
119115<div id="leftHand">
120116 <img src="/left_hand.png" alt="Logo" width="100" height="100" />