mirror of
https://git.wownero.com/wowlet/wowlet-backend.git
synced 2024-08-15 01:03:13 +00:00
Initial support for a trollbox
This commit is contained in:
parent
e79059820f
commit
0619522bb1
6 changed files with 104 additions and 12 deletions
|
@ -7,6 +7,7 @@ import asyncio
|
|||
from typing import List, Set
|
||||
from datetime import datetime
|
||||
|
||||
from asyncio_multisubscriber_queue import MultisubscriberQueue
|
||||
from quart import Quart
|
||||
from quart_session import Session
|
||||
import aioredis
|
||||
|
@ -18,7 +19,7 @@ now = datetime.now()
|
|||
app: Quart = None
|
||||
cache = None
|
||||
user_agents: List[str] = None
|
||||
connected_websockets: Set[asyncio.Queue] = set()
|
||||
broadcast = MultisubscriberQueue()
|
||||
_is_primary_worker_thread = False
|
||||
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ from quart import websocket, jsonify, send_from_directory
|
|||
import settings
|
||||
from wowlet_backend.factory import app
|
||||
from wowlet_backend.wsparse import WebsocketParse
|
||||
from wowlet_backend.utils import collect_websocket, feather_data
|
||||
from wowlet_backend.utils import broadcast, feather_data
|
||||
|
||||
|
||||
@app.route("/")
|
||||
|
@ -28,8 +28,7 @@ async def suchwow(name: str):
|
|||
|
||||
|
||||
@app.websocket('/ws')
|
||||
@collect_websocket
|
||||
async def ws(queue):
|
||||
async def ws():
|
||||
data = await feather_data()
|
||||
|
||||
# blast available data on connect
|
||||
|
@ -56,7 +55,7 @@ async def ws(queue):
|
|||
|
||||
async def tx():
|
||||
while True:
|
||||
data = await queue.get()
|
||||
data = await broadcast.get()
|
||||
payload = json.dumps(data).encode()
|
||||
await websocket.send(payload)
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ class WowletTask:
|
|||
self._running = False
|
||||
|
||||
async def start(self, *args, **kwargs):
|
||||
from wowlet_backend.factory import app, connected_websockets
|
||||
from wowlet_backend.factory import app, broadcast
|
||||
if not self._active:
|
||||
# invalid task
|
||||
return
|
||||
|
@ -94,11 +94,10 @@ class WowletTask:
|
|||
propagate = False
|
||||
|
||||
if propagate:
|
||||
for queue in connected_websockets:
|
||||
await queue.put({
|
||||
"cmd": self._websocket_cmd,
|
||||
"data": result
|
||||
})
|
||||
await broadcast.put({
|
||||
"cmd": self._websocket_cmd,
|
||||
"data": result
|
||||
})
|
||||
|
||||
# optional: cache the result
|
||||
if self._cache_key and result:
|
||||
|
|
26
wowlet_backend/trollbox.py
Normal file
26
wowlet_backend/trollbox.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
import re, sys, os
|
||||
from datetime import datetime
|
||||
import time
|
||||
|
||||
from wowlet_backend.factory import broadcast
|
||||
|
||||
HISTORY = []
|
||||
|
||||
|
||||
async def add_chat(message: str, balance: float = 0):
|
||||
global HISTORY
|
||||
item = {
|
||||
"message": message,
|
||||
"balance": balance,
|
||||
"date": int(time.time())
|
||||
}
|
||||
|
||||
HISTORY.append(item)
|
||||
if len(HISTORY) >= 25:
|
||||
HISTORY = HISTORY[:25]
|
||||
|
||||
await broadcast.put({
|
||||
"cmd": "trollEntry",
|
||||
"data": item
|
||||
})
|
||||
|
|
@ -161,3 +161,54 @@ async def image_resize(buffer: bytes, max_bounding_box: int = 512, quality: int
|
|||
buffer.seek(0)
|
||||
|
||||
return buffer.read()
|
||||
|
||||
|
||||
async def whaleornot(amount: float):
|
||||
if amount <= 0:
|
||||
fish_str = "amoeba"
|
||||
elif amount < 100:
|
||||
fish_str = "plankton"
|
||||
elif amount >= 100 and amount < 200:
|
||||
fish_str = "Paedocypris"
|
||||
elif amount >= 200 and amount < 500:
|
||||
fish_str = "Dwarf Goby"
|
||||
elif amount >= 500 and amount < 1000:
|
||||
fish_str = "European Pilchard"
|
||||
elif amount >= 1000 and amount < 2000:
|
||||
fish_str = "Goldfish"
|
||||
elif amount >= 2000 and amount < 4000:
|
||||
fish_str = "Herring"
|
||||
elif amount >= 4000 and amount < 7000:
|
||||
fish_str = "Atlantic Macerel"
|
||||
elif amount >= 7000 and amount < 9000:
|
||||
fish_str = "Gilt-head Bream"
|
||||
elif amount >= 9000 and amount < 12000:
|
||||
fish_str = "Salmonidae"
|
||||
elif amount >= 12000 and amount < 20000:
|
||||
fish_str = "Gadidae"
|
||||
elif amount >= 20000 and amount < 40000:
|
||||
fish_str = "Norwegian Delicious Salmon"
|
||||
elif amount >= 40000 and amount < 60000:
|
||||
fish_str = "Electric eel"
|
||||
elif amount >= 60000 and amount < 80000:
|
||||
fish_str = "Tuna"
|
||||
elif amount >= 80000 and amount < 100000:
|
||||
fish_str = "Wels catfish"
|
||||
elif amount >= 100000 and amount < 120000:
|
||||
fish_str = "Black marlin"
|
||||
elif amount >= 120000 and amount < 160000:
|
||||
fish_str = "Shark"
|
||||
elif amount >= 160000 and amount < 220000:
|
||||
fish_str = "Dolphin"
|
||||
elif amount >= 220000 and amount < 320000:
|
||||
fish_str = "Narwhal"
|
||||
elif amount >= 320000 and amount < 500000:
|
||||
fish_str = "Orca"
|
||||
elif amount >= 500000 and amount < 700000:
|
||||
fish_str = "Blue Whale"
|
||||
elif amount >= 700000 and amount < 1000000:
|
||||
fish_str = "Leviathan"
|
||||
else:
|
||||
fish_str = "Cthulu"
|
||||
return fish_str
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ from typing import Dict, Union, Optional
|
|||
from copy import deepcopy
|
||||
import asyncio
|
||||
import re
|
||||
from wowlet_backend.trollbox import add_chat
|
||||
|
||||
from wowlet_backend.utils import RE_ADDRESS
|
||||
|
||||
|
@ -27,6 +28,21 @@ class WebsocketParse:
|
|||
return await WebsocketParse.requestPIN(data)
|
||||
elif cmd == "lookupPIN":
|
||||
return await WebsocketParse.lookupPIN(data)
|
||||
elif cmd == "trollbox":
|
||||
pass
|
||||
#return await WebsocketParse.addTrollChat(data)
|
||||
|
||||
@staticmethod
|
||||
async def addTrollChat(data):
|
||||
if not data or not isinstance(data, dict):
|
||||
return
|
||||
for needle in ['message', 'balance']:
|
||||
if needle not in data:
|
||||
return
|
||||
|
||||
message = data['message']
|
||||
balance = data['balance']
|
||||
await add_chat(message, balance)
|
||||
|
||||
@staticmethod
|
||||
async def txFiatHistory(data=None):
|
||||
|
|
Loading…
Reference in a new issue