Files
youlegames/games/minipro/calculation/miniprogram/pages/relative/relative.ts

153 lines
3.5 KiB
TypeScript

/* Simplified Relationship Data */
const relationships: Record<string, string> = {
'default': '我',
'f': '父亲',
'm': '母亲',
'h': '丈夫',
'w': '妻子',
's': '儿子',
'd': '女儿',
'ob': '哥哥',
'yb': '弟弟',
'os': '姐姐',
'ys': '妹妹',
'f,f': '爷爷',
'f,m': '奶奶',
'f,h': '爸爸', // redundancy
'f,w': '妈妈', // redundancy
'f,s': '兄弟', // Context dependent, usually brother or self
'f,d': '姐妹',
'f,ob': '伯父',
'f,yb': '叔叔',
'f,os': '姑妈',
'f,ys': '姑姑',
'm,f': '姥爷',
'm,m': '姥姥',
'm,ob': '舅舅',
'm,yb': '舅舅',
'm,os': '姨妈',
'm,ys': '姨妈',
'f,f,f': '太爷爷',
'f,f,m': '太奶奶',
'f,m,f': '太姥爷',
'f,m,m': '太姥姥',
'h,f': '公公',
'h,m': '婆婆',
'w,f': '岳父',
'w,m': '岳母',
'ob,w': '嫂子',
'yb,w': '弟妹',
'os,h': '姐夫',
'ys,h': '妹夫',
'f,ob,w': '伯母',
'f,yb,w': '婶婶',
'f,os,h': '姑父',
'f,ys,h': '姑父',
'm,ob,w': '舅妈',
'm,yb,w': '舅妈',
'm,os,h': '姨父',
'm,ys,h': '姨父'
};
/* Reverse lookup or more complex logic could be added here */
Page({
data: {
screenText: '我', // Display the chain: 父亲 的 母亲
resultText: '', // Display the result: 奶奶
currentChain: [] as string[], // Stores codes: ['f', 'm']
gender: 1, // 1: Male, 0: Female (of the user)
buttons: [
{ text: '父', id: 'f' },
{ text: '母', id: 'm' },
{ text: '夫', id: 'h' },
{ text: '妻', id: 'w' },
{ text: '儿', id: 's' },
{ text: '女', id: 'd' },
{ text: '兄', id: 'ob' },
{ text: '弟', id: 'yb' },
{ text: '姐', id: 'os' },
{ text: '妹', id: 'ys' }
]
},
onLoad() {
},
handleBtnClick(e: any) {
const id = e.currentTarget.dataset.id;
const text = e.currentTarget.dataset.text;
const newChain = [...this.data.currentChain, id];
const newScreenText = this.data.currentChain.length === 0 ? text : this.data.screenText + ' 的 ' + text;
this.setData({
currentChain: newChain,
screenText: newScreenText
});
this.calculate(newChain);
},
handleBack() {
if (this.data.currentChain.length === 0) return;
const newChain = [...this.data.currentChain];
newChain.pop();
let newScreenText = '我';
if (newChain.length > 0) {
// Rebuild text
newScreenText = newChain.map(code => {
const btn = this.data.buttons.find(b => b.id === code);
return btn ? btn.text : '';
}).join(' 的 ');
}
this.setData({
currentChain: newChain,
screenText: newScreenText
});
if (newChain.length > 0) {
this.calculate(newChain);
} else {
this.setData({ resultText: '' });
}
},
handleClear() {
this.setData({
currentChain: [],
screenText: '我',
resultText: ''
});
},
calculate(chain: string[]) {
const key = chain.join(',');
const result = relationships[key];
if (result) {
this.setData({ resultText: result });
} else {
this.setData({ resultText: '未知亲戚 / 关系太远' });
}
},
switchGender() {
this.setData({ gender: this.data.gender === 1 ? 0 : 1 });
// Might affect initial 'Me' context if we expand logic
}
});