From 91e0557b205c4d5fa3db727d049da04545b96838 Mon Sep 17 00:00:00 2001 From: Yamozha Date: Wed, 3 Feb 2021 18:45:10 +0200 Subject: [PATCH] included "database" and primitive balance checking let's make it so we have a way to integrate it with sql in later stages. PLEASE FIX the "fstring" in the js and the id to balance sequence --- .../__pycache__/databasefornow.cpython-36.pyc | Bin 0 -> 393 bytes Server/databasefornow.py | 7 ++++++ Server/websocketServer.py | 14 ++++++++++- Website/index.html | 4 ++- Website/scripts/Cookies.js | 1 + Website/scripts/getCurrentCoins.js | 23 ++++++++++++++++-- 6 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 Server/__pycache__/databasefornow.cpython-36.pyc create mode 100644 Server/databasefornow.py create mode 100644 Website/scripts/Cookies.js diff --git a/Server/__pycache__/databasefornow.cpython-36.pyc b/Server/__pycache__/databasefornow.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..88fdde9712271042c6fd8b651d0aecc4f2b00033 GIT binary patch literal 393 zcmYjN%TB{E5L`Qbgou6shy&MLa9XYiAzBF`q+B8n_=Ib_G)fy+c3KovZit_NAK+`? zoGX99iFE{swPrN(?2gtx9t@)AkNw+s0Qf}wRe9`@U rj^DnA1SG;^`>4`5u>N({o9#`z#Xs@`O?a|2YQ4~>{zshe#RU2dwfbEv literal 0 HcmV?d00001 diff --git a/Server/databasefornow.py b/Server/databasefornow.py new file mode 100644 index 00000000..910686fe --- /dev/null +++ b/Server/databasefornow.py @@ -0,0 +1,7 @@ +userNames = {"1":"boyan", "2":"nikola"} +usersCoins = {"1":"200", "2":"100"} + +def checkBalance(ID): + userName = userNames[f"{ID}"] + userCoins = usersCoins[f"{ID}"] + return userName, userCoins diff --git a/Server/websocketServer.py b/Server/websocketServer.py index c2868feb..95d22d6d 100644 --- a/Server/websocketServer.py +++ b/Server/websocketServer.py @@ -1,12 +1,19 @@ import asyncio import websockets import logging - +from ratelimit import limits +import time +from databasefornow import * logger = logging.getLogger('websockets') logger.setLevel(logging.INFO) logger.addHandler(logging.StreamHandler()) +print("Ready") + +# calls in number of requests +# period in seconds +@limits(calls=20, period=5) async def serveStuff(websocket, path): requestText = await websocket.recv() print(requestText) @@ -15,6 +22,11 @@ async def serveStuff(websocket, path): currencyStatus = file.readline() file.close() await websocket.send(currencyStatus) + + # bad way to send balance to given user - FIX!!! + elif requestText == "STATUSONUSER": + name, balance = checkBalance("1") + await websocket.send(balance) else: await websocket.send("We don't accept DoS") diff --git a/Website/index.html b/Website/index.html index 416899c3..03705454 100644 --- a/Website/index.html +++ b/Website/index.html @@ -3,12 +3,14 @@ + Currency site -

This is where you'll see the thing

+

Your balance is:

+ diff --git a/Website/scripts/Cookies.js b/Website/scripts/Cookies.js new file mode 100644 index 00000000..1983dbd1 --- /dev/null +++ b/Website/scripts/Cookies.js @@ -0,0 +1 @@ +document.cookie = "id=1"; diff --git a/Website/scripts/getCurrentCoins.js b/Website/scripts/getCurrentCoins.js index 589157b9..4664e088 100644 --- a/Website/scripts/getCurrentCoins.js +++ b/Website/scripts/getCurrentCoins.js @@ -1,5 +1,9 @@ +socketAdress="ws://localhost:8765" +// Lacking protocol for ids lmao +document.cookie = "id=1"; + function currencyTicker(){ - var ws = new WebSocket("ws://localhost:8765/"); + var ws = new WebSocket(socketAdress); ws.onopen = function(){ console.log("Connection is Established"); @@ -12,5 +16,20 @@ function currencyTicker(){ }; } -currencyTicker() + +function userBalance(){ + var ws = new WebSocket(socketAdress); + ws.onopen = function(){ + ws.send("STATUSONUSER"); + }; + + ws.onmessage = function(evt){ + var balanceStatus = evt.data; + // this doesnt work, fix pls + var websiteReplace = "Your balance is:${balanceStatus}" + w = document.getElementById("balance").innerHTML = websiteReplace + } + +} var t=setInterval(currencyTicker,5000); +var y=setInterval(userBalance,10000);