diff --git a/.gitignore b/.gitignore index ebf6e3c..f881b68 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,7 @@ setup.cfg pic/ # cache -__pycache__/ \ No newline at end of file +__pycache__/ + +# test +test.* \ No newline at end of file diff --git a/app.py b/app.py index 80f06ef..d3fa846 100644 --- a/app.py +++ b/app.py @@ -8,13 +8,11 @@ # here put the import lib import os -import lib.elo as elo -import lib.utils as utils +from lib import * from flask import Flask, request, redirect, render_template -P0 = None -P1 = None -ses = utils.get_session() +P0, P1 = None, None +ses = get_session() app = Flask(__name__, template_folder='./templates/', static_folder='./static/') app.config['ROOT_FOLDER'] = os.path.abspath('.') @@ -22,24 +20,44 @@ app.config['ROOT_FOLDER'] = os.path.abspath('.') @app.route('/new', methods=["GET"]) def root(): global P0, P1 - P0, P1 = utils.match(ses) + + P0, P1 = match(ses) return render_template( 'index.html', p0src='./static/pic/{:d}.jpeg'.format(P0.id), p0name=P0.name, - p0work='{:.2f}'.format(P0.rate), + p0work=P0.work, p1src='./static/pic/{:d}.jpeg'.format(P1.id), p1name=P1.name, - p1work='{:.2f}'.format(P1.rate) + p1work=P1.work ) @app.route('/elo', methods=["GET"]) def elo_res(): + global P0, P1 + res = request.args.get('win') - elo.set_result(P0, P1, int(res)) + set_result(P0, P1, int(res)) + ses.commit() + P0, P1 = None, None + return redirect('/new') +@app.route('/list', methods=["GET"]) +def rank_list(): + b = int(request.args.get('b')) + len = int(request.args.get('len')) if request.args.get('len') else 10 + + res = get_rank(ses, b, len) + kw = dict() + for k in range(len): + kw["P{:d}rank".format(k)] = b + k + kw["P{:d}name".format(k)] = res[k].name + kw["P{:d}rate".format(k)] = '{:.2f}'.format(res[k].rate) + + return render_template('list.html', **kw) + if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=4002) diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 0000000..35842e0 --- /dev/null +++ b/lib/__init__.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +''' +@File : __init__.py +@Author : liuyihui +@Email : liuyihui02@gmail.com +''' + +# here put the import lib +from .db_init import Person +from .elo import set_result +from .utils import match, get_rank, get_session + +__all__ = [ + "match", + "Person", + "set_result", + "get_rank", + "get_session" +] diff --git a/lib/db_init.py b/lib/db_init.py index 191dc16..4cc4aab 100644 --- a/lib/db_init.py +++ b/lib/db_init.py @@ -19,10 +19,14 @@ class Person(Base): id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String) work = Column(String, default='无') - rate = Column(Float, default=1400) + rate = Column(Float, default=1400.0) + hist = Column(String, default='') + count = Column(Integer, default=0) def __repr__(self): - return "" % (self.name, self.work, self.rate) + return "" % ( + self.name, self.work, self.rate, self.count + ) if __name__ == '__main__': diff --git a/lib/elo.py b/lib/elo.py index e3d0d36..0c073cf 100644 --- a/lib/elo.py +++ b/lib/elo.py @@ -8,7 +8,7 @@ # here put the import lib import math -from lib.db_init import Person +from .db_init import Person def __compute_wre(A: Person, B: Person) -> float: '''compute win rate expectation (wre for short) @@ -32,6 +32,11 @@ def __compute_K(P: Person) -> float: else: return 36 +def __add_history(P: Person): + P.count += 1 + if P.count % 10 == 0: + P.hist += str('{:.2f}'.format(P.rate)) + def set_result(A: Person, B: Person, res: float) -> None: '''set result of this game @@ -45,3 +50,6 @@ def set_result(A: Person, B: Person, res: float) -> None: if res >= 0: A.rate += __compute_K(A) * (1 - res - wre1) B.rate += __compute_K(B) * (res - wre2) + + __add_history(A) + __add_history(B) diff --git a/lib/utils.py b/lib/utils.py index 42ff651..6e19179 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -9,7 +9,7 @@ # here put the import lib import math from typing import List -from lib.db_init import Person +from . import db_init from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.orm.session import Session @@ -17,6 +17,7 @@ from sqlalchemy.sql.expression import desc, func engine = create_engine('sqlite:///data.db?check_same_thread=False', echo=False) session_maker = sessionmaker(bind=engine) +Person = db_init.Person def get_session() -> Session: '''make a session instance diff --git a/requirements.txt b/requirements.txt index 8ab6294..36ecb36 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -flask \ No newline at end of file +flask +sqlalchemy \ No newline at end of file diff --git a/static/style.css b/static/style.css index 7000669..74e1181 100644 --- a/static/style.css +++ b/static/style.css @@ -106,4 +106,56 @@ a { } .col-sm p { margin-top : 1%; +} +.tg { + border-collapse: collapse; + border-color : #aaa; + border-spacing: 0; + border-style : solid; + border-width : 1px; + width : 75%; + margin : 3% auto; +} +td, th { + text-align : center; + height : 3%; +} +.tg td { + background : #fff; + border-color : #aaa; + border-style : solid; + border-width : 0px; + color : #333; + font-size : 14px; + overflow : hidden; + padding : 10px 5px; + word-break : normal; +} +.tg th { + background : #f38630; + border-color : #aaa; + border-style : solid; + border-width : 0px; + color : #fff; + font-size : 14px; + font-weight : normal; + overflow : hidden; + padding : 10px 5px; + word-break : normal; +} +.tg .tg-7d57 { + background : #FCFBE3; + border-color : inherit; + vertical-align: top; +} +.tg .tg-0pky { + border-color : inherit; + vertical-align: top; +} +.tg .tg-0lax { + vertical-align: top; +} +.tg .tg-dg7a { + background : #FCFBE3; + vertical-align: top; } \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 13257a3..f7a22ef 100644 --- a/templates/index.html +++ b/templates/index.html @@ -10,14 +10,14 @@