def lobby(request, id): lobby = get_object_or_404(Lobby, id=id, game=None) if request.user.avatar_set.count() > 0: avatar = request.user.avatar_set.all()[0].image else: avatar = Lobby.default_cpu_image() if request.user.is_authenticated(): member = Member( lobby = lobby, user = request.user, image = avatar ) try: member.save() except: pass send_message_form = SendMessageForm({'lobby': lobby.id, 'content':" "}) lobby_dict = expand(lobby) from django.conf import settings lobby_dict['media_url'] = settings.MEDIA_URL lobby_json = json.dumps(lobby_dict) user_json = json.dumps(expand(request.user)) return locals()
def serialize(self, player, firsttime=False): game_dict = {} game_dict['game'] = expand(self.game) _players = self.game.player_set.all() game_dict['players'] = serialize_qs(_players) game_dict['me'] = expand(player) game_dict['my_cubicle'] = expand(player.playercell) game_dict['unviewed_alerts'] = serialize_qs(player.alert_set.filter(viewed=None).order_by('-created').all()) game_dict['turns'] = { 'min': self.get_cached('min_turns', 0), 'max': self.get_cached('max_turns', 0), 'allowed': self.turns_allowed(player), 'max_turn_id': self.get_cached('max_turn_id')} game_dict['correct_guesses'] = serialize_qs(player.guess_set.filter(correct=1)) if firsttime: player_cells = PlayerCell.objects.filter(game=self.game).all() game_dict['player_cells'] = [] game_dict['cells'] = serialize_qs(Cell.objects.filter(game=self.game).all()) for cell in player_cells: _cell = expand(cell) _cell['player_id'] = cell.player_id game_dict['player_cells'].append(_cell) game_dict['player_extra'] = {} for _p in _players: game_dict['player_extra'][_p.id] = {'user': expand(_p.user)} roles = PlayerRole.objects.filter(player__in=self.game.player_set.values('id')).all() for role in roles: game_dict['player_extra'][role.player.id]['role'] = expand(role) game_dict['player_extra'][role.player.id]['role']['name'] = role.role.name game_dict['player_extra'][role.player.id]['role']['color'] = role.role.color return game_dict
def guess(request, game_id): game = get_object_or_404(Game, id=game_id) player = Player.objects.get( user = request.user, game = game ) other_player = Player.objects.get(id=request.POST['player']) role = Role.objects.get(id=request.POST['role']) cell = PlayerCell.objects.get(id=request.POST['cell']) guess = Guess( player = player, other_player = other_player, role = role, cell = cell, ) print "Guessing:", other_player.user.username print guess.other_player.playerrole.role, role print guess.other_player.playercell, cell if guess.other_player.playerrole.role == role and guess.other_player.playercell == cell: guess.correct = True else: guess.correct = False guess.save() alert = Alert( player = player, ) alert.content_type = ContentType.objects.get_for_model(guess) alert.object_id = guess.id if guess.correct: alert.type = 'correct_guess' else: alert.type = 'wrong_guess' alert.save() turn = Turn( action = "guess", params = guess.id, player = player ) turn.save() if player.unkown_facts() == 0 and player.is_current: print "Player finished: %s" % str(player) #Tell this player they finished alert = Alert( player = player, type = "message", text = "You solved the puzzle!, waiting for other players to finish.", important = True ) alert.content_type = ContentType.objects.get_for_model(guess) alert.object_id = guess.id alert.save() for other in game.player_set.exclude(pk=player.id).all(): consolation_guess = other.guess_set.filter(other_player=player, correct=True).all() if len(consolation_guess) == 0: print "Giving consolation guess to %s" % str(other) consolation_guess = Guess( player = other, other_player = player, correct = True, tally = False, role = player.playerrole.role, cell = player.playercell, ) consolation_guess.save() #Tell each other player that this player finished alert = Alert( player = other, type = "message", text = "%s solved the puzzle, wait for other players to finish." % (player.user.username), important = True ) alert.content_type = ContentType.objects.get_for_model(guess) alert.object_id = guess.id alert.save() player.is_current = False player.x = player.playercell.x player.y = player.playercell.y player.save() return HttpResponse(simplejson.dumps(expand(guess)))
def game(request, game_id): #404 for invalid game game = get_object_or_404(Game, id=game_id) bg = BoardGame(game) message = "" show_board = True #404 if user is not a player in game player = Player.objects.select_related('user', 'cell').get(user=request.user, game=game) if player.is_current == False: message = "You are no longer active in this game." show_board = False turn = Turn ( player = player ) if request.method == 'POST': game_dict = { 'complete': False } if bg.is_over(): game_dict["complete"] = True extra_data = {} if 'move' in request.POST: move_coords = request.POST['move'] x, y = move_coords.split(',') #print "NEW LOCATION %s , %s" % (x, y) #print "PLAYER LOCATION %s , %s" % (player.x, player.y) cells = game.get_player_cells_within(int(x), int(y), 0) if cells: #direction = player.get_direction_of_movement(x, y) #print "DIRECTION: %s" % direction cubicle = cells[0] cubicle_owner = cubicle.player new_clue = player.investigate_cell(cubicle) if new_clue: alert = Alert( player = player, type = 'cubicle_clue', content_type = ContentType.objects.get_for_model(Clue), object_id = new_clue.id ) alert.save() extra_data['new_alerts'] = [expand(alert)] try: player.move_to(x, y) turn.action = 'move' turn.params = move_coords except ValueError: print "Too late move: %s" % (move_coords) elif 'investigate' in request.POST: investigating_player_id = request.POST['investigate'] investigating_player = Player.objects.get(id=investigating_player_id, game=game) new_clue = player.investigate(investigating_player) if new_clue: alert = Alert( player = player, type = 'investigate_clue', content_type = ContentType.objects.get_for_model(Clue), object_id = new_clue.id ) alert.save() extra_data['new_alerts'] = [expand(alert)] turn.action = 'investigate' turn.params = investigating_player_id elif 'update' in request.POST: if game.is_current: bg.move_for_cpus() if turn.action: turn.save() player.turn_count +=1 player.save() if 'update' in request.POST and int(request.POST['update']) == 0: #first time game_dict.update(bg.serialize(player, True)) else: game_dict['data'] = game_dict.update(bg.serialize(player)) game_dict['extra'] = extra_data return HttpResponse(simplejson.dumps(game_dict), content_type='application/json') context = { 'game': game, 'player': player, 'message': message, } return context
def serialize(self): ex = expand(self) ex['str'] = str(self) return ex