def get(self): try: task = Task.objects.get(solver=current_user) except DoesNotExist: return render_json({'type': 'error', 'message': u'Для остановки времени нужно решать таск.'}) try: duration = abs(int(request.args.get('duration', None))) except ValueError: return render_json({'type': 'error', 'message': u'Неверное время заморозки'}) cost = duration * 10 / 60 # calculate cost from freeze duration if current_user.get_money() < cost: return render_json({'type': 'error', 'message': u'Недстаточно денег!'}) else: if task.expired(): return render_json({'type': 'error', 'message': u'Таск уже просрочен!'}) freeze = Freeze.current(task) if freeze: # if we have mo then one added freeze in future append to end freezes = Freeze.objects(created_at__gt=freeze.created_at).order_by('-created_at') if len(freezes): freeze = freezes[0] created = freeze.created_at + datetime.timedelta(seconds=freeze.duration) else: created = datetime.datetime.now() current_user.money -= cost current_user.save() freeze = Freeze(created_at=created, user=current_user.to_dbref(), task=task, duration=duration) freeze.save() return render_json({'type': 'success', 'message': u'Время таска заморожено!'})
def get(self): task_id = request.args.get('id') try: task = Task.objects.get(id=task_id) except ValidationError: return render_json({'type': 'error', 'message': u'Неверный таск'}) if task in current_user.solved_tasks: return render_json({'type': 'error', 'message': u'Невозможно начать уже решенный таск'}) if task in current_user.closed_tasks: return render_json({'type': 'error', 'message': u'Невозможно начать проваленный таск'}) if task.solver: return render_json({'type': 'error', 'message': u'Таск уже решают'}) if task.owner: cost = task.cost else: cost = task.base_cost if current_user.get_money() < cost: return render_json({'type': 'error', 'message': u'Денег нет?'}) if len(Task.objects(solver=current_user.id)): return render_json({'type': 'error', 'message': u'Вы уже решаете таск, сначала завершите его.'}) current_user.money -= cost current_user.task_started_at = datetime.datetime.now() current_user.save() print 'open task:', current_user.task_started_at, datetime.datetime.now() task.solver = current_user.to_dbref() task.save() return render_json({'type': 'success', 'message': u'Вы начали таск {}. Вы должны решить его за {} сек.'.format(task.name, task.base_time)})
def get(self): task_id = request.args.get('id', None) try: cost = abs(int(request.args.get('cost', None))) except ValueError: return render_json({'type': 'error', 'message': u'Неверная стоимость таска'}) try: task = Task.objects.get(id=task_id) except DoesNotExist: return render_json({'type': 'error', 'message': u'Неверный таск id'}) if task in current_user.solved_tasks: if current_user.get_money() < cost: return render_json({'type': 'error', 'message': u'Недостаточно денег!'}) diff = (datetime.datetime.now() - task.last_solved_at) current_user.money += (diff.seconds // 60) * task.cost * 0.02 current_user.money -= cost print('SetCoin: Current money - ' + current_user.money) current_user.save() task.last_solved_at = datetime.datetime.now() task.cost += cost task.save() return render_json({'type': 'success', 'message': u'Стоимость таска обновлена!'}) else: return render_json({'type': 'error', 'message': u'Стоимость таска может менять только его хозяин.'})
def index(): form = BiddingForm() yourBids = Bid.query.filter_by(user_id=current_user.username).all() allVegetables = Vegi.query.all() if request.method == 'POST': if form.validate_on_submit(): if Vegi.query.filter_by( vegetable=form.vegetable.data).first() is None: flash('VERBOTEN!! (vegi not found)') return redirect(url_for('index')) if current_user.get_money() < form.amount.data: flash('VERBOTEN!! (not enough izhk)') return redirect(url_for('index')) prevBid = Bid.query.filter_by( vegetable=form.vegetable.data).first() if prevBid is not None: current_user.set_money(current_user.get_money() + prevBid.get_amount()) current_user.set_money(current_user.get_money() - form.amount.data) prevBid.set_amount(form.amount.data) db.session.commit() else: myBid = Bid(amount=form.amount.data, vegetable=form.vegetable.data, user_id=current_user.username) current_user.set_money(current_user.get_money() - form.amount.data) db.session.add(myBid) db.session.commit() flash('Crangrats! Your bid is placed!') return render_template('index.html', title='Home', form=form, yourBids=yourBids, allVegetables=allVegetables)
def get(self): user = jsonify(current_user, include=['id'], exclude=['password_hash']) user['money'] = current_user.get_money() user['time'] = str(datetime.datetime.now()) return render_json(user)