From 659251e87d2140793cb5234d9376e9a6f75e1312 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 8 May 2013 22:43:03 -0700 Subject: [PATCH 01/18] moved index, removed django reqs --- requirements.txt | 2 -- {templates => static}/index.html | 1 - 2 files changed, 3 deletions(-) delete mode 100644 requirements.txt rename {templates => static}/index.html (97%) diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 17311a1..0000000 --- a/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -Django==1.5.1 -gunicorn==0.17.2 diff --git a/templates/index.html b/static/index.html similarity index 97% rename from templates/index.html rename to static/index.html index 5f8ac89..789ecb1 100644 --- a/templates/index.html +++ b/static/index.html @@ -1,4 +1,3 @@ -{% load staticfiles %} From aff09ed2a1b3e6182bc7801fc6ed96759301c586 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 8 May 2013 22:43:18 -0700 Subject: [PATCH 02/18] added initial go file --- main.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..7ffa295 --- /dev/null +++ b/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "flag" + "github.com/gorilla/sessions" + "log" + "net/http" +) + +var addr = flag.String("addr", ":8000", "address I'll listen on.") +var static_files = flag.String("static", "./static", "location of static files") + +var store = sessions.NewCookieStore([]byte("hello world")) + +func main() { + flag.Parse() + http.Handle("/", + http.FileServer(http.Dir(*static_files))) + if err := http.ListenAndServe(*addr, nil); err != nil { + log.Fatal("ListenAndServe:", err) + } +} From 60fc35e6c85edf08cbd2d9523a9bf339c709ee2f Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 8 May 2013 22:57:52 -0700 Subject: [PATCH 03/18] stubbed out go handlers --- handlers.go | 13 +++++++++++++ main.go | 2 ++ static/index.html | 8 ++++---- 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 handlers.go diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..3652453 --- /dev/null +++ b/handlers.go @@ -0,0 +1,13 @@ +package main + +import ( + "net/http" +) + +func attempt(w http.ResponseWriter, req *http.Request) { + w.Write([]byte("hello")) +} + +func problem(w http.ResponseWriter, req *http.Request) { + w.Write([]byte("hello")) +} diff --git a/main.go b/main.go index 7ffa295..49cdfb5 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,8 @@ func main() { flag.Parse() http.Handle("/", http.FileServer(http.Dir(*static_files))) + http.HandleFunc("/api/v0/attempt/", attempt) + http.HandleFunc("/api/v0/problem/", problem) if err := http.ListenAndServe(*addr, nil); err != nil { log.Fatal("ListenAndServe:", err) } diff --git a/static/index.html b/static/index.html index 789ecb1..09c0ff9 100644 --- a/static/index.html +++ b/static/index.html @@ -1,9 +1,9 @@ - {{ title }} + Mardson's Math Game - +
@@ -17,7 +17,7 @@
score:
- - + + From d3cbdfaf1ff7a53a25959c641a36a963aa941334 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 8 May 2013 23:11:03 -0700 Subject: [PATCH 04/18] returning random json objects from problem --- handlers.go | 23 +++++++++++++++++++++-- main.go | 3 +++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/handlers.go b/handlers.go index 3652453..71a51de 100644 --- a/handlers.go +++ b/handlers.go @@ -1,13 +1,32 @@ package main import ( + "encoding/json" + "fmt" + "log" + "math/rand" "net/http" ) func attempt(w http.ResponseWriter, req *http.Request) { - w.Write([]byte("hello")) + operation := "+" + if r := rand.Intn(2); r == 0 { + operation = "-" + } + + r := map[string]string{ + "operation": operation, + } + + b, err := json.Marshal(r) + if err != nil { + log.Fatal("issue with json marshalling") + } + j := string(b) + fmt.Println(j) + fmt.Fprintf(w, j) } func problem(w http.ResponseWriter, req *http.Request) { - w.Write([]byte("hello")) + fmt.Fprintf(w, "hello world") } diff --git a/main.go b/main.go index 49cdfb5..f10e77c 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "github.com/gorilla/sessions" "log" "net/http" + "math/rand" + "time" ) var addr = flag.String("addr", ":8000", "address I'll listen on.") @@ -13,6 +15,7 @@ var static_files = flag.String("static", "./static", "location of static files") var store = sessions.NewCookieStore([]byte("hello world")) func main() { + rand.Seed( time.Now().UTC().UnixNano()) flag.Parse() http.Handle("/", http.FileServer(http.Dir(*static_files))) From 8a17bd966f4b4675678ef870fc5c93891d81b74f Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 8 May 2013 23:11:51 -0700 Subject: [PATCH 05/18] ignore vim swap files --- .hgignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.hgignore b/.hgignore index 41ed3d8..590cdc8 100644 --- a/.hgignore +++ b/.hgignore @@ -1,2 +1,3 @@ \.pyc$ \.zip$ +\.swp$ From 8ae92884dd52a8f9bd888f0a1dcf88a20103d706 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 8 May 2013 23:21:18 -0700 Subject: [PATCH 06/18] implemented problem route --- handlers.go | 22 +++++++++++++++++++--- main.go | 2 ++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/handlers.go b/handlers.go index 71a51de..b53db79 100644 --- a/handlers.go +++ b/handlers.go @@ -8,21 +8,37 @@ import ( "net/http" ) +type prob struct { + Operation string + First int + Last int +} + func attempt(w http.ResponseWriter, req *http.Request) { operation := "+" if r := rand.Intn(2); r == 0 { operation = "-" } - r := map[string]string{ - "operation": operation, + first := rand.Intn(MAX) + var second int + if operation == "-" { + if first == 0 { + second = 0 + } else { + second = rand.Intn(first) + } + } else { + second = rand.Intn(MAX) } + r := prob{operation, first, second} + b, err := json.Marshal(r) if err != nil { log.Fatal("issue with json marshalling") } - j := string(b) + j := string(b) fmt.Println(j) fmt.Fprintf(w, j) } diff --git a/main.go b/main.go index f10e77c..15aaa09 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ import ( "time" ) +const MAX = 12 + var addr = flag.String("addr", ":8000", "address I'll listen on.") var static_files = flag.String("static", "./static", "location of static files") From a8ceec3d6bb6f4f8790875c24bd0d74af9cf6fcc Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 8 May 2013 23:21:42 -0700 Subject: [PATCH 07/18] misspelled route function names --- handlers.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/handlers.go b/handlers.go index b53db79..5c7a326 100644 --- a/handlers.go +++ b/handlers.go @@ -14,7 +14,7 @@ type prob struct { Last int } -func attempt(w http.ResponseWriter, req *http.Request) { +func problem(w http.ResponseWriter, req *http.Request) { operation := "+" if r := rand.Intn(2); r == 0 { operation = "-" @@ -43,6 +43,6 @@ func attempt(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, j) } -func problem(w http.ResponseWriter, req *http.Request) { +func attempt(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "hello world") } From 97f18f1df24a88e6d36e7b2df77e68e4aaf5050e Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 8 May 2013 23:41:59 -0700 Subject: [PATCH 08/18] added a json handler --- handlers.go | 7 +++++++ main.go | 4 ++-- static/math.js | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/handlers.go b/handlers.go index 5c7a326..6ab46f8 100644 --- a/handlers.go +++ b/handlers.go @@ -14,6 +14,13 @@ type prob struct { Last int } +type JsonHandler func(http.ResponseWriter, *http.Request) + +func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { + w.Header().Set("Content-Type", "application/json") + h(w, req) +} + func problem(w http.ResponseWriter, req *http.Request) { operation := "+" if r := rand.Intn(2); r == 0 { diff --git a/main.go b/main.go index 15aaa09..4e1f35e 100644 --- a/main.go +++ b/main.go @@ -21,8 +21,8 @@ func main() { flag.Parse() http.Handle("/", http.FileServer(http.Dir(*static_files))) - http.HandleFunc("/api/v0/attempt/", attempt) - http.HandleFunc("/api/v0/problem/", problem) + http.HandleFunc("/api/v0/attempt/", JsonHandler(attempt)) + http.Handle("/api/v0/problem/", JsonHandler(problem)) if err := http.ListenAndServe(*addr, nil); err != nil { log.Fatal("ListenAndServe:", err) } diff --git a/static/math.js b/static/math.js index 1cfb180..7600e6d 100644 --- a/static/math.js +++ b/static/math.js @@ -7,7 +7,7 @@ function update_board(f, o, s, score) { function new_problem() { $.get("/api/v0/problem/", function(d) { - update_board(d["first"], d["operation"], d["second"], d["score"]); + update_board(d["First"], d["Operation"], d["Second"], d["Score"]); $("#answer").val(""); }) } From 4fa2e2bbd36e712c36a095c215e2cf1b23413d12 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 8 May 2013 23:43:39 -0700 Subject: [PATCH 09/18] should be Seocnd, not Last --- handlers.go | 2 +- main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/handlers.go b/handlers.go index 6ab46f8..80e8191 100644 --- a/handlers.go +++ b/handlers.go @@ -11,7 +11,7 @@ import ( type prob struct { Operation string First int - Last int + Second int } type JsonHandler func(http.ResponseWriter, *http.Request) diff --git a/main.go b/main.go index 4e1f35e..fe24991 100644 --- a/main.go +++ b/main.go @@ -21,8 +21,8 @@ func main() { flag.Parse() http.Handle("/", http.FileServer(http.Dir(*static_files))) - http.HandleFunc("/api/v0/attempt/", JsonHandler(attempt)) http.Handle("/api/v0/problem/", JsonHandler(problem)) + http.Handle("/api/v0/attempt/", JsonHandler(attempt)) if err := http.ListenAndServe(*addr, nil); err != nil { log.Fatal("ListenAndServe:", err) } From 6aaa72475aa5a89e2e9e7a294bf91e92ea8664ac Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Thu, 9 May 2013 00:16:54 -0700 Subject: [PATCH 10/18] implemented answer checking, modulo scores --- handlers.go | 55 ++++++++++++++++++++++++++++++++++++++++++++------ static/math.js | 2 +- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/handlers.go b/handlers.go index 80e8191..f2c09fb 100644 --- a/handlers.go +++ b/handlers.go @@ -6,19 +6,26 @@ import ( "log" "math/rand" "net/http" + "strconv" ) type prob struct { Operation string First int - Second int + Second int + Score int +} + +type solution struct { + Status bool + Score int } type JsonHandler func(http.ResponseWriter, *http.Request) func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") - h(w, req) + h(w, req) } func problem(w http.ResponseWriter, req *http.Request) { @@ -39,17 +46,53 @@ func problem(w http.ResponseWriter, req *http.Request) { second = rand.Intn(MAX) } - r := prob{operation, first, second} + r := prob{operation, first, second, 66} b, err := json.Marshal(r) if err != nil { - log.Fatal("issue with json marshalling") + log.Fatal("issue with json marshalling", err) } j := string(b) - fmt.Println(j) + fmt.Println("problem", j) fmt.Fprintf(w, j) } func attempt(w http.ResponseWriter, req *http.Request) { - fmt.Fprintf(w, "hello world") + first, err := strconv.Atoi(req.FormValue("first")) + if err != nil { + log.Fatal("cannot parse first", err) + } + operation := req.FormValue("operation") + second, err := strconv.Atoi(req.FormValue("second")) + if err != nil { + log.Fatal("cannot parse second", err) + } + var guess int + answer := req.FormValue("answer") + if answer == "" { + guess = 0 + } else { + guess, err = strconv.Atoi(answer) + if err != nil { + log.Fatal("cannot parser answer", err) + } + } + if err != nil { + log.Fatal("cannot parse answer", err) + } + + var result bool + if operation == "+" { + result = first+second == guess + } else if operation == "-" { + result = first-second == guess + } + + b, err := json.Marshal(solution{result, 66}) + if err != nil { + log.Fatal("cannot marshal solution", err) + } + j := string(b) + fmt.Println("attempt", j) + fmt.Fprintf(w, j) } diff --git a/static/math.js b/static/math.js index 7600e6d..f31dede 100644 --- a/static/math.js +++ b/static/math.js @@ -20,7 +20,7 @@ function deal_with_answer(e) { "answer": $("#answer").val(), }; $.post("/api/v0/attempt/", data, function(d) { - if(d["status"]) { + if(d["Status"]) { $("body").removeClass("wrong"); new_problem(); } From 8458c384681f8742b88fa4231d1f9113e4e7d2ed Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Thu, 9 May 2013 00:37:35 -0700 Subject: [PATCH 11/18] implemented scores --- handlers.go | 40 +++++++++++++++++++++++++++++++++++----- static/math.js | 1 + 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/handlers.go b/handlers.go index f2c09fb..994cecb 100644 --- a/handlers.go +++ b/handlers.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "github.com/gorilla/sessions" "log" "math/rand" "net/http" @@ -28,6 +29,20 @@ func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { h(w, req) } +func getScore(session *sessions.Session) int { + score := session.Values["Score"] + var parsed_score int + if score == nil { + parsed_score = 0 + } else { + parsed_score = score.(int) + } + return parsed_score +} + +func setScore(session *sessions.Session) { +} + func problem(w http.ResponseWriter, req *http.Request) { operation := "+" if r := rand.Intn(2); r == 0 { @@ -46,7 +61,10 @@ func problem(w http.ResponseWriter, req *http.Request) { second = rand.Intn(MAX) } - r := prob{operation, first, second, 66} + session, _ := store.Get(req, "Score") + score := getScore(session) + + r := prob{operation, first, second, score} b, err := json.Marshal(r) if err != nil { @@ -62,11 +80,14 @@ func attempt(w http.ResponseWriter, req *http.Request) { if err != nil { log.Fatal("cannot parse first", err) } + operation := req.FormValue("operation") + second, err := strconv.Atoi(req.FormValue("second")) if err != nil { log.Fatal("cannot parse second", err) } + var guess int answer := req.FormValue("answer") if answer == "" { @@ -77,9 +98,6 @@ func attempt(w http.ResponseWriter, req *http.Request) { log.Fatal("cannot parser answer", err) } } - if err != nil { - log.Fatal("cannot parse answer", err) - } var result bool if operation == "+" { @@ -88,7 +106,19 @@ func attempt(w http.ResponseWriter, req *http.Request) { result = first-second == guess } - b, err := json.Marshal(solution{result, 66}) + session, _ := store.Get(req, "Score") + score := getScore(session) + if result { + score += 1 + } else { + score -= 1 + } + + session, _ = store.Get(req, "Score") + session.Values["Score"] = score + session.Save(req, w) + + b, err := json.Marshal(solution{result, score}) if err != nil { log.Fatal("cannot marshal solution", err) } diff --git a/static/math.js b/static/math.js index f31dede..8a577ae 100644 --- a/static/math.js +++ b/static/math.js @@ -20,6 +20,7 @@ function deal_with_answer(e) { "answer": $("#answer").val(), }; $.post("/api/v0/attempt/", data, function(d) { + $("#score").text(d["Score"]); if(d["Status"]) { $("body").removeClass("wrong"); new_problem(); From 1bf4f37921af7a91e7167afa3ec21417e82d59c6 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Thu, 9 May 2013 00:39:21 -0700 Subject: [PATCH 12/18] got rid of extraneous func --- handlers.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/handlers.go b/handlers.go index 994cecb..5984430 100644 --- a/handlers.go +++ b/handlers.go @@ -40,9 +40,6 @@ func getScore(session *sessions.Session) int { return parsed_score } -func setScore(session *sessions.Session) { -} - func problem(w http.ResponseWriter, req *http.Request) { operation := "+" if r := rand.Intn(2); r == 0 { From a2671f2d77bdecbf66552f5758ec445f1794396c Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Thu, 9 May 2013 00:41:06 -0700 Subject: [PATCH 13/18] removed extraneous call to store.Get --- handlers.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/handlers.go b/handlers.go index 5984430..a85f67e 100644 --- a/handlers.go +++ b/handlers.go @@ -110,8 +110,6 @@ func attempt(w http.ResponseWriter, req *http.Request) { } else { score -= 1 } - - session, _ = store.Get(req, "Score") session.Values["Score"] = score session.Save(req, w) From 7667aa1a9363e0fb2697e27fb3e1400a65a8ed06 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Thu, 9 May 2013 00:42:30 -0700 Subject: [PATCH 14/18] removed django files --- README.md | 6 ++++ README.rst | 9 ----- manage.py | 10 ------ mmg/__init__.py | 0 mmg/settings.py | 88 ------------------------------------------------- mmg/urls.py | 8 ----- mmg/views.py | 63 ----------------------------------- mmg/wsgi.py | 32 ------------------ 8 files changed, 6 insertions(+), 210 deletions(-) create mode 100644 README.md delete mode 100644 README.rst delete mode 100644 manage.py delete mode 100644 mmg/__init__.py delete mode 100644 mmg/settings.py delete mode 100644 mmg/urls.py delete mode 100644 mmg/views.py delete mode 100644 mmg/wsgi.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..0f578a5 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# mmg.mcquay.me + +## background + +This is a silly little go project that I created to help my child learn his +addition/subtraction tables. diff --git a/README.rst b/README.rst deleted file mode 100644 index 3323410..0000000 --- a/README.rst +++ /dev/null @@ -1,9 +0,0 @@ -============= -mmg.mcquay.me -============= - -background -========== - -This is a silly little django project that I created to help my child learn his -addition/subtraction tables. diff --git a/manage.py b/manage.py deleted file mode 100644 index 256b565..0000000 --- a/manage.py +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env python -import os -import sys - -if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mmg.settings") - - from django.core.management import execute_from_command_line - - execute_from_command_line(sys.argv) diff --git a/mmg/__init__.py b/mmg/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/mmg/settings.py b/mmg/settings.py deleted file mode 100644 index ca0b45a..0000000 --- a/mmg/settings.py +++ /dev/null @@ -1,88 +0,0 @@ -import os - -DEBUG = False -TEMPLATE_DEBUG = DEBUG - -ADMINS = ( - ('Stephen McQuay', 'stephen@mcquay.me'), -) -MANAGERS = ADMINS -ALLOWED_HOSTS = ['mmg.mcquay.me', 'midna.local'] - -TIME_ZONE = 'America/Chicago' - -LANGUAGE_CODE = 'en-us' -SITE_ID = 1 -USE_I18N = True -USE_L10N = True -USE_TZ = True -MEDIA_ROOT = '' -MEDIA_URL = '' - -STATIC_ROOT = os.path.expanduser('~/tmp/mmg') -STATIC_URL = 'http://tmp.mcquay.me/mmg/' -STATICFILES_DIRS = ( - os.path.expanduser('~/src/mmg/static'), -) - -STATICFILES_FINDERS = ( - 'django.contrib.staticfiles.finders.FileSystemFinder', - 'django.contrib.staticfiles.finders.AppDirectoriesFinder', -) - -SECRET_KEY = os.environ.get('MMG_SECRET_KEY') - -TEMPLATE_LOADERS = ( - 'django.template.loaders.filesystem.Loader', - 'django.template.loaders.app_directories.Loader', -) - -MIDDLEWARE_CLASSES = ( - 'django.middleware.common.CommonMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', -) - -ROOT_URLCONF = 'mmg.urls' - -WSGI_APPLICATION = 'mmg.wsgi.application' - -TEMPLATE_DIRS = ( - os.path.expanduser('~/src/mmg/templates'), -) - -INSTALLED_APPS = ( - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.sites', - 'django.contrib.messages', - 'django.contrib.staticfiles', -) - -SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies' - -LOGGING = { - 'version': 1, - 'disable_existing_loggers': False, - 'filters': { - 'require_debug_false': { - '()': 'django.utils.log.RequireDebugFalse' - } - }, - 'handlers': { - 'mail_admins': { - 'level': 'ERROR', - 'filters': ['require_debug_false'], - 'class': 'django.utils.log.AdminEmailHandler' - } - }, - 'loggers': { - 'django.request': { - 'handlers': ['mail_admins'], - 'level': 'ERROR', - 'propagate': True, - }, - } -} diff --git a/mmg/urls.py b/mmg/urls.py deleted file mode 100644 index 535f3ca..0000000 --- a/mmg/urls.py +++ /dev/null @@ -1,8 +0,0 @@ -from django.conf.urls import patterns, url - -urlpatterns = patterns( - '', - url(r'^$', 'mmg.views.home', name='home'), - url(r'^api/v0/attempt/$', 'mmg.views.attempt', name='attempt'), - url(r'^api/v0/problem/$', 'mmg.views.problem', name='problem'), -) diff --git a/mmg/views.py b/mmg/views.py deleted file mode 100644 index 60ababf..0000000 --- a/mmg/views.py +++ /dev/null @@ -1,63 +0,0 @@ -import json -import random - -from django.http import HttpResponse -from django.shortcuts import render - -MAX = 12 - - -def _generate_problem(): - operation = random.choice(['+', '-']) - first = random.choice(range(MAX)) - if operation == '-': - if first == 0: - second = 0 - else: - second = random.choice(range(first)) - else: - second = random.choice(range(MAX)) - return { - 'first': first, - 'operation': operation, - 'second': second, - } - - -def _validate_solution(a): - f = int(a['first']) - o = a['operation'] - s = int(a['second']) - if a['answer'] == '': - g = 0 - else: - g = int(a['answer']) - r = False - if o == '+': - r = bool(f + s == g) - else: - r = bool(f - s == g) - return r - - -def home(request): - return render(request, 'index.html') - - -def attempt(request): - d = request.POST.dict() - r = _validate_solution(d) - if r: - s = request.session.get('score', 0) + 1 - else: - s = request.session.get('score', 0) - 1 - request.session['score'] = s - return HttpResponse( - json.dumps({'status': r, 'score': s}), - content_type="application/json") - - -def problem(request): - d = _generate_problem() - d['score'] = request.session.get('score', 0) - return HttpResponse(json.dumps(d), content_type="application/json") diff --git a/mmg/wsgi.py b/mmg/wsgi.py deleted file mode 100644 index 12ad688..0000000 --- a/mmg/wsgi.py +++ /dev/null @@ -1,32 +0,0 @@ -""" -WSGI config for mmg project. - -This module contains the WSGI application used by Django's development server -and any production WSGI deployments. It should expose a module-level variable -named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover -this application via the ``WSGI_APPLICATION`` setting. - -Usually you will have the standard Django WSGI application here, but it also -might make sense to replace the whole Django WSGI application with a custom one -that later delegates to the Django one. For example, you could introduce WSGI -middleware here, or combine a Django application with an application of another -framework. - -""" -import os - -# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks -# if running multiple sites in the same mod_wsgi process. To fix this, use -# mod_wsgi daemon mode with each site in its own daemon process, or use -# os.environ["DJANGO_SETTINGS_MODULE"] = "mmg.settings" -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mmg.settings") - -# This application object is used by any WSGI server configured to use this -# file. This includes Django's development server, if the WSGI_APPLICATION -# setting points here. -from django.core.wsgi import get_wsgi_application -application = get_wsgi_application() - -# Apply WSGI middleware here. -# from helloworld.wsgi import HelloWorldApplication -# application = HelloWorldApplication(application) From e1e7c1619397c0db75696adb2f3c2b11ec1fcc3e Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Thu, 9 May 2013 00:48:42 -0700 Subject: [PATCH 15/18] ignore mmg binary --- .hgignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.hgignore b/.hgignore index 590cdc8..041f497 100644 --- a/.hgignore +++ b/.hgignore @@ -1,3 +1,4 @@ \.pyc$ \.zip$ \.swp$ +mmg From b8e0bd64f6092092ba377bbe09093ff2d7ca3898 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Thu, 9 May 2013 00:48:56 -0700 Subject: [PATCH 16/18] gofmt --- handlers.go | 14 +++++++------- main.go | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/handlers.go b/handlers.go index a85f67e..c8591bc 100644 --- a/handlers.go +++ b/handlers.go @@ -105,13 +105,13 @@ func attempt(w http.ResponseWriter, req *http.Request) { session, _ := store.Get(req, "Score") score := getScore(session) - if result { - score += 1 - } else { - score -= 1 - } - session.Values["Score"] = score - session.Save(req, w) + if result { + score += 1 + } else { + score -= 1 + } + session.Values["Score"] = score + session.Save(req, w) b, err := json.Marshal(solution{result, score}) if err != nil { diff --git a/main.go b/main.go index fe24991..9172626 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,9 @@ import ( "flag" "github.com/gorilla/sessions" "log" + "math/rand" "net/http" - "math/rand" - "time" + "time" ) const MAX = 12 @@ -17,12 +17,12 @@ var static_files = flag.String("static", "./static", "location of static files") var store = sessions.NewCookieStore([]byte("hello world")) func main() { - rand.Seed( time.Now().UTC().UnixNano()) + rand.Seed(time.Now().UTC().UnixNano()) flag.Parse() http.Handle("/", http.FileServer(http.Dir(*static_files))) - http.Handle("/api/v0/problem/", JsonHandler(problem)) - http.Handle("/api/v0/attempt/", JsonHandler(attempt)) + http.Handle("/api/v0/problem/", JsonHandler(problem)) + http.Handle("/api/v0/attempt/", JsonHandler(attempt)) if err := http.ListenAndServe(*addr, nil); err != nil { log.Fatal("ListenAndServe:", err) } From a1286417ea0a618e41a71135978d9807b981c2a9 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Thu, 9 May 2013 00:49:04 -0700 Subject: [PATCH 17/18] also allow submission on space --- static/math.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/math.js b/static/math.js index 8a577ae..f49a1c1 100644 --- a/static/math.js +++ b/static/math.js @@ -36,7 +36,7 @@ function deal_with_answer(e) { $(function() { new_problem(); $("#answer").keypress(function(e) { - if( e.which == 13) { + if (e.which == 13 || e.which == 32) { deal_with_answer(e); } }); From 984dba924238d166907a823ed08d675e4d4f061f Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Thu, 9 May 2013 00:53:17 -0700 Subject: [PATCH 18/18] grab secret key from env --- main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 9172626..6ee659d 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "log" "math/rand" "net/http" + "os" "time" ) @@ -14,7 +15,7 @@ const MAX = 12 var addr = flag.String("addr", ":8000", "address I'll listen on.") var static_files = flag.String("static", "./static", "location of static files") -var store = sessions.NewCookieStore([]byte("hello world")) +var store = sessions.NewCookieStore([]byte(os.Getenv("MMG_SECRET_KEY"))) func main() { rand.Seed(time.Now().UTC().UnixNano())