File size: 4,731 Bytes
62b3eea
cfdf3bc
62b3eea
273f76c
 
 
4e6be8d
a5a667b
647ff2a
5784cf3
 
7b96f10
83e7365
647ff2a
5784cf3
273f76c
 
62b3eea
7b96f10
de7f21d
cfdf3bc
eb076d0
 
 
 
 
 
 
 
 
cfdf3bc
 
 
7b96f10
 
 
cfdf3bc
7b96f10
 
 
 
 
024e4ed
cfdf3bc
de7f21d
647ff2a
 
273f76c
 
de7f21d
8e9bd24
647ff2a
d79f5f2
273f76c
11254d8
 
9acbebf
11254d8
57e2032
11254d8
57e2032
11254d8
 
 
 
 
 
 
57e2032
11254d8
57e2032
11254d8
 
 
 
 
 
273f76c
b002f06
39977eb
e3d42b8
b002f06
11254d8
 
b002f06
11254d8
 
273f76c
2b99ec2
00cd8e1
 
2b99ec2
013e105
 
 
 
 
 
 
 
de7f21d
013e105
 
 
 
 
 
 
11254d8
 
273f76c
2bbbf6e
 
 
 
9dae054
273f76c
d79f5f2
 
26e6bb5
2bbbf6e
 
 
26ab58a
106b011
d79f5f2
5784cf3
647ff2a
 
 
66b03ab
647ff2a
 
 
4f341ae
7b96f10
 
 
4862feb
00cd8e1
26ab58a
07dc7e1
 
106b011
26ab58a
07dc7e1
83e7365
273f76c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import json
import logging

import gradio as gr

from theme import Seafoam
from utils.mes_player_model import Player
from utils.mes_achievements import Achievement
from utils.mes_player_activity_model import PlayerActivity
from utils.utils import (
    get_content,
    render_player_data,
    save_latest_player_data,
    render_finished,
)

seafoam = Seafoam()


def get_player_info(player_backend_user_id):

    try:
        with open("latest_player_data.json", "r", encoding="utf-8") as file:
            player_info = json.load(file)
    except FileNotFoundError:
        logging.error("No player data found. Loading new player data.")
        save_latest_player_data()
        with open("latest_player_data.json", "r", encoding="utf-8") as file:
            player_info = json.load(file)
            
    if player_backend_user_id in player_info:
        return player_info[player_backend_user_id]
    else:
        logging.info(
            f"No data found for player ID {player_backend_user_id}. Initializing new player data."
        )

        new_player = Player(
            player_backend_user_id=player_backend_user_id,
            init=True,
            available_achievements=Achievement.get_available_achievements(),
        )

        return new_player.to_dict()

def create_new_player_activity():
    return PlayerActivity()

# start of gradio interface
with gr.Blocks(theme=seafoam, css=get_content("css/style.css")) as demo:
    player_info = gr.State()
    player_activity_tracker = gr.State(create_new_player_activity)

    with gr.Row():
        with gr.Column(
            scale=1,
            elem_classes="gallery_container",
        ):
            pet_description = gr.Markdown("# 夥伴", elem_id="pet_avatar_description")
            pet_gallery = gr.Gallery(
                [],
                label="夥伴",
                preview=False,
                elem_id="pet_gallery",
                columns=30,
                height=200,
            )

            badge_description = gr.Markdown("# 徽章", elem_id="badge_avatar_description")
            badge_gallery = gr.Gallery(
                [],
                label="徽章",
                preview=False,
                elem_id="badge_gallery",
                columns=30,
                height=200,
            )

        with gr.Column(scale=1, elem_id="player_avatar_container"):
            avatar = gr.Image(
                "avatar/blank_avatar.png",
                elem_id="player_avatar",
            )
            avatar_description = gr.Markdown(
                "# 光束守護者", elem_id="player_avatar_description"
            )

        with gr.Column(scale=1):
            adventure_description = gr.Markdown(
                    "# 冒險階段", elem_id="adventure_description"
                )
            adventure = gr.Slider(
                    value=0,
                    show_label=False,
                    interactive=False,
                    elem_id="adventure_slider",
                    info="",
                )
            achievements_description = gr.Markdown(
                "# 達成成就", elem_id="achievements_description"
            )
            achievements = gr.HighlightedText(
                value=[],
                elem_classes="achievements",
                color_map={
                    "完成": "green",
                    "未完成": "red",
                },
            )

    with gr.Row():
        html = (
            "<div style='max-width:100%; max-height:360px; overflow:auto'>"
            + get_content("htmls/adventure_blank.html")
        )
        adventure_log = gr.HTML(html, label="Adventure Log", elem_id="adventure_log")

    # handling player info
    with gr.Row():
        player_backend_id = gr.Textbox("", elem_id="player_backend_id", visible=False)
        player_info_query_btn = gr.Button(
            "Query", elem_id="trigger_button", visible=False
        )
        pull_newest_player_data = gr.Textbox("", visible=False)
        update_status = gr.Textbox("", visible=False)

    # actions when player login
    # define args
    send_player_login_info = dict(
        fn=render_finished,
        inputs=[player_activity_tracker, player_info],
        outputs=None,
    )

    player_info_query_btn.click(get_player_info, player_backend_id, player_info).then(
        render_player_data,
        player_info,
        [avatar, pet_gallery, badge_gallery, adventure_log, achievements, adventure],
    ).then(**send_player_login_info)

    pull_newest_player_data.submit(
        save_latest_player_data,
        None,
        update_status,
        api_name="pull_newest_player_data",
    )

if __name__ == "__main__":
    demo.launch()