This commit is contained in:
ZaneYork 2024-09-10 10:55:19 +08:00
commit 7c35b0f46a
14 changed files with 748 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/.idea/
*.sqlite3

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# 自助投票抽签点餐工具
解决晚餐点餐困难问题

138
dao.py Normal file
View File

@ -0,0 +1,138 @@
import sqlite3
from datetime import datetime, timedelta
from flask import request
db_path = './data.sqlite3'
db = sqlite3.connect(db_path)
cursor = db.cursor()
try:
cursor.execute('''
create table if not exists user_menu
(
user text,
menu text,
datestr text,
primary key (user, datestr)
)
''')
cursor.execute('''
create table if not exists roll_result
(
datestr text primary key,
value text
)
''')
cursor.execute('''
create table if not exists menu
(
name text primary key,
label text
)
''')
db.commit()
finally:
cursor.close()
db.close()
def get_user():
client_ip = request.remote_addr
return client_ip
def get_user_menu():
user = get_user()
db = sqlite3.connect(db_path)
cursor = db.cursor()
datestr = datetime.now().strftime('%Y-%m-%d')
try:
cursor.execute("select menu from user_menu where user = ? and datestr=?", (user, datestr))
row = cursor.fetchone()
if row:
return row[0]
else:
cursor.execute("select menu from user_menu where user = ? order by datestr desc limit 1", (user,))
row = cursor.fetchone()
if row:
return row[0]
return ''
finally:
cursor.close()
db.close()
def set_user_menu(menu, user=None):
datestr = datetime.now().strftime('%Y-%m-%d')
if user is None:
user = get_user()
db = sqlite3.connect(db_path)
cursor = db.cursor()
try:
cursor.execute("insert or replace into user_menu(user,menu,datestr) values(?,?,?)",
(user, menu, datestr))
db.commit()
finally:
cursor.close()
db.close()
def fetch_all_today_menu():
datestr = datetime.now().strftime('%Y-%m-%d')
db = sqlite3.connect(db_path)
cursor = db.cursor()
try:
cursor.execute("select user, menu from user_menu where menu != '' and datestr=? order by user",
(datestr,))
row = cursor.fetchone()
while row:
yield row[0], row[1]
row = cursor.fetchone()
finally:
cursor.close()
db.close()
def fetch_all_menu():
db = sqlite3.connect(db_path)
cursor = db.cursor()
try:
cursor.execute("select name, label from menu order by order_num")
row = cursor.fetchone()
while row:
yield row[0], row[1]
row = cursor.fetchone()
finally:
cursor.close()
db.close()
def fetch_roll_result(interval=0):
date = datetime.now() + timedelta(days=interval)
datestr = date.strftime('%Y-%m-%d')
db = sqlite3.connect(db_path)
cursor = db.cursor()
try:
cursor.execute("select value from roll_result where datestr=?",
(datestr,))
row = cursor.fetchone()
if row:
return row[0]
return None
finally:
cursor.close()
db.close()
def set_roll_result(value):
datestr = datetime.now().strftime('%Y-%m-%d')
db = sqlite3.connect(db_path)
cursor = db.cursor()
try:
cursor.execute("insert or replace into roll_result(datestr, value) values(?,?)",
(datestr, value))
db.commit()
finally:
cursor.close()
db.close()

114
dinner.py Normal file
View File

@ -0,0 +1,114 @@
import json
from session import app
from flask import send_from_directory, render_template, request, make_response, abort
import re
import random
from dao import *
MOBILE_AGENTS_PATTERN = re.compile(
r"(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino",
re.IGNORECASE,
)
def is_mobile_request(user_agent):
return bool(MOBILE_AGENTS_PATTERN.search(user_agent))
def fetch_menu_summary():
all_menu = list(fetch_all_today_menu())
if len(all_menu) > 0:
menus = list(map(lambda x: json.loads(x[1]), all_menu))
menu_keys = set()
for x in menus:
for k in x:
menu_keys.add(k)
result = {}
for k in menu_keys:
result[k] = 0
for x in menus:
for k in x:
result[k] += x[k]
return result
return []
def within_time():
start_time = datetime.strptime(str(datetime.now().date()) + ' 8:00', '%Y-%m-%d %H:%M')
end_time = datetime.strptime(str(datetime.now().date()) + ' 17:30', '%Y-%m-%d %H:%M')
if start_time < datetime.now() < end_time:
return True
return False
def check_roll():
result = fetch_roll_result()
if result is not None:
return -1
return 0 if within_time() else 1
@app.route('/dinner/update')
def dinner_update():
user_agent_string = request.headers.get('User-Agent')
if not is_mobile_request(user_agent_string):
abort(403)
if check_roll() != 0:
return make_response(json.dumps(dict(code=-1, data="来晚了,提交失败")))
value = request.args.get('value').strip()
if not value:
set_user_menu('')
return make_response(json.dumps(dict(code=0, data="OK")))
value = json.loads(value)
summary = 0
for key in value:
summary += int(value[key])
if summary <= 0:
set_user_menu('')
return make_response(json.dumps(dict(code=0, data="OK")))
for key in value:
value[key] = int(value[key]) / summary
set_user_menu(json.dumps(value))
return make_response(json.dumps(dict(code=0, data="OK")))
@app.route('/dinner/roll')
def dinner_roll():
if check_roll() != 1:
return make_response(json.dumps(dict(code=-1, data="目前不能抽签")))
summary = fetch_menu_summary()
last_result = fetch_roll_result(-1)
for k in summary:
if last_result == k:
summary[k] = summary[k] * 7 / 10
summary[k] = int(round(summary[k] * 100))
pool = []
for k in summary:
for i in range(summary[k]):
pool.append(k)
random.shuffle(pool)
result = pool[0]
set_roll_result(result)
return make_response(json.dumps(dict(code=0, data="OK")))
@app.route('/dinner')
def dinner():
menu = get_user_menu()
if menu:
menu = json.loads(menu)
else:
menu = {}
summary = fetch_menu_summary()
result = fetch_roll_result()
last_result = fetch_roll_result(-1)
can_roll = (check_roll() == 1)
all_choice = list(map(lambda x: {'name': x[0], 'label': x[1]}, fetch_all_menu()))
return render_template('dinner.html',
all_choice=all_choice,
menu=menu,
summary=summary,
result=result,
can_roll=can_roll,
last_result=last_result)

3
session.py Normal file
View File

@ -0,0 +1,3 @@
from flask import Flask
app = Flask(__name__)

20
start.py Normal file
View File

@ -0,0 +1,20 @@
import subprocess, os
from flask import render_template, redirect
import threading
from dinner import *
from session import app
lock = threading.Lock()
@app.route('/')
def index():
return redirect('dinner')
# return render_template('index.html')
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'fd-logo.png')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=80, debug=True, threaded=True)

2
start_web.bat Normal file
View File

@ -0,0 +1,2 @@
python -u start.py
pause

7
static/bootstrap.bundle.min.js vendored Normal file

File diff suppressed because one or more lines are too long

6
static/bootstrap.min.css vendored Normal file

File diff suppressed because one or more lines are too long

BIN
static/fd-logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

4
static/jquery-3.2.1.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,338 @@
/*!
SerializeJSON jQuery plugin.
https://github.com/marioizquierdo/jquery.serializeJSON
version 3.2.1 (Feb, 2021)
Copyright (c) 2012-2021 Mario Izquierdo
Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*/
(function (factory) {
/* global define, require, module */
if (typeof define === "function" && define.amd) { // AMD. Register as an anonymous module.
define(["jquery"], factory);
} else if (typeof exports === "object") { // Node/CommonJS
var jQuery = require("jquery");
module.exports = factory(jQuery);
} else { // Browser globals (zepto supported)
factory(window.jQuery || window.Zepto || window.$); // Zepto supported on browsers as well
}
}(function ($) {
"use strict";
var rCRLF = /\r?\n/g;
var rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i;
var rsubmittable = /^(?:input|select|textarea|keygen)/i;
var rcheckableType = /^(?:checkbox|radio)$/i;
$.fn.serializeJSON = function (options) {
var f = $.serializeJSON;
var $form = this; // NOTE: the set of matched elements is most likely a form, but it could also be a group of inputs
var opts = f.setupOpts(options); // validate options and apply defaults
var typeFunctions = $.extend({}, opts.defaultTypes, opts.customTypes);
// Make a list with {name, value, el} for each input element
var serializedArray = f.serializeArray($form, opts);
// Convert the serializedArray into a serializedObject with nested keys
var serializedObject = {};
$.each(serializedArray, function (_i, obj) {
var nameSansType = obj.name;
var type = $(obj.el).attr("data-value-type");
if (!type && !opts.disableColonTypes) { // try getting the type from the input name
var p = f.splitType(obj.name); // "foo:string" => ["foo", "string"]
nameSansType = p[0];
type = p[1];
}
if (type === "skip") {
return; // ignore fields with type skip
}
if (!type) {
type = opts.defaultType; // "string" by default
}
var typedValue = f.applyTypeFunc(obj.name, obj.value, type, obj.el, typeFunctions); // Parse type as string, number, etc.
if (!typedValue && f.shouldSkipFalsy(obj.name, nameSansType, type, obj.el, opts)) {
return; // ignore falsy inputs if specified in the options
}
var keys = f.splitInputNameIntoKeysArray(nameSansType);
f.deepSet(serializedObject, keys, typedValue, opts);
});
return serializedObject;
};
// Use $.serializeJSON as namespace for the auxiliar functions
// and to define defaults
$.serializeJSON = {
defaultOptions: {}, // reassign to override option defaults for all serializeJSON calls
defaultBaseOptions: { // do not modify, use defaultOptions instead
checkboxUncheckedValue: undefined, // to include that value for unchecked checkboxes (instead of ignoring them)
useIntKeysAsArrayIndex: false, // name="foo[2]" value="v" => {foo: [null, null, "v"]}, instead of {foo: ["2": "v"]}
skipFalsyValuesForTypes: [], // skip serialization of falsy values for listed value types
skipFalsyValuesForFields: [], // skip serialization of falsy values for listed field names
disableColonTypes: false, // do not interpret ":type" suffix as a type
customTypes: {}, // extends defaultTypes
defaultTypes: {
"string": function(str) { return String(str); },
"number": function(str) { return Number(str); },
"boolean": function(str) { var falses = ["false", "null", "undefined", "", "0"]; return falses.indexOf(str) === -1; },
"null": function(str) { var falses = ["false", "null", "undefined", "", "0"]; return falses.indexOf(str) === -1 ? str : null; },
"array": function(str) { return JSON.parse(str); },
"object": function(str) { return JSON.parse(str); },
"skip": null // skip is a special type used to ignore fields
},
defaultType: "string",
},
// Validate and set defaults
setupOpts: function(options) {
if (options == null) options = {};
var f = $.serializeJSON;
// Validate
var validOpts = [
"checkboxUncheckedValue",
"useIntKeysAsArrayIndex",
"skipFalsyValuesForTypes",
"skipFalsyValuesForFields",
"disableColonTypes",
"customTypes",
"defaultTypes",
"defaultType"
];
for (var opt in options) {
if (validOpts.indexOf(opt) === -1) {
throw new Error("serializeJSON ERROR: invalid option '" + opt + "'. Please use one of " + validOpts.join(", "));
}
}
// Helper to get options or defaults
return $.extend({}, f.defaultBaseOptions, f.defaultOptions, options);
},
// Just like jQuery's serializeArray method, returns an array of objects with name and value.
// but also includes the dom element (el) and is handles unchecked checkboxes if the option or data attribute are provided.
serializeArray: function($form, opts) {
if (opts == null) { opts = {}; }
var f = $.serializeJSON;
return $form.map(function() {
var elements = $.prop(this, "elements"); // handle propHook "elements" to filter or add form elements
return elements ? $.makeArray(elements) : this;
}).filter(function() {
var $el = $(this);
var type = this.type;
// Filter with the standard W3C rules for successful controls: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2
return this.name && // must contain a name attribute
!$el.is(":disabled") && // must not be disable (use .is(":disabled") so that fieldset[disabled] works)
rsubmittable.test(this.nodeName) && !rsubmitterTypes.test(type) && // only serialize submittable fields (and not buttons)
(this.checked || !rcheckableType.test(type) || f.getCheckboxUncheckedValue($el, opts) != null); // skip unchecked checkboxes (unless using opts)
}).map(function(_i, el) {
var $el = $(this);
var val = $el.val();
var type = this.type; // "input", "select", "textarea", "checkbox", etc.
if (val == null) {
return null;
}
if (rcheckableType.test(type) && !this.checked) {
val = f.getCheckboxUncheckedValue($el, opts);
}
if (isArray(val)) {
return $.map(val, function(val) {
return { name: el.name, value: val.replace(rCRLF, "\r\n"), el: el };
} );
}
return { name: el.name, value: val.replace(rCRLF, "\r\n"), el: el };
}).get();
},
getCheckboxUncheckedValue: function($el, opts) {
var val = $el.attr("data-unchecked-value");
if (val == null) {
val = opts.checkboxUncheckedValue;
}
return val;
},
// Parse value with type function
applyTypeFunc: function(name, strVal, type, el, typeFunctions) {
var typeFunc = typeFunctions[type];
if (!typeFunc) { // quick feedback to user if there is a typo or missconfiguration
throw new Error("serializeJSON ERROR: Invalid type " + type + " found in input name '" + name + "', please use one of " + objectKeys(typeFunctions).join(", "));
}
return typeFunc(strVal, el);
},
// Splits a field name into the name and the type. Examples:
// "foo" => ["foo", ""]
// "foo:boolean" => ["foo", "boolean"]
// "foo[bar]:null" => ["foo[bar]", "null"]
splitType : function(name) {
var parts = name.split(":");
if (parts.length > 1) {
var t = parts.pop();
return [parts.join(":"), t];
} else {
return [name, ""];
}
},
// Check if this input should be skipped when it has a falsy value,
// depending on the options to skip values by name or type, and the data-skip-falsy attribute.
shouldSkipFalsy: function(name, nameSansType, type, el, opts) {
var skipFromDataAttr = $(el).attr("data-skip-falsy");
if (skipFromDataAttr != null) {
return skipFromDataAttr !== "false"; // any value is true, except the string "false"
}
var optForFields = opts.skipFalsyValuesForFields;
if (optForFields && (optForFields.indexOf(nameSansType) !== -1 || optForFields.indexOf(name) !== -1)) {
return true;
}
var optForTypes = opts.skipFalsyValuesForTypes;
if (optForTypes && optForTypes.indexOf(type) !== -1) {
return true;
}
return false;
},
// Split the input name in programatically readable keys.
// Examples:
// "foo" => ["foo"]
// "[foo]" => ["foo"]
// "foo[inn][bar]" => ["foo", "inn", "bar"]
// "foo[inn[bar]]" => ["foo", "inn", "bar"]
// "foo[inn][arr][0]" => ["foo", "inn", "arr", "0"]
// "arr[][val]" => ["arr", "", "val"]
splitInputNameIntoKeysArray: function(nameWithNoType) {
var keys = nameWithNoType.split("["); // split string into array
keys = $.map(keys, function (key) { return key.replace(/\]/g, ""); }); // remove closing brackets
if (keys[0] === "") { keys.shift(); } // ensure no opening bracket ("[foo][inn]" should be same as "foo[inn]")
return keys;
},
// Set a value in an object or array, using multiple keys to set in a nested object or array.
// This is the main function of the script, that allows serializeJSON to use nested keys.
// Examples:
//
// deepSet(obj, ["foo"], v) // obj["foo"] = v
// deepSet(obj, ["foo", "inn"], v) // obj["foo"]["inn"] = v // Create the inner obj["foo"] object, if needed
// deepSet(obj, ["foo", "inn", "123"], v) // obj["foo"]["arr"]["123"] = v //
//
// deepSet(obj, ["0"], v) // obj["0"] = v
// deepSet(arr, ["0"], v, {useIntKeysAsArrayIndex: true}) // arr[0] = v
// deepSet(arr, [""], v) // arr.push(v)
// deepSet(obj, ["arr", ""], v) // obj["arr"].push(v)
//
// arr = [];
// deepSet(arr, ["", v] // arr => [v]
// deepSet(arr, ["", "foo"], v) // arr => [v, {foo: v}]
// deepSet(arr, ["", "bar"], v) // arr => [v, {foo: v, bar: v}]
// deepSet(arr, ["", "bar"], v) // arr => [v, {foo: v, bar: v}, {bar: v}]
//
deepSet: function (o, keys, value, opts) {
if (opts == null) { opts = {}; }
var f = $.serializeJSON;
if (isUndefined(o)) { throw new Error("ArgumentError: param 'o' expected to be an object or array, found undefined"); }
if (!keys || keys.length === 0) { throw new Error("ArgumentError: param 'keys' expected to be an array with least one element"); }
var key = keys[0];
// Only one key, then it's not a deepSet, just assign the value in the object or add it to the array.
if (keys.length === 1) {
if (key === "") { // push values into an array (o must be an array)
o.push(value);
} else {
o[key] = value; // keys can be object keys (strings) or array indexes (numbers)
}
return;
}
var nextKey = keys[1]; // nested key
var tailKeys = keys.slice(1); // list of all other nested keys (nextKey is first)
if (key === "") { // push nested objects into an array (o must be an array)
var lastIdx = o.length - 1;
var lastVal = o[lastIdx];
// if the last value is an object or array, and the new key is not set yet
if (isObject(lastVal) && isUndefined(f.deepGet(lastVal, tailKeys))) {
key = lastIdx; // then set the new value as a new attribute of the same object
} else {
key = lastIdx + 1; // otherwise, add a new element in the array
}
}
if (nextKey === "") { // "" is used to push values into the nested array "array[]"
if (isUndefined(o[key]) || !isArray(o[key])) {
o[key] = []; // define (or override) as array to push values
}
} else {
if (opts.useIntKeysAsArrayIndex && isValidArrayIndex(nextKey)) { // if 1, 2, 3 ... then use an array, where nextKey is the index
if (isUndefined(o[key]) || !isArray(o[key])) {
o[key] = []; // define (or override) as array, to insert values using int keys as array indexes
}
} else { // nextKey is going to be the nested object's attribute
if (isUndefined(o[key]) || !isObject(o[key])) {
o[key] = {}; // define (or override) as object, to set nested properties
}
}
}
// Recursively set the inner object
f.deepSet(o[key], tailKeys, value, opts);
},
deepGet: function (o, keys) {
var f = $.serializeJSON;
if (isUndefined(o) || isUndefined(keys) || keys.length === 0 || (!isObject(o) && !isArray(o))) {
return o;
}
var key = keys[0];
if (key === "") { // "" means next array index (used by deepSet)
return undefined;
}
if (keys.length === 1) {
return o[key];
}
var tailKeys = keys.slice(1);
return f.deepGet(o[key], tailKeys);
}
};
// polyfill Object.keys to get option keys in IE<9
var objectKeys = function(obj) {
if (Object.keys) {
return Object.keys(obj);
} else {
var key, keys = [];
for (key in obj) { keys.push(key); }
return keys;
}
};
var isObject = function(obj) { return obj === Object(obj); }; // true for Objects and Arrays
var isUndefined = function(obj) { return obj === void 0; }; // safe check for undefined values
var isValidArrayIndex = function(val) { return /^[0-9]+$/.test(String(val)); }; // 1,2,3,4 ... are valid array indexes
var isArray = Array.isArray || function(obj) { return Object.prototype.toString.call(obj) === "[object Array]"; };
}));

100
templates/dinner.html Normal file
View File

@ -0,0 +1,100 @@
<html lang="zh">
<head>
<title>晚餐吃什么鸭</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="./static/bootstrap.min.css">
</head>
<body>
<div class="container text-center" style="padding-top: 120px">
<p>每天8:00-17:30间开放匿名投票更新17:30以后允许发起抽签抽签结果确定后不可更改</p>
<p>随机抽签,按得票数决定中签概率</p>
{% if last_result %}
<p>今日{{ last_result }}中签概率降低30%</p>
{% endif %}
<form id="inputForm" class="form-inline">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">我选择</span>
</div>
{% for choice in all_choice %}
<div class="input-group">
<label for="range{{ loop.index }}" class="form-label">{{ choice['label'] }}
- {{ '{:.2f}'.format((menu.get(choice['name']) or 0) * 100) }}%</label>
<input type="range" class="form-range" id="range{{ loop.index }}" name="{{ choice['name'] }}"
min="0"
max="10" step="1" value="{{ (menu.get(choice['name']) or 0) * 10 }}">
</div>
{% endfor %}
</div>
</form>
<button type="button" class="btn btn-primary" onclick="update()">更新</button>
<button type="button" class="btn btn-secondary" onclick="clearValue()">不吃</button>
<button type="button" class="btn btn-secondary" {{ "" if can_roll == True else 'disabled="disabled"' | safe }}
onclick="roll()">开始抽签
</button>
<div style="padding-top: 40px">
<label>大家的选择</label>
<ul style="padding-top: 20px; margin-bottom: 80px" class="list-group">
{% for key in summary %}
<li class="list-group-item {{ "active" if result == key else "" }}">
<div class="row justify-content-between">
<span class="col-8">{{ key }}</span>
<span class="col-3">{{ '{:.2f}'.format(summary[key] | round(2)) }}票</span>
</div>
</li>
{% endfor %}
</ul>
</div>
<div class="fixed-bottom"><a class="link-secondary" style="text-decoration: none; font-size: 0.8rem" href="https://git.zaneyork.cn:8443/ZaneYork/dinner_vote">本项目抽签完全公开透明,源码开放欢迎随时审查</a></div>
</div>
<script src="./static/jquery-3.2.1.min.js"></script>
<script src="./static/jquery.serializejson.js"></script>
<script src="./static/bootstrap.bundle.min.js"></script>
<script>
function update() {
var data = $('#inputForm').serializeJSON();
$.ajax({
url: 'dinner/update?value=' + JSON.stringify(data),
dataType: 'json',
success: function (result) {
if (result.code == 0) {
window.location.reload();
} else {
alert('更新失败: ' + result.data)
}
}
})
}
function clearValue() {
$.ajax({
url: 'dinner/update?value=',
dataType: 'json',
success: function (result) {
if (result.code == 0) {
window.location.reload();
} else {
alert('更新失败: ' + result.data)
}
}
})
}
function roll() {
$.ajax({
url: 'dinner/roll',
dataType: 'json',
success: function (result) {
if (result.code == 0) {
window.location.reload();
} else {
alert('更新失败: ' + result.data)
}
}
})
}
</script>
</body>
</html>

11
templates/index.html Normal file
View File

@ -0,0 +1,11 @@
<html lang="zh">
<head>
<script src="./static/jquery-3.2.1.min.js"></script>
<script src="./static/bootstrap.min.js"></script>
<link rel="stylesheet" href="./static/bootstrap.min.css">
</head>
<body class="container text-center" style="padding-top: 120px">
<a href="dinner">点餐投票</a>
</body>
</html>