Untitled

mail@pastecode.io avatar
unknown
python
a year ago
4.7 kB
11
Indexable
Never
import asyncio

from rustplus import *
from rustplus import RustSocket
from config import localTime

from quart import Quart, redirect, render_template, request, session, Markup
from database import Session, func

from models import user, Tokens, Base

from io import BytesIO
import base64

# Web variables
web = Quart(__name__)
web.secret_key = "your_secret_key"
session_instance = Session()

# Rust variables
socket = None  # Initialize the socket variable

# General variables

"""
    # Rust shit
"""
async def run_rust_bot():
    global socket 
    tokens = session_instance.query(Tokens).first()
    rustToken = tokens.rustToken
    steamid = tokens.steamID
    ipAdress = tokens.IP
    port = tokens.port

    socket = RustSocket(ipAdress, port, steamid, rustToken, use_proxy=True)
    await socket.connect()


async def getTime():
    gameTime = (await socket.get_time()).time
    print(gameTime)
    return gameTime



"""
    # Flask and routes for handling web
"""

@web.route("/", methods=["GET", "POST"])
async def login():
    if request.method == "POST":
        form = await request.form
        usernameForm = form['username']
        passwordForm = form["password"]
        user_obj = session_instance.query(user).filter_by(name=usernameForm).first()

        if user_obj and user_obj.password == passwordForm:
            session["username"] = usernameForm
            session_instance.close()

            return redirect("/addBot")
        else:
            session_instance.close()
            return "Wrong name or password"
    else:
        return await render_template("index.html")

@web.route("/register", methods=["GET", "POST"])
async def register():
    if request.method == "POST":
        form = await request.form
        name = form['username']
        password = form['password']

        max_uid = session_instance.query(func.max(user.uID)).scalar() or 0
        usr = user(
            uID=max_uid + 1,
            name=name,
            password=password
        )
        session_instance.add(usr)
        session_instance.commit()

        return redirect("/")

    return await render_template('register.html')


@web.route("/server", methods=["GET", "POST"])
async def server():
    if "username" not in session:
        return redirect("/")
    if request.method == "POST":
        if request.form['submit_button'] == f'Launch {name}':
            redirect("/bot/{name}")
        elif request.form['submit_button'] == f'Launch {name}':
            redirect("/bot/{name}")

    uservar = session_instance.query(user).filter_by(name=session.get("username")).first()
    username = uservar.name

    rustInfo = await socket.get_info()

    map = await socket.get_map(False, False, False)
    image_buffer = BytesIO()
    map.save(image_buffer, format='PNG')
    image_data = base64.b64encode(image_buffer.getvalue()).decode('utf-8')
    html_code = f'<img src="data:image/png;base64,{image_data}" class="img-small">'
    safe_html_code = Markup(html_code)

    name = rustInfo.name
    players = rustInfo.players
    maxPlayers = rustInfo.max_players
    size = rustInfo.size

    return await render_template(
        "server.html", 
        username=username, 
        name=name, 
        players=players, 
        maxPlayers=maxPlayers, 
        size=size,
        map=safe_html_code    
    )
    
@web.route("/bot", methods=["GET", "POST"])
async def bot():
    if "username" not in session:
        return redirect("/")
    
    if request.method == "POST":
        loop = asyncio.get_event_loop()
        loop.create_task(run_rust_bot())

    return await render_template("bot.html")

@web.route("/addBot", methods=["GET", "POST"])
async def addBot():
    if "username" not in session:
        return redirect("/")
    
    if request.method == "POST":
        form = await request.form
        rustToken = form['rustToken']
        steamID = form['steamID']
        ip = form['IP']
        port = form['port']

        tokens = Tokens(
            rustToken=rustToken,
            steamID=steamID,
            IP=ip,
            port=port
        )
        session_instance.add(tokens)
        session_instance.commit()

    return await render_template("addBot.html")

@web.route("/logout", methods=["GET", "POST"])
async def logout():
    if "username" in session:
        session.pop("username", None)
        return redirect("/")
    else: 
        return "You were not logged in"

async def startWeb():
    await web.run_task(host="0.0.0.0", port=8003)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()

    async def startup():
        await startWeb()

    loop.create_task(startup())
    loop.run_forever()