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
This commit is contained in:
Yamozha 2021-02-03 18:45:10 +02:00
parent 9de43a3ba5
commit 91e0557b20
6 changed files with 45 additions and 4 deletions

Binary file not shown.

7
Server/databasefornow.py Normal file
View File

@ -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

View File

@ -1,12 +1,19 @@
import asyncio import asyncio
import websockets import websockets
import logging import logging
from ratelimit import limits
import time
from databasefornow import *
logger = logging.getLogger('websockets') logger = logging.getLogger('websockets')
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler()) logger.addHandler(logging.StreamHandler())
print("Ready")
# calls in number of requests
# period in seconds
@limits(calls=20, period=5)
async def serveStuff(websocket, path): async def serveStuff(websocket, path):
requestText = await websocket.recv() requestText = await websocket.recv()
print(requestText) print(requestText)
@ -15,6 +22,11 @@ async def serveStuff(websocket, path):
currencyStatus = file.readline() currencyStatus = file.readline()
file.close() file.close()
await websocket.send(currencyStatus) 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: else:
await websocket.send("We don't accept DoS") await websocket.send("We don't accept DoS")

View File

@ -3,12 +3,14 @@
<head> <head>
<link rel="stylesheet" href="css/home.css"> <link rel="stylesheet" href="css/home.css">
<link rel="stylesheet" href="http://github.hubspot.com/odometer/themes/odometer-theme-default.css"> <link rel="stylesheet" href="http://github.hubspot.com/odometer/themes/odometer-theme-default.css">
<script src="http://github.hubspot.com/odometer/odometer.js"></script>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Currency site</title> <title>Currency site</title>
</head> </head>
<body> <body>
<script src="http://github.hubspot.com/odometer/odometer.js"></script>
<h1 id="odometer" class="odometer">This is where you'll see the thing</h1> <h1 id="odometer" class="odometer">This is where you'll see the thing</h1>
<script type="text/javascript" src="scripts/getCurrentCoins.js"></script> <script type="text/javascript" src="scripts/getCurrentCoins.js"></script>
<h2 id="balance">Your balance is:</h2>
</body> </body>
</html> </html>

View File

@ -0,0 +1 @@
document.cookie = "id=1";

View File

@ -1,5 +1,9 @@
socketAdress="ws://localhost:8765"
// Lacking protocol for ids lmao
document.cookie = "id=1";
function currencyTicker(){ function currencyTicker(){
var ws = new WebSocket("ws://localhost:8765/"); var ws = new WebSocket(socketAdress);
ws.onopen = function(){ ws.onopen = function(){
console.log("Connection is Established"); 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 t=setInterval(currencyTicker,5000);
var y=setInterval(userBalance,10000);