def system_buy_shoes(request): ''' Check the logged in user ''' user = users.get_current_user() marketing = 1 dd = [] if user: islandQuery = Island.all() islandCount = islandQuery.count() islands = islandQuery.fetch(islandCount) for island in islands: islandId = island.key().id() playerQuery = Player.gql("WHERE islandId = :1",islandId) numPlayers = playerQuery.count() plantQuery = Plant.gql("WHERE islandId = :1",islandId) plantCount = plantQuery.count() plants = plantQuery.fetch(plantCount) for plant in plants: plantId = plant.key().id() plantRegion = plant.region shoeQuery = Shoe.gql("WHERE plantId = :1",plantId) shoeCount = shoeQuery.count() shoes = shoeQuery.fetch(shoeCount) for shoe in shoes: body = shoe.body sole = shoe.sole color = shoe.color qty = shoe.qty attractiveness = math.fabs((0.75*sole + body + 1.25 * color) / 3) saleRegion = "" #randomly select sale-to-region i = random.randint(1,4) if i == 1: saleRegion = "north" elif i == 2: saleRegion = "east" elif i == 3: saleRegion = "west" elif i == 4: saleRegion = "south" _shippingcost = shippingCost[plantRegion][saleRegion] price = shoe.price + _shippingcost salesVolume = int(math.ceil(numPlayers * math.pow((attractiveness*marketing/price),2))) if salesVolume > qty: salesVolume = qty shoe.qty = shoe.qty - salesVolume shoe.put() dd.append(shoe.key().id()) dd.append(salesVolume) n = {'count':len(dd)} totalShippingCost = salesVolume * _shippingcost totalRevenue = salesVolume * shoe.price player = Player.get_by_id(plant.playerId) player.money = player.money - totalShippingCost + totalRevenue player.money_spent = player.money_spent + totalShippingCost player.networth = player.money + player.money_spent player.put() plant.currentCapacity = plant.currentCapacity - salesVolume plant.put() demandTransaction = DemandTransaction(shoeId = shoe.key().id(),salePrice = shoe.price,shippingCost = _shippingcost,qty = salesVolume) demandTransaction.put() else: return redirect(users.create_login_url("/system_buy_shoes")) return HttpResponse(simplejson.dumps(n),mimetype="application/json")