Exemple #1
0
def manufacture_shoe(request):
    '''
    Check user login
    Check plantId, playerId, sole, body, color, sellprice, qty exist in querydict (request.GET)
    Check plantId belongs to account
    Check shoeId belongs to plant
    Get plant by plantId
    '''

    if request.session.__contains__('account'):
        args = request.GET
        if args.__contains__('sole') and args.__contains__('body') and args.__contains__('color') and args.__contains__('playerId') and args.__contains__('plantId') and args.__contains__('sellprice') and args.__contains__('qty'):
            _sole = int(args.__getitem__('sole'))
            _body = int(args.__getitem__('body'))
            _color= int(args.__getitem__('color'))
            _qty= int(args.__getitem__('qty'))
            _sellprice = float(args.__getitem__('sellprice'))
            _plantId = int(args.__getitem__('plantId'))
            _playerId = int(args.__getitem__('playerId'))
            
            plant = Plant.get_by_id(_plantId)
            if plant != None and plant.playerId == _playerId:
                if _body < 10 or _sole < 10 or _color < 10:
                    return HttpResponse("Minimum shoe body, sole and color is 10")
                player = Player.get_by_id(_playerId)
                _costPrice = _body * shoeMatCost['body'] + _sole * shoeMatCost['sole'] + _color * shoeMatCost['color']
                _totalCost = _costPrice * _qty
                ##check if player has money
                hasMoney = (_totalCost <= player.money)
                ##has enough capacity
                hasCapacity = (_qty+plant.currentCapacity <= plant.productionLimit)
                if hasMoney == True and hasCapacity == True:
                    shoe = Shoe(plantId = _plantId, sole = _sole, body = _body, color = _color, price = _sellprice, qty = _qty, costPrice = _costPrice)
                    shoe.put()
                    player.money -= _totalCost
                    player.money_spent += _totalCost
                    player.networth = player.money + player.money_spent
                    player.put()
                    plant.currentCapacity += _qty
                    plant.put()
                    supTrxn = SupplyTransaction(shoeId = shoe.key().id(), costPerUnit = _costPrice, qty = _qty, totalCost = _totalCost, trxnDate = datetime.datetime.now())
                    supTrxn.put()
                    return redirect("/app/main.html#/theworld")
                elif hasMoney == False:
                    return HttpResponse("not enough money")
                else:
                    return HttpResponse("insufficient factory capacity")
            else:
                return HttpResponse("plant does not belong to player")
        else:
            return HttpResponse("value missing")
    else:
        return HttpResponse("Please Login.")