Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mechanism of moving users to the beta feature, but it's not active yet, except for development and cosmin #2414

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions src/components/big-interactive-pages/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const onRun = async () => {

interface EditorProps {
persistenceState: Signal<PersistenceState>;
roomState?: Signal<RoomState>;
roomState?: Signal<RoomState> | undefined;
cookies: {
outputAreaSize: number | null;
helpAreaSize: number | null;
Expand Down Expand Up @@ -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) => {
Expand Down
10 changes: 8 additions & 2 deletions src/components/codemirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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 : '', () => {
Expand All @@ -118,7 +125,6 @@ export default function CodeMirror(props: CodeMirrorProps) {
props.onEditorView?.(editor)
return
}

if(!props.roomState) return
if(!props.persistenceState) return
try{
Expand Down
190 changes: 140 additions & 50 deletions src/pages/~/[id].astro
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "[email protected]" || session.user.email == "[email protected]"){
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>(_persistenceState)
let _roomState = {
connectionStatus: ConnectionStatus.DISCONNECTED,
roomId: game.id,
password: "",
participants: [],
}
roomState = signal<RoomState>(_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>(_persistenceState)
isMobile = mobileUserAgent(Astro.request.headers.get('user-agent') ?? '')
}

const persistenceState = signal<PersistenceState>(_persistenceState)
const isMobile = mobileUserAgent(Astro.request.headers.get('user-agent') ?? '')
---
<script>
import { isNewSaveStrat } from "../../lib/state";
isNewSaveStrat.value = false; // This code is executed on the client side and it disables the new features implemented (save strat and collab)
</script>
<html lang='en'>
<head>
<StandardHead title='Editor' />
Expand All @@ -78,8 +166,10 @@ const isMobile = mobileUserAgent(Astro.request.headers.get('user-agent') ?? '')
<Editor
client:load
persistenceState={persistenceState}
roomState={roomState}
cookies={{
outputAreaSize: Astro.cookies.get('outputAreaSize').number(),
helpAreaSize: Astro.cookies.get('helpAreaSize').number(),
hideHelp: Astro.cookies.get('hideHelp').boolean()
}}
/>
Expand Down
20 changes: 15 additions & 5 deletions src/pages/~/beta/[id].astro
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "[email protected]") Astro.redirect(`/~/${game.id}`)
if(game.ownerId !== session.user.id) checkRoom = true;

const fileRegexp = /^.*\/(.+)-(\d+)\.md$/
Expand All @@ -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) {
Expand All @@ -50,7 +60,7 @@ if(checkRoom){
Astro.cookies.delete('sprigTempGame', { path: '/' })

_persistenceState = {
kind: 'PERSISTED',
kind: PersistenceStateKind.PERSISTED,
session,
cloudSaveState: 'SAVED',
game,
Expand All @@ -64,7 +74,7 @@ if(checkRoom){
if (game.ownerId !== session.user.id) checkRoom = true

_persistenceState = {
kind: 'PERSISTED',
kind: PersistenceStateKind.PERSISTED,
session,
cloudSaveState: 'SAVED',
game,
Expand Down
Loading