Ejemplo n.º 1
0
def getPcList(request):
    uid = request.user.id
    found = False
    gameID = ifPCInGame(uid)
    if gameID == 0:
        return simplejson.dumps([])
    
    try:
        p = PC.objects.get(player_id=uid) 
        if p:
            found = True
    except:
        pass
    
    ip = get_client_ip(request)
    
    if not found: 
        ol = PC()
        
        ol.player_id = uid
        ol.ip = ip
        ol.sanity = 3
        ol.save()
    
    ols = PC.objects.filter( Q(gameStatue=gameID) )

    olplayers = []
    for player in ols:
        nick = User.objects.get(pk=player.player_id).username 
        olplayers.append({'id':player.player_id, 
                          'player':nick,
                          'sanity':player.sanity
                          })
    r = simplejson.dumps(olplayers)
    return r
Ejemplo n.º 2
0
def save_log(request,result,msg):
	user = request.session.get('user_name')
	ip = get_client_ip.get_client_ip(request)
	msg = msg
	result = result
	logdate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
	ywlog = YwLog(user=user,ipaddress=ip,msg=msg,result=result,logdate=logdate)
	ywlog.save()
Ejemplo n.º 3
0
def updateOnlineList(request):
    try:
        pc = PC.objects.get(player_id=request.user.id)
    except:
	pc = PC()

        pc.player_id = request.user.id
        pc.active_time = datetime.datetime.now()
        pc.ip = get_client_ip(request)
        pc.gameStatue = 0
        pc.sanity = 3
	pc.save()
	return True

    ip = get_client_ip(request)
    at = datetime.datetime.now()
    data_dic = {'ip':ip, 'active_time':at}
    pc.save(update_fields=data_dic)
    return True
Ejemplo n.º 4
0
def say(request):
    req =simplejson.loads(request.body)
    content = req['content']
    cmd = req['cmd']
    target = req['target']
    uid = request.user.id
    
    
    if not cmd:
        return HttpResponse(simplejson.dumps({'success':False}), mimetype = 'application/json')

    chat = Chat()
    pattern1 = re.compile(r'<')
    content_t = re.sub(pattern1, '&lt;', content)

    pattern2 = re.compile(r'>')
    content_t = re.sub(pattern2, '&gt;', content_t)

    chat.username_id = request.user.id

    #................................................................
    if cmd == "chat":
        insertMsg(content_t,uid,datetime.datetime.now(),cmd,-1,'pub',ifPCInGame(uid))
	return HttpResponse(simplejson.dumps({'success':True}), mimetype = 'application/json')

    #..............................OK..................................
    if cmd == "GPCL":   #get pc list
	bValue = updateOnlineList(request)
	target_t = uid 
	content_t = getPcList(request)
	insertMsg( content_t, request.user.id, datetime.datetime.now(),"OLPC",uid,"pri",ifPCInGame(uid))
	return HttpResponse(simplejson.dumps({'success':True}), mimetype = 'application/json')

    #..............................OK..................................
    if cmd == "CRTG":   # create game
        bValue = updateOnlineList(request)
        
        g = createGame(uid, get_client_ip(request))

        debugLog("CRTG: createGame() return value="+str(g))
        
        if g["gid"]>0: 
            try:
                grinfo = getGIRNDByUid(uid, request.user.username)
                rCRTG = insertMsg( simplejson.dumps(grinfo),uid, datetime.datetime.now(), "CRTG", uid, "pri", g["gid"])
            except Exception,data:
                debugLog("CRTG error:"+str(Exception)+":"+str(data))
                
        else:
            rCRTG = insertMsg( g["errmsg"],uid, datetime.datetime.now(), "CRTG", uid, "pri", -1)
        
        return HttpResponse(simplejson.dumps({'success':True}), mimetype = 'application/json')
Ejemplo n.º 5
0
def login_user(request):
	if request.method == "POST":
		username = request.POST.get('username')
		password = request.POST.get('password')
		user = auth.authenticate(username=username,password=password)
		if user and user.is_active:
			auth.login(request,user)
	#		request.session.set_expiry(60*30)
			request.session['user_name'] = user.username
			request.session['user_role'] = user.role
			request.session['user_id'] = user.id
			save_log(request,1,'登录系统')
			return HttpResponseRedirect('/')
		else:
			user = username 
			ip = get_client_ip.get_client_ip(request)
			msg = '登录系统'
			result = 0
			logdate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
			ywlog = YwLog(user=user,ipaddress=ip,msg=msg,result=result,logdate=logdate)
			ywlog.save()
			return render_to_response('account/login.html',{'login_err': '当前登录用户不存在或密码错误!'},RequestContext(request))
	else:
		return render_to_response('account/login.html',RequestContext(request))
Ejemplo n.º 6
0
                grinfo = getGIRNDByUid(uid, request.user.username)
                rCRTG = insertMsg( simplejson.dumps(grinfo),uid, datetime.datetime.now(), "CRTG", uid, "pri", g["gid"])
            except Exception,data:
                debugLog("CRTG error:"+str(Exception)+":"+str(data))
                
        else:
            rCRTG = insertMsg( g["errmsg"],uid, datetime.datetime.now(), "CRTG", uid, "pri", -1)
        
        return HttpResponse(simplejson.dumps({'success':True}), mimetype = 'application/json')

    #..............................OK..................................
    if cmd == "JOIN":   # join game
        jGid = req['gid']
        bValue = updateOnlineList(request)
        
        jg = joinGame(int(jGid), uid, get_client_ip(request))
        
        if jg =="":
	    grinfo = getGIRNDByUid(uid, request.user.username)
            insertMsg( simplejson.dumps(grinfo), uid, datetime.datetime.now(),"JOIN",0,"gam",jGid)
        else:
            insertMsg( "", uid, datetime.datetime.now(),"JOIN",uid,"pri",0)
        return HttpResponse(simplejson.dumps({'success':True}), mimetype = 'application/json')

    #.............................nearly.OK..................................
    if cmd == "STRT":   # start game
        crntGid = ifPCInGame(uid)
        
        if crntGid >0:
            sg = startGame(crntGid)