Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mathis committed Oct 3, 2023
1 parent 3eb586b commit c12d771
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 75 deletions.
6 changes: 6 additions & 0 deletions client/src/ui/lib/editAutomapper.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
})
rmap = mapView.getRenderMap()
rmap.gameLayer.visible = false
if (rmap.teleLayer) rmap.teleLayer.visible = false
if (rmap.speedupLayer) rmap.speedupLayer.visible = false
if (rmap.frontLayer) rmap.frontLayer.visible = false
if (rmap.switchLayer) rmap.switchLayer.visible = false
if (rmap.tuneLayer) rmap.tuneLayer.visible = false
$server.on('create/automapper', onUploadAutomapper)
$server.on('delete/automapper', onDeleteAutomapper)
Expand Down
9 changes: 7 additions & 2 deletions client/src/ui/lib/editInfo.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<script lang="ts">
import type { MapInfo } from '../../twmap/map'
import { sync } from '../../server/util'
import { server, map } from '../global'
$: syncInfo = sync($server, $map.info, { query: 'edit/info' })
$: syncInfo = sync($server, cloneInfo($map.info), { query: 'edit/info' })
function cloneInfo(info: MapInfo): MapInfo {
return { ...info }
}
function onChangeSettings(e: Event & { currentTarget: HTMLTextAreaElement }) {
$syncInfo.settings = e.currentTarget.value.split('\n').filter(s => s !== '')
onChange()
}
function onChange() {
$syncInfo = { ...$syncInfo }
syncInfo.sync($syncInfo)
}
</script>

Expand Down
5 changes: 0 additions & 5 deletions client/src/ui/lib/editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@
}
$automappers = $automappers
}
function serverOnError(e: string) {
showError('Server Error: ' + e)
}
async function onServerClosed() {
await showError('You have been disconnected from the server.')
navigate('/')
Expand All @@ -124,7 +120,6 @@
$server.on('edit/automap', serverOnApplyAutomapper)
$server.on('delete/automapper', serverOnDeleteAutomapper)
$server.on('create/automapper', serverOnUploadAutomapper)
$server.onError(serverOnError)
$server.query('get/users', undefined)
.then(u => $peers = u)
Expand Down
11 changes: 9 additions & 2 deletions client/src/ui/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,24 @@ export async function download(file: string, name: string) {
}

export async function uploadMap(httpRoot: string, name: string, file: Blob) {
await fetch(`${httpRoot}/maps/${name}`, {
const resp = await fetch(`${httpRoot}/maps/${name}`, {
method: 'PUT',
body: file
})

if (!resp.ok)
throw await resp.text()
}

export async function createMap(httpRoot: string, name: string, create: MapCreation) {
await fetch(`${httpRoot}/maps/${name}`, {
const resp = await fetch(`${httpRoot}/maps/${name}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(create)
})

if (!resp.ok)
throw await resp.text()
}

// export async function uploadImage(httpRoot: string, mapName: string, imageName: string, file: Blob) {
Expand Down
12 changes: 11 additions & 1 deletion client/src/ui/routes/edit.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import EditAutomapper from '../lib/editAutomapper.svelte'
import Headerbar from '../lib/headerbar.svelte'
import Fence from '../lib/fence.svelte'
import { onDestroy } from 'svelte'
import { onDestroy, onMount } from 'svelte'
import { showError } from '../lib/dialog'
export let name: string
Expand All @@ -17,7 +18,16 @@
$map = map_
})()
function serverOnError(e: string) {
showError('Server Error: ' + e)
}
onMount(() => {
$server.onError(serverOnError)
})
onDestroy(() => {
$server.onError(() => {})
$server.query('leave', name)
})
Expand Down
50 changes: 26 additions & 24 deletions client/src/ui/routes/lobby.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -197,31 +197,31 @@
const { name, method } = modalCreateMap
const access = modalCreateMap.public ? 'public' : 'unlisted'
showInfo('Querying the server…', 'none')
if (method === 'upload' && modalCreateMap.uploadFile !== null) {
await uploadMap(serverConfs[serverId].httpUrl, name, modalCreateMap.uploadFile)
}
else if (method === 'blank') {
await createMap(serverConfs[serverId].httpUrl, name, {
version: 'ddnet06', // TODO
access,
blank: {
w: modalCreateMap.blankWidth,
h: modalCreateMap.blankHeight,
}
})
}
else if (method === 'clone' && modalCreateMap.clone !== undefined) {
await createMap(serverConfs[serverId].httpUrl, name, {
version: 'ddnet06',
access,
clone: maps[modalCreateMap.clone].name
})
}
const id = showInfo('Querying the server…', 'none')
try {
clearDialog()
if (method === 'upload' && modalCreateMap.uploadFile !== null) {
await uploadMap(serverConfs[serverId].httpUrl, name, modalCreateMap.uploadFile)
}
else if (method === 'blank') {
await createMap(serverConfs[serverId].httpUrl, name, {
version: 'ddnet06', // TODO
access,
blank: {
w: modalCreateMap.blankWidth,
h: modalCreateMap.blankHeight,
}
})
}
else if (method === 'clone' && modalCreateMap.clone !== undefined) {
await createMap(serverConfs[serverId].httpUrl, name, {
version: 'ddnet06',
access,
clone: maps[modalCreateMap.clone].name
})
}
clearDialog(id)
if (access === 'unlisted') {
const url = window.location.origin + '/edit/' + encodeURIComponent(name)
showWarning(
Expand All @@ -230,7 +230,9 @@
)
}
navigate('/edit/' + name)
} catch (e) {
}
catch (e) {
clearDialog(id)
showError('Map creation failed: ' + e)
}
}
Expand Down
10 changes: 8 additions & 2 deletions server/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,16 @@ pub enum CreationMethod {
Blank { w: u32, h: u32 },
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Version {
DDNet06,
Teeworlds07,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MapCreation {
#[serde(default)]
pub version: Option<twmap::Version>,
pub version: Option<Version>,
#[serde(default)]
pub access: Option<MapAccess>,
#[serde(flatten)]
Expand Down
6 changes: 4 additions & 2 deletions server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
checks::PartialCheck,
cli::Cli,
error::Error,
map_cfg::MapAccess,
protocol::*,
room::{Peer, Room},
twmap_map_checks::InternalMapChecking,
Expand Down Expand Up @@ -296,6 +297,7 @@ impl Server {
pub fn get_maps(&self) -> Vec<MapDetail> {
self.rooms()
.iter()
.filter(|(k, v)| v.config.access == MapAccess::Public)
.map(|(k, v)| MapDetail {
name: k.to_owned(),
users: v.peer_count(),
Expand Down Expand Up @@ -340,7 +342,7 @@ impl Server {
}
CreationMethod::Blank { w, h } => {
if let Some(version) = creation.version {
if version != twmap::Version::DDNet06 {
if version != Version::DDNet06 {
return Err(Error::UnsupportedMapType);
}
}
Expand Down Expand Up @@ -378,7 +380,7 @@ impl Server {
std::fs::create_dir(new_room.automapper_path())
.map_err(|e| Error::ServerError(e.to_string().into()))?;

new_room.config.access = creation.access.unwrap_or(crate::map_cfg::MapAccess::Public);
new_room.config.access = creation.access.unwrap_or(MapAccess::Public);
new_room
.save_config()
.map_err(|e| Error::ServerError(e.into()))?;
Expand Down
72 changes: 35 additions & 37 deletions server/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{fmt::Display, path::Path, sync::OnceLock};

use regex::Regex;
use serde::Serializer;
use std::{fmt::Display, path::Path};

use crate::{
protocol::AutomapperKind,
Expand All @@ -17,40 +15,40 @@ where
serializer.collect_str(value)
}

pub(crate) mod serialize_partial_index {
use serde::{de, Deserialize, Deserializer, Serializer};

pub(crate) fn serialize<S: Serializer>(
opt_index: &Option<Option<u16>>,
serializer: S,
) -> Result<S::Ok, S::Error> {
match opt_index {
None => serializer.serialize_unit(),
Some(None) => serializer.serialize_none(),
Some(Some(v)) => serializer.serialize_some(&v.to_string()),
}
}

pub(crate) fn deserialize<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Option<Option<u16>>, D::Error> {
let opt_name: Option<String> = Deserialize::deserialize(deserializer)?;
let opt_index = match opt_name {
Some(opt_name) => opt_name
.split('_')
.next()
.ok_or(de::Error::invalid_value(
de::Unexpected::Str(&opt_name),
&"a mapdir-compatible resource index",
))?
.parse::<u16>()
.map(|v| Some(Some(v)))
.map_err(de::Error::custom)?,
None => Some(None),
};
Ok(opt_index)
}
}
// pub(crate) mod serialize_partial_index {
// use serde::{de, Deserialize, Deserializer, Serializer};

// pub(crate) fn serialize<S: Serializer>(
// opt_index: &Option<Option<u16>>,
// serializer: S,
// ) -> Result<S::Ok, S::Error> {
// match opt_index {
// None => serializer.serialize_unit(),
// Some(None) => serializer.serialize_none(),
// Some(Some(v)) => serializer.serialize_some(&v.to_string()),
// }
// }

// pub(crate) fn deserialize<'de, D: Deserializer<'de>>(
// deserializer: D,
// ) -> Result<Option<Option<u16>>, D::Error> {
// let opt_name: Option<String> = Deserialize::deserialize(deserializer)?;
// let opt_index = match opt_name {
// Some(opt_name) => opt_name
// .split('_')
// .next()
// .ok_or(de::Error::invalid_value(
// de::Unexpected::Str(&opt_name),
// &"a mapdir-compatible resource index",
// ))?
// .parse::<u16>()
// .map(|v| Some(Some(v)))
// .map_err(de::Error::custom)?,
// None => Some(None),
// };
// Ok(opt_index)
// }
// }

pub(crate) mod macros {
macro_rules! apply_partial {
Expand Down

0 comments on commit c12d771

Please sign in to comment.