fix: package import;add: hist and count,rank list

This commit is contained in:
liuyihui 2021-12-30 13:24:57 +08:00
parent a5712c9b17
commit 8476f11897
11 changed files with 237 additions and 18 deletions

3
.gitignore vendored
View File

@ -11,3 +11,6 @@ pic/
# cache # cache
__pycache__/ __pycache__/
# test
test.*

36
app.py
View File

@ -8,13 +8,11 @@
# here put the import lib # here put the import lib
import os import os
import lib.elo as elo from lib import *
import lib.utils as utils
from flask import Flask, request, redirect, render_template from flask import Flask, request, redirect, render_template
P0 = None P0, P1 = None, None
P1 = None ses = get_session()
ses = utils.get_session()
app = Flask(__name__, template_folder='./templates/', static_folder='./static/') app = Flask(__name__, template_folder='./templates/', static_folder='./static/')
app.config['ROOT_FOLDER'] = os.path.abspath('.') app.config['ROOT_FOLDER'] = os.path.abspath('.')
@ -22,24 +20,44 @@ app.config['ROOT_FOLDER'] = os.path.abspath('.')
@app.route('/new', methods=["GET"]) @app.route('/new', methods=["GET"])
def root(): def root():
global P0, P1 global P0, P1
P0, P1 = utils.match(ses)
P0, P1 = match(ses)
return render_template( return render_template(
'index.html', 'index.html',
p0src='./static/pic/{:d}.jpeg'.format(P0.id), p0src='./static/pic/{:d}.jpeg'.format(P0.id),
p0name=P0.name, p0name=P0.name,
p0work='{:.2f}'.format(P0.rate), p0work=P0.work,
p1src='./static/pic/{:d}.jpeg'.format(P1.id), p1src='./static/pic/{:d}.jpeg'.format(P1.id),
p1name=P1.name, p1name=P1.name,
p1work='{:.2f}'.format(P1.rate) p1work=P1.work
) )
@app.route('/elo', methods=["GET"]) @app.route('/elo', methods=["GET"])
def elo_res(): def elo_res():
global P0, P1
res = request.args.get('win') res = request.args.get('win')
elo.set_result(P0, P1, int(res)) set_result(P0, P1, int(res))
ses.commit() ses.commit()
P0, P1 = None, None
return redirect('/new') 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__': if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=4002) app.run(debug=True, host='0.0.0.0', port=4002)

20
lib/__init__.py Normal file
View File

@ -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"
]

View File

@ -19,10 +19,14 @@ class Person(Base):
id = Column(Integer, primary_key=True, autoincrement=True) id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String) name = Column(String)
work = Column(String, default='') 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): def __repr__(self):
return "<Person(name='%s', work='%s', rate='%f')>" % (self.name, self.work, self.rate) return "<Person(name='%s', work='%s', rate='%f', count='%d')>" % (
self.name, self.work, self.rate, self.count
)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -8,7 +8,7 @@
# here put the import lib # here put the import lib
import math import math
from lib.db_init import Person from .db_init import Person
def __compute_wre(A: Person, B: Person) -> float: def __compute_wre(A: Person, B: Person) -> float:
'''compute win rate expectation (wre for short) '''compute win rate expectation (wre for short)
@ -32,6 +32,11 @@ def __compute_K(P: Person) -> float:
else: else:
return 36 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: def set_result(A: Person, B: Person, res: float) -> None:
'''set result of this game '''set result of this game
@ -45,3 +50,6 @@ def set_result(A: Person, B: Person, res: float) -> None:
if res >= 0: if res >= 0:
A.rate += __compute_K(A) * (1 - res - wre1) A.rate += __compute_K(A) * (1 - res - wre1)
B.rate += __compute_K(B) * (res - wre2) B.rate += __compute_K(B) * (res - wre2)
__add_history(A)
__add_history(B)

View File

@ -9,7 +9,7 @@
# here put the import lib # here put the import lib
import math import math
from typing import List from typing import List
from lib.db_init import Person from . import db_init
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.session import Session 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) engine = create_engine('sqlite:///data.db?check_same_thread=False', echo=False)
session_maker = sessionmaker(bind=engine) session_maker = sessionmaker(bind=engine)
Person = db_init.Person
def get_session() -> Session: def get_session() -> Session:
'''make a session instance '''make a session instance

View File

@ -1 +1,2 @@
flask flask
sqlalchemy

View File

@ -107,3 +107,55 @@ a {
.col-sm p { .col-sm p {
margin-top : 1%; 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;
}

View File

@ -10,14 +10,14 @@
</head> </head>
<body> <body>
<header id="header" class="header"> <header id="header" class="header">
<div class="textlogo"><p><a href="#">ELO Star</a></p></div> <div class="textlogo"><p><a href="/">ELO Star</a></p></div>
<div class="nav"> <div class="nav">
<div class="row"> <div class="row">
<div class="col-sm"> <div class="col-sm">
<p><a href="#">排行榜</a></p> <p><a href="/list?b=1&len=10">排行榜</a></p>
</div> </div>
<div class="col-sm"> <div class="col-sm">
<p><a href="#">添加资料</a></p> <p><a href="/">添加资料</a></p>
</div> </div>
<div class="col-sm"> <div class="col-sm">
<p><a href="https://git.foolishfox.cn/fox/ELOStar">Gitea</a></p> <p><a href="https://git.foolishfox.cn/fox/ELOStar">Gitea</a></p>

0
templates/info.html Normal file
View File

112
templates/list.html Normal file
View File

@ -0,0 +1,112 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ELO Star</title>
<!-- <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}"> -->
<script src="{{ url_for('static', filename='jquery-3.1.1.min.js') }}"></script>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<header id="header" class="header">
<div class="textlogo"><p><a href="/">ELO Star</a></p></div>
<div class="nav">
<div class="row">
<div class="col-sm">
<p><a href="/list?b=1&len=10">排行榜</a></p>
</div>
<div class="col-sm">
<p><a href="/">添加资料</a></p>
</div>
<div class="col-sm">
<p><a href="https://git.foolishfox.cn/fox/ELOStar">Gitea</a></p>
</div>
</div>
</div>
</header>
<script>
function title_height() {
var height = document.getElementById('header').clientHeight;
$("#title").css("line-height", height*0.5+"px");
}
</script>
<div class="main">
<table class="tg">
<thead>
<tr>
<th class="tg-0pky" style="width: 30%;">排名</th>
<th class="tg-0pky" style="width: 35%;">姓名</th>
<th class="tg-0lax" style="width: 35%;">分数</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-7d57">{{ P0rank }}</td>
<td class="tg-7d57">{{ P0name }}</td>
<td class="tg-dg7a">{{ P0rate }}</td>
</tr>
<tr>
<td class="tg-0pky">{{ P1rank }}</td>
<td class="tg-0pky">{{ P1name }}</td>
<td class="tg-0lax">{{ P1rate }}</td>
</tr>
<tr>
<td class="tg-7d57">{{ P2rank }}</td>
<td class="tg-7d57">{{ P2name }}</td>
<td class="tg-dg7a">{{ P2rate }}</td>
</tr>
<tr>
<td class="tg-0lax">{{ P3rank }}</td>
<td class="tg-0lax">{{ P3name }}</td>
<td class="tg-0lax">{{ P3rate }}</td>
</tr>
<tr>
<td class="tg-dg7a">{{ P4rank }}</td>
<td class="tg-dg7a">{{ P4name }}</td>
<td class="tg-dg7a">{{ P4rate }}</td>
</tr>
<tr>
<td class="tg-0lax">{{ P5rank }}</td>
<td class="tg-0lax">{{ P5name }}</td>
<td class="tg-0lax">{{ P5rate }}</td>
</tr>
<tr>
<td class="tg-dg7a">{{ P6rank }}</td>
<td class="tg-dg7a">{{ P6name }}</td>
<td class="tg-dg7a">{{ P6rate }}</td>
</tr>
<tr>
<td class="tg-0lax">{{ P7rank }}</td>
<td class="tg-0lax">{{ P7name }}</td>
<td class="tg-0lax">{{ P7rate }}</td>
</tr>
<tr>
<td class="tg-dg7a">{{ P8rank }}</td>
<td class="tg-dg7a">{{ P8name }}</td>
<td class="tg-dg7a">{{ P8rate }}</td>
</tr>
<tr>
<td class="tg-0lax">{{ P9rank }}</td>
<td class="tg-0lax">{{ P9name }}</td>
<td class="tg-0lax">{{ P9rate }}</td>
</tr>
</tbody>
</table>
</div>
<footer class="footer">
<div class="footerRow1">
<div class="container">
<div class="row">
<div class="col-sm">
<p>© 2021 ELO Star</p>
</div>
<div class="col-sm">
<p>Made By <a href="https://foolishfox.cn">Fox</a></p>
</div>
</div>
</div>
</div>
</footer>
</body>
</html>