Coverage for server/parts/reward.py : 100%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2from __future__ import annotations
4# -- stdlib --
5from typing import TYPE_CHECKING
6import logging
8# -- third party --
9# -- own --
10from server.base import Game
12# -- typing --
13if TYPE_CHECKING:
14 from server.core import Core # noqa: F401
17# -- code --
18log = logging.getLogger('Reward')
21class Reward(object):
22 def __init__(self, core: Core):
23 self.core = core
25 core.events.game_ended += self.handle_game_ended
27 def __repr__(self) -> str:
28 return self.__class__.__name__
30 def handle_game_ended(self, g: Game) -> Game:
31 core = self.core
33 if core.game.is_crashed(g):
34 return g
36 if core.game.is_aborted(g):
37 return g
39 rewards = []
41 users = core.room.users_of(g)
42 fleed = [u for u in users if core.game.is_fleed(g, u)]
43 rewards.extend([{
44 'playerId': core.auth.uid_of(u),
45 'type': 'game',
46 'amount': 1,
47 } for u in users])
49 rewards.extend([{
50 'playerId': core.auth.uid_of(u),
51 'type': 'drop',
52 'amount': 1,
53 } for u in fleed])
55 good = list(set(users) - set(fleed))
56 winners_good = list(set(core.game.winners_of(g)) - set(fleed))
57 bonus = len(users) * 5 / len(winners_good) if winners_good else 0
59 rewards.extend([{
60 'playerId': core.auth.uid_of(u),
61 'type': 'jiecao',
62 'amount': 2 + bonus if u in winners_good else 2,
63 } for u in good])
65 core.backend.query('''
66 mutation AddReward($gid: Int!, $rewards: [GameRewardInput!]!) {
67 game {
68 addReward(gameId: $gid, rewards: $rewards) {
69 id
70 }
71 }
72 }
73 ''', gid=core.room.gid_of(g), rewards=rewards)
75 return g