0

I'm making a text-based game, which is based largely in if-, elif- and else-statements. However, I also use while loops for different "areas" within the game. (Play_loop, Main_loop, Shop_loop, Fish_loop, etc.).

Lately I've implemented admin commands which I use in-game to change things on the go, and I want these to be available in every loop. I also have some general commands which I want to be available (help, leave, quit, logout, go to another area, check inventory, etc.).

The issue I'm facing is knowing that duplicated code should be avoided, but i'm wondering if this is necessary in this situation. I've already made many functions to make each command code pretty short, about 2-15 lines in general.

Should I add the code blocks into functions that do the same thing, and then just repeat the function, or should I just keep it as is? Or maybe I should do something else that I havent even thought about?

Example code:

elif command == '/user info':
    if user.admin:
        print(f'User-list: {users}')
        who = input('Name of user: ').strip()
        who_user = admin_load_user(who, users)
        if who_user:
            print(who_user.info())
            print(who_user.display_inv())
        else:
            print(DNEError)
    else:
        print(PError)
elif command == '/add coins':
    who = input('Who gets coins? ').strip()
    amount = int(input('How much? ').strip())
    admin_add_coins(who, amount, users)
    save_users(users)
glennrv
  • 3
  • 2
  • I would consider making the entire game in a class. That way class variables can be passed easily and used instead of a global. You can then map each command line to a function. which can be called anytime from any situation. I.e. '/add coins' should immediately call the 'add_coins' function. You could diffrentiate between admin commands and user commands but thats up to your project. – Jason Chia Nov 02 '20 at 13:28
  • @JasonChia I have two classes this far, a player class and a vendor class. Is this what you mean? Alot of the admin commands are applied to other players or vendors, not the admin itself. – glennrv Nov 02 '20 at 13:41

2 Answers2

0

Code that is repeated should typically be put into functions, so that you have better overview and control over what they are doing. There you can also easily give them default arguments and expand on the code without bloating the main function of your program.

Here is a concise argument for using functions. It's written for C but applies to python as well.

Then, as Jason Chia pointed out, you should consider thinking about building your game into classes, as they solve some of the problems you mentioned and generally are an important control instrument for bigger programs where you need to coordinate different states (e.g. changing something in a room based on the player's actions).

This tutorial could help with that.

Does that about answer your question?

Stimmot
  • 999
  • 1
  • 7
  • 22
  • I think it answers my question. I'm quite new at using classes, but I have quite a few methods in my player class. Most of these Admin commands are not inside the class, but are instead just global functions. – glennrv Nov 02 '20 at 15:29
0

You should use decorator style to do it nice to read and write. get inspiration here:

def requires_admin(f):
    def wrapper(f):
        @wraps(f)
        def wrapped(*args, **kwargs):
            #if not admin:
                #return render_template('error.html')
            return f(*args, **kwargs)
        return wrapped
    return wrapper


@app.route('/admin/action')
@requires_admin
def AdminAction():
    whatyouwant()
laticoda
  • 100
  • 14