我应该如何实施排名系统

时间:2016-10-19 01:19:55

标签: mysql

我正在开发一个项目,如果架构或查询需要进行一些更改或者完全需要更改,我希望您快速查看。

该项目旨在为羽毛球队创建一个排名系统,其排名将基于锦标赛中的得分规则

  • +2点/匹配将被授予获胜团队。
  • 如果比赛是四分之一决赛,获胜队将获得额外+1分。
  • 如果比赛是半决赛,将获得额外+2分奖励。
  • 如果比赛是决赛,将获得额外+5分。
  • 赢得所有泳池比赛将为您的团队成绩增加4分。
  • 赢得3场以上的比赛将为您的团队增加15分。

我首先创建了以下表格

球员

+----------+-------------------------------------+------+-----+--------------
| Field    | Type                                | Null | Key | +----------+-------------------------------------+------+-----+--------------
| id       | int(11)                             | NO   | PRI | 
| name     | varchar(250)                        | NO   |     | 
| image    | text                                | YES  |     | 
| plays    | enum('RH','LH')                     | NO   |     | 
| added_on | datetime                            | NO   |     | 
| status   | enum('active','inactive','retired') | NO   |     |   +----------+-------------------------------------+------+-----+--------------

小组

+------------+----------------------------+------+-----+-------------------+----------------+
    | Field      | Type                       | Null | Key | Default           | Extra          |
    +------------+----------------------------+------+-----+-------------------+----------------+
    | id         | int(11)                    | NO   | PRI | NULL              | auto_increment |
    | name       | varchar(150)               | NO   | UNI | NULL              |                |
    | image      | text                       | YES  |     | NULL              |                |
    | status     | enum('active','in-active') | NO   |     | active            |                |
    | added_on   | datetime                   | NO   |     | CURRENT_TIMESTAMP |                |
    | updated_on | datetime                   | YES  |     | NULL              |                |
    +------------+----------------------------+------+-----+-------------------+----------------+

球员加入队伍

+-----------+---------------------------+------+-----+-------------------+----------------+
| Field     | Type                      | Null | Key | Default           | Extra          |
+-----------+---------------------------+------+-----+-------------------+----------------+
| id        | int(11)                   | NO   | PRI | NULL              | auto_increment |
| player_id | int(11)                   | NO   | MUL | NULL              |                |
| team_id   | int(11)                   | NO   |     | NULL              |                |
| status    | enum('active','inactive') | NO   |     | NULL              |                |
| added_on  | datetime                  | NO   |     | CURRENT_TIMESTAMP |                |
+-----------+---------------------------+------+-----+-------------------+----------------+

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | NO   |     | NULL    |                |
| year  | int(4)       | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

匹配

+---------------+---------------------------------------+------+-----+-------------------+----------------+
| Field         | Type                                  | Null | Key | Default           | Extra          |
+---------------+---------------------------------------+------+-----+-------------------+----------------+
| id            | int(11)                               | NO   | PRI | NULL              | auto_increment |
| team_one      | int(11)                               | NO   | MUL | NULL              |                |
| team_two      | int(11)                               | NO   |     | NULL              |                |
| winner_id     | int(11)                               | NO   |     | NULL              |                |
| tournament_id | int(11)                               | NO   | MUL | 1                 |                |
| added_on      | datetime                              | NO   |     | CURRENT_TIMESTAMP |                |
| match_type    | enum('pool','quarter','semi','final') | NO   |     | pool              |                |
| sets          | smallint(2)                           | NO   |     | 1                 |                |
+---------------+---------------------------------------+------+-----+-------------------+----------------+

匹配分数

+----------+-------------------+------+-----+-------------------+----------------+
| Field    | Type              | Null | Key | Default           | Extra          |
+----------+-------------------+------+-----+-------------------+----------------+
| id       | int(11)           | NO   | PRI | NULL              | auto_increment |
| match_id | int(11)           | NO   | MUL | NULL              |                |
| team_id  | int(11)           | NO   | MUL | NULL              |                |
| set_num  | enum('1','2','3') | NO   |     | NULL              |                |
| score    | smallint(2)       | NO   |     | NULL              |                |
| added_on | datetime          | NO   |     | CURRENT_TIMESTAMP |                |
+----------+-------------------+------+-----+-------------------+----------------+

+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| id            | int(11)     | NO   | PRI | NULL    | auto_increment |
| name          | varchar(10) | NO   | UNI | NULL    |                |
| tournament_id | int(11)     | NO   | MUL | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+

有一件事你会注意到我没有在任何地方保存排名得分,我在运行时使用以下查询计算

SELECT
    T.id, T.name, T.status,
    IFNULL(T.image,'no-image.png') as DP,
    (SELECT COUNT(*)
    FROM badminton_matches M
    WHERE (M.team_one = T.id OR M.team_two = T.id)) as played,
    (SELECT COUNT(*)
    FROM badminton_matches M
    WHERE M.winner_id = T.id) as won,
    (SELECT COUNT(*)
    FROM badminton_matches M
    WHERE ((M.team_one = T.id OR M.team_two = T.id)
        AND (M.winner_id != T.id))) as lost,
    (
          ((SELECT COUNT(*)
    FROM badminton_matches M
    WHERE M.winner_id = T.id) * 2) +
          (((SELECT COUNT(*)
    FROM badminton_matches M
    WHERE (M.match_type = 'quarter'
        AND M.winner_id = T.id)) * 2) + 1) +
            (((SELECT COUNT(*)
    FROM badminton_matches M
    WHERE (M.match_type = 'semi'
        AND M.winner_id = T.id)) * 2) + 2) +
            (((SELECT COUNT(*)
    FROM badminton_matches M
    WHERE (M.match_type = 'final' AND M.winner_id = T.id)) * 2) + 5)) as Points
FROM
    badminton_teams T
ORDER BY 
        (Points) DESC;

首先

  • 使用查询或

  • 计算运行时评分是否正确 每次在数据库中保存匹配结果时,
  • 是否应保存更新

  • 我应该为此目的安排一个cron工作

编辑:

将查询更新为以下

SELECT
    T.id, T.name, T.status,
    IFNULL(T.image,'no-image.png') as DP,
    (SELECT COUNT(*)
    FROM badminton_matches M
    WHERE ((M.team_one =T.id or M.team_two = T.id) and M.winner_id IS NOT NULL)) as played,
    (SELECT COUNT(*)
    FROM badminton_matches M
    WHERE M.winner_id=T.id) as won, (SELECT COUNT(*)
    FROM badminton_matches M
    WHERE ((M.team_one =T.id or M.team_two = T.id) AND (M.winner_id!=T.id))) as lost,
    ((SELECT (SUM(BMS.points_won)-SUM(BMS.points_lost))
    FROM
        badminton_match_score BMS
        JOIN badminton_matches M ON (M.id=BMS.match_id)
    where M.team_one=T.id OR M.team_two=T.id and M.winner_id is not null)/(SELECT COUNT(*)
    FROM badminton_matches M
    WHERE ((M.team_one =T.id or M.team_two = T.id) and M.winner_id IS NOT NULL))) AS AVG_SCORE,
    (
            ((SELECT COUNT(*)
    FROM badminton_matches M
    WHERE M.winner_id=T.id)*2) + 
(SELECT COUNT(*)
    FROM badminton_matches M
    WHERE (M.match_type='quarter' AND M.winner_id=T.id))

            +
            ((SELECT COUNT(*)
    FROM badminton_matches M
    WHERE (M.match_type='semi' AND M.winner_id=T.id))*2)
                +
            ((SELECT COUNT(*)
    FROM badminton_matches M
    WHERE (M.match_type='final' AND M.winner_id=T.id))*5)
            ) 
as Points
FROM badminton_teams T
order by (Points) DESC, lost ASC, AVG_SCORE DESC

1 个答案:

答案 0 :(得分:2)

  

使用查询

计算运行时评分是否正确

如果您希望它是实时应用程序。在我看来,是的,如果积分没有累积,将在比赛后重置。对于Sql性能问题。

  每次我在数据库中保存匹配结果时,我应该保存更新吗

更新仅用于修改数据,在匹配历史记录后保存数据就足够了。

  

我应该为此目的安排一个cron工作

如果在更大的数据库和生产力方面,是的。