all repos — felt @ 4f9ca53e1bfb24febbe981c746405c50194f9303

virtual tabletop for dungeons and dragons (and similar) using Go, MongoDB, and websockets

static/admin.js (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
let adminToken = null;
const adminWrapper = document.getElementById("adminWrapper");
const adminZone = document.getElementById("adminZone");
const createTableForm = document.getElementById("createTableForm");
const newTableName = document.getElementById("newTableName");
const newTablePass = document.getElementById("newTablePass");

async function getTable(name, pass) {
  try {
    const headers = new Headers();
    headers.set('Authorization', 'Bearer ' + adminToken.access_token);
    const res = await fetch(`/admin/api/table/${name}?passcode=${pass}`, {
      method: 'GET',
      headers: headers,
    });
  
    if (res.ok) {
      document.getElementById("input_table_name").value = name;
      document.getElementById("input_table_pass").value = pass;
      dial();
      infoHtml = "<a href='#' onclick='getTables()'>&larr; table list</a><br><pre>";
      infoHtml += await res.text();
      infoHtml += "</pre>"
      adminZone.innerHTML = infoHtml;
    }
    else {
      console.log(res.status);
    }
  } catch (err) {
    console.dir(err)
  }
}

async function getTables() {
  try {
    const headers = new Headers();
    headers.set('Authorization', 'Bearer ' + adminToken.access_token);
    const res = await fetch('/admin/api/table/', {
      method: 'GET',
      headers: headers
    });
    if (res.ok) {
      const tableList = await res.json();
      let tableListHTML = "<ul>\n";
      for (const t of tableList) {
        tableListHTML +=  `<li><a href="#" onclick="getTable('${t.name}','${t.passcode}');return false;">${t.name}</a></li>\n`
      }
      tableListHTML += "</ul>"
      adminZone.innerHTML = tableListHTML;
    } else {
      if (res.status == 404) {
        return;
      }
      setErr(await res.headers.get("Quartzgun-Error"));
    }
  } catch {
  }
}

async function doLogin() {
  const adminUsrInput = document.getElementById("name_entry");
  const adminPassInput = document.getElementById("input_admin_pass");
  
  if (adminUsrInput && adminPassInput) {
    adminToken = await getAdminToken(adminUsrInput.value, adminPassInput.value);
    if (adminToken) {
      adminWrapper.style.display="block";
      getTables();
      adminZone.style.display = "block";
    } else {
      setErr("Incorrect credentials");
    }
  }
}

function showAdminModal(show) {
  const modal = document.getElementById("admin_modal")
  if (modal) {
    modal.style.display = show ? "block" : "none";
  }
}

async function getAdminToken(user, pass) {
  const headers = new Headers();
  headers.set('Authorization', 'Basic ' + btoa(user + ":" + pass));
  try {
    const res = await fetch('/admin/api/auth/', {
      method: 'POST',
      headers: headers
    });
    
    if (res.ok) {
      return await res.json();
    }
    return null;
  } catch (err) {
    return null;
  }
}

function setTableCreateFormVisible(v) {
  if (createTableForm) {
    createTableForm.style.display = v ? "block" : "none";
  }
  if (!v) {
    if (newTableName) {
      newTableName.value = "";
    }
    if (newTablePass) {
      newTablePass.value = "";
    }
  }
}

async function createTable() {
  const headers = new Headers();
  headers.set('Authorization', 'Bearer ' + adminToken.access_token);
    
  const formData = new FormData();
  formData.set("name", newTableName.value);
  formData.set("passcode", newTablePass.value);
  
  let bodyStr = "{";
  for (const pair of formData.entries()) {
    bodyStr += `"${pair[0]}": "${pair[1]}",`;
  }
  bodyStr = bodyStr.slice(0, -1);
  bodyStr += "}";
  const res = await fetch('/admin/api/table/', {
    method: 'POST',
    headers: headers,
    body: bodyStr,
  });
  if (res.ok) {
    getTables();
    setTableCreateFormVisible(false);
  }
}