ziwei-mcp
Version:
紫微斗数 MCP (Ziwei Doushu Model Context Protocol) - 基于 MCP 协议的紫微斗数命理分析工具
250 lines (223 loc) • 6.91 kB
JavaScript
/**
* 人生时间轴分析工具
*/
class LifeTimelineAnalyzer {
async analyzeLifeTimeline(args) {
try {
const { chartId, startAge, endAge, analysisGranularity = "year" } = args;
// 获取命盘数据
const chart = this.getChartById(chartId);
if (!chart) {
throw new Error(`未找到命盘: ${chartId}`);
}
const timeline = this.generateLifeTimeline(
chart,
startAge,
endAge,
analysisGranularity,
);
return {
success: true,
data: {
chartId,
ageRange: { start: startAge, end: endAge },
granularity: analysisGranularity,
timeline: timeline,
majorPeriods: this.identifyMajorPeriods(timeline),
keyTransitions: this.identifyKeyTransitions(timeline),
overallTrend: this.analyzeOverallTrend(timeline),
},
};
} catch (error) {
return {
success: false,
error: error.message,
};
}
}
getChartById(chartId) {
// 模拟数据
return {
id: chartId,
info: { name: "张三", birthYear: 1990 },
palaces: [],
};
}
generateLifeTimeline(chart, startAge, endAge, granularity) {
const timeline = [];
if (granularity === "decade") {
for (let age = startAge; age <= endAge; age += 10) {
timeline.push(this.analyzeDecade(chart, age));
}
} else if (granularity === "year") {
for (let age = startAge; age <= endAge; age++) {
timeline.push(this.analyzeYear(chart, age));
}
} else if (granularity === "month") {
for (let age = startAge; age <= endAge; age++) {
for (let month = 1; month <= 12; month++) {
timeline.push(this.analyzeMonth(chart, age, month));
}
}
}
return timeline;
}
analyzeDecade(chart, startAge) {
return {
period: `${startAge}-${startAge + 9}岁`,
type: "decade",
fortune: {
overall: Math.floor(Math.random() * 40) + 60,
career: Math.floor(Math.random() * 40) + 60,
wealth: Math.floor(Math.random() * 40) + 60,
relationship: Math.floor(Math.random() * 40) + 60,
health: Math.floor(Math.random() * 40) + 60,
},
keyEvents: this.predictDecadeEvents(startAge),
opportunities: this.identifyDecadeOpportunities(startAge),
challenges: this.identifyDecadeChallenges(startAge),
advice: this.getDecadeAdvice(startAge),
};
}
analyzeYear(chart, age) {
return {
age: age,
year: chart.info.birthYear + age,
type: "year",
fortune: {
overall: Math.floor(Math.random() * 40) + 60,
career: Math.floor(Math.random() * 40) + 60,
wealth: Math.floor(Math.random() * 40) + 60,
relationship: Math.floor(Math.random() * 40) + 60,
health: Math.floor(Math.random() * 40) + 60,
},
highlights: this.getYearHighlights(age),
warnings: this.getYearWarnings(age),
recommendations: this.getYearRecommendations(age),
};
}
analyzeMonth(chart, age, month) {
return {
age: age,
month: month,
type: "month",
fortune: Math.floor(Math.random() * 40) + 60,
focus: this.getMonthFocus(month),
advice: this.getMonthAdvice(month),
};
}
identifyMajorPeriods(timeline) {
return [
{
name: "成长期",
ageRange: "0-18岁",
characteristics: "学习成长,性格形成",
},
{
name: "奋斗期",
ageRange: "19-35岁",
characteristics: "事业起步,建立家庭",
},
{
name: "成熟期",
ageRange: "36-55岁",
characteristics: "事业巅峰,财富积累",
},
{
name: "收获期",
ageRange: "56-70岁",
characteristics: "享受成果,传承经验",
},
{
name: "智慧期",
ageRange: "71岁以上",
characteristics: "颐养天年,智慧传承",
},
];
}
identifyKeyTransitions(timeline) {
return [
{ age: 18, event: "成年转折", description: "从学生转向社会人" },
{ age: 30, event: "而立之年", description: "事业和家庭的重要节点" },
{ age: 40, event: "不惑之年", description: "人生观念的重大转变" },
{ age: 50, event: "知天命", description: "对人生有更深的理解" },
{ age: 60, event: "花甲之年", description: "进入人生新阶段" },
];
}
analyzeOverallTrend(timeline) {
const avgFortune =
timeline.reduce((sum, period) => {
return sum + (period.fortune?.overall || period.fortune || 70);
}, 0) / timeline.length;
return {
trend: avgFortune >= 75 ? "上升" : avgFortune >= 65 ? "平稳" : "需努力",
score: Math.round(avgFortune),
description: this.getTrendDescription(avgFortune),
};
}
predictDecadeEvents(startAge) {
const events = {
0: ["启蒙教育", "性格形成"],
10: ["求学深造", "兴趣发展"],
20: ["事业起步", "恋爱结婚"],
30: ["事业发展", "家庭建设"],
40: ["事业巅峰", "子女教育"],
50: ["财富积累", "健康关注"],
60: ["退休规划", "享受生活"],
70: ["颐养天年", "传承智慧"],
};
return events[startAge] || ["人生发展", "经验积累"];
}
identifyDecadeOpportunities(startAge) {
return [`${startAge}岁阶段的主要机会`, "发展潜力分析"];
}
identifyDecadeChallenges(startAge) {
return [`${startAge}岁阶段的主要挑战`, "需要注意的问题"];
}
getDecadeAdvice(startAge) {
return [`${startAge}岁阶段的发展建议`, "重点关注方向"];
}
getYearHighlights(age) {
return [`${age}岁的亮点事件`, "值得期待的发展"];
}
getYearWarnings(age) {
return [`${age}岁需要注意的问题`, "潜在风险提醒"];
}
getYearRecommendations(age) {
return [`${age}岁的行动建议`, "发展重点"];
}
getMonthFocus(month) {
const focuses = [
"新年规划",
"感情发展",
"事业推进",
"财务管理",
"健康养生",
"学习提升",
"人际关系",
"创新突破",
"收获成果",
"调整方向",
"总结反思",
"准备来年",
];
return focuses[month - 1];
}
getMonthAdvice(month) {
return `${month}月的具体建议和注意事项`;
}
getTrendDescription(avgFortune) {
if (avgFortune >= 80) {
return "整体运势优秀,人生发展顺利";
} else if (avgFortune >= 70) {
return "整体运势良好,稳步向前发展";
} else if (avgFortune >= 60) {
return "整体运势平稳,需要努力进取";
} else {
return "整体运势一般,需要加倍努力";
}
}
}
module.exports = {
LifeTimelineAnalyzer,
};