Ejemplo n.º 1
0
def checkingin(request):
    if request.method == 'GET':

        HOSTNAME = 'outclan.com'
        PORT = 5555
        sid = 0
        try:
            (sid, hbtimeout, ctimeout) = handshake(
                HOSTNAME, PORT)  #handshaking according to socket.io spec.
        except Exception as e:
            print e
            pass
        ws = websocket.create_connection(
            "ws://%s:%d/socket.io/1/websocket/%s" % (HOSTNAME, PORT, sid))

        userName = request.GET['username']
        locName = request.GET['checkin']
        locLong = request.GET['lng']
        locLang = request.GET['lat']
        up = UserProfile.objects.get(user__username=userName)
        location, created = Location.objects.get_or_create(name=locName,
                                                           lng=float(locLong),
                                                           lat=float(locLang))
        try:
            users = Friend.objects.get(user=up.user).friends.all()
            friends = []
            for user in users:
                if user.friends_stream:
                    friends.append(user)
        except Exception:
            friends = []

        if created:
            location.subscription = 1
            location.save()

        checkin = Checkin(user=up, location=location)
        nowdatetime = datetime.now()

        try:
            battle = Battle.objects.get(Q(attacker=up) | Q(defender=up),
                                        active=True)
            try:
                latest_checkin = Checkin.objects.filter(
                    user=up, location=location).order_by('-created_at')[0]
                if (nowdatetime -
                        latest_checkin.created_at) < timedelta(seconds=1):
                    return HttpResponse('to short')
            except Exception:
                pass

            checkin.created_at = nowdatetime
            checkin.battle = battle
            checkin.save()
            try:
                if battle.attacker == up:
                    enemy = battle.defender
                else:
                    enemy = battle.attacker

                terrytory = battle.territory

                if terrytory.owner == up or terrytory.owner == enemy:
                    my_loyalty, created = Loyalty.objects.get_or_create(
                        location=location, userProfile=up)
                    my_loyalty.value += 10

                    enemy_loyalty, created = Loyalty.objects.get_or_create(
                        location=location, userProfile=enemy)

                    print enemy.user.username

                    if check_loyalty(up):
                        battle.active = False
                        battle.winner = up

                        print location.territory
                        location.territory.owner = up

                        location.territory.save()
                        my_loyalty.active = False
                        enemy_loyalty.active = False
                        battle.save()
                    else:

                        enemy_loyalty.value -= 10

                    enemy_loyalty.save()
                    my_loyalty.save()
            except Exception:
                pass
        except Battle.DoesNotExist:
            try:
                latest_checkin = Checkin.objects.filter(
                    user=up, location=location).order_by('-created_at')[0]
                if (nowdatetime -
                        latest_checkin.created_at) < timedelta(seconds=2):
                    return HttpResponse('to short2')
            except Exception:
                pass

            checkin.created_at = nowdatetime
            checkin.save()

            money_rate = up.lvl * up.lvl
            exp_rate = up.lvl

            if up.lvl == 1:
                money_rate = 5
                exp_rate = 2

            money = float(location.subscription) * money_rate
            exp = location.subscription * exp_rate

            up.money += (money * (100 - up.money_to_all)) / 100
            up.exp += (exp * (100 - up.exp_to_all)) / 100
            if up.exp >= 5 * up.lvl * up.lvl:
                up.lvl += 1
            up.save()

            try:
                alliance = Alliance.objects.get(members=up)
                users = alliance.members.all()
                node_users = []
                for user in users:
                    node_users.append(user.user.username)
                    user.money += up.money_to_all
                    user.exp += up.exp_to_all
                    if user.exp >= 5 * user.lvl * user.lvl:
                        user.lvl += 1
                    user.save()

                membership = AllianceMembership.objects.get(alliance=alliance)
                membership.reputation += up.lvl
                membership.save()

                alliance.exp += up.lvl
                if alliance.exp > (alliance.lvl * alliance.lvl) * 10:
                    alliance.lvl += 1
                alliance.save()

            except Alliance.DoesNotExist:
                pass

            try:
                if location.territory:
                    owner = location.territory.owner
                    if owner:
                        if owner != up:
                            try:
                                owner.exp += up.lvl * location.subscription / Alliance.objects.get(
                                    members=up).members.all().count() / 2
                                owner.money += up.lvl * up.lvl * location.subscription / 2
                                if owner.exp >= 5 * owner.lvl * owner.lvl:
                                    owner.lvl += 1
                                owner.save()
                            except Alliance.DoesNotExist:
                                pass
            except UserProfile.DoesNotExist:
                pass

        if friends:
            ws.send('2::')
            ws.send('5:1::{"name":"handshaking", "args":[{"user":"******"}]}')
            for friend in friends:
                ws.send('2::')
                try:
                    converted_location = unicode(str(location.name), "ascii")
                except UnicodeError:
                    converted_location = unicode(str(location.name), "utf-8")
                else:
                    converted_location = str(location.name)
                ws.send('5:1::{"name":"checkin", "args":[{"user":"******", "gravatar_url":"' +
                        str(up.gravatar_url) + '", "locationName":"' +
                        converted_location + '", "locationLat":"' +
                        str(location.lat) + '", "locationLng": "' +
                        str(location.lng) + '"}]}')
            ws.close()
            return HttpResponse('done')
        else:
            return HttpResponse('done')

    return HttpResponse('a')
Ejemplo n.º 2
0
def checkingin(request):
    if request.method == 'GET':
        userName = request.GET['username']
        locName = request.GET['checkin']
        locLong = request.GET['lng']
        locLang = request.GET['lat']
        up = UserProfile.objects.get(user__username=userName)
        location, created = Location.objects.get_or_create(name=locName, lng=locLong, lat=locLang)
        if created:
            location.subscription = 1
            location.save()

        checkin = Checkin(user=up, location=location)
        nowdatetime = datetime.now()

        try:
            battle = Battle.objects.get(Q(attacker__members=up)|Q(defender__members=up), active=True, capital=location)
            try:
                latest_checkin = Checkin.objects.filter(user=up, location=location).order_by('-created_at')[0]
                if (nowdatetime - latest_checkin.created_at) < timedelta (seconds = 1):
                    return HttpResponse('to short')
            except Exception:
                pass

            checkin.created_at = nowdatetime
            checkin.battle = battle
            checkin.save()

            if nowdatetime - battle.start_time >= timedelta(hours = 24):
                if getbattleresults(battle) < 0:
                    battle.winner = battle.defender
                else:
                    battle.winner = battle.attacker

                battle.save()
                    
        except Battle.DoesNotExist:
            try:
                latest_checkin = Checkin.objects.filter(user=up, location=location).order_by('-created_at')[0]
                if (nowdatetime - latest_checkin.created_at) < timedelta (seconds = 2):
                    return HttpResponse('to short2')
            except Exception:
                pass

            checkin.created_at = nowdatetime
            checkin.save()



            money = location.subscription * up.lvl * up.lvl
            exp = location.subscription * up.lvl

            up.money += ( money * (100 - up.money_to_all) ) / 100
            up.exp += ( exp * (100 - up.exp_to_all ) ) / 100
            if up.exp >= 5 * up.lvl * up.lvl:
                up.lvl += 1
            up.save()

            checkin_notification({'type':'up_notification', 'recipients':up.user.username})
            try:
                alliance = Alliance.objects.get(members=up)
                users = alliance.members.all()
                node_users = []
                for user in users:
                    node_users.append(user.user.username)
                    user.money += up.money_to_all
                    user.exp += up.exp_to_all
                    if user.exp >= 5 * user.lvl * user.lvl:
                        user.lvl += 1
                    user.save()
                checkin_notification({'type':'alliance_notification', 'recipients':node_users})
            except Alliance.DoesNotExist:
                pass
            try:
                owner = UserProfile.objects.get(territory__locations=location)
                if owner != up:
                    owner.exp += up.lvl * location.subscription / UserProfile.objects.filter(alliance_set__members=owner).count() / 2
                    owner.lvl += up.lvl * up.lvl * location.subscription / 2
                    if owner.exp >= 5 * owner.lvl * owner.lvl:
                        owner.lvl += 1
                    owner.save()
            except UserProfile.DoesNotExist:
                pass
    return HttpResponse('a')
Ejemplo n.º 3
0
def checkingin(request):
    if request.method == 'GET':

        HOSTNAME = 'outclan.com'
        PORT = 5555
        sid = 0
        try:
            (sid, hbtimeout, ctimeout) = handshake(HOSTNAME, PORT) #handshaking according to socket.io spec.
        except Exception as e:
            print e
            pass
        ws = websocket.create_connection("ws://%s:%d/socket.io/1/websocket/%s" % (HOSTNAME, PORT, sid))
        
        userName = request.GET['username']
        locName = request.GET['checkin']
        locLong = request.GET['lng']
        locLang = request.GET['lat']
        up = UserProfile.objects.get(user__username=userName)
        location, created = Location.objects.get_or_create(name=locName, lng=float(locLong), lat=float(locLang))
        try:
            users = Friend.objects.get(user=up.user).friends.all()
            friends = []
            for user in users:
                if user.friends_stream:
                    friends.append(user)
        except Exception:
            friends = []

        if created:
            location.subscription = 1
            location.save()

        checkin = Checkin(user=up, location=location)
        nowdatetime = datetime.now()

        try:
            battle = Battle.objects.get(Q(attacker=up)|Q(defender=up), active=True)
            try:
                latest_checkin = Checkin.objects.filter(user=up, location=location).order_by('-created_at')[0]
                if (nowdatetime - latest_checkin.created_at) < timedelta (seconds = 1):
                    return HttpResponse('to short')
            except Exception:
                pass

            checkin.created_at = nowdatetime
            checkin.battle = battle
            checkin.save()
            try:
                if battle.attacker == up:
                    enemy = battle.defender
                else:
                    enemy = battle.attacker

                terrytory = battle.territory

                if terrytory.owner == up or terrytory.owner == enemy:
                    my_loyalty, created = Loyalty.objects.get_or_create(location=location, userProfile=up)
                    my_loyalty.value += 10

                    enemy_loyalty, created = Loyalty.objects.get_or_create(location=location, userProfile=enemy)

                    print enemy.user.username

                    if check_loyalty(up):
                        battle.active = False
                        battle.winner = up

                        print location.territory
                        location.territory.owner = up
                        

                        location.territory.save()
                        my_loyalty.active = False
                        enemy_loyalty.active = False
                        battle.save()
                    else:

                        enemy_loyalty.value -= 10

                    enemy_loyalty.save()
                    my_loyalty.save()
            except Exception:
                pass
        except Battle.DoesNotExist:
            try:
                latest_checkin = Checkin.objects.filter(user=up, location=location).order_by('-created_at')[0]
                if (nowdatetime - latest_checkin.created_at) < timedelta (seconds = 2):
                    return HttpResponse('to short2')
            except Exception:
                pass

            checkin.created_at = nowdatetime
            checkin.save()

            money_rate = up.lvl * up.lvl
            exp_rate = up.lvl

            if up.lvl == 1:
                money_rate = 5
                exp_rate = 2

            money = float(location.subscription) * money_rate
            exp = location.subscription * exp_rate
            
            up.money += ( money * (100 - up.money_to_all) ) / 100
            up.exp += ( exp * (100 - up.exp_to_all ) ) / 100
            if up.exp >= 5 * up.lvl * up.lvl:
                up.lvl += 1
            up.save()

            try:
                alliance = Alliance.objects.get(members=up)
                users = alliance.members.all()
                node_users = []
                for user in users:
                    node_users.append(user.user.username)
                    user.money += up.money_to_all
                    user.exp += up.exp_to_all
                    if user.exp >= 5 * user.lvl * user.lvl:
                        user.lvl += 1
                    user.save()

                membership = AllianceMembership.objects.get(alliance=alliance)
                membership.reputation += up.lvl
                membership.save()

                alliance.exp += up.lvl
                if alliance.exp > (alliance.lvl*alliance.lvl)*10:
                    alliance.lvl += 1
                alliance.save()
                
            except Alliance.DoesNotExist:
                pass
            
            try:
                if location.territory:
                    owner = location.territory.owner
                    if owner:
                        if owner != up:
                            try:
                                owner.exp += up.lvl * location.subscription / Alliance.objects.get(members=up).members.all().count() / 2
                                owner.money += up.lvl * up.lvl * location.subscription / 2
                                if owner.exp >= 5 * owner.lvl * owner.lvl:
                                    owner.lvl += 1
                                owner.save()
                            except Alliance.DoesNotExist:
                                pass
            except UserProfile.DoesNotExist:
                pass

        if friends:
            ws.send('2::')
            ws.send('5:1::{"name":"handshaking", "args":[{"user":"******"}]}')
            for friend in friends:
                ws.send('2::')
                try:
                    converted_location = unicode(str(location.name), "ascii")
                except UnicodeError:
                    converted_location = unicode(str(location.name), "utf-8")
                else:
                    converted_location = str(location.name)
                ws.send('5:1::{"name":"checkin", "args":[{"user":"******", "gravatar_url":"'+str(up.gravatar_url)+'", "locationName":"'+converted_location+'", "locationLat":"'+str(location.lat)+'", "locationLng": "'+str(location.lng)+'"}]}')
            ws.close()
            return HttpResponse('done')
        else:
            return HttpResponse('done')

    return HttpResponse('a')