MateOverMongoDB

This commit is contained in:
hyperbel 2023-07-14 08:52:37 +02:00
parent f44ad58d73
commit 34ff3b6382
2 changed files with 38 additions and 7 deletions

View File

@ -1,7 +1,7 @@
from flask import Flask, jsonify, render_template, session, redirect
from flask_socketio import SocketIO, send, emit
from http import HTTPStatus
from mate import generate_kiste
from mate import generate_kiste, MateKiste
from chat import Chat
import bson.json_util as json_util
@ -57,7 +57,7 @@ def api_mate():
@app.route('/api/mate/status', methods=['GET'])
def api_mate_status():
FLASCHE_COUNT = 20
response = jsonify(kiste=generate_kiste(FLASCHE_COUNT))
response = jsonify(kiste=MateKiste.getStatus())
response.status_code = 200
return response
@ -87,8 +87,9 @@ def handle_chat_get_message(data):
@socketio.on('mate-status')
def handle_mate_status():
FLASCHE_COUNT = 20
return { 'data': generate_kiste(FLASCHE_COUNT) }
status = MateKiste.getStatus()
print(status)
return { 'data': status }
@socketio.on('connect')
def handle_connect():
@ -130,5 +131,5 @@ def page_not_found(error):
if __name__ == '__main__':
Chat.init()
MateKiste.init()
socketio.run(app, debug=True, host='localhost', port=5000) #pragma: no cover

View File

@ -8,5 +8,35 @@ def generate_kiste(count: int) -> list[bool]:
class MateKiste():
def __init__(self):
pass
status = None
CONNECTION_STRING = None
client = None
collection = None
initialized = False
FlaschenBreite = 5
FlaschenHoehe = 4
@classmethod
def init(self, flaschenAnzahl: tuple[int, int] = None):
self.CONNECTION_STRING = "mongodb://localhost:27017/"
self.client = MongoClient(self.CONNECTION_STRING)['partyyy']
self.collection = self.client['mate']
self.collection.delete_many({}) # delete all
if flaschenAnzahl is not None:
self.FlaschenBreite = flaschenAnzahl[0]
self.FlaschenHoehe = flaschenAnzahl[1]
flaschenAnzahl = self.FlaschenBreite * self.FlaschenHoehe
self.collection.insert_one({'mateKiste': True, 'status': generate_kiste(flaschenAnzahl)})
self.initialized = True
@classmethod
def getStatus(self) -> list[bool]:
return list(self.collection.find_one()['status']) # wir haben nur 1 ding da drinne, was geupdated wird
@classmethod
def removeAt(self, x: int, y: int):
self.collection.update_one({'mateKiste': True}, {'$set': {f'status.{y * self.FlaschenBreite + x}': False}})