Exemplo n.º 1
0
logger = logging.getLogger(__name__)

item_fields = {
    "item_id": fields.Integer(attribute="id"),
    "item_name": fields.String(attribute="name"),
    "price": fields.Integer(),
    "amount": fields.Integer
}


class Items(Resource):
    @marshal_with(item_fields)
    def get(self):
        items = models.Item.query.all()
        for item in items:
            item.price = item.get_current_price()
        return items


class Item(Resource):
    @marshal_with(item_fields)
    def get(self, item_id=None):
        if item_id is None: abort(404)
        item = models.Item.query.filter_by(id=item_id).first()
        item.price = item.get_current_price()
        return item


api.add_resource(Items, '/items')
api.add_resource(Item, '/items/<int:item_id>')
Exemplo n.º 2
0
        items = {}
        while time < end_time and data_point_index < 100:
            data_point_index += 1
            while next_user_history is not None and time > next_user_history.datetime:
                current_user_history = next_user_history
                index += 1
                next_user_history = user_history[index] if index < len(user_history) else None            
                if current_user_history.item_id not in items:
                    items[current_user_history.item] = 0
                if current_user_history.buy_sell:
                    items[current_user_history.item] += current_user_history.amount
                else: 
                    items[current_user_history.item] -= current_user_history.amount
                    if items[current_user_history.item] == 0:
                        del items[current_user_history.item]
            
            stock_value = sum([item.get_price_at(time) * items[item] for item in items])

            data.append({
                "time" : time,
                "balance" : current_user_history.balance if current_user_history is not None else 100000,
                "stock_value" : stock_value,
                "stock_amount" : sum([items[item] for item in items])
            })
            time += time_delta

        return data

api.add_resource(Users, "/users")
api.add_resource(User, "/users/<int:user_id>")
api.add_resource(UserHistory, "/users/<int:user_id>/history")
Exemplo n.º 3
0
        sell_price = sell_amount * item_price
        if g.current_user.balance + sell_price < transaction_fee:
            abort(
                400,
                f"You cannot afford the transaction fee for this sale. The fee is [{transaction_fee}] and you will have [{g.current_user.balance + sell_price}] after sale."
            )

        try:
            g.current_user.balance += sell_price - transaction_fee
            user_item.item.amount += sell_amount
            user_item.amount -= sell_amount
            if user_item.amount == 0:
                db.session.delete(user_item)
            user_log = UserLog(user_id=g.current_user.id,
                               item_id=item_id,
                               amount=sell_amount,
                               buy_sell=True,
                               datetime=datetime.datetime.now(),
                               balance=g.current_user.balance)
            db.session.add(user_log)
            db.session.commit()
            return 200

        except Exception as e:
            db.session.rollback()
            logger.error(e)
            abort(500)


api.add_resource(Sell, "/sell")
Exemplo n.º 4
0
                win_user = models.User.query.filter_by(
                    id=game.win_user_id).first()
                if win_user is None:
                    logger.debug("Can't find the winner in the database.")
                else:
                    win_user.games_won = win_user.games_won + 1

                lose_user_id = game.host_user_id if game.host_user_id != game.win_user_id else game.guest_user_id
                lose_user = models.User.query.filter_by(
                    id=lose_user_id).first()
                if lose_user is None:
                    logger.debug("Can't find the loser in the database.")
                else:
                    lose_user.games_lost = lose_user.games_lost + 1
        else:
            game.next_user_id = convert_player_id(game, next_player_id)

        db.session.commit()


def convert_player_id(game, player_id):
    if game.host_goes_first:
        return game.host_user_id if player_id == 0 else game.guest_user_id
    else:
        return game.guest_user_id if player_id == 0 else game.host_user_id


api.add_resource(Games, "/games")
api.add_resource(Game, "/games/<int:game_id>")
api.add_resource(Turns, "/games/<int:game_id>/turns")
Exemplo n.º 5
0
        user_name = request.json.get('user_name')
        if user_name is None: abort(400, "No [user_name] provided.")
        if models.User.query.filter_by(user_name=user_name).count() > 0:
            abort(400, "[user_name] already exists.")
        try:
            user = models.User(user_name=user_name,
                               token=str(uuid.uuid4()),
                               datetime_created=datetime.datetime.now())
            db.session.add(user)
            db.session.commit()
            return user, 201
        except Exception as e:
            logger.error(e)
            abort(500)

    @auth.login_required
    def delete(self):
        db.session.delete(g.current_user)


class User(Resource):
    @marshal_with(user_fields)
    def get(self, user_id=None):
        if user_id is None: abort(400, "Must provide a user id")
        user = models.User.query.filter_by(id=user_id).first()
        if user is None: abort(400, "No user found with that id.")
        return user, 200


api.add_resource(Users, "/users")
api.add_resource(User, "/users/<int:user_id>")
Exemplo n.º 6
0
            user_item = UserItem.query.filter_by(user_id=g.current_user.id,
                                                 item_id=item_id).first()
            if user_item is None:
                user_item = UserItem(user_id=g.current_user.id,
                                     item_id=item_id,
                                     amount=purchase_amount)
                db.session.add(user_item)
            else:
                user_item.amount += purchase_amount

            g.current_user.balance -= purchase_cost
            item.amount -= purchase_amount

            user_log = UserLog(user_id=g.current_user.id,
                               item_id=item_id,
                               amount=purchase_amount,
                               buy_sell=True,
                               datetime=datetime.datetime.now(),
                               balance=g.current_user.balance)
            db.session.add(user_log)
            db.session.commit()
            return 200

        except Exception as e:
            db.session.rollback()
            logger.exception(e)
            abort(500)


api.add_resource(Buy, "/buy")