Hide keyboard shortcuts

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 

3 

4# -- stdlib -- 

5from typing import TYPE_CHECKING 

6import logging 

7 

8# -- third party -- 

9# -- own -- 

10from server.base import Game 

11 

12# -- typing -- 

13if TYPE_CHECKING: 

14 from server.core import Core # noqa: F401 

15 

16 

17# -- code -- 

18log = logging.getLogger('Reward') 

19 

20 

21class Reward(object): 

22 def __init__(self, core: Core): 

23 self.core = core 

24 

25 core.events.game_ended += self.handle_game_ended 

26 

27 def __repr__(self) -> str: 

28 return self.__class__.__name__ 

29 

30 def handle_game_ended(self, g: Game) -> Game: 

31 core = self.core 

32 

33 if core.game.is_crashed(g): 

34 return g 

35 

36 if core.game.is_aborted(g): 

37 return g 

38 

39 rewards = [] 

40 

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]) 

48 

49 rewards.extend([{ 

50 'playerId': core.auth.uid_of(u), 

51 'type': 'drop', 

52 'amount': 1, 

53 } for u in fleed]) 

54 

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 

58 

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]) 

64 

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) 

74 

75 return g