Exemplo n.º 1
0
    def get_next_state(self, is_continue=False):
        vars = {'title':self.title}
        user_state = game_utils.get_user_state(self.request)
        prev_state = None
        if user_state:        
            prev_state = user_state.get('state', None)
        if (self.state == self.START) or (is_continue and prev_state == self.state):
            # dont check anything if they want to start over
            # don't check if they are continuing where they left off
            vars["invalid_move"] = False
        else:
            valid_move = game_utils.check_state(prev_state, self.state, self.game_graph)
            if not valid_move:
                vars["invalid_move"] = True
                print "invalid move prev: %s, next: %s" % (prev_state, self.state)
                return vars
            else:
                vars["invalid_move"] = False
        
        # valid move
        if not (is_continue and prev_state == self.state):
            game_utils.set_user_state(self.request, self.state, self.town)
        
        # call the subclass method
        state = getattr(self, self.state)()
        vars['state'] = state
        
        # execute actions
        if state.actions:
            for action in state.actions:
                action_str = 'action[0]'
                args = ""
                for i in range(1, len(action)):
                    args += 'action[%d],' % i
                action_str += "(%s)" % args 
                eval(action_str)
                
        # check if they died
        if state.die:
            state.choices = {}   # no choices upon death
            game_utils.kill_user(self.request, self.START)

        return vars    
Exemplo n.º 2
0
def index(request, town=None, state=None):
    log = logging.getLogger('controller_view')
    is_continue = False
    vars = {}
    if town == None and state == None:
        user_state = game_utils.get_user_state(request)
        if user_state != None:
            state = user_state['state']
            town = user_state['town']
            is_continue = True
            log.debug("player is continuing: %s, %s" % (town, state))
            # get user history
            history = []
            s_history = game_utils.get_user_history(request)
            for his_dict in s_history:
                his_state = his_dict['state']
                his_town = his_dict['town']
                d = get_dungeon_class(request, his_state, his_town)
                history.append(getattr(d, his_state)())
            vars['history'] = history[1:]
        else:
            state = BaseDungeon.START
            town = 'intro'
            
    d = get_dungeon_class(request, state, town)
    d_vars = d.get_next_state(is_continue)
    vars.update(d_vars)
    vars["request"] = request
    vars["state_name"] = state
    vars["DEBUG"] = settings.DEBUG
    if town: 
        vars["town_name"] = town.lower()
    
    if (state == BaseDungeon.START and town == 'intro' and not is_continue) or is_continue:        
        return render_to_response("game.html", vars)
    else:
        vars["hidden"] = True
        return render_to_response("state.html", vars)