make tavern into a class, dont know why i didnt do that before
This commit is contained in:
parent
295c9438af
commit
2ed7602595
1 changed files with 99 additions and 95 deletions
|
@ -1,107 +1,111 @@
|
|||
const tavern = {
|
||||
socket: socket = new WebSocket('ws:/' + window.location.host + '/ws'),
|
||||
msgs: [],
|
||||
connected: false,
|
||||
loggedIn: false,
|
||||
call: (f, ...args) => {
|
||||
class Tavern {
|
||||
constructor() {
|
||||
this.socket = new WebSocket('ws:/' + window.location.host + '/ws');
|
||||
this.socket.onopen = () => this.connected = true;
|
||||
this.msgs = [];
|
||||
this.connected = false;
|
||||
this.loggedIn = false;
|
||||
this.socket.onmessage = (m) => {
|
||||
m = JSON.parse(m.data);
|
||||
console.log(m);
|
||||
if (m.login) {
|
||||
this.socket.loggedIn = m.login.success;
|
||||
this.call(this.onlogin, this.socket.loggedIn);
|
||||
}
|
||||
if (m.message) {
|
||||
this.add_msg_to_history(m.message);
|
||||
this.call(this.onmessage, m.message);
|
||||
}
|
||||
if (m.get_chat_history) {
|
||||
m.get_chat_history.forEach(msg => {
|
||||
this.add_msg_to_history(msg);
|
||||
this.call(this.onmessage, msg);
|
||||
});
|
||||
}
|
||||
if (m.spawn_token) {
|
||||
this.call(this.onspawntoken, m.spawn_token);
|
||||
}
|
||||
if (m.move_token) {
|
||||
this.call(this.onmovetoken, m.move_token);
|
||||
}
|
||||
if (m.show_scene) {
|
||||
this.call(this.onshowscene, m.show_scene);
|
||||
}
|
||||
if (m.scene_list) {
|
||||
this.call(this.onscenelist, m.scene_list);
|
||||
}
|
||||
}
|
||||
}
|
||||
call(f, ...args) {
|
||||
if (typeof (f) == "function") {
|
||||
f(...args);
|
||||
}
|
||||
}
|
||||
};
|
||||
tavern.add_msg_to_history = (m) => {
|
||||
let id = m.id - 1;
|
||||
if (id >= 0) {
|
||||
if (id < tavern.msgs.length) {
|
||||
if (tavern.msgs[id].id == id + 1) {
|
||||
tavern.msgs[id] = m;
|
||||
}
|
||||
else {
|
||||
for (let i = 0; i < tavern.msgs.length; i += 1) {
|
||||
if (tavern.msgs[i].id > id) {
|
||||
tavern.msgs.splice(i, 0, m);
|
||||
break;
|
||||
add_msg_to_history = (m) => {
|
||||
let id = m.id - 1;
|
||||
if (id >= 0) {
|
||||
if (id < this.msgs.length) {
|
||||
if (this.msgs[id].id == id + 1) {
|
||||
this.msgs[id] = m;
|
||||
}
|
||||
else {
|
||||
for (let i = 0; i < this.msgs.length; i += 1) {
|
||||
if (this.msgs[i].id > id) {
|
||||
this.msgs.splice(i, 0, m);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.msgs.push(m)
|
||||
}
|
||||
}
|
||||
else {
|
||||
tavern.msgs.push(m)
|
||||
}
|
||||
login = (username, password) => {
|
||||
if (!this.connected || this.loggedIn) { return false; }
|
||||
this.socket.send(JSON.stringify({ login: { username, password } }));
|
||||
}
|
||||
simple_msg = (msg, token) => {
|
||||
if (!this.connected || this.loggedIn) { return false; }
|
||||
this.socket.send(JSON.stringify({ message: { text: msg, character: token ?? "" } }));
|
||||
}
|
||||
edit_msg = (new_text, id) => {
|
||||
if (id <= this.msgs.length && id > 0) {
|
||||
let msg = this.msgs[id - 1];
|
||||
msg.text = new_text;
|
||||
this.socket.send(JSON.stringify({ message: msg }));
|
||||
}
|
||||
}
|
||||
get_chat_history = (from, amount) => {
|
||||
if (!this.connected || this.loggedIn) { return false; }
|
||||
this.socket.send(JSON.stringify({ get_chat_history: { from: from, amount: amount } }))
|
||||
}
|
||||
get_last_msgs = (amount) => {
|
||||
if (!this.connected || this.loggedIn) { return false; }
|
||||
this.socket.send(JSON.stringify({ get_last_messages: { amount: amount } }))
|
||||
}
|
||||
get_tokens = (mapId) => {
|
||||
if (!this.connected || this.loggedIn) { return false; }
|
||||
this.socket.send(JSON.stringify({ get_tokens: { scene: mapId } }));
|
||||
}
|
||||
move_token = (id, x, y) => {
|
||||
if (!this.connected || this.loggedIn) { return false; }
|
||||
this.socket.send(JSON.stringify({ move_token: { token_id: id, x: x, y: y } }));
|
||||
}
|
||||
action_result = (name, source, targets, results) => {
|
||||
if (!this.connected || this.loggedIn) { return false; }
|
||||
this.socket.send(JSON.stringify({ action_result: { name: name, source: source ?? '', targets: targets ?? [], results: results } }));
|
||||
}
|
||||
get_scene = (id) => {
|
||||
if (!this.connected || this.loggedIn) { return; }
|
||||
this.socket.send(JSON.stringify({ get_scene: { id: id } }))
|
||||
}
|
||||
get_scene_list = () => {
|
||||
if (!this.connected || this.loggedIn) { return; }
|
||||
this.socket.send(JSON.stringify('get_scene_list'))
|
||||
}
|
||||
}
|
||||
|
||||
tavern.socket.onopen = () => tavern.connected = true;
|
||||
tavern.socket.onmessage = (m) => {
|
||||
m = JSON.parse(m.data);
|
||||
console.log(m);
|
||||
if (m.login) {
|
||||
tavern.socket.loggedIn = m.login.success;
|
||||
tavern.call(tavern.onlogin, tavern.socket.loggedIn);
|
||||
}
|
||||
if (m.message) {
|
||||
tavern.add_msg_to_history(m.message);
|
||||
tavern.call(tavern.onmessage, m.message);
|
||||
}
|
||||
if (m.get_chat_history) {
|
||||
m.get_chat_history.forEach(msg => {
|
||||
tavern.add_msg_to_history(msg);
|
||||
tavern.call(tavern.onmessage, msg);
|
||||
});
|
||||
}
|
||||
if (m.spawn_token) {
|
||||
tavern.call(tavern.onspawntoken, m.spawn_token);
|
||||
}
|
||||
if (m.move_token) {
|
||||
tavern.call(tavern.onmovetoken, m.move_token);
|
||||
}
|
||||
if (m.show_scene) {
|
||||
tavern.call(tavern.onshowscene, m.show_scene);
|
||||
}
|
||||
if (m.scene_list) {
|
||||
tavern.call(tavern.onscenelist, m.scene_list);
|
||||
}
|
||||
}
|
||||
tavern.login = (username, password) => {
|
||||
if (!tavern.connected || tavern.loggedIn) { return false; }
|
||||
tavern.socket.send(JSON.stringify({ login: { username, password } }));
|
||||
}
|
||||
tavern.simple_msg = (msg, token) => {
|
||||
if (!tavern.connected || tavern.loggedIn) { return false; }
|
||||
tavern.socket.send(JSON.stringify({ message: { text: msg, character: token ?? "" } }));
|
||||
}
|
||||
tavern.edit_msg = (new_text, id) => {
|
||||
if (id <= tavern.msgs.length && id > 0) {
|
||||
let msg = tavern.msgs[id - 1];
|
||||
msg.text = new_text;
|
||||
tavern.socket.send(JSON.stringify({ message: msg }));
|
||||
}
|
||||
}
|
||||
tavern.get_chat_history = (from, amount) => {
|
||||
if (!tavern.connected || tavern.loggedIn) { return false; }
|
||||
tavern.socket.send(JSON.stringify({ get_chat_history: { from: from, amount: amount } }))
|
||||
}
|
||||
tavern.get_last_msgs = (amount) => {
|
||||
if (!tavern.connected || tavern.loggedIn) { return false; }
|
||||
tavern.socket.send(JSON.stringify({ get_last_messages: { amount: amount } }))
|
||||
}
|
||||
tavern.get_tokens = (mapId) => {
|
||||
if (!tavern.connected || tavern.loggedIn) { return false; }
|
||||
tavern.socket.send(JSON.stringify({ get_tokens: { scene: mapId } }));
|
||||
}
|
||||
tavern.move_token = (id, x, y) => {
|
||||
if (!tavern.connected || tavern.loggedIn) { return false; }
|
||||
tavern.socket.send(JSON.stringify({ move_token: { token_id: id, x: x, y: y } }));
|
||||
}
|
||||
tavern.action_result = (name, source, targets, results) => {
|
||||
if (!tavern.connected || tavern.loggedIn) { return false; }
|
||||
tavern.socket.send(JSON.stringify({ action_result: { name: name, source: source ?? '', targets: targets ?? [], results: results } }));
|
||||
}
|
||||
tavern.get_scene = (id) => {
|
||||
if (!tavern.connected || tavern.loggedIn) { return; }
|
||||
tavern.socket.send(JSON.stringify({ get_scene: { id: id } }))
|
||||
}
|
||||
tavern.get_scene_list = () => {
|
||||
if (!tavern.connected || tavern.loggedIn) { return; }
|
||||
tavern.socket.send(JSON.stringify('get_scene_list'))
|
||||
}
|
||||
const tavern = new Tavern();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue