Example #1
0
def island_ranking(request):
    if request.session.__contains__('islandId') and request.session.__contains__('account'):
        islandId = int(request.session['islandId'])
        playerQuery = Player.gql("WHERE islandId = :1 ORDER BY networth DESC",islandId)
        
        players = playerQuery.fetch(10)
        results = []
        
        for player in players:
            _playerDict = player.__dict__['_entity']
            playerId = player.key().id()
            plantQuery = Plant.gql("WHERE playerId = :1",playerId)
            countPlant = plantQuery.count()
            _playerDict['numPlants'] = countPlant
            _playerDict['numShoes'] = 0
            _playerDict['numWorkers'] = 0
            plants = plantQuery.fetch(countPlant)
            for plant in plants:
                _playerDict['numWorkers'] += plant.numOfWorkers
                plantId = plant.key().id()
                shoeQuery = Shoe.gql("WHERE plantId = :1",plantId)
                shoes = shoeQuery.fetch(1000)
                for shoe in shoes:
                    _playerDict['numShoes'] += shoe.qty
            results.append(_playerDict)
        
        return HttpResponse(simplejson.dumps(results),mimetype="application/json")
        
    else:
        dd = {}
        dd['error'] = "Error: No island selected. Please join or resume your game."
        return HttpResponse(simplejson.dumps(dd),mimetype="application/json")
Example #2
0
def join_island(request):
    try:
        q = request.GET
        #jEmail = q.__getitem__('email')
        jEmail = request.session['account'].email
        jIslandId = int(q.__getitem__('islandId'))
        query = Player.gql("WHERE email = :1 and islandId = :2",jEmail,jIslandId)
        if query.count() > 0:
            return redirect("/play?islandId="+str(jIslandId)) #change to play game
        else:
            
            island = Island.get_by_id(jIslandId)
            if island != None and island.numOfPlayer < island.maxNumOfPlayers:
                player = Player(email = jEmail,money_spent=0.0,money=1000000.0,islandId=jIslandId,networth=1000000.0)
                player.put()
                island.numOfPlayer = island.numOfPlayer + 1
                island.put()
                return redirect("/play?islandId="+str(island.key().id()))
            else:
                return HttpResponse("error","text")
        #dd = {}
        #return HttpResponse(simplejson.dumps(dd),mimetype="application/json")
    except KeyError:
        return redirect("/")
    except ValueError:
        return "/app/error.html?error=ValueError"
Example #3
0
def load_player(request):
    session = request.session
    dd = {}
    
    #check necessary arguments
    if session.__contains__('islandId') == False:
        dd['error'] = "missing islandId"
        return HttpResponse(simplejson.dumps(dd),mimetype="application/json")
    
    if session.__contains__('account') == False:
        dd['error'] = "Error: Not logined."
        return HttpResponse(simplejson.dumps(dd),mimetype="application/json")
    
    email = session['account'].email
    islandId = session['islandId']
    island = Island.get_by_id(islandId)
    player_q = Player.gql("WHERE email = :1 and islandId = :2",email,islandId)
    player = player_q.get()
    player_dict = player.__dict__['_entity']
    player_dict['playerId'] = player.key().id()
    #get number of factories
    plantQuery = Plant.gql("WHERE playerId = :1 and islandId = :2",player.key().id(), islandId)
    player_dict['numPlants'] = plantQuery.count()
    player_dict['numWorkers'] = 0
    plants = plantQuery.fetch(1000)
    plantIds = []
    for p in plants:
        player_dict['numWorkers'] += p.numOfWorkers
        plantIds.append(p.key().id())
    player_dict['numShoes'] = 0
    shoeQuery = Shoe.gql("WHERE plantId in :1",plantIds)
    shoes = shoeQuery.fetch(1000)
    for shoe in shoes:
        player_dict['numShoes'] += shoe.qty
    player_dict['islandName'] = island.name
    jsonString = simplejson.dumps(player_dict)
    
    return HttpResponse(jsonString,mimetype="application/json")
Example #4
0
def get_account_players(request):
    session = request.session
    try:
        if session['account']:
            acc = session['account']
            q = Player.gql("WHERE email = :1",acc.email)
            f = q.fetch(1000)
            players = []
            for i in f:
                islandId = i.islandId
                island = Island.get_by_id(islandId)
                dd = i.__dict__['_entity']
                dd['playerId'] = i.key().id()
                dd['islandName'] = island.name
                players.append(dd)
            return HttpResponse(simplejson.dumps(players),mimetype="application/json")
        else:
            return HttpResponse("error","text")
    except KeyError:
        pass
    dd = {}
    dd['error'] = "Error: cannot get session account"
    return HttpResponse(simplejson.dumps(dd),mimetype="application/json")
Example #5
0
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")