reworking the project on django
This commit is contained in:
parent
98f8b51a42
commit
3dfd277b99
BIN
old/Server/__pycache__/databasefornow.cpython-36.pyc
Normal file
BIN
old/Server/__pycache__/databasefornow.cpython-36.pyc
Normal file
Binary file not shown.
1
old/Server/currency.txt
Normal file
1
old/Server/currency.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
6969
|
7
old/Server/databasefornow.py
Normal file
7
old/Server/databasefornow.py
Normal 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
|
39
old/Server/websocketServer.py
Normal file
39
old/Server/websocketServer.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
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)
|
||||||
|
if requestText == "STATUSONCOIN":
|
||||||
|
file = open("currency.txt","r")
|
||||||
|
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")
|
||||||
|
print(name)
|
||||||
|
await websocket.send(f"Your balance is: {balance}")
|
||||||
|
await websocket.send(f"Welcome, {name}!")
|
||||||
|
else:
|
||||||
|
await websocket.send("We don't accept DoS")
|
||||||
|
|
||||||
|
|
||||||
|
start_server = websockets.serve(serveStuff, "localhost", 8765)
|
||||||
|
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server)
|
||||||
|
asyncio.get_event_loop().run_forever()
|
35
old/Website/css/home.css
Normal file
35
old/Website/css/home.css
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
@import url(https://fonts.googleapis.com/css?family=Roboto:300);
|
||||||
|
|
||||||
|
:root {
|
||||||
|
--base-grid: 8px;
|
||||||
|
--colour-white: #fff;
|
||||||
|
--colour-black: #1a1a1a;
|
||||||
|
}
|
||||||
|
@viewport {
|
||||||
|
width: device-width ;
|
||||||
|
zoom: 1.0 ;
|
||||||
|
}
|
||||||
|
|
||||||
|
*,
|
||||||
|
:after,
|
||||||
|
:before {
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
html {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
background-position: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
position: absolute;
|
||||||
|
left: 50%;
|
||||||
|
margin-right: -50%;
|
||||||
|
transform: translate(-50%, -50%);
|
||||||
|
top: 50%;
|
||||||
|
font-size: 1.2em;
|
||||||
|
font-family: 'Roboto', sans-serif;
|
||||||
|
line-height: 1.4;
|
||||||
|
|
||||||
|
}
|
18
old/Website/index.html
Normal file
18
old/Website/index.html
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en" dir="ltr">
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet" href="css/home.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">
|
||||||
|
<title>Currency site</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1 id="odometer" class="odometer">This is where you'll see the thing</h1>
|
||||||
|
<script type="text/javascript" src="scripts/getCurrentCoins.js"></script>
|
||||||
|
<br>
|
||||||
|
<h2 id="balance">Your balance is:</h2>
|
||||||
|
<h2 id="username"></h2>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
1
old/Website/scripts/Cookies.js
Normal file
1
old/Website/scripts/Cookies.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
document.cookie = "id=1";
|
37
old/Website/scripts/getCurrentCoins.js
Normal file
37
old/Website/scripts/getCurrentCoins.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
socketAdress="ws://localhost:8765"
|
||||||
|
// Lacking protocol for ids lmao
|
||||||
|
document.cookie = "id=2";
|
||||||
|
|
||||||
|
function currencyTicker(){
|
||||||
|
var ws = new WebSocket(socketAdress);
|
||||||
|
|
||||||
|
ws.onopen = function(){
|
||||||
|
console.log("Connection is Established");
|
||||||
|
ws.send("STATUSONCOIN");
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onmessage = function(evt) {
|
||||||
|
var received_msg = evt.data;
|
||||||
|
w = document.getElementById("odometer").innerHTML = received_msg;
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
w = document.getElementById("balance").innerHTML = balanceStatus;
|
||||||
|
ws.onmessage = function(evt){
|
||||||
|
w = document.getElementById("username").innerHTML = evt.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
var t=setInterval(currencyTicker,5000);
|
||||||
|
var y=setInterval(userBalance,5000);
|
Reference in New Issue
Block a user