def createGame(uid, ip): r = {"errmsg":"","gid":0} # check items bValue1 = checkOblation(uid) iValue = ifPCInGame(uid) if bValue1 and iValue==0: gi = GameInfo() gi.cthulhuSanity = 0 gi.pclist = str(uid) gi.winner = -1 gi.statue = 1 gi.createTime = datetime.datetime.now() gi.creator_id = uid gi.save() try: crntgame = GameInfo.objects.filter(creator_id=uid).order_by('-id')[:1] if len(crntgame)==0: r["errmsg"] = "create game fail." return r gid = crntgame[0] r["gid"] = gid.id except: r["errmsg"] = "create game fail." return r # modify creators' game statue try: ol = PC.objects.get(player_id = uid) ol.active_time = datetime.datetime.now() ol.ip = ip ol.gameStatue = gid.id ol.sanity = 3 ol.save(update_fields=['ip','active_time','gameStatue','sanity']) except: ol = PC() ol.player_id = uid ol.active_time = datetime.datetime.now() ol.ip = ip ol.gameStatue = gid.id ol.sanity = 3 ol.save() return r if iValue>0: r["errmsg"] = "already in a game!" r["gid"] = iValue if not bValue1: r["errmsg"] = "not enough oblation!" return r
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
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
def joinGame(gid, uid, ip): errmsg = '' # check player's statue and if in a game - and jump to current game if there's any iValue = ifPCInGame(uid) if iValue>0: errmsg = "already in a game!" return errmsg g = ifGameAvailable(gid) if g["gameAvailable"]: if g["statue"] == 1: # check how many players in game num = PC.objects.filter(gameStatue=gid)#.count() if len(num) < 6: # modify creators' game statue try: ol = PC.objects.get(player_id = uid) ol.active_time = datetime.datetime.now() ol.ip = ip ol.gameStatue = gid ol.sanity = 3 ol.save(update_fields=["ip","active_time","gameStatue","sanity"]) except Exception,data: ol = PC() ol.player_id = uid ol.active_time = datetime.datetime.now() ol.ip = ip ol.gameStatue = gid ol.sanity = 3 ol.save() return errmsg else: errmsg = "You cannot join the game!" else: errmsg = "You cannnot join the game!"