def ajax(request): if not request.is_ajax(): return False # dbug("handling ajax request") # dbug(request.GET) chatrefreshpattern = re.compile(r'^chat(\d+)refresh$') playerspattern = re.compile(r'^players(\d)refresh$') returnObject = { "chatqueues" : {}, "playerupdates" : {}} for key in request.GET: m = chatrefreshpattern.search(key) if m: chatid = m.group(1) lastrefresh = request.GET[key] mychat = Chat.objects.get(pk=chatid) lines = mychat.listen(request.user, lastrefresh) newlast = 0 saytext = "" returnObject["chatqueues"][chatid] = [] hadLines = False for line in lines: hadLines = True formatted_line = line.format_for_output() newlast = line.id for fline in formatted_line: #may be more than one line per return returnObject["chatqueues"][chatid].append(fline) if hadLines: returnObject["chatqueues"][chatid].append(["newlastid", newlast]) else: m = playerspattern.search(key) if m: gameid = m.group(1) mygame = Game.objects.get(pk=gameid) dbug(mygame) tempdic = {} team1 = [] team2 = [] for player in mygame.team1.players.all(): team1.append(player.username) for player in mygame.team2.players.all(): team2.append(player.username) tempdic["team1"] = team1 tempdic["team2"] = team2 returnObject["playerupdates"][gameid] = tempdic dbug(tempdic) chatjson = json.dumps(returnObject) return HttpResponse(chatjson)
def action(self, actor, action, more_info={}): dbug("received action! Actor is " + actor.username + " and action is " + action) if actor not in self.users.all() and action != "join": dbug("bailing because user is not in and action is not join") return False if action == "join" and actor in self.users.all(): return False # joining but already in chat if action == "leave" and action not in self.users.all(): return False # leaving, but not in chat # We are handling max users on a higher level, based on the object this chat belongs to # So a game needs to check that a user does not exceed its max users. By the time # a higher level object calls a chat, we assume that the max users check is done. if action == "join": dbug("creating line...") self.line_set.create(is_action=True, user=actor, text="join") return True if action == "leave": self.line_set.create(is_action=True, user=actor, text="leave") return True
def listen(self, userlistening, start=0): """ Returns a number of line objects """ if userlistening not in self.users.all(): dbug("User " + userlistening.username + " is not in chat " + str(self.id)) return False return self.line_set.filter(pk__gt=start)