# Coverage for server/parts/view.py : 100%

## 19 statements 19 run 0 missing 4 excluded 0 partial

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 -*-

2 from __future__ import annotations

3

4# -- stdlib --

5 from typing import TYPE_CHECKING

6 import logging

7

8# -- third party --

9# -- own --

10 from server.base import Game as ServerGame

11 from server.endpoint import Client

12 import wire

13

14# -- typing --

15 if TYPE_CHECKING:

16 from server.core import Core # noqa: F401

17

18

19# -- code --

20 log = logging.getLogger('server.parts.view')

21

22

23 class View(object):

24 def __init__(self, core: Core):

25 self.core = core

26

27 def __repr__(self) -> str:

28 return self.__class__.__name__

29

30 def User(self, u: Client) -> wire.model.User:

31 core = self.core

32

33 return {

34 'uid': core.auth.uid_of(u),

35 'state': core.lobby.state_of(u).state,

36 }

37

38 def Game(self, g: ServerGame) -> wire.model.Game:

39 core = self.core

40

41 return {

42 'gid': core.room.gid_of(g),

43 'type': g.__class__.__name__,

44 'name': core.room.name_of(g),

45 'started': core.room.is_started(g),

46 'online': len(core.room.online_users_of(g)),

47 }

48

49 def GameDetail(self, g: ServerGame) -> wire.model.GameDetail:

50 core = self.core

51

52 return {

53 # HACK: workaround TypedDict limitations

54 'gid': core.room.gid_of(g),

55 'type': g.__class__.__name__,

56 'name': core.room.name_of(g),

57 'started': core.room.is_started(g),

58 'online': len(core.room.online_users_of(g)),

59 # **self.Game(g),

60 'users': [self.User(u) for u in core.room.users_of(g)],

61 'params': core.game.params_of(g),

62 'items': core.item.item_skus_of(g),

63 }