Explorar el Código

mojang status

Light-Beacon hace 2 meses
padre
commit
4205dd8428

+ 1 - 1
Project/configs/config.yml

@@ -4,6 +4,6 @@ ProjectInfo.GitInfo.Enable: True
 Public.domain: news.bugjump.net
 Server.Version.By: hash
 Refresher.Enable: true
-Refresher.Precision: d
+Refresher.Precision: m
 NewsHomepage.Debug: False
 #NewsHomepage.Protocol: https

+ 19 - 0
Project/libraries/Homepage/MojangStatus/MojangStatusCompress.xaml

@@ -0,0 +1,19 @@
+<local:MyCard Margin="0,3,0,5">
+    <Grid Margin="10,10,12,10">
+        <Grid.ColumnDefinitions>
+            <ColumnDefinition Width="*"/>
+            <ColumnDefinition Width="Auto"/>
+        </Grid.ColumnDefinitions>
+        <StackPanel Orientation="Horizontal">
+        <TextBlock Margin="5,0,8,0" IsHitTestVisible="False" FontSize="12" FontWeight="Bold">
+            <InlineUIContainer>
+                <Border Background="${@MojangMajorStatusColor}" CornerRadius="2" Height="10px" Width="10px"/>
+            </InlineUIContainer>
+            <Run Text=" Mojang 服务状态:${@MojangMajorStatusText}" />
+        </TextBlock>
+        <local:MyIconButton HorizontalAlignment="Right" ToolTip="数据由 mcstate.net 提供" EventType="打开网页" EventData="https://www.mcstate.net/mojang-status" Height="14" Width="14"
+                        LogoScale="3.2" Logo="M509.92 795.84c157.904 0 285.92-128.016 285.92-285.92C795.84 352 667.808 224 509.92 224 352 224 224 352 224 509.92c0 157.904 128 285.92 285.92 285.92z m0 48C325.504 843.84 176 694.32 176 509.92 176 325.504 325.504 176 509.92 176c184.416 0 333.92 149.504 333.92 333.92 0 184.416-149.504 333.92-333.92 333.92z m-2.448-400.704h16a16 16 0 0 1 16 16v201.728a16 16 0 0 1-16 16h-16a16 16 0 0 1-16-16V459.136a16 16 0 0 1 16-16z m0-100.176h16a16 16 0 0 1 16 16v23.648a16 16 0 0 1-16 16h-16a16 16 0 0 1-16-16v-23.648a16 16 0 0 1 16-16z"/>
+        </StackPanel>
+        <TextBlock Grid.Column="1" VerticalAlignment="Center" TextWrapping="Wrap" Margin="0,0,10,0" Foreground="#888" FontSize="12" Text="更新时间:${@MojangStatusTimeCompressed}" />
+    </Grid>
+</local:MyCard>

+ 74 - 0
Project/libraries/Homepage/MojangStatus/MojangStatusDetail.xaml

@@ -0,0 +1,74 @@
+<local:MyCard Margin="0,0,0,5" CanSwap="False">
+    <local:MyCard.Inlines>
+        <InlineUIContainer>
+            <Border Background="${@MojangMajorStatusColor}" CornerRadius="2" Height="10px" Width="10px"/>
+        </InlineUIContainer>
+        <Run Text=" Mojang 服务状态:${@MojangMajorStatusText}"/>
+    </local:MyCard.Inlines>
+    <StackPanel  Margin="25,45,25,15">
+    <WrapPanel>
+        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="5" Width="170">
+            <Border Background="${@MojangStatusColor|session}" CornerRadius="2" Height="14px" Width="14px" Margin="5"/>
+            <TextBlock Margin="5,0,10,0">
+                <Run Text="会话服务器"/>
+                <Run Text="${@MojangStatusText|session}" Foreground="${@MojangStatusColor|session}" />
+                <LineBreak/>
+                <Run Text="多人游戏验证" Foreground="#888" FontSize="12" />
+            </TextBlock>
+        </StackPanel>
+        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="5" Width="170">
+            <Border Background="${@MojangStatusColor|api}" CornerRadius="2" Height="14px" Width="14px" Margin="5"/>
+            <TextBlock Margin="5,0,10,0">
+                <Run Text="Mojang API"  />
+                <Run Text="${@MojangStatusText|api}" Foreground="${@MojangStatusColor|api}" />
+                <LineBreak/>
+                <Run Text="玩家登录与信息查询" Foreground="#888" FontSize="12" />
+            </TextBlock>
+        </StackPanel>
+        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="5" Width="170">
+            <Border Background="${@MojangStatusColor|microsoft}" CornerRadius="2" Height="14px" Width="14px" Margin="5"/>
+            <TextBlock Margin="5,0,10,0">
+                <Run Text="微软认证服务器" />
+                <Run Text="${@MojangStatusText|microsoft}" Foreground="${@MojangStatusColor|microsoft}" />
+                <LineBreak/>
+                <Run Text="微软登录认证" Foreground="#888" FontSize="12" />
+            </TextBlock>
+        </StackPanel>
+        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="5" Width="170">
+            <Border Background="${@MojangStatusColor|accounts}" CornerRadius="2" Height="14px" Width="14px" Margin="5"/>
+            <TextBlock Margin="5,0,10,0">
+                <Run Text="账户服务器" />
+                <Run Text="${@MojangStatusText|accounts}" Foreground="${@MojangStatusColor|accounts}" />
+                <LineBreak/>
+                <Run Text="账户管理" Foreground="#888" FontSize="12" />
+            </TextBlock>
+        </StackPanel>
+        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="5" Width="170">
+            <Border Background="${@MojangStatusColor|textures}" CornerRadius="2" Height="14px" Width="14px" Margin="5"/>
+            <TextBlock Margin="5,0,10,0">
+                <Run Text="纹理服务器" />
+                <Run Text="${@MojangStatusText|textures}" Foreground="${@MojangStatusColor|textures}" />
+                <LineBreak/>
+                <Run Text="皮肤与披风资源" Foreground="#888" FontSize="12" />
+            </TextBlock>
+        </StackPanel>
+        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="5" Width="170">
+            <Border Background="${@MojangStatusColor|website}" CornerRadius="2" Height="14px" Width="14px" Margin="5"/>
+            <TextBlock Margin="5,0,10,0">
+                <Run Text="Minecraft 官网" />
+                <Run Text="${@MojangStatusText|website}" Foreground="${@MojangStatusColor|website}" />
+                <LineBreak/>
+                <Run Text="设置、博文与更新日志" Foreground="#888" FontSize="12" />
+            </TextBlock>
+        </StackPanel>
+    </WrapPanel>
+    <WrapPanel Margin="0,10,0,0">
+    <TextBlock TextWrapping="Wrap" Margin="0,0,10,0" Foreground="#888" FontSize="12" VerticalAlignment="Center"
+                Text="信息更新时间:${@MojangStatusTime}" />
+    <TextBlock TextWrapping="Wrap" Margin="0,0,10,0" Foreground="#888" FontSize="12" VerticalAlignment="Center">
+        数据由<local:MyTextButton Margin="2,0,2,-2" Text="mcstate.net" EventType="打开网页" 
+    EventData="https://www.mcstate.net/mojang-status"/>提供
+    </TextBlock>
+    </WrapPanel>
+    </StackPanel>
+</local:MyCard>

+ 4 - 0
Project/libraries/Homepage/MojangStatus/__LIBRARY__.yml

@@ -0,0 +1,4 @@
+name: MojangStatusCards
+default:
+  templates:
+    - Raw

+ 158 - 0
Project/modules/mojang_status.py

@@ -0,0 +1,158 @@
+import requests
+from queue import Queue
+from datetime import datetime, timedelta
+from typing import TypedDict, Literal, TypeAlias
+from homepagebuilder.interfaces import script
+
+API_URL = "https://www.mcstate.net/api/mojang-status"
+QUEUE_MAX_SIZE = 3
+
+GREEN_COLOR = "#41D041"
+YELLOW_COLOR = "#D1A041"
+RED_COLOR = "#D14241"
+GRAY_COLOR = "#888888"
+
+StatusState: TypeAlias = Literal['up', 'down', 'unknown']
+class StatusDetials(TypedDict):
+    session: StatusState = "unknown"
+    accounts: StatusState = "unknown"
+    microsoft: StatusState = "unknown"
+    api: StatusState = "unknown"
+    textures: StatusState = "unknown"
+    website: StatusState = "unknown"
+
+class StatusRecord(TypedDict):
+    status: StatusDetials
+    check_time: datetime = datetime.min
+
+CHECK_TIME: datetime = datetime.now()
+UPDATE_INTERVAL = 150
+UPDATE_TIME = datetime.min
+STATUS_RECORDS: Queue[StatusRecord | None] = Queue(maxsize=QUEUE_MAX_SIZE)
+CALCULATED_STATUS_CACHE: StatusRecord = None
+
+def get_status():
+    try:
+        response = requests.get(API_URL)
+        response.raise_for_status()  # Raise an error for bad responses
+        return response.json()  # Return the JSON response as a dictionary
+    except requests.RequestException as e:
+        print(f"Error fetching Mojang status: {e}")
+        return None
+
+def get_service_status(status_dict, service_id):
+    return next((service['status'] for service in status_dict['services'] if service['id'] == service_id), 'unknown')
+
+def update_status():
+    global UPDATE_TIME, CALCULATED_STATUS_CACHE
+    result = get_status()
+    status: StatusRecord = {'status': StatusDetials()}
+    if result is not None:
+        status['status']['session'] = get_service_status(result, 'session')
+        status['status']['accounts'] = get_service_status(result, 'accounts')
+        status['status']['microsoft'] = get_service_status(result, 'microsoft')
+        status['status']['api'] = get_service_status(result, 'api')
+        status['status']['textures'] = get_service_status(result, 'textures')
+        status['status']['website'] = get_service_status(result, 'website')
+        status['check_time'] = datetime.strptime(result.get('checkedAt'), "%Y-%m-%dT%H:%M:%S.%fZ") + timedelta(hours=8)
+    if STATUS_RECORDS.full():
+        STATUS_RECORDS.get()
+    STATUS_RECORDS.put(status)
+    UPDATE_TIME = datetime.now()
+    CALCULATED_STATUS_CACHE = None
+
+def should_update_status():
+    global UPDATE_TIME
+    return (datetime.now() - UPDATE_TIME).total_seconds() >= UPDATE_INTERVAL
+
+def get_calculated_status():
+    global CALCULATED_STATUS_CACHE, STATUS_RECORDS
+    if CALCULATED_STATUS_CACHE is not None:
+        return CALCULATED_STATUS_CACHE
+    status: StatusRecord = StatusRecord()
+    status['status'] = StatusDetials()
+    for item in STATUS_RECORDS.queue:
+        if item is None:
+            continue
+        for key, value in item['status'].items():
+            if key == 'check_time':
+                continue
+            if value == 'down' and status.get(key) != 'up':
+                status['status'][key] = 'down'
+            elif value == 'up':
+                status['status'][key] = 'up'
+    status['check_time'] = max((item['check_time'] for item in STATUS_RECORDS.queue if item is not None), default=datetime.min)
+    return status
+        
+
+def get_mojang_major_status():
+    status = get_calculated_status()['status']
+    if all(value == 'unknown' for value in status.values()):
+        return "未知"
+    if status['api'] == 'down' or status['session'] == 'down' or status['microsoft'] == 'down':
+        return "异常"
+    elif status['accounts'] == 'down' or status['textures'] == 'down' or status['website'] == 'down':
+        return "问题"
+    else:
+        return "正常"
+
+@script('MojangStatusText')
+def mojang_status_text(name, **_):
+    if should_update_status():
+        update_status()
+    status = get_calculated_status()['status'].get(name, 'unknown')
+    if status == 'up':
+        return "正常"
+    elif status == 'down':
+        return "异常"
+    else:
+        return "未知"
+
+@script('MojangMajorStatusText')
+def mojang_major_status_text(**_):
+    if should_update_status():
+        update_status()
+    return get_mojang_major_status()
+
+@script('MojangStatusColor')
+def mojang_status_color(name, **_):
+    if should_update_status():
+        update_status()
+    status = get_calculated_status()['status'].get(name, 'unknown')
+    if status == 'up':
+        return GREEN_COLOR
+    elif status == 'down':
+        return RED_COLOR
+    else:
+        return GRAY_COLOR
+
+@script('MojangMajorStatusColor')
+def mojang_major_status_color(**_):
+    if should_update_status():
+        update_status()
+    major_status = get_mojang_major_status()
+    if major_status == "正常":
+        return GREEN_COLOR
+    elif major_status == "异常":
+        return RED_COLOR
+    elif major_status == "问题":
+        return YELLOW_COLOR 
+    else:
+        return GRAY_COLOR
+
+
+@script('MojangStatusTime')
+def mojang_status_time(**_):
+    return get_calculated_status()['check_time'].strftime("%Y年%m月%d日 %H:%M")
+
+@script('MojangStatusTimeCompressed')
+def mojang_status_time(**_):
+    return get_calculated_status()['check_time'].strftime("%H:%M")
+
+@script('MojangStatusCardReference')
+def mojang_status(**_):
+    status = get_mojang_major_status()
+    if status == "异常":
+        return "MojangStatusDetail"
+    else:
+        return "MojangStatusCompress"

+ 1 - 0
Project/pages/News.yml

@@ -6,6 +6,7 @@ alias:
 cards:
  - animated_noticebar | s = false
  - ${@IF|${args.debug}|debug}
+ - ${@MojangStatusCardReference}
  - ${@NewsList}
  - ${@MainPageVersions|new}
 # - VersionLatestListCard | title=更新日志 | enable-card-style=True