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 as ServerGame 

11from server.endpoint import Client 

12import wire 

13 

14# -- typing -- 

15if TYPE_CHECKING: 

16 from server.core import Core # noqa: F401 

17 

18 

19# -- code -- 

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

21 

22 

23class 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 }