Browse Source

feat: 添加基础类

SongZihuan 3 years ago
parent
commit
6f3bbadd44

+ 0 - 1
main.py

@@ -1 +0,0 @@
-print("Hi, HGSSystem")

+ 6 - 0
src/conf/__init__.py

@@ -0,0 +1,6 @@
+"""
+文件名: conf/__init__.py
+配置文件
+"""
+
+import sys

+ 0 - 0
src/core/__init__.py


+ 49 - 0
src/core/garbage.py

@@ -0,0 +1,49 @@
+from tool.time_ import HGSTime, hgs_time_t
+from tool.location import HGSLocation, hgs_location_t
+from tool.type_ import *
+from enum import Enum
+
+
+class GarbageBagNotUse(Exception):
+    pass
+
+
+class GarbageType(Enum):
+    recyclable: enum = 1
+    kitchen: enum = 2
+    hazardous: enum = 3
+    other: enum = 4
+
+
+class GarbageBag:
+    def __init__(self, gid: gid_t, last_time: time_t):
+        self._gid: gid_t = gid
+        self._have_use: bool = False
+        self.last_time: HGSTime = HGSTime(last_time)
+
+        self._type: Union[enum, None] = None
+        self._use_time: Union[HGSTime, None] = None
+        self._user: Union[uid_t, None] = None
+        self._loc: Union[HGSLocation, None] = None
+
+    def is_use(self) -> bool:
+        return self._have_use
+
+    def get_user(self) -> uid_t:
+        if not self._have_use:
+            raise GarbageBagNotUse
+        return self._user
+
+    def config_use(self, garbage_type: enum, use_time: hgs_time_t, user: uid_t, location: hgs_location_t):
+        self._have_use = True
+        if not isinstance(use_time, HGSTime):
+            use_time = HGSTime(use_time)
+        if not isinstance(location, HGSLocation):
+            location = HGSLocation(location)
+        self._type: enum = garbage_type
+        self._use_time: enum = use_time
+        self._user: enum = user
+        self._loc: enum = location
+
+    def is_out_of_date(self) -> bool:
+        return self.last_time.is_out_of_date()

+ 0 - 0
src/core/tool/__init__.py


+ 12 - 0
src/core/tool/location.py

@@ -0,0 +1,12 @@
+from type_ import *
+
+
+class HGSLocation:
+    def __init__(self, location: location_t):
+        self.loc: location_t = location
+
+    def get_location(self) -> location_t:
+        return self.loc
+
+
+hgs_location_t: TypeAlias = Union[HGSLocation, location_t]

+ 6 - 0
src/core/tool/login.py

@@ -0,0 +1,6 @@
+from .type_ import *
+import hashlib
+
+
+def check_login(uid: uid_t, name: uname_t, passwd: passwd_t, salt: str) -> bool:
+    ...

+ 17 - 0
src/core/tool/time_.py

@@ -0,0 +1,17 @@
+from .type_ import *
+import time
+
+
+class HGSTime:
+    def __init__(self, second: time_t = None):
+        if second is None:
+            self._time: time_t = time.time()
+        else:
+            self._time: time_t = second
+        self._time_local: time.struct_time = time.localtime(self._time)
+
+    def is_out_of_date(self):
+        return time.time() > self._time
+
+
+hgs_time_t: TypeAlias = Union[HGSTime, time_t]

+ 15 - 0
src/core/tool/type_.py

@@ -0,0 +1,15 @@
+from typing import Union, TypeAlias
+
+gid_t: TypeAlias = str  # garbage bag id 类型
+uid_t: TypeAlias = str  # user id 类型
+
+uname_t: TypeAlias = str  # user name 类型
+passwd_t: TypeAlias = str  # user password 类型
+
+count_t: TypeAlias = int  # 计数类型 (垃圾计数)
+score_t: TypeAlias = int  # 积分类型
+location_t: TypeAlias = str
+time_t: TypeAlias = float  # 时间类型
+day_t: TypeAlias = int
+
+enum: int  # 枚举类型

+ 80 - 0
src/core/user.py

@@ -0,0 +1,80 @@
+from tool.type_ import *
+from enum import Enum
+
+
+class UserNotSupportError(Exception):
+    pass
+
+
+class UserType(Enum):
+    normal: enum = 1
+    manager: enum = 2
+
+
+class User:
+    def __init__(self, name: uname_t, uid: uid_t, user_type: enum):
+        self._name: uname_t = name
+        self._uid: uid_t = uid
+        self._type: enum = user_type
+
+    def get_uid(self) -> uid_t:
+        return self._uid
+
+    def get_name(self) -> uname_t:
+        return self._name
+
+    def get_info(self) -> Dict[str: uid_t, str: uname_t]:
+        return {
+            "name": self._name,
+            "uid": self._uid,
+        }
+
+    def evaluate(self) -> score_t:
+        raise UserNotSupportError
+
+    def add_score(self, score: score_t) -> score_t:
+        raise UserNotSupportError
+
+    def throw_rubbish(self) -> count_t:
+        raise UserNotSupportError
+
+
+class NormalUser(User):
+    def __init__(self, name: uname_t, uid: uid_t, reputation: score_t, rubbish: count_t, score: score_t):
+        super(NormalUser, self).__init__(name, uid, UserType.normal)
+        self._reputation = reputation
+        self._rubbish = rubbish
+        self._score = score
+
+    def get_info(self) -> Dict[str: uid_t, str: uname_t, str: score_t, str: count_t]:
+        """
+        获取当前用户的简单信息
+        :return: 用户信息字典
+        """
+        return {
+            "name": self._name,
+            "uid": self._uid,
+            "reputation": self._reputation,
+            "rubbish": self._rubbish,
+            "score": self._score
+        }
+
+    def evaluate(self) -> score_t:
+        """
+        评估信誉积分
+        :return: 信誉积分
+        """
+        ...
+
+    def add_score(self, score: score_t) -> score_t:
+        self._score += score
+        return self._score
+
+    def throw_rubbish(self) -> count_t:
+        self._rubbish += 1
+        return self._rubbish
+
+
+class ManagerUser(User):
+    def __init__(self, name: uname_t, uid: uid_t):
+        super(ManagerUser, self).__init__(name, uid, UserType.normal)

+ 2 - 0
src/main.py

@@ -0,0 +1,2 @@
+import conf
+import core

+ 0 - 0
steup.py