~johanvandegriff/games.johanv.xyz

games.johanv.xyz/games.py -rw-r--r-- 4.7 KiB
f5159cbaJohan Vandegriff more hornswiggle words 15 days ago
                                                                                
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import Flask, request, render_template, url_for

import os, re, sys

import CARL, boggle, boggle_old
import hornswiggle
from nav import nav
import profanity_test

app = Flask(__name__)

def run_perl_blacklist(s):
    singlequote = "'"
    return re.sub('[";'+singlequote+']', '', s)


def run_perl(request, perl_program):
    args = request.args.to_dict()
    cgi_vars = ""
    for v in args:
        cgi_vars += " " + '"' + v + "=" + run_perl_blacklist(args[v]).replace('+', '%2B') + '"'
    #return '<pre>'+cgi_vars+'</pre>' + os.popen("perl " + perl_program + cgi_vars).read()
    return os.popen("perl " + perl_program + cgi_vars).read()

def run_perl_page(request, perl_program, title):
    page = run_perl(request, perl_program)
    return render_template("header.html", nav=nav, active=title) + page + render_template("footer.html")

@app.route("/")
def main():
    return render_template("index.html", nav=nav, active="games")

#START checks
@app.route("/check.txt")
def check():
    return "success"

@app.route("/profanity-check-1")
def profanity_check_1():
    return profanity_test.test()

@app.route("/profanity-check-2")
def profanity_check_2():
    try:
        channel = request.args.get("channel", "")
    except ValueError:
        channel = ""
    return profanity_test.test2(channel)

@app.route("/tensorflow-check")
def tensorflow_check():
    import tensorflow as tf
    return tf.__version__
#END checks

#START STOICHIOMETRY
def chem_whitelist(s):
    return re.sub('[^a-zA-Z\d\w.()+-=>→↔⇆]', '', s)

@app.route("/chem", methods=["GET", "POST"])
def stoichiometry():
    args = request.args.to_dict()
    result = ""
    equation = ""
    compound = ""
    grams_or_moles = ""
    grams_or_moles_value = ""

    if "equation" in args:
        equation = chem_whitelist(args["equation"])
        optional_args = ""
        if "compound" in args and "grams_or_moles" in args and "grams_or_moles_value" in args:
            compound = chem_whitelist(args["compound"])
            grams_or_moles = args["grams_or_moles"]
            grams_or_moles_value = chem_whitelist(args["grams_or_moles_value"])
            optional_args = " '" + compound + "' " + grams_or_moles_value + " " + grams_or_moles
        result = os.popen("java -jar Stoichiometry.jar '" + equation + "'" + optional_args).read()
    return render_template("stoichiometry.html", nav=nav, active="chem", result=result, equation=equation, compound=compound, grams_or_moles_value=grams_or_moles_value, grams_or_moles=grams_or_moles)
#END STOICHIOMETRY

#START MATH
@app.route("/math")
def math_page():
    return render_template("math.html", nav=nav, active="math")

@app.route("/math_game.pl", methods=["GET", "POST"])
def math_game():
    return run_perl_page(request, "math_game.pl", "Math")

@app.route("/math_score.pl", methods=["GET", "POST"])
def math_score():
    return run_perl_page(request, "math_score.pl", "Math")
#END MATH

#START WHACK
@app.route("/whack.pl", methods=["GET", "POST"])
def whack_page():
    return run_perl_page(request, "whack.pl", "whack")
#END WHACK

#START MAZE
@app.route("/maze")
def maze_page():
    return render_template("maze.html", nav=nav, active="maze")

@app.route("/showmaze.pl", methods=["GET", "POST"])
def showmaze_page():
    return run_perl_page(request, "showmaze.pl", "maze")
#END MAZE

#START BOGGLE
@app.route("/boggle", methods=["GET", "POST"])
def boggle_page():
    return boggle.app(request)
#END BOGGLE

#START BOGGLE_OLD
@app.route("/boggle_old", methods=["GET", "POST"])
def boggle_old_page():
    return boggle_old.page(request.args)
#END BOGGLE_OLD

#START CARL
@app.route("/carl_api", methods=["GET", "POST"])
def carl_api():
    carl = request.args.get("carl", "")
    user = request.args.get("user", "")
    allowProfanity = request.args.get("profanity", "") == "true"

    return CARL.answer(carl, user, allowProfanity)

@app.route("/carl", methods=["GET", "POST"])
def carl_page():
    carl = request.args.get("carl", "")
    user = request.args.get("user", "")
    allowProfanity = request.args.get("profanity", "") == "true"

    carl2 = CARL.answer(carl, user, allowProfanity)

    return render_template(
        "carl.html",
        nav=nav,
        active="CARL",
        allowProfanity=allowProfanity,
        carl=carl,
        user=user,
        carl2=carl2
    )
#END CARL

#START HORNSWIGGLE
@app.route("/hornswiggle_api", methods=["GET", "POST"])
def hornswiggle_api():
    return hornswiggle.generate()

@app.route("/hornswiggle", methods=["GET", "POST"])
def hornswiggle_page():
    return render_template(
        "hornswiggle.html",
        nav=nav,
        active="Dr. H",
        item=hornswiggle.generate()
    )
#END HORNSWIGGLE

if __name__ == "__main__":
    # app.run()
    app.run(host= '0.0.0.0') # for local testing