From 3dfd277b99bb080eff94b79832aa34a2bb2dc609 Mon Sep 17 00:00:00 2001 From: Yamozha Date: Fri, 5 Feb 2021 00:57:22 +0200 Subject: [PATCH] reworking the project on django --- .../__pycache__/databasefornow.cpython-36.pyc | Bin 0 -> 393 bytes old/Server/currency.txt | 1 + old/Server/databasefornow.py | 7 ++++ old/Server/websocketServer.py | 39 ++++++++++++++++++ old/Website/css/home.css | 35 ++++++++++++++++ old/Website/index.html | 18 ++++++++ old/Website/scripts/Cookies.js | 1 + old/Website/scripts/getCurrentCoins.js | 37 +++++++++++++++++ 8 files changed, 138 insertions(+) create mode 100644 old/Server/__pycache__/databasefornow.cpython-36.pyc create mode 100644 old/Server/currency.txt create mode 100644 old/Server/databasefornow.py create mode 100644 old/Server/websocketServer.py create mode 100644 old/Website/css/home.css create mode 100644 old/Website/index.html create mode 100644 old/Website/scripts/Cookies.js create mode 100644 old/Website/scripts/getCurrentCoins.js diff --git a/old/Server/__pycache__/databasefornow.cpython-36.pyc b/old/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/old/Server/currency.txt b/old/Server/currency.txt new file mode 100644 index 00000000..8ae1164a --- /dev/null +++ b/old/Server/currency.txt @@ -0,0 +1 @@ +6969 diff --git a/old/Server/databasefornow.py b/old/Server/databasefornow.py new file mode 100644 index 00000000..910686fe --- /dev/null +++ b/old/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/old/Server/websocketServer.py b/old/Server/websocketServer.py new file mode 100644 index 00000000..d40f6b35 --- /dev/null +++ b/old/Server/websocketServer.py @@ -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() diff --git a/old/Website/css/home.css b/old/Website/css/home.css new file mode 100644 index 00000000..82b65800 --- /dev/null +++ b/old/Website/css/home.css @@ -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; + +} diff --git a/old/Website/index.html b/old/Website/index.html new file mode 100644 index 00000000..72940815 --- /dev/null +++ b/old/Website/index.html @@ -0,0 +1,18 @@ + + + + + + + + Currency site + + +

This is where you'll see the thing

+ +
+

Your balance is:

+

+ + + diff --git a/old/Website/scripts/Cookies.js b/old/Website/scripts/Cookies.js new file mode 100644 index 00000000..1983dbd1 --- /dev/null +++ b/old/Website/scripts/Cookies.js @@ -0,0 +1 @@ +document.cookie = "id=1"; diff --git a/old/Website/scripts/getCurrentCoins.js b/old/Website/scripts/getCurrentCoins.js new file mode 100644 index 00000000..d8fe0728 --- /dev/null +++ b/old/Website/scripts/getCurrentCoins.js @@ -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);