generated from ztimson/template
Refactored code formatting to use consistent indentation and object destructuring across client and server files.
All checks were successful
Build and publish / Build Container (push) Successful in 1m44s
All checks were successful
Build and publish / Build Container (push) Successful in 1m44s
This commit is contained in:
258
public/navi.mjs
258
public/navi.mjs
@@ -1,4 +1,4 @@
|
||||
export default class Navi {
|
||||
class Navi {
|
||||
api;
|
||||
connected = false;
|
||||
icon;
|
||||
@@ -12,11 +12,11 @@ export default class Navi {
|
||||
#secret;
|
||||
#world;
|
||||
|
||||
constructor(api = window.location.origin, secret = '') {
|
||||
this.api = api.replace(/\/$/, '');
|
||||
constructor(api = window.location.origin, secret = '') {
|
||||
this.api = api.replace(/\/$/, '');
|
||||
this.icon = `${this.api}/favicon.png`;
|
||||
this.#secret = secret;
|
||||
}
|
||||
}
|
||||
|
||||
async init() {
|
||||
if(this.#init) return this.#init;
|
||||
@@ -56,7 +56,7 @@ export default class Navi {
|
||||
this.connected = true;
|
||||
this.emit('init');
|
||||
res(this);
|
||||
} catch (err) {
|
||||
} catch(err) {
|
||||
rej(err);
|
||||
}
|
||||
});
|
||||
@@ -86,41 +86,49 @@ export default class Navi {
|
||||
callbacks.forEach(cb => cb(data));
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// REST API
|
||||
// ============================================
|
||||
// ============================================
|
||||
// REST API
|
||||
// ============================================
|
||||
|
||||
async sendUserMessage(userId, message) {
|
||||
const response = await fetch(`${this.api}/api/message`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ message })
|
||||
});
|
||||
if (!response.ok) throw new Error('Message failed to send bestie');
|
||||
async sendUserMessage(userId, message) {
|
||||
const response = await fetch(`${this.api}/api/message`, {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({message})
|
||||
});
|
||||
if(!response.ok) throw new Error('Message failed to send bestie');
|
||||
|
||||
const result = await response.json();
|
||||
this.emit('message:sent', { userId, message, result });
|
||||
return result;
|
||||
}
|
||||
const result = await response.json();
|
||||
this.emit('message:sent', {
|
||||
userId,
|
||||
message,
|
||||
result
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
async linkPet(petId, targetApiUrl) {
|
||||
const response = await fetch(`${this.api}/api/link`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ targetApiUrl })
|
||||
});
|
||||
if (!response.ok) throw new Error('Link connection is bussin (negatively)');
|
||||
async linkPet(petId, targetApiUrl) {
|
||||
const response = await fetch(`${this.api}/api/link`, {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({targetApiUrl})
|
||||
});
|
||||
if(!response.ok) throw new Error('Link connection is bussin (negatively)');
|
||||
|
||||
const result = await response.json();
|
||||
this.emit('pet:linked', { petId, targetApiUrl, result });
|
||||
return result;
|
||||
}
|
||||
const result = await response.json();
|
||||
this.emit('pet:linked', {
|
||||
petId,
|
||||
targetApiUrl,
|
||||
result
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// WORLD SOCKET
|
||||
// ============================================
|
||||
// ============================================
|
||||
// WORLD SOCKET
|
||||
// ============================================
|
||||
|
||||
connect(apiOrWorld, world) {
|
||||
connect(apiOrWorld, world) {
|
||||
let api;
|
||||
if(world) {
|
||||
api = apiOrWorld;
|
||||
@@ -129,12 +137,12 @@ export default class Navi {
|
||||
world = apiOrWorld;
|
||||
}
|
||||
|
||||
if(!this.world) this.world = {};
|
||||
if(this.#world && this.world.api !== api) {
|
||||
if(!this.world) this.world = {};
|
||||
if(this.#world && this.world.api !== api) {
|
||||
this.#world.disconnect();
|
||||
this.#world = null;
|
||||
}
|
||||
if(!this.#world) this.#world = io(`${api}/world`, {auth: this.#secret ? {token: this.#secret} : null});
|
||||
}
|
||||
if(!this.#world) this.#world = io(`${api}/world`, {auth: this.#secret ? {token: this.#secret} : null});
|
||||
this.world.api = api;
|
||||
this.world.data = null;
|
||||
this.world.name = world;
|
||||
@@ -142,113 +150,127 @@ export default class Navi {
|
||||
|
||||
const callbacks = {
|
||||
move: (x, y) => {
|
||||
this.#world.emit('move', {x, y});
|
||||
this.#world.emit('move', {
|
||||
x,
|
||||
y
|
||||
});
|
||||
},
|
||||
leave: () => {
|
||||
this.#world.disconnect();
|
||||
this.world = null;
|
||||
},
|
||||
onData: (data) => { },
|
||||
onPlayers: (players) => { },
|
||||
onJoined: (player) => { },
|
||||
onMoved: (player) => { },
|
||||
onLeft: (player) => { },
|
||||
onError: (error) => { }
|
||||
}
|
||||
onData: (data) => {
|
||||
},
|
||||
onPlayers: (players) => {
|
||||
},
|
||||
onJoined: (player) => {
|
||||
},
|
||||
onMoved: (player) => {
|
||||
},
|
||||
onLeft: (player) => {
|
||||
},
|
||||
onError: (error) => {
|
||||
}
|
||||
};
|
||||
|
||||
this.#world.on('data', (data) => {
|
||||
this.#world.on('data', (data) => {
|
||||
this.world.data = data;
|
||||
callbacks.onData(data);
|
||||
callbacks.onData(data);
|
||||
this.emit('world:data', data);
|
||||
});
|
||||
});
|
||||
|
||||
this.#world.on('players', (players) => {
|
||||
this.world.players.clear();
|
||||
players.forEach(p => this.world.players.set(p.socketId, p));
|
||||
callbacks.onPlayers(players);
|
||||
this.emit('world:players', players)
|
||||
});
|
||||
this.#world.on('players', (players) => {
|
||||
this.world.players.clear();
|
||||
players.forEach(p => this.world.players.set(p.socketId, p));
|
||||
callbacks.onPlayers(players);
|
||||
this.emit('world:players', players);
|
||||
});
|
||||
|
||||
this.#world.on('joined', (player) => {
|
||||
this.world.players.set(player.socketId, player);
|
||||
callbacks.onJoined(player);
|
||||
this.#world.on('joined', (player) => {
|
||||
this.world.players.set(player.socketId, player);
|
||||
callbacks.onJoined(player);
|
||||
this.emit('world:joined', player);
|
||||
});
|
||||
});
|
||||
|
||||
this.#world.on('moved', (data) => {
|
||||
const player = this.world.players.get(data.socketId);
|
||||
if(player) {
|
||||
player.x = data.x;
|
||||
player.y = data.y;
|
||||
}
|
||||
callbacks.onMoved(player);
|
||||
this.emit('world:moved', player);
|
||||
});
|
||||
|
||||
this.#world.on('left', (data) => {
|
||||
this.#world.on('moved', (data) => {
|
||||
const player = this.world.players.get(data.socketId);
|
||||
this.world.players.delete(data.socketId);
|
||||
callbacks.onLeft(player);
|
||||
if(player) {
|
||||
player.x = data.x;
|
||||
player.y = data.y;
|
||||
}
|
||||
callbacks.onMoved(player);
|
||||
this.emit('world:moved', player);
|
||||
});
|
||||
|
||||
this.#world.on('left', (data) => {
|
||||
const player = this.world.players.get(data.socketId);
|
||||
this.world.players.delete(data.socketId);
|
||||
callbacks.onLeft(player);
|
||||
this.emit('world:left', player);
|
||||
});
|
||||
});
|
||||
|
||||
this.#world.on('error', (error) => {
|
||||
console.error('World error:', error);
|
||||
callbacks.onError(error);
|
||||
this.#world.on('error', (error) => {
|
||||
console.error('World error:', error);
|
||||
callbacks.onError(error);
|
||||
this.emit('world:error', error);
|
||||
});
|
||||
});
|
||||
|
||||
this.#world.emit('join', {world, api});
|
||||
this.#world.emit('join', {
|
||||
world,
|
||||
api
|
||||
});
|
||||
|
||||
this.emit('world:join', world);
|
||||
return callbacks;
|
||||
}
|
||||
return callbacks;
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// LLM
|
||||
// ============================================
|
||||
// ============================================
|
||||
// LLM
|
||||
// ============================================
|
||||
|
||||
ask(message, stream) {
|
||||
this.llmCallback = stream;
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
this.llmResolve = resolve;
|
||||
this.llmReject = reject;
|
||||
this.#socket.emit('llm-ask', {message});
|
||||
this.emit('llm:ask')
|
||||
});
|
||||
ask(message, stream) {
|
||||
this.llmCallback = stream;
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
this.llmResolve = resolve;
|
||||
this.llmReject = reject;
|
||||
this.#socket.emit('llm-ask', {message});
|
||||
this.emit('llm:ask');
|
||||
});
|
||||
|
||||
promise.abort = () => {
|
||||
this.#socket.emit('llm-abort');
|
||||
if(this.llmReject) this.llmReject(new Error('Aborted by user'));
|
||||
this.llmCallback = null;
|
||||
this.llmResolve = null;
|
||||
this.llmReject = null;
|
||||
this.emit('llm:abort')
|
||||
};
|
||||
promise.abort = () => {
|
||||
this.#socket.emit('llm-abort');
|
||||
if(this.llmReject) this.llmReject(new Error('Aborted by user'));
|
||||
this.llmCallback = null;
|
||||
this.llmResolve = null;
|
||||
this.llmReject = null;
|
||||
this.emit('llm:abort');
|
||||
};
|
||||
|
||||
return promise;
|
||||
}
|
||||
return promise;
|
||||
}
|
||||
|
||||
clearChat() {
|
||||
if(this.#socket) this.#socket.emit('llm-clear');
|
||||
clearChat() {
|
||||
if(this.#socket) this.#socket.emit('llm-clear');
|
||||
this.emit('llm:clear');
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// UTILITY
|
||||
// ============================================
|
||||
// ============================================
|
||||
// UTILITY
|
||||
// ============================================
|
||||
|
||||
disconnect() {
|
||||
this.connected = false;
|
||||
disconnect() {
|
||||
this.connected = false;
|
||||
this.icon = this.info = this.theme = this.world = this.#init = this.#secret = null;
|
||||
if(this.#world) {
|
||||
this.#world.disconnect();
|
||||
this.#world = null;
|
||||
}
|
||||
if(this.#socket) {
|
||||
this.#socket.disconnect();
|
||||
this.#socket = null;
|
||||
}
|
||||
if(this.#world) {
|
||||
this.#world.disconnect();
|
||||
this.#world = null;
|
||||
}
|
||||
if(this.#socket) {
|
||||
this.#socket.disconnect();
|
||||
this.#socket = null;
|
||||
}
|
||||
this.emit('disconnected');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Navi;
|
||||
|
||||
Reference in New Issue
Block a user