1- from game .common .action import Action
2- from game .common .game_object import GameObject
1+ from game .client .user_client import UserClient
32from game .common .avatar import Avatar
43from game .common .enums import *
5- from game .client . user_client import UserClient
4+ from game .common . game_object import GameObject
65
76
87class Player (GameObject ):
@@ -18,17 +17,18 @@ class Player(GameObject):
1817 """
1918
2019 def __init__ (self , code : object | None = None , team_name : str | None = None , actions : list [ActionType ] = [],
21- avatar : Avatar | None = None ):
20+ avatars : dict [ ObjectType , Avatar ] | None = None ):
2221 super ().__init__ ()
2322 self .object_type : ObjectType = ObjectType .PLAYER
2423 self .functional : bool = True
2524 self .error : str | None = None
2625 self .file_name : str | None = None
2726 self .team_name : str | None = team_name
2827 self .code : UserClient | None = code
29- # self.action: Action = action
3028 self .actions : list [ActionType ] = actions
31- self .avatar : Avatar | None = avatar
29+ self .avatars : dict [ObjectType , Avatar ] | None = avatars
30+ self .__selected_avatar_type : ObjectType = ObjectType .AVATAR
31+ self .score : int = 0
3232
3333 @property
3434 def error (self ) -> str | None :
@@ -89,15 +89,41 @@ def file_name(self, file_name: str | None) -> None:
8989 self .__file_name = file_name
9090
9191 @property
92- def avatar (self ) -> Avatar :
93- return self .__avatar
92+ def avatars (self ) -> dict [ObjectType , Avatar ]:
93+ return self .__avatars
94+
95+ @avatars .setter
96+ def avatars (self , avatars : dict [ObjectType , Avatar ]) -> None :
97+ if (avatars is not None and not (isinstance (avatars , dict )
98+ and len (avatars ) > 0
99+ and all (map (lambda name_and_avatar : isinstance (name_and_avatar [0 ], ObjectType ) and isinstance (name_and_avatar [1 ], Avatar ), avatars .items ())))):
100+ raise ValueError (
101+ f'{ self .__class__ .__name__ } .avatars must be a dict with a key of ObjectType and a value of Avatar or None. It is a(n) { avatars .__class__ .__name__ } and has the value of { avatars } .' )
102+ self .__avatars = avatars
103+
104+ def select_avatar (self , avatar_type : ObjectType ) -> None :
105+ if avatar_type is None or not isinstance (avatar_type , ObjectType ):
106+ raise ValueError (
107+ f'avatar_name must be a ObjectType. It is a(n) { avatar_type .__class__ .__name__ } and has the value of '
108+ f'{ avatar_type } '
109+ )
110+ self .__selected_avatar_type = avatar_type
111+
112+ @property
113+ def avatar (self ) -> Avatar | None :
114+ return self .avatars .get (self .__selected_avatar_type , None ) if self .avatars is not None else None
94115
95- @avatar .setter
96- def avatar (self , avatar : Avatar ) -> None :
97- if avatar is not None and not isinstance (avatar , Avatar ):
116+ @property
117+ def score (self ) -> int :
118+ return self .__score
119+
120+ @score .setter
121+ def score (self , score : int ) -> None :
122+ if score is None or not isinstance (score , int ):
98123 raise ValueError (
99- f'{ self .__class__ .__name__ } .avatar must be Avatar or None. It is a(n) { avatar .__class__ .__name__ } and has the value of { avatar } .' )
100- self .__avatar = avatar
124+ f'{ self .__class__ .__name__ } .score must be an int. It is a(n) { score .__class__ .__name__ } and has the value of '
125+ f'{ score } ' )
126+ self .__score : int = score
101127
102128 @property
103129 def object_type (self ) -> ObjectType :
@@ -118,7 +144,9 @@ def to_json(self):
118144 data ['team_name' ] = self .team_name
119145 data ['file_name' ] = self .file_name
120146 data ['actions' ] = [act .value for act in self .actions ]
121- data ['avatar' ] = self .avatar .to_json () if self .avatar is not None else None
147+ data ['selected_avatar_type' ] = self .__selected_avatar_type
148+ data ['avatars' ] = { k : v .to_json () if v is not None else None for k , v in self .avatars } if self .avatars is not None else None
149+ data ['score' ] = self .score
122150
123151 return data
124152
@@ -129,32 +157,15 @@ def from_json(self, data):
129157 self .error = data ['error' ]
130158 self .team_name = data ['team_name' ]
131159 self .file_name = data ['file_name' ]
132-
133- self .actions : list [ActionType ] = [ObjectType (action ) for action in data ['actions' ]]
134- avatar : Avatar | None = data ['avatar' ]
135- if avatar is None :
136- self .avatar = None
160+ self .score = data ['score' ]
161+ self .actions : list [ActionType ] = [ActionType (action ) for action in data ['actions' ]]
162+ self .__selected_avatar_type = data ['selected_avatar_type' ]
163+ avatars : dict [str , Avatar ] = data ['avatars' ]
164+ if avatars is None :
165+ self .avatars = None
137166 return self
138- # match case for action
139- # match action['object_type']:
140- # case ObjectType.ACTION:
141- # self.action = Action().from_json(data['action'])
142- # case None:
143- # self.action = None
144- # case _: # checks if it is anything else
145- # raise Exception(f'Could not parse action: {self.action}')
146-
147- # match case for avatar
148- match ObjectType (avatar ['object_type' ]):
149- case ObjectType .AVATAR :
150- self .avatar = Avatar ().from_json (data ['avatar' ])
151- case None :
152- self .avatar = None
153- case _:
154- raise Exception (f'Could not parse avatar: { self .avatar } ' )
167+ self .avatars = { k :Avatar ().from_json (v ) if v is not None else None for k , v in data ['avatars' ]}
155168 return self
156- # self.action = Action().from_json(data['action']) if data['action'] is not None else None
157- # self.avatar = Avatar().from_json(data['avatar']) if data['avatar'] is not None else None
158169
159170 # to String
160171 def __str__ (self ):
0 commit comments