async def nfc_submit(et_id: str, request: Request, db: Session = Depends(get_db)): # et_id just needs to match first few characters logger.debug(f"[et_id] {et_id}") et = None for candidate in get_all_event_types(db): #logger.debug(f"[candidate] {candidate}") idstr = str(candidate.id) if idstr.startswith(et_id): et = candidate break if not et: return None checkin = Checkin(timestamp=datetime.now(), event_type=et.id, value=True) logger.debug(f"[eventtype] {et}") logger.debug(f"[checkin] {checkin}") create_checkin(db, checkin) #return checkin url = await random_cute_image_url() affirmation = random_affirmation_text() return templates.TemplateResponse("nfc_success.html", { "request": request, 'et': et, 'image_url': url, 'affirmation': affirmation })
def startlesson(self): if self.course.status != 0: return {'error': 101, 'message': u'课程时间冲突,请关闭未结束的课程'} if self.status not in (LESSON_STATUS_AWAIT, LESSON_STATUS_NEW_AWAIT): return {'error': 101, 'message': u'课程不能开启'} nowtime = time.localtime() self.starttime = time.strftime('%Y-%m-%d %H:%M:%S', nowtime) self.status = LESSON_STATUS_NOW self.save() # for checkin nowstudent = Checkin.objects.filter(lesson=self).values_list('student', flat=True) studentcourse = Studentcourse.objects.filter( course=self.course).exclude(student__in=nowstudent).all() newstudent = [] for s in studentcourse: newstudent.append( Checkin(lesson=self, status=CHECKIN_STATUS_SUCCESS, student=s.student)) Checkin.objects.bulk_create(newstudent) students = Checkin.objects.filter(lesson=self).values_list('student', flat=True) starttime, endtime = self.getTime starttime = time.strftime('%Y-%m-%d %H:%M:%S', starttime) endtime = time.strftime('%Y-%m-%d %H:%M:%S', endtime) askstudents_private = Ask.objects.filter( student__in=students, status=ASK_STATUS_APPROVE, type=CHECKIN_STATUS_PRIVATE_ASK).filter( Q(starttime__lte=starttime, endtime__gte=starttime) | Q(starttime__lte=endtime, endtime__gte=endtime) | Q(starttime__gte=starttime, endtime__lte=endtime) ).values_list('student', flat=True) askstudents_public = Ask.objects.filter( student__in=students, status=ASK_STATUS_APPROVE, type=CHECKIN_STATUS_PUBLIC_ASK).filter( Q(starttime__lte=starttime, endtime__gte=starttime) | Q(starttime__lte=endtime, endtime__gte=endtime) | Q(starttime__gte=starttime, endtime__lte=endtime) ).values_list('student', flat=True) Checkin.objects.filter(lesson=self, student__in=askstudents_private).update( laststatus=F('status'), status=CHECKIN_STATUS_PRIVATE_ASK) Checkin.objects.filter(lesson=self, student__in=askstudents_public).update( laststatus=F('status'), status=CHECKIN_STATUS_PUBLIC_ASK) logger.info('lesson %d start' % self.id) return { 'error': 0, 'message': u'课程成功开启', 'newstatus': self.status, 'starttime': time.strftime('%Y-%m-%d %H:%M:%S', nowtime) }
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')
async def post_checkin(checkin: Checkin, db: Session = Depends(get_db)): logger.debug("checkin pre:", checkin) if not checkin.timestamp: checkin.timestamp = datetime.now() create_checkin(db, checkin) return True
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')
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')