summaryrefslogtreecommitdiffstats
path: root/nfsw/io.py
blob: 2716151868511a62353df6de259ab7852962463b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
#
#   SPDX-License-Identifier: ISC
#
#   Copyright (C) 2019 rsiddharth <s@ricketyspace.net>
#
#   This file is part of nfsw.
#

import functools

import nfsw.scenes as scenes

from nfsw.auth import login_required, login_required_ajax, logout
from nfsw.redis import redis
from nfsw.scenes import current_scene
from nfsw.util import read_junk

from flask import (
    Blueprint, render_template, request
)


bp = Blueprint('io', __name__)


def preprocess(view):
    def setup():
        r = redis()

        if not r.exists('scene'):
            r.set('scene', 'sexshop')


    @functools.wraps(view)
    def wrapped_view(**kwargs):
        setup()

        return view(**kwargs)

    return wrapped_view


@bp.route('/io', endpoint='io')
@login_required
def io():
    return render_template('io.html')


@bp.route('/io/reset')
@login_required
def reset():
    r = redis()

    for k in r.keys():
        r.delete(k)

    return 'Game reset'


@bp.route('/io/query', methods=['POST'])
@login_required_ajax
@preprocess
def query():
    r = redis()

    def sanitize(q):
        return q.strip().lower()

    r = redis()
    q = request.get_data(as_text=True)

    # Log query.
    r.rpush('log', q)


    # logout
    if q == 'nfsw logout':
        logout()
        return {
            'ans': 'Logging you out...',
            'logout': True
        }


    # help.
    if q == 'help':
        return {
            'ans': read_junk('help/senditdown')
        }

    # reset.
    if q == 'nfsw reset':
        reset()
        return {
            'ans': 'Initiating game reset...',
            'reset': True
        }

    # colophon
    if q == 'colophon':
        return {
            'ans': read_junk('colophon/ohno')
        }

    # Get current scene.
    scene = current_scene()
    if scene is None:
        return {
            'ans': 'Your game state is fucked. Ping Siddharth.'
        }

    # Respond.
    return {
        'ans': scene({'q': sanitize(q)})
    }


@bp.route('/io/intro', methods=['POST'])
@login_required_ajax
@preprocess
def intro():

    # Get current scene
    scene = current_scene()
    if scene is None:
        return {
            'intro': read_junk('foobar/god')
        }, 500

    return {
        'intro': scene({'intro': 1})
    }