Skip to content

Commit

Permalink
may be broken, just wanna push
Browse files Browse the repository at this point in the history
  • Loading branch information
skysthelimitt committed Nov 18, 2024
1 parent f791414 commit f94b965
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 59 deletions.
10 changes: 6 additions & 4 deletions account.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,11 @@ async function generateCookie(name, pass) {
}

async function verifyCookie(cookie) {
let decrypted = JSON.parse(await decryptCookie(cookie));
if (decrypted) {
return (await isLoginValid(decrypted["n"], decrypted["p"])) && new Date(decrypted["e"]) > new Date();
if(cookie){
let decrypted = JSON.parse(await decryptCookie(cookie));
if (decrypted) {
return (await isLoginValid(decrypted["n"], decrypted["p"])) && new Date(decrypted["e"]) > new Date();
}
}
return false;
}
Expand Down Expand Up @@ -319,7 +321,7 @@ async function decryptCookie(cookie) {
}
} catch (e) {
console.error(e);
return false;
return {};
}
}
}
Expand Down
38 changes: 37 additions & 1 deletion ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,38 @@ let groq_keys = JSON.parse(process.env.GROQ_API_KEY);
// }

const groq = new Groq({ apiKey: groq_keys[0] });
let models = {
"average": [
"gemma-7b-it",
"gemma2-9b-it",
"llama-3.1-8b-instant",
"llama-3.2-11b-vision-preview",
"llama3-8b-8192",
"mixtral-8x7b-32768"
],
"smart": [
"llama-3.1-70b-versatile",
"llama3-70b-8192",
"llama-3.2-90b-vision-preview"
],
"dumb": [
"llama-3.2-3b-preview",
"llama-3.2-1b-preview"
]
}
let freeTokens = {
"gemma-7b-it": "500000",
"gemma2-9b-it": "500000",
"llama-3.1-8b-instant": "500000",
"llama-3.2-11b-vision-preview": "500000",
"llama3-8b-8192": "500000",
"mixtral-8x7b-32768": "500000",
"llama-3.1-70b-versatile": "500000",
"llama3-70b-8192": "500000",
"llama-3.2-90b-vision-preview": "500000",
"llama-3.2-3b-preview": "500000",
"llama-3.2-1b-preview": "500000"
}

async function infiniteCraft(firstWord, secondWord) {
let data = (
Expand All @@ -29,6 +61,9 @@ async function infiniteCraft(firstWord, secondWord) {
return data;
}
async function chatBot(model, messages, user) {
console.log(models[model]);
let usedModel = models[model][Math.floor(Math.random() * models[model].length) + 1]
console.log(usedModel);
let prompt = [
{
role: "system",
Expand All @@ -44,9 +79,10 @@ async function chatBot(model, messages, user) {
prompt.push.apply(prompt, );
let data = await groq.chat.completions.create({
messages: prompt,
model: "llama-3.2-1b-preview",
model: usedModel,
max_tokens: 512,
});
console.log(prompt);
prompt.push({
role: "assistant",
content: data.choices[0]?.message?.content
Expand Down
6 changes: 5 additions & 1 deletion database.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { Database } from "bun:sqlite";

const accs = new Database(`${process.env.DATA_PATH}/accounts.sqlite`);
const infdb = new Database(`${process.env.DATA_PATH}/infinitecraft.sqlite`);
const friends = new Database(`${process.env.DATA_PATH}/friends.sqlite`);
const polytrack = new Database(`${process.env.DATA_PATH}/polytrack.sqlite`);

infdb.exec("PRAGMA journal_mode = WAL;");
accs.exec("PRAGMA journal_mode = WAL;");
friends.exec("PRAGMA journal_mode = WAL;");
polytrack.exec("PRAGMA journal_mode = WAL;");

export { accs, infdb };
export { accs, infdb, friends,polytrack };
10 changes: 4 additions & 6 deletions html/ai.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
let message = document.getElementById("chatbox").value;
document.getElementById("chatbox").value = "";
sendMessage(1, message);
sendMessage(0, await handleAI(message, model));
sendMessage(0, await handleAI(message));
}
});
});
Expand All @@ -76,11 +76,9 @@ <h1>AI Chat Bot</h1>
<p class="evensmaller">Messages are logged for quality assurance. Don't type any personal information.</p>
<select id="models">
<option>Select a model:</option>
<option value="1b">LLAMA 3.2 1B (Speed: 100%, Quality: 40%)</option>
<option value="3b">LLAMA 3.2 3B (Speed: 90%, Quality: 50%)</option>
<option value="11b">LLAMA 3.2 11B (Speed: 60%, Quality: 70%, image support)</option>
<option value="8b">LLAMA 3.1 8B (Speed: 85%, Quality: 55%)</option>
<option value="70b">LLAMA 3.1 70B (Speed: 30%, Quality: 90%)</option>
<option value="average">Average (Recommended)</option>
<option value="dumb">Dumb (but fast)</option>
<option value="genius">Genius (but slow)</option>
</select>
<div id="chat">
<div id="messages">
Expand Down
66 changes: 64 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { log } from "./log.js";
import bodyParser from "body-parser";
import express from "express";
import cookieParser from "cookie-parser";
import fs from "node:fs/promises";
import { fileURLToPath } from "url";
import path, { dirname } from "node:path";
import mime from "mime-types";
import compression from "compression";
import { accs, infdb } from "./database.js";
import { accs, infdb, polytrack } from "./database.js";
import { createProxyMiddleware } from "http-proxy-middleware";
import { banUser, removeAccount, verifyCookie, getUsers, getUserFromCookie, getRawData, retrieveData, createAccount, resetPassword, generateAccountPage, loginAccount, editProfile, addBadge, isAdmin, saveData } from "./account.js";
import { infiniteCraft, chatBot } from "./ai.js";
Expand All @@ -20,9 +21,11 @@ const app = express();
app.use(compression());
app.use(cookieParser());
app.use(express.json({ limit: "10mb" }));
app.use(express.urlencoded({ extended: true }));
app.use(express.urlencoded({ extended: false }));
app.use(express.text());

import WebSocket, { WebSocketServer } from "ws";
import { getDefaultAgent } from "groq-sdk/_shims/index.mjs";
const wss = new WebSocketServer({ noServer: true });
wss.on("connection", function connection(ws, req, res) {
setInterval(() => {
Expand Down Expand Up @@ -188,6 +191,64 @@ app.get("/api/infinite/get", async (req, res, next) => {
}
}
});
app.use("/semag/polytrack/data/", async (req, res, next) => {
let path = req.path.substring(1, req.path.length);
if(path == "user") {
res.sendStatus(200);
} else if(path == "leaderboard") {
let data = {};

if(req.method == "POST") {
req.body.split("&").forEach((item) => {
data[item.split("=")[0]] = item.split("=")[1]
});
console.log(data);
const getExistingRuns = polytrack.query(`SELECT * FROM polytrack WHERE userid = $usrid AND trackid = $trackid`);
let existingRuns = getExistingRuns.all({ $usrid: data["userToken"], $trackid: data["trackId"] });
let saveRun = true;
if(existingRuns !== null) {
existingRuns.forEach((item) => {
if(saveRun) {
if(data.frames > item.frames) {
saveRun = false;
} else {
let deleteRun = polytrack.query(`DELETE FROM polytrack WHERE id = $id`);
deleteRun.run({ $id: item.id })
}
}
})
}
if(saveRun) {
const addRun = polytrack.query(`INSERT INTO polytrack (trackid, username, colors, recording, frames, userid) VALUES ($id, $usr, $clr, $record, $frames, $usrid)`)
let runData = addRun.run({ $id: data["trackId"], $usr: data["name"], $clr: data["carColors"], $record: data["recording"], $usrid: data["userToken"], $frames: data["frames"] });
console.log("run", runData);
res.send(runData.lastInsertRowid);
}
} else {
let leaderboard = polytrack.query(`SELECT * FROM polytrack WHERE trackid = $id LIMIT $limit OFFSET $offset`).all({ $id: req.query.trackId, $limit: req.query.amount, $offset: req.query.skip })
console.log(leaderboard);
let returnValue = {"total": leaderboard.length, "entries":[]}
for(let i = 0; i<leaderboard.length;i++) {
returnValue["entries"][i] = {};
returnValue["entries"][i]["id"] = leaderboard[i]["id"];
returnValue["entries"][i]["name"] = decodeURIComponent(leaderboard[i]["username"]);
returnValue["entries"][i]["carColors"] = leaderboard[i]["colors"];
returnValue["entries"][i]["frames"] = leaderboard[i]["frames"];
returnValue["entries"][i]["verifiedState"] = true;
returnValue["entries"][i]["isSelf"] = false;
}
res.send(returnValue);
}
} else if(path == "recording") {
let recordingQuery = polytrack.query(`SELECT * FROM polytrack WHERE id = $id`).get({ $id: req.query.recordingId });
res.send({
"recording": recordingQuery.recording,
"frames": recordingQuery.frames,
"verifiedState": true,
"carColors": recordingQuery.colors
});
}
})
app.use("/api/account/load", async (req, res, next) => {
if (req.cookies.token && (await verifyCookie(req.cookies.token))) {
let status = await retrieveData(req.cookies.token);
Expand All @@ -202,6 +263,7 @@ app.use("/api/account/load", async (req, res, next) => {
});

app.use("/api/getUsers", async (req, res, next) => {
console.log(req.query)
let status = await getUsers(req.query.page, req.query.query);
res.status(200).send(status);
});
Expand Down
81 changes: 36 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"type": "module",
"dependencies": {
"axios": "^1.3.1",
"body-parser": "^2.0.2",
"chalk": "^5.3.0",
"compression": "^1.7.4",
"cookie-parser": "^1.4.6",
Expand Down

0 comments on commit f94b965

Please sign in to comment.