Spaces:
Running
Running
randomplayer
Browse files
app.py
CHANGED
@@ -1,13 +1,60 @@
|
|
1 |
import gradio as gr
|
2 |
-
from poke_env import
|
|
|
|
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# iframe code to embed Pokemon Showdown
|
13 |
iframe_code = """
|
@@ -21,15 +68,23 @@ iframe_code = """
|
|
21 |
"""
|
22 |
|
23 |
def main():
|
|
|
|
|
|
|
24 |
with gr.Blocks() as demo:
|
25 |
-
gr.Markdown("#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
gr.Markdown("### Pokémon Showdown
|
|
|
33 |
gr.HTML(iframe_code)
|
34 |
|
35 |
return demo
|
|
|
1 |
import gradio as gr
|
2 |
+
from poke_env.player.random_player import RandomPlayer
|
3 |
+
from poke_env.server_configuration import ShowdownServerConfiguration
|
4 |
+
from poke_env.player_configuration import PlayerConfiguration
|
5 |
+
import asyncio
|
6 |
+
import threading
|
7 |
|
8 |
+
# Set up the random player
|
9 |
+
server_config = ShowdownServerConfiguration()
|
10 |
+
random_player = None
|
11 |
+
player_thread = None
|
12 |
|
13 |
+
# Function to start the random player in a separate thread
|
14 |
+
def start_random_player():
|
15 |
+
loop = asyncio.new_event_loop()
|
16 |
+
asyncio.set_event_loop(loop)
|
17 |
+
global random_player
|
18 |
+
|
19 |
+
# Configure the random player with a default name
|
20 |
+
player_config = PlayerConfiguration("RandomBot", None)
|
21 |
+
random_player = RandomPlayer(
|
22 |
+
player_configuration=player_config,
|
23 |
+
server_configuration=server_config,
|
24 |
+
battle_format="gen8randombattle"
|
25 |
+
)
|
26 |
+
|
27 |
+
# Start the player
|
28 |
+
loop.run_until_complete(random_player.accept_challenges(max_concurrent_battles=1))
|
29 |
|
30 |
+
# Start the random player in a background thread
|
31 |
+
def initialize_random_player():
|
32 |
+
global player_thread
|
33 |
+
player_thread = threading.Thread(target=start_random_player)
|
34 |
+
player_thread.daemon = True
|
35 |
+
player_thread.start()
|
36 |
|
37 |
+
# Function to send a battle invite
|
38 |
+
async def send_battle_invite(username):
|
39 |
+
if random_player is None:
|
40 |
+
return f"Error: Random player not initialized. Try again."
|
41 |
+
|
42 |
+
try:
|
43 |
+
# Send a challenge to the user
|
44 |
+
await random_player.send_challenges(username, n_challenges=1)
|
45 |
+
return f"Battle invitation sent to {username}! Check the Pokemon Showdown interface below."
|
46 |
+
except Exception as e:
|
47 |
+
return f"Error sending challenge: {str(e)}"
|
48 |
+
|
49 |
+
# Wrapper for the async function to use in Gradio
|
50 |
+
def invite_to_battle(username):
|
51 |
+
if not username.strip():
|
52 |
+
return "Please enter a valid username."
|
53 |
+
|
54 |
+
loop = asyncio.new_event_loop()
|
55 |
+
result = loop.run_until_complete(send_battle_invite(username))
|
56 |
+
loop.close()
|
57 |
+
return result
|
58 |
|
59 |
# iframe code to embed Pokemon Showdown
|
60 |
iframe_code = """
|
|
|
68 |
"""
|
69 |
|
70 |
def main():
|
71 |
+
# Initialize the random player when the app starts
|
72 |
+
initialize_random_player()
|
73 |
+
|
74 |
with gr.Blocks() as demo:
|
75 |
+
gr.Markdown("# Pokémon Showdown Battle Bot")
|
76 |
+
|
77 |
+
with gr.Row():
|
78 |
+
with gr.Column():
|
79 |
+
gr.Markdown("### Enter your Pokémon Showdown username to receive a battle invitation:")
|
80 |
+
name_input = gr.Textbox(label="Your Pokémon Showdown Username", placeholder="Enter the username you're using on Showdown")
|
81 |
+
battle_button = gr.Button("Send Battle Invitation")
|
82 |
+
result_text = gr.Textbox(label="Result", interactive=False)
|
83 |
+
|
84 |
+
battle_button.click(fn=invite_to_battle, inputs=name_input, outputs=result_text)
|
85 |
|
86 |
+
gr.Markdown("### Pokémon Showdown Interface")
|
87 |
+
gr.Markdown("Log in to Pokémon Showdown in the interface below, using the same username you entered above.")
|
88 |
gr.HTML(iframe_code)
|
89 |
|
90 |
return demo
|