From e1f5619d8b773255ab9cff5cdbefcb5b8b9d4e7f Mon Sep 17 00:00:00 2001 From: kwaroran Date: Sat, 25 Nov 2023 17:37:11 +0900 Subject: [PATCH] [feat] multuser --- src/ts/sync/multiuser.ts | 51 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/src/ts/sync/multiuser.ts b/src/ts/sync/multiuser.ts index a7638be4..9f0b6999 100644 --- a/src/ts/sync/multiuser.ts +++ b/src/ts/sync/multiuser.ts @@ -1,11 +1,12 @@ import { v4 } from 'uuid'; import { alertError, alertInput, alertNormal, alertWait } from '../alert'; import { get } from 'svelte/store'; -import { DataBase, setDatabase, type character } from '../storage/database'; +import { DataBase, setDatabase, type character, saveImage } from '../storage/database'; import { selectedCharID } from '../stores'; import { cloneDeep } from 'lodash'; import { findCharacterIndexbyId, sleep } from '../util'; import type { DataConnection, Peer } from 'peerjs'; +import { readImage } from '../storage/globalApi'; async function importPeerJS(){ return await import('peerjs'); @@ -36,7 +37,7 @@ export async function createMultiuserRoom(){ connections.push(conn) console.log("new connection", conn) - function requestChar(){ + async function requestChar(excludeAssets:string[]|null = null){ const db = get(DataBase) const selectedCharId = get(selectedCharID) const char = cloneDeep(db.characters[selectedCharId]) @@ -48,6 +49,36 @@ export async function createMultiuserRoom(){ type: 'receive-char', data: char }); + if(excludeAssets !== null){ + if(char.additionalAssets){ + const ass = char.additionalAssets.filter((asset) => { + return !excludeAssets.includes(asset[1]) + }) + + for(const a of ass){ + conn.send({ + type: 'receive-asset', + id: a[1], + data: await readImage(a[1]) + }) + } + + } + if(char.emotionImages){ + const ass = char.emotionImages.filter((asset) => { + return !excludeAssets.includes(asset[1]) + }) + + for(const a of ass){ + conn.send({ + type: 'receive-asset', + id: a[1], + data: await readImage(a[1]) + }) + } + } + + } } conn.on('data', function(data:ReciveData) { @@ -92,8 +123,13 @@ interface ReciveFirst{ interface RequestFirst{ type: 'request-char' } +interface ReciveAsset{ + type: 'receive-asset', + id: string, + data: Uint8Array +} -type ReciveData = ReciveFirst|RequestFirst +type ReciveData = ReciveFirst|RequestFirst|ReciveAsset export async function joinMultiuserRoom(){ @@ -142,8 +178,17 @@ export async function joinMultiuserRoom(){ setDatabase(db) break } + case 'receive-asset':{ + saveImage(data.data, data.id) + } } }); + + conn.on('close', function() { + alertError("Connection closed") + connectionOpen = false + selectedCharID.set(-1) + }) let waitTime = 0 while(!open){