Skip to content

Commit

Permalink
started working on auth
Browse files Browse the repository at this point in the history
  • Loading branch information
k2d222 committed Oct 21, 2024
1 parent ca2c98b commit 5dce5ff
Show file tree
Hide file tree
Showing 17 changed files with 327 additions and 82 deletions.
14 changes: 11 additions & 3 deletions client/src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ type Base64 = string

export interface Config {
name: string
access: 'public' | 'unlisted'
public: boolean
password: string
version: 'ddnet06' | 'teeworlds07'
}

export interface MapDetail {
Expand Down Expand Up @@ -74,7 +76,8 @@ export interface AutomapperDiagnostic {

export type MapCreation = {
version: 'ddnet06' | 'teeworlds07'
access: 'public' | 'unlisted'
public: boolean
password: string
} & ({
clone: string
} | {
Expand All @@ -86,6 +89,11 @@ export type MapCreation = {
upload: Base64
})

export interface JoinReq {
name: string,
password: string,
}

export interface MapGetReq {
users: undefined
cursors: undefined
Expand Down Expand Up @@ -249,7 +257,7 @@ export interface Send {
"delete/automapper": MapDelReq['automapper']
"cursor": Cursor
"save": undefined
"join": string
"join": JoinReq
"leave": string
"create": EditReq['map']
"delete": DeleteReq['map']
Expand Down
1 change: 0 additions & 1 deletion client/src/twmap/datafile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export class DataFile {

// calculate checksum
this.crc = crc32(new Uint8Array(this.data))
console.info('map crc', this.crc.toString(16))

// we only support datafile version 4
if (this.version != 4) {
Expand Down
22 changes: 17 additions & 5 deletions client/src/ui/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,41 @@
import storage from '../storage'
import { WebSocketServer } from '../server/server'
import { server, serverCfg } from './global'
import { serverWsUrl } from '../server/util'
import { serverHttpUrl, serverWsUrl } from '../server/util'
import { queryConfig } from './lib/util'
export let url = ''
function joinServer() {
async function joinServer(params: { [name: string]: string }) {
const serverCfgs = storage.load('servers')
const serverId = storage.load('currentServer')
$serverCfg = serverCfgs[serverId]
console.log('joining server', $serverCfg)
const wsUrl = serverWsUrl($serverCfg)
$server = new WebSocketServer(wsUrl)
return new Promise((resolve, reject) => {
const connected = new Promise((resolve, reject) => {
$server.socket.addEventListener('open', resolve, { once: true })
$server.socket.addEventListener('error', () => reject("Failed to connect to the server"), { once: true })
})
await connected
let config = await queryConfig(serverHttpUrl($serverCfg), params.mapName)
if (config.password) {
return prompt("enter password")
} else {
return ''
}
}
</script>

<Router {url}>
<Route path="edit/*mapName" let:params>
<Fence fullscreen signal={joinServer()} loadText="Connecting to server…">
<Edit name={params.mapName} />
{@const signal = joinServer(params)}
<Fence fullscreen {signal} let:result={password} loadText="Connecting to server…">
<Edit name={params.mapName} {password} />
</Fence>
</Route>
<Route path="/">
Expand Down
10 changes: 10 additions & 0 deletions client/src/ui/lib/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ export function showDialog(
}, ...dialog.messages]
})

if (controls !== 'yesno') {
if (type === 'info') {
console.info(`[%c%s%c] ${message}`, 'color: green', 'INFO', 'color: unset')
} else if (type === 'warning') {
console.warn(`[%c%s%c] ${message}`, 'color: orange', 'WARN', 'color: unset')
} else if (type === 'error') {
console.error(`[%c%s%c] ${message}`, 'color: red', 'ERROR', 'color: unset')
}
}

if (controls === 'none')
return id
else
Expand Down
10 changes: 6 additions & 4 deletions client/src/ui/lib/fence.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
<script lang="ts" generics="T">
import { InlineNotification, Loading } from 'carbon-components-svelte'
import { showError } from './dialog'
export let signal: Promise<unknown>
export let signal: Promise<T>
export let loadText = 'Loading'
export let errorText = 'Error'
export let fullscreen = false
Expand All @@ -14,9 +15,10 @@
<div class="text">{loadText}</div>
</div>
</div>
{:then}
<slot />
{:then result}
<slot {result} />
{:catch error}
{showError(error) && ''}
<InlineNotification
kind="error"
hideCloseButton
Expand Down
10 changes: 8 additions & 2 deletions client/src/ui/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Image } from '../../twmap/image'
import { AnyTilesLayer, FrontLayer, GameLayer, SpeedupLayer, SwitchLayer, TeleLayer, TilesLayer, TuneLayer } from '../../twmap/tilesLayer'
import { TilesLayerFlags } from '../../twmap/types'
import type { WebSocketServer } from '../../server/server'
import type { MapCreation, MapDetail } from '../../server/protocol'
import type { Config, MapCreation, MapDetail } from '../../server/protocol'
import * as MapDir from '../../twmap/mapdir'
import { QuadsLayer } from '../../twmap/quadsLayer'
import { clearDialog, showInfo } from './dialog'
Expand Down Expand Up @@ -57,7 +57,7 @@ export async function createMap(httpRoot: string, name: string, create: MapCreat
}

// export async function uploadImage(httpRoot: string, mapName: string, imageName: string, file: Blob) {
// await fetch(`${httpRoot}/maps/${mapName}/map/images/${imageName}`, {
// await fetch(`${httpRoot}/maps/${mapName}/images/${imageName}`, {
// method: 'POST',
// body: file
// })
Expand Down Expand Up @@ -102,6 +102,12 @@ export async function queryMaps(httpRoot: string): Promise<MapDetail[]> {
return maps
}

export async function queryConfig(httpRoot: string, mapName: string): Promise<Config> {
const resp = await fetch(`${httpRoot}/maps/${mapName}/config`)
const config: Config = await resp.json()
return config
}

export async function queryMap(httpRoot: string, mapName: string): Promise<Map> {
const resp = await fetch(`${httpRoot}/maps/${mapName}`)
const data = await resp.arrayBuffer()
Expand Down
3 changes: 2 additions & 1 deletion client/src/ui/routes/edit.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import { serverHttpUrl } from '../../server/util'
export let name: string
export let password: string
let loadingSignal = (async () => {
reset()
await $server.query('join', name)
await $server.query('join', { name, password })
const httpUrl = serverHttpUrl($serverCfg)
const map_ = await queryMap(httpUrl, name)
const ams = await $server.query('get/automappers', undefined)
Expand Down
19 changes: 15 additions & 4 deletions client/src/ui/routes/lobby.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
open: boolean
name: string
public: boolean
password: string,
method: 'upload' | 'blank' | 'clone'
clone: number | undefined
cloneItems: { id: number; text: string }[]
Expand All @@ -82,6 +83,7 @@
open: false,
name: 'My Map',
public: true,
password: '',
method: 'upload',
clone: undefined,
cloneItems: [],
Expand Down Expand Up @@ -231,7 +233,6 @@
async function onCreateMap() {
const { name, method } = modalCreateMap
const access = modalCreateMap.public ? 'public' : 'unlisted'
const id = showInfo('Querying the server…', 'none')
Expand All @@ -242,7 +243,8 @@
else if (method === 'blank') {
await createMap(httpUrl, name, {
version: 'ddnet06', // TODO
access,
public: modalCreateMap.public,
password: modalCreateMap.password,
blank: {
w: modalCreateMap.blankWidth,
h: modalCreateMap.blankHeight,
Expand All @@ -252,13 +254,14 @@
else if (method === 'clone' && modalCreateMap.clone !== undefined) {
await createMap(httpUrl, name, {
version: 'ddnet06',
access,
public: modalCreateMap.public,
password: modalCreateMap.password,
clone: maps[modalCreateMap.clone].name
})
}
clearDialog(id)
if (access === 'unlisted') {
if (!modalCreateMap.public) {
showWarning("You created a map that won't be publicly listed. To access it in the future, use the access key '" + name + "'.")
}
navigate('/edit/' + name)
Expand All @@ -275,6 +278,10 @@
}
</script>

<svelte:head>
<title>DDNet Map Editor</title>
</svelte:head>

<div id="header">
<div class="left" />
<div class="middle">
Expand Down Expand Up @@ -510,6 +517,10 @@
labelB="public"
bind:toggled={modalCreateMap.public}
/>
<TextInput
labelText="Password (leave blank for public maps)"
bind:value={modalCreateMap.password}
/>
</div>
</Modal>

Expand Down
43 changes: 43 additions & 0 deletions desktop/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5dce5ff

Please sign in to comment.