Skip to content
This repository has been archived by the owner on Nov 24, 2024. It is now read-only.

Commit

Permalink
db credit money
Browse files Browse the repository at this point in the history
  • Loading branch information
Olegogonich committed Nov 23, 2024
1 parent 8b114ec commit ce539f0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion apps/backend/src/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
db.connect()

db.cursor.execute('''CREATE TABLE users
(id TEXT, money INTEGER, history TEXT)''')
(id TEXT, money INTEGER, credit INTEGER, history TEXT)''')
db.conn.commit()

db.close_db()
16 changes: 12 additions & 4 deletions apps/backend/src/lib/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from flask import Flask, request, make_response, Response, g
from flask_cors import CORS, cross_origin

from apps.backend.src.lib.classes import Event, User
from lib.utils import get_latest_event_from_history
from lib.db import DB
from lib.events import events
Expand All @@ -22,6 +23,9 @@ def get_db() -> DB:
db.connect()
return db

def make_on_event_json_response(event: Event, user: User) -> dict:
return {"event": event.to_json(), "user": user.to_json()}

@app.after_request
def apply_caching(response: Response):
response.headers['Access-Control-Allow-Credentials'] = 'true'
Expand Down Expand Up @@ -50,30 +54,34 @@ def get_data():

db = get_db()
user = db.get_user(user_id)
print(f"user: {user.to_json()}")

if not data:
event = get_random_event()
print(f"returned event: {event.to_json()}")

db.add_event_to_user_history(_id=user_id, event_id=event._id)
return event.to_json()
return make_on_event_json_response(event, user)

anwser_id = data.get('answer_id')
anwser_id = data.get('clickedId')

event = get_latest_event_from_history(user.history)
answer = event.answers.get(anwser_id)

db.set_user_money(_id=user_id, money=user.money + answer.delta_money - user.credit * 0.1)
db.set_user_credit(_id=user_id, credit=user.credit * 0.9 if user.credit * 0.1 >= 20 else 0)

db.add_answer_to_user_history(_id=user_id, answer_id=answer._id)

print(f"user: {user.to_json()}")

print(f"answer: {answer.to_json()}")

event = get_random_event()

print(f"returned event: {event.to_json()}")

db.add_event_to_user_history(_id=user_id, event_id=event._id)
return event.to_json()
return make_on_event_json_response(event, user)


@app.route("/create_user", methods=['POST'])
Expand Down
8 changes: 5 additions & 3 deletions apps/backend/src/lib/classes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class User:
def __init__(self, data):
self._id, self.money, self.history = data
self._id, self.money, self.credit, self.history = data

def to_json(self):
return {'_id': self._id, 'money': self.money, 'history': self.history}
return {'_id': self._id, 'money': self.money, 'credit': self.credit, 'history': self.history, 'image': 'home'}


class Event:
Expand All @@ -23,10 +23,11 @@ def to_json(self):


class Answer:
def __init__(self, _id, text, chance_bad, delta_money=0, analytic_text=""):
def __init__(self, _id, text, chance_bad=0, credit=0, delta_money=0, analytic_text=""):
self._id = _id
self.text = text
self.chance_bad = chance_bad
self.credit = credit
self.delta_money = delta_money
self.analytic_text = analytic_text

Expand All @@ -35,6 +36,7 @@ def to_json(self):
"id": self._id,
"text": self.text,
"chance_bad": self.chance_bad,
"credit": self.credit,
"delta_money": self.delta_money,
"analytic_text": self.analytic_text
}
Expand Down
14 changes: 11 additions & 3 deletions apps/backend/src/lib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def connect(self):
def close_db(self):
self.conn.close()

def create_user(self, _id, money, history=""):
self.cursor.execute('''INSERT INTO users (id, money, history) VALUES (?, ?, ?)''', (_id, money, history))
def create_user(self, _id, money, credit=0, history=""):
self.cursor.execute('''INSERT INTO users (id, money, credit, history) VALUES (?, ?, ?, ?)''', (_id, money, credit, history))
self.conn.commit()

def get_user(self, _id) -> User:
Expand All @@ -35,10 +35,18 @@ def get_user_history(self, _id):
history = self.cursor.execute('''SELECT history FROM users where id = ?''', (_id,))
return history.fetchone()[0]

def set_user_history(self, _id, history,):
def set_user_history(self, _id, history):
self.cursor.execute('''UPDATE users SET history = ? WHERE id = ?''', (history, _id))
self.conn.commit()

def set_user_money(self, _id, money):
self.cursor.execute('''UPDATE users SET money = ? WHERE id = ?''', (money, _id))
self.conn.commit()

def set_user_credit(self, _id, credit):
self.cursor.execute('''UPDATE users SET credit = ? WHERE id = ?''', (credit, _id))
self.conn.commit()

def add_event_to_user_history(self, _id, event_id):
prev_history = self.get_user_history(_id)

Expand Down
8 changes: 6 additions & 2 deletions apps/backend/src/lib/events.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from lib.classes import Event, Answer

events = {
events_good = {
"1": Event(
_id="1",
description="У вас есть немного лишних денег, что Вы предпримете для улучшения своего бизнеса?",
Expand Down Expand Up @@ -36,7 +36,7 @@
answers={
"1": Answer(_id="1", text="Заказать рекламу у популярного блогера", chance_bad=0, delta_money=-5000),
"2": Answer(_id="2", text="Принять участие в городской ярмарке", chance_bad=20)
}
}
),
"5": Event(
_id="5",
Expand Down Expand Up @@ -95,4 +95,8 @@
}
),
}
events_bad = {

}

events = {**events_good, **events_bad}

0 comments on commit ce539f0

Please sign in to comment.