def calc_score_all():
    "スコア計算"
    authors = get_Q_author_all()
    #print "authors=", authors
    q_factors = {}
    q_point = {}
    ok_point = {}
    bonus_point = {}
    result = {}
    misc = {}
    query = Answer.query(ancestor=userlist_key())
    q = query.fetch()
    all_numbers = {}
    all_users = {}
    for i in q:
        #anum = 'A%d' % i.anum
        anum = 'A%02d' % i.anum
        username = i.owner
        all_numbers[anum] = 1
        all_users[username] = 1
        # 正解ポイント
        if not(anum in ok_point):
            ok_point[anum] = {}
        ok_point[anum][username] = i.judge
        # 品質ポイント
        if not(anum in q_factors):
            q_factors[anum] = {}
        q_factors[anum][username] = i.q_factor
        # 出題ボーナスポイント
        if i.judge in (0,1) and authors[i.anum] == username:
            #print "check_bonus:", i.anum, i.judge, authors[i.anum], username
            if not(anum in bonus_point):
                bonus_point[anum] = {}
            bonus_point[anum][username] = i.judge
        # result(ログメッセージ)
        if not(anum in result):
            result[anum] = {}
        result[anum][username] = i.result
        # (その他) date, cpu_sec, mem_byte, misc_text
        if not(anum in misc):
            misc[anum] = {}
        misc[anum][username] = [i.date, i.cpu_sec, i.mem_byte, i.misc_text]
    #print "ok_point=", ok_point
    #print "bonus_point=", bonus_point
    #print "q_factors=", q_factors
    #print "result=\n", result
    # 品質ポイントを計算する
    q_pt = 10.0
    for anum, values in q_factors.iteritems(): # 問題番号ごとに
        #print "anum=", anum
        qf_total = 0.0 # Q_factorの合計
        for user, qf in values.iteritems():
            #print "qf=", qf
            qf_total += qf
        #print "qf_total=", qf_total
        for user, qf in values.iteritems():
            if qf_total == 0.0:
                tmp = 0.0
            else:
                tmp = q_pt * qf / qf_total
            if not anum in q_point:
                q_point[anum] = {}
            q_point[anum][user] = tmp
    #print "q_point=", q_point
    # 集計する
    tmp = ['']*(len(all_numbers) + 1)
    i = 0
    for anum in sorted(all_numbers.keys()):
        tmp[i] = anum
        i += 1
    tmp[i] = 'TOTAL'
    score_board = {'/header/': tmp} # 見出しの行
    for user in sorted(all_users.keys()):
        #print user
        if not(user in score_board):
            score_board[user] = [0]*(len(all_numbers) + 1)
        i = 0
        ptotal = 0.0
        for anum in sorted(all_numbers.keys()):
            #print anum
            p = 0.0
            if user in ok_point[anum]: p += ok_point[anum][user]
            if user in q_point[anum]:  p += q_point[anum][user]
            if anum in bonus_point and user in bonus_point[anum]:
                    p += bonus_point[anum][user]
            #print "p=", p
            score_board[user][i] = p
            ptotal += p
            i += 1
        score_board[user][i] = ptotal
    #print "score_board=", score_board
    return score_board, ok_point, q_point, bonus_point, q_factors, result, misc