Created
May 14, 2018 12:05
-
-
Save 1328/1824c6e19256b7fb311601f3d429c4e9 to your computer and use it in GitHub Desktop.
Mock up for disco adventure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Message(): | |
'''mockup for discord message''' | |
def __init__(self, user, incoming): | |
self.user = user | |
self.incoming = incoming | |
def send_message(self, what): | |
print('{}: {}'.format(self.user, what)) | |
class User(): | |
def __init__(self, uid, place = 'start'): | |
self.uid = uid | |
self.place = place | |
class DiscoAdventure(): | |
def __init__(self): | |
self.routes = {} | |
self.users = {} | |
def route(self, route_str): | |
def decorator(f): | |
self.routes[route_str] = f | |
return f | |
return decorator | |
def add_user(self, user_name, place = 'start'): | |
user = User(user_name) | |
self.users[user_name] = user | |
def generic_handler(self, message): | |
'''handle non adventure messages | |
return None if there is still more to be done | |
''' | |
if message.incoming == 'cookie': | |
return 'coookkkieie' | |
def process(self, message): | |
response = self.generic_handler(message) | |
if response: | |
return response | |
user = self.users.get(message.user) | |
if not user: | |
# user has not been registered, so send to generic room | |
# TODO: Make better errors for missing unregistered | |
handler = self.routes['unregistered'] | |
return handler(message) | |
handler = self.routes.get(user.place) | |
if not handler: | |
raise ValueError('Place "{}"" has not been registered'.format(path)) | |
return handler(user, message) | |
app = DiscoAdventure() | |
@app.route('unregistered') | |
def unregistered_room(message): | |
if message.incoming == 'register': | |
app.add_user(message.user) | |
return '{} has been registered'.format(message.user) | |
else: | |
return "I don't know {}".format(message.incoming) | |
@app.route('start') | |
def start_room(user, message): | |
msg = 'You are in the start room' | |
if message.incoming == 'look': | |
msg += '\n\tit is empty' | |
elif message.incoming.startswith('go '): | |
where = message.incoming[3:] | |
if where not in app.routes: | |
msg += '\n\ti do not know {}'.format(where) | |
else: | |
user.place = where | |
msg += '\n\t{} has entered {}'.format(user.uid, where) | |
return msg | |
@app.route('waterfall') | |
def waterfalll_room(user, message): | |
msg = 'You are in the start room' | |
if message.incoming == 'look': | |
msg += '\n\tthere is a water fall over there' | |
return msg | |
def main(): | |
run = [ | |
Message('test', 'gooop'), | |
Message('test', 'register'), | |
Message('test', 'look'), | |
Message('test', 'cookie'), | |
Message('test', 'go battle'), | |
Message('test', 'go waterfall'), | |
Message('test', 'look'), | |
] | |
for message in run: | |
response = app.process(message) | |
message.send_message(response) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment