- 精华
- 阅读权限
- 90
- 好友
- 相册
- 分享
- 听众
- 收听
- 注册时间
- 2010-1-8
- 在线时间
- 小时
- 最后登录
- 1970-1-1
|
我今天做个代码 可以拿源代码来实现他有什么帮助可以在下面留言我会回复
看图
(1)定义GM日志记录函数
- import sqlite3
- from datetime import datetime
- def log_gm_action(db_connection, gm_id, command_id, player_id, action_details):
- cursor = db_connection.cursor()
- cursor.execute("""
- INSERT INTO GMLogs (gm_id, command_id, player_id, timestamp, action_details)
- VALUES (?, ?, ?, ?, ?)
- """, (gm_id, command_id, player_id, datetime.now(), action_details))
- db_connection.commit()
复制代码 定义GM指令执行函数
- def execute_gm_command(db_connection, gm_id, command_name, player_id, params):
- cursor = db_connection.cursor()
-
- # 查询指令信息
- cursor.execute("SELECT command_id, required_permission FROM GMCommands WHERE command_name = ?", (command_name,))
- command = cursor.fetchone()
- if not command:
- print("Invalid command.")
- return
-
- command_id, required_permission = command
-
- # 模拟权限检查
- cursor.execute("SELECT permission_level FROM GMAccounts WHERE gm_id = ?", (gm_id,))
- gm_permission = cursor.fetchone()
- if not gm_permission or gm_permission[0] < required_permission:
- print("Permission denied.")
- return
-
- # 根据指令类型执行操作
- if command_name == ".modify":
- attribute, value = params.split()
- cursor.execute(f"UPDATE Players SET {attribute} = ? WHERE player_id = ?", (value, player_id))
- db_connection.commit()
- log_gm_action(db_connection, gm_id, command_id, player_id, f"Modified {attribute} to {value}")
- print(f"Modified player {player_id}'s {attribute} to {value}.")
- elif command_name == ".additem":
- item_id, quantity = params.split()
- cursor.execute("UPDATE Players SET items = json_set(items, '$.items', json(items || json(?))) WHERE player_id = ?", (f'{{"item_id":{item_id}, "quantity":{quantity}}}', player_id))
- db_connection.commit()
- log_gm_action(db_connection, gm_id, command_id, player_id, f"Added item {item_id} x{quantity}")
- print(f"Added item {item_id} x{quantity} to player {player_id}.")
- else:
- print("Command not implemented.")
复制代码 服务端接收GM指令服务端需要监听GM工具发送的指令,并调用相应的存储函数执行操作
- import socket
- import threading
- def handle_client(client_socket, db_connection):
- while True:
- try:
- command = client_socket.recv(1024).decode('utf-8')
- if not command:
- break
- print(f"Received command: {command}")
- # 解析指令并执行
- gm_id, command_name, player_id, params = command.split('|')
- execute_gm_command(db_connection, int(gm_id), command_name, int(player_id), params)
- except Exception as e:
- print(f"Error: {e}")
- break
- client_socket.close()
- def start_server():
- db_connection = sqlite3.connect("game.db")
- server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- server_socket.bind(('0.0.0.0', 8888))
- server_socket.listen(5)
- print("Server started. Listening on port 8888...")
- while True:
- client_socket, addr = server_socket.accept()
- print(f"Accepted connection from {addr}")
- client_handler = threading.Thread(target=handle_client, args=(client_socket, db_connection))
- client_handler.start()
- if __name__ == "__main__":
- start_server()
复制代码 客户端发送GM指令客户端通过网络连接到服务端,发送GM指令
- import socket
- def send_gm_command(gm_id, command_name, player_id, params):
- client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- client_socket.connect(('127.0.0.1', 8888))
- command = f"{gm_id}|{command_name}|{player_id}|{params}"
- client_socket.send(command.encode('utf-8'))
- client_socket.close()
- # 示例:发送GM指令
- send_gm_command(1, ".modify", 123, "hp 1000")
- send_gm_command(1, ".additem", 123, "101 5")
复制代码 创建数据库表
- CREATE TABLE GMAccounts (
- gm_id INT PRIMARY KEY,
- username VARCHAR(255) NOT NULL,
- password_hash VARCHAR(255) NOT NULL,
- permission_level INT NOT NULL
- );
- CREATE TABLE GMCommands (
- command_id INT PRIMARY KEY,
- command_name VARCHAR(255) NOT NULL,
- description TEXT,
- required_permission INT NOT NULL
- );
- CREATE TABLE GMLogs (
- log_id INT PRIMARY KEY,
- gm_id INT NOT NULL,
- command_id INT NOT NULL,
- player_id INT NOT NULL,
- timestamp DATETIME NOT NULL,
- action_details TEXT
- );
- CREATE TABLE Players (
- player_id INT PRIMARY KEY,
- name VARCHAR(255) NOT NULL,
- level INT NOT NULL,
- hp INT NOT NULL,
- mana INT NOT NULL,
- gold INT NOT NULL,
- items TEXT
- );
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
|