File size: 16,704 Bytes
4c75d73 |
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
#!/usr/bin/env python3
"""
Simple MMORPG Game Client
========================
A basic client implementation for connecting to the MMORPG game server via MCP.
This client can join the game, move around, chat, and interact with NPCs.
Requirements:
pip install mcp aiohttp asyncio
Usage:
python simple_game_client.py
Features:
- Connect to game server via MCP
- Register as a player
- Move around the game world
- Send chat messages
- Interact with NPCs
- Get game state information
- Interactive console interface
"""
import asyncio
import json
import uuid
import sys
from typing import Dict, List, Optional
try:
from mcp import ClientSession
from mcp.client.sse import sse_client
from contextlib import AsyncExitStack
except ImportError:
print("❌ Missing dependencies. Install with: pip install mcp aiohttp")
sys.exit(1)
class SimpleGameClient:
"""Simple client for connecting to the MMORPG game server"""
def __init__(self, server_url="http://127.0.0.1:7868/gradio_api/mcp/sse"):
self.server_url = server_url
self.session = None
self.connected = False
self.tools = []
self.exit_stack = None
self.client_id = f"client_{uuid.uuid4().hex[:8]}"
self.agent_id = None
self.player_name = None
print(f"🎮 Game Client initialized with ID: {self.client_id}")
async def connect(self):
"""Connect to the game server"""
print(f"🔌 Connecting to {self.server_url}...")
try:
# Clean up any existing connection
if self.exit_stack:
await self.exit_stack.aclose()
self.exit_stack = AsyncExitStack()
# Establish SSE connection to MCP server
transport = await self.exit_stack.enter_async_context(
sse_client(self.server_url)
)
read_stream, write_callable = transport
# Create MCP client session
self.session = await self.exit_stack.enter_async_context(
ClientSession(read_stream, write_callable)
)
# Initialize the session
await self.session.initialize()
# Get available tools/commands from server
response = await self.session.list_tools()
self.tools = response.tools
self.connected = True
tool_names = [tool.name for tool in self.tools]
print(f"✅ Connected successfully!")
print(f"📋 Available commands: {', '.join(tool_names)}")
return True
except Exception as e:
print(f"❌ Connection failed: {e}")
self.connected = False
return False
async def register_player(self, player_name: str):
"""Register as a new player in the game"""
if not self.connected:
print("❌ Not connected to server")
return False
print(f"👤 Registering player: {player_name}")
try:
# Find the register tool
register_tool = self._find_tool(['register', 'agent'])
if not register_tool:
print("❌ Register tool not available on server")
return False
# Register the player
result = await self.session.call_tool(
register_tool.name,
{"name": player_name, "client_id": self.client_id}
)
# Parse the result
content = self._extract_content(result)
print(f"📝 Registration response: {content}")
if "registered" in content.lower() or "success" in content.lower():
self.player_name = player_name
# Try to extract agent_id from response
self._parse_agent_id(content)
print(f"✅ Successfully registered as: {player_name}")
return True
else:
print(f"❌ Registration failed: {content}")
return False
except Exception as e:
print(f"❌ Registration error: {e}")
return False
async def move_player(self, direction: str):
"""Move the player in the specified direction"""
if not self._check_ready():
return False
direction = direction.lower()
valid_directions = ['north', 'south', 'east', 'west', 'up', 'down']
if direction not in valid_directions:
print(f"❌ Invalid direction. Use: {', '.join(valid_directions)}")
return False
try:
# Find the move tool
move_tool = self._find_tool(['move', 'agent'])
if not move_tool:
print("❌ Move tool not available")
return False
# Execute the move
result = await self.session.call_tool(
move_tool.name,
{"client_id": self.client_id, "direction": direction}
)
content = self._extract_content(result)
print(f"🚶 Move {direction}: {content}")
return True
except Exception as e:
print(f"❌ Move error: {e}")
return False
async def send_chat(self, message: str):
"""Send a chat message to other players"""
if not self._check_ready():
return False
if not message.strip():
print("❌ Cannot send empty message")
return False
try:
# Find the chat tool
chat_tool = self._find_tool(['chat', 'send'])
if not chat_tool:
print("❌ Chat tool not available")
return False
# Send the chat message
result = await self.session.call_tool(
chat_tool.name,
{"client_id": self.client_id, "message": message}
)
content = self._extract_content(result)
print(f"💬 Chat sent: {content}")
return True
except Exception as e:
print(f"❌ Chat error: {e}")
return False
async def get_game_state(self):
"""Get current game state information"""
if not self.connected:
print("❌ Not connected to server")
return None
try:
# Find the game state tool
state_tool = self._find_tool(['state', 'game'])
if not state_tool:
print("❌ Game state tool not available")
return None
# Get game state
result = await self.session.call_tool(state_tool.name, {})
content = self._extract_content(result)
try:
# Try to parse as JSON
game_state = json.loads(content)
return game_state
except json.JSONDecodeError:
# Return as text if not JSON
return {"info": content}
except Exception as e:
print(f"❌ Game state error: {e}")
return None
async def interact_with_npc(self, npc_id: str, message: str):
"""Interact with an NPC"""
if not self._check_ready():
return False
if not npc_id.strip() or not message.strip():
print("❌ NPC ID and message are required")
return False
try:
# Find the interact tool
interact_tool = self._find_tool(['interact', 'npc'])
if not interact_tool:
print("❌ NPC interaction tool not available")
return False
# Interact with NPC
result = await self.session.call_tool(
interact_tool.name,
{"client_id": self.client_id, "npc_id": npc_id, "message": message}
)
content = self._extract_content(result)
print(f"🤖 {npc_id}: {content}")
return True
except Exception as e:
print(f"❌ NPC interaction error: {e}")
return False
async def list_tools(self):
"""List all available tools on the server"""
if not self.connected:
print("❌ Not connected to server")
return
print("🛠️ Available Tools:")
for i, tool in enumerate(self.tools, 1):
print(f" {i}. {tool.name}")
if hasattr(tool, 'description') and tool.description:
print(f" Description: {tool.description}")
def _find_tool(self, keywords: List[str]):
"""Find a tool by keywords"""
for keyword in keywords:
tool = next((t for t in self.tools if keyword in t.name.lower()), None)
if tool:
return tool
return None
def _extract_content(self, result):
"""Extract content from MCP result"""
try:
if hasattr(result, 'content') and result.content:
if isinstance(result.content, list):
content_parts = []
for item in result.content:
if hasattr(item, 'text'):
content_parts.append(item.text)
else:
content_parts.append(str(item))
return ''.join(content_parts)
elif hasattr(result.content, 'text'):
return result.content.text
else:
return str(result.content)
return str(result)
except Exception as e:
return f"Error extracting content: {e}"
def _parse_agent_id(self, content: str):
"""Try to extract agent ID from response"""
lines = content.split('\n')
for line in lines:
if any(keyword in line.lower() for keyword in ['agent_id', 'id:', 'player id']):
parts = line.split(':')
if len(parts) > 1:
self.agent_id = parts[1].strip()
print(f"🆔 Agent ID: {self.agent_id}")
break
def _check_ready(self):
"""Check if client is ready for game operations"""
if not self.connected:
print("❌ Not connected to server")
return False
if not self.player_name:
print("❌ Not registered as a player")
return False
return True
async def disconnect(self):
"""Disconnect from the server"""
if self.exit_stack:
await self.exit_stack.aclose()
self.connected = False
self.player_name = None
self.agent_id = None
print("🔌 Disconnected from server")
class InteractiveGameClient:
"""Interactive console interface for the game client"""
def __init__(self):
self.client = SimpleGameClient()
self.running = True
async def start(self):
"""Start the interactive client"""
print("🎮 MMORPG Simple Game Client")
print("=" * 40)
self.show_help()
while self.running:
try:
command = input("\n> ").strip()
if command:
await self.process_command(command)
except KeyboardInterrupt:
print("\n\n👋 Goodbye!")
break
except EOFError:
break
await self.client.disconnect()
async def process_command(self, command: str):
"""Process a user command"""
parts = command.split()
if not parts:
return
cmd = parts[0].lower()
args = parts[1:]
if cmd == "help":
self.show_help()
elif cmd == "connect":
await self.client.connect()
elif cmd == "register":
if args:
name = " ".join(args)
await self.client.register_player(name)
else:
print("❌ Usage: register <player_name>")
elif cmd == "move":
if args:
direction = args[0]
await self.client.move_player(direction)
else:
print("❌ Usage: move <direction>")
print(" Directions: north, south, east, west")
elif cmd == "chat":
if args:
message = " ".join(args)
await self.client.send_chat(message)
else:
print("❌ Usage: chat <message>")
elif cmd == "state":
state = await self.client.get_game_state()
if state:
print("📊 Game State:")
print(json.dumps(state, indent=2))
elif cmd == "npc":
if len(args) >= 2:
npc_id = args[0]
message = " ".join(args[1:])
await self.client.interact_with_npc(npc_id, message)
else:
print("❌ Usage: npc <npc_id> <message>")
print(" Example: npc weather_oracle What's the weather in Berlin?")
elif cmd == "tools":
await self.client.list_tools()
elif cmd in ["quit", "exit", "q"]:
self.running = False
else:
print(f"❓ Unknown command: {cmd}")
print(" Type 'help' for available commands")
def show_help(self):
"""Show available commands"""
print("\n📋 Available Commands:")
print(" help - Show this help")
print(" connect - Connect to game server")
print(" register <name> - Register as a player")
print(" move <direction> - Move player (north/south/east/west)")
print(" chat <message> - Send chat message")
print(" npc <id> <message> - Talk to an NPC")
print(" state - Get game state")
print(" tools - List available server tools")
print(" quit - Exit the client")
print("\n💡 Example session:")
print(" > connect")
print(" > register MyPlayer")
print(" > move north")
print(" > chat Hello everyone!")
print(" > npc weather_oracle What's the weather?")
async def quick_demo():
"""Quick demo showing basic functionality"""
print("🚀 Running Quick Demo...")
client = SimpleGameClient()
# Connect
if not await client.connect():
print("❌ Demo failed - could not connect")
return
# Register
if not await client.register_player("DemoPlayer"):
print("❌ Demo failed - could not register")
return
# Send greeting
await client.send_chat("Hello! Demo player here!")
# Move around
moves = ["north", "east", "south", "west"]
for direction in moves:
await client.move_player(direction)
await asyncio.sleep(1)
# Get state
state = await client.get_game_state()
if state:
print(f"📊 Current game state: {json.dumps(state, indent=2)}")
# Try NPC interaction
await client.interact_with_npc("weather_oracle", "Hello there!")
await client.disconnect()
print("✅ Demo completed!")
def main():
"""Main entry point"""
if len(sys.argv) > 1 and sys.argv[1] == "demo":
# Run quick demo
asyncio.run(quick_demo())
else:
# Run interactive client
client = InteractiveGameClient()
asyncio.run(client.start())
if __name__ == "__main__":
main()
|