From 7657eea30bfd5474815cc1d1905ed180b932ea6d Mon Sep 17 00:00:00 2001 From: Cosmin-Mare Date: Fri, 4 Oct 2024 11:49:00 +0300 Subject: [PATCH 1/2] Make mechanism of moving users to the beta feature, but it's not active yet, except for development and cosmin --- .../big-interactive-pages/editor.tsx | 12 +- src/components/codemirror.tsx | 10 +- src/components/navbar-editor.tsx | 1 + src/pages/~/[id].astro | 190 +++++++++++++----- src/pages/~/beta/[id].astro | 20 +- 5 files changed, 165 insertions(+), 68 deletions(-) diff --git a/src/components/big-interactive-pages/editor.tsx b/src/components/big-interactive-pages/editor.tsx index 34c7773aea..3c6348625b 100644 --- a/src/components/big-interactive-pages/editor.tsx +++ b/src/components/big-interactive-pages/editor.tsx @@ -66,7 +66,7 @@ export const onRun = async () => { interface EditorProps { persistenceState: Signal; - roomState?: Signal; + roomState?: Signal | undefined; cookies: { outputAreaSize: number | null; helpAreaSize: number | null; @@ -254,16 +254,6 @@ export default function Editor({ persistenceState, cookies, roomState }: EditorP const [sessionId] = useState(nanoid()); - - useEffect(() => { - if(roomState){ - isNewSaveStrat.value = true; - } else { - isNewSaveStrat.value = false; - } - }, []) - - useEffect(() => { const channel = new BroadcastChannel('session_channel'); channel.onmessage = (event) => { diff --git a/src/components/codemirror.tsx b/src/components/codemirror.tsx index 3e5da8b40d..b96a44478e 100644 --- a/src/components/codemirror.tsx +++ b/src/components/codemirror.tsx @@ -52,6 +52,14 @@ export default function CodeMirror(props: CodeMirrorProps) { }); }; + useEffect(() => { + if(props.roomState){ + isNewSaveStrat.value = true; + } else { + isNewSaveStrat.value = false; + } + }, []) + // Alert the parent to code changes (not reactive) const onCodeChangeRef = useRef(props.onCodeChange) useEffect(() => { onCodeChangeRef.current = props.onCodeChange }, [props.onCodeChange]) @@ -104,7 +112,6 @@ export default function CodeMirror(props: CodeMirrorProps) { }); useEffect(() => { if (!parent.current) throw new Error('Oh golly! The editor parent ref is null') - if(!isNewSaveStrat.value){ const editor = new EditorView({ state: createEditorState(props.initialCode ? props.initialCode : '', () => { @@ -118,7 +125,6 @@ export default function CodeMirror(props: CodeMirrorProps) { props.onEditorView?.(editor) return } - if(!props.roomState) return if(!props.persistenceState) return try{ diff --git a/src/components/navbar-editor.tsx b/src/components/navbar-editor.tsx index 6d0e11dba2..a7169a1010 100644 --- a/src/components/navbar-editor.tsx +++ b/src/components/navbar-editor.tsx @@ -565,6 +565,7 @@ export default function EditorNavbar(props: EditorNavbarProps) { userEmail ?? "???" : props.roomState?.value.participants.filter((participant) => { + console.log(participant.userEmail) if(participant.isHost) return true return false })[0]?.userEmail === userEmail ? userEmail : "the host" diff --git a/src/pages/~/[id].astro b/src/pages/~/[id].astro index b9899cd63a..cef0b9d618 100644 --- a/src/pages/~/[id].astro +++ b/src/pages/~/[id].astro @@ -4,72 +4,160 @@ import { firestore, getGame, getSession } from '../../lib/game-saving/account' import Editor from '../../components/big-interactive-pages/editor' import StandardHead from '../../components/standard-head.astro' import { signal } from '@preact/signals' -import type { PersistenceState } from '../../lib/state' +import { ConnectionStatus, PersistenceStateKind, RoomState, type PersistenceState } from '../../lib/state' import MobileUnsupported from '../../components/popups-etc/mobile-unsupported' import { mobileUserAgent } from '../../lib/utils/mobile' const session = await getSession(Astro.cookies) if (!session) return Astro.redirect(`/login?to=${Astro.request.url}`, 302) -const game = await getGame(Astro.params.id!) -if (!game || game.ownerId !== session.user.id) return Astro.redirect('/404', 302) - -const fileRegexp = /^.*\/(.+)-(\d+)\.md$/ - -let tutorial: string[] | undefined; -let tutorialIndex: number | undefined; -if (game.tutorialName) { - const files = await Astro.glob('/games/*.md') - - tutorial = files.filter(file => { - const regexedFile = file.file.match(fileRegexp) - return regexedFile && regexedFile[1] === game.tutorialName - }) - ?.map(md => md.compiledContent()) - tutorialIndex = game.tutorialIndex +function convertStringToNumber(id:string){ + let number = ""; + for(let i = 0; i < id.length; i++){ + number+=id.charCodeAt(i) + } + return Number(number) } +let persistenceState; +let isMobile; +let roomState; +if(convertStringToNumber(session.user.id) % 10 == -1 || session.user.email == "development@hackclub.com" || session.user.email == "cosmin@hackclub.com"){ + let checkRoom = false; + const game = await getGame(Astro.params.id!) + if (!game) return Astro.redirect('/404', 302) + if(game.ownerId !== session.user.id) checkRoom = true; + + const fileRegexp = /^.*\/(.+)-(\d+)\.md$/ + + let tutorial: string[] | undefined; + let tutorialIndex: number | undefined; + if (game.tutorialName) { + const files = await Astro.glob('/games/*.md') + + tutorial = files.filter(file => { + const regexedFile = file.file.match(fileRegexp) + return regexedFile && regexedFile[1] === game.tutorialName + }) + ?.map(md => md.compiledContent()) + tutorialIndex = game.tutorialIndex + } -let _persistenceState: PersistenceState -if (session.session.full) { - if (game.unprotected) { - await firestore.collection('games').doc(game.id).update({ unprotected: false }) - game.unprotected = false + let _persistenceState: PersistenceState + if(checkRoom){ + _persistenceState = { + kind: PersistenceStateKind.COLLAB, + game: game.id, + password: undefined, + cloudSaveState: "SAVED", + session: session, + stale: false + } + } else { + if (session.session.full) { + if (game.unprotected) { + await firestore.collection('games').doc(game.id).update({ unprotected: false }) + game.unprotected = false + } + if (Astro.cookies.get('sprigTempGame').value === game.id) + Astro.cookies.delete('sprigTempGame', { path: '/' }) + + _persistenceState = { + kind: PersistenceStateKind.PERSISTED, + session, + cloudSaveState: 'SAVED', + game, + tutorial, + tutorialIndex, + stale: false + } + + } else { + if (!game.unprotected) return Astro.redirect(`/login?to=${Astro.request.url}`, 302) + if (game.ownerId !== session.user.id) checkRoom = true + + _persistenceState = { + kind: PersistenceStateKind.PERSISTED, + session, + cloudSaveState: 'SAVED', + game, + tutorial, + tutorialIndex, + stale: false + } + } } - if (Astro.cookies.get('sprigTempGame').value === game.id) - Astro.cookies.delete('sprigTempGame', { path: '/' }) - - _persistenceState = { - kind: 'PERSISTED', - session, - cloudSaveState: 'SAVED', - game, - tutorial, - tutorialIndex, - stale: false + if(checkRoom){ + if(game.roomParticipants?.filter(participant => participant.userEmail === session.user.email)[0]?.isBanned) return Astro.redirect('/404', 302) + if(game.password == undefined) return Astro.redirect('/404', 302) } + persistenceState = signal(_persistenceState) + let _roomState = { + connectionStatus: ConnectionStatus.DISCONNECTED, + roomId: game.id, + password: "", + participants: [], + } + roomState = signal(_roomState) + isMobile = mobileUserAgent(Astro.request.headers.get('user-agent') ?? '') } else { - if (!game.unprotected) return Astro.redirect(`/login?to=${Astro.request.url}`, 302) - if (game.ownerId !== session.user.id) return Astro.redirect('/404', 302) - - _persistenceState = { - kind: 'PERSISTED', - session, - cloudSaveState: 'SAVED', - game, - tutorial, - tutorialIndex, - stale: false + const game = await getGame(Astro.params.id!) + if (!game || game.ownerId !== session.user.id) return Astro.redirect('/404', 302) + + const fileRegexp = /^.*\/(.+)-(\d+)\.md$/ + + let tutorial: string[] | undefined; + let tutorialIndex: number | undefined; + if (game.tutorialName) { + const files = await Astro.glob('/games/*.md') + + tutorial = files.filter(file => { + const regexedFile = file.file.match(fileRegexp) + return regexedFile && regexedFile[1] === game.tutorialName + }) + ?.map(md => md.compiledContent()) + tutorialIndex = game.tutorialIndex + } + + let _persistenceState: PersistenceState + if (session.session.full) { + if (game.unprotected) { + await firestore.collection('games').doc(game.id).update({ unprotected: false }) + game.unprotected = false + } + if (Astro.cookies.get('sprigTempGame').value === game.id) + Astro.cookies.delete('sprigTempGame', { path: '/' }) + + _persistenceState = { + kind: PersistenceStateKind.PERSISTED, + session, + cloudSaveState: 'SAVED', + game, + tutorial, + tutorialIndex, + stale: false + } + + } else { + if (!game.unprotected) return Astro.redirect(`/login?to=${Astro.request.url}`, 302) + if (game.ownerId !== session.user.id) return Astro.redirect('/404', 302) + + _persistenceState = { + kind: PersistenceStateKind.PERSISTED, + session, + cloudSaveState: 'SAVED', + game, + tutorial, + tutorialIndex, + stale: false + } } + + persistenceState = signal(_persistenceState) + isMobile = mobileUserAgent(Astro.request.headers.get('user-agent') ?? '') } -const persistenceState = signal(_persistenceState) -const isMobile = mobileUserAgent(Astro.request.headers.get('user-agent') ?? '') --- - @@ -78,8 +166,10 @@ const isMobile = mobileUserAgent(Astro.request.headers.get('user-agent') ?? '') diff --git a/src/pages/~/beta/[id].astro b/src/pages/~/beta/[id].astro index 11ff9b6666..13aacbb3a2 100644 --- a/src/pages/~/beta/[id].astro +++ b/src/pages/~/beta/[id].astro @@ -4,16 +4,25 @@ import { firestore, getGame, getSession } from '../../../lib/game-saving/account import Editor from '../../../components/big-interactive-pages/editor' import StandardHead from '../../../components/standard-head.astro' import { signal } from '@preact/signals' -import { RoomState, ConnectionStatus, type PersistenceState } from '../../../lib/state' +import { RoomState, ConnectionStatus, type PersistenceState, PersistenceStateKind } from '../../../lib/state' import MobileUnsupported from '../../../components/popups-etc/mobile-unsupported' import { mobileUserAgent } from '../../../lib/utils/mobile' +function convertStringToNumber(id:string){ + let number = ""; + for(let i = 0; i < id.length; i++){ + number+=id.charCodeAt(i) + } + return Number(number) +} + const session = await getSession(Astro.cookies) if (!session) return Astro.redirect(`/login?to=${Astro.request.url}`, 302) let checkRoom = false; const game = await getGame(Astro.params.id!) if (!game) return Astro.redirect('/404', 302) +if(convertStringToNumber(session.user.id) % 10 == 0 || session.user.email == "development@hackclub.com") Astro.redirect(`/~/${game.id}`) if(game.ownerId !== session.user.id) checkRoom = true; const fileRegexp = /^.*\/(.+)-(\d+)\.md$/ @@ -34,11 +43,12 @@ if (game.tutorialName) { let _persistenceState: PersistenceState if(checkRoom){ _persistenceState = { - kind: 'COLLAB', + kind: PersistenceStateKind.COLLAB, game: game.id, password: undefined, session: session, - stale: false + stale: false, + cloudSaveState: "SAVED" } } else { if (session.session.full) { @@ -50,7 +60,7 @@ if(checkRoom){ Astro.cookies.delete('sprigTempGame', { path: '/' }) _persistenceState = { - kind: 'PERSISTED', + kind: PersistenceStateKind.PERSISTED, session, cloudSaveState: 'SAVED', game, @@ -64,7 +74,7 @@ if(checkRoom){ if (game.ownerId !== session.user.id) checkRoom = true _persistenceState = { - kind: 'PERSISTED', + kind: PersistenceStateKind.PERSISTED, session, cloudSaveState: 'SAVED', game, From 26618cc3d3be5acb18d84a08248cf538958d0a96 Mon Sep 17 00:00:00 2001 From: Cosmin-Mare Date: Fri, 4 Oct 2024 13:44:57 +0300 Subject: [PATCH 2/2] remove console log --- src/components/navbar-editor.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/navbar-editor.tsx b/src/components/navbar-editor.tsx index a7169a1010..6d0e11dba2 100644 --- a/src/components/navbar-editor.tsx +++ b/src/components/navbar-editor.tsx @@ -565,7 +565,6 @@ export default function EditorNavbar(props: EditorNavbarProps) { userEmail ?? "???" : props.roomState?.value.participants.filter((participant) => { - console.log(participant.userEmail) if(participant.isHost) return true return false })[0]?.userEmail === userEmail ? userEmail : "the host"