Skip to content

Commit

Permalink
introduce ganzhi (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommitoan authored Jul 25, 2024
1 parent f7dbfa0 commit 8c4577d
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
44 changes: 44 additions & 0 deletions internal/fourpillars/get_four_pillars.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package fourpillars

import (
"encoding/json"
"github.com/tommitoan/bazica/internal/ultis"
"github.com/tommitoan/bazica/model"
"log/slog"
"strings"
"time"
)

Expand All @@ -14,6 +16,8 @@ func GetFourPillars(dateTime time.Time, loc *time.Location, prefixPath ...string
}

var fourPillars model.FourPillars
var ganzhi, mainElement string
var valueElement int

// Get Year pillar
yearPillar, err := GetYearPillar(path, dateTime)
Expand All @@ -22,27 +26,67 @@ func GetFourPillars(dateTime time.Time, loc *time.Location, prefixPath ...string
}
fourPillars.YearPillar = yearPillar

var yearStemBranch strings.Builder
yearStemBranch.WriteString(yearPillar.HeavenlyStem.Name)
yearStemBranch.WriteString(" ")
yearStemBranch.WriteString(yearPillar.EarthlyBranch.Name)

ganzhi, mainElement, valueElement = ultis.GetGanzhi(yearStemBranch.String())
fourPillars.YearPillar.GanZhi.Name = ganzhi
fourPillars.YearPillar.GanZhi.ElementName = mainElement
fourPillars.YearPillar.GanZhi.ElementValue = valueElement

// Get Month pillar
monthPillar, passed, remaining, err := GetMonthPillar(path, yearPillar, dateTime)
if err != nil {
return nil, 0, 0, err
}
fourPillars.MonthPillar = monthPillar

var monthStemBranch strings.Builder
monthStemBranch.WriteString(monthPillar.HeavenlyStem.Name)
monthStemBranch.WriteString(" ")
monthStemBranch.WriteString(monthPillar.EarthlyBranch.Name)

ganzhi, mainElement, valueElement = ultis.GetGanzhi(monthStemBranch.String())
fourPillars.MonthPillar.GanZhi.Name = ganzhi
fourPillars.MonthPillar.GanZhi.ElementName = mainElement
fourPillars.MonthPillar.GanZhi.ElementValue = valueElement

// Get Day pillar
dayPillar, err := GetDayPillar(dateTime, loc)
if err != nil {
return nil, 0, 0, err
}
fourPillars.DayPillar = dayPillar

var dayStemBranch strings.Builder
dayStemBranch.WriteString(dayPillar.HeavenlyStem.Name)
dayStemBranch.WriteString(" ")
dayStemBranch.WriteString(dayPillar.EarthlyBranch.Name)

ganzhi, mainElement, valueElement = ultis.GetGanzhi(dayStemBranch.String())
fourPillars.DayPillar.GanZhi.Name = ganzhi
fourPillars.DayPillar.GanZhi.ElementName = mainElement
fourPillars.DayPillar.GanZhi.ElementValue = valueElement

// Get Hour pillar
hourPillar, err := GetHourPillar(dayPillar, dateTime)
if err != nil {
return nil, 0, 0, err
}
fourPillars.HourPillar = hourPillar

var hourStemBranch strings.Builder
hourStemBranch.WriteString(hourPillar.HeavenlyStem.Name)
hourStemBranch.WriteString(" ")
hourStemBranch.WriteString(hourPillar.EarthlyBranch.Name)

ganzhi, mainElement, valueElement = ultis.GetGanzhi(hourStemBranch.String())
fourPillars.HourPillar.GanZhi.Name = ganzhi
fourPillars.HourPillar.GanZhi.ElementName = mainElement
fourPillars.HourPillar.GanZhi.ElementValue = valueElement

jsonData, _ := json.Marshal(fourPillars)
slog.Info(string(jsonData))
return &fourPillars, passed, remaining, nil
Expand Down
40 changes: 40 additions & 0 deletions internal/fourpillars/get_four_pillars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,41 @@ func TestGetFourPillarChart(t *testing.T) {
HeavenlyStem: model.HeavenlyStem{Name: model.YinFireName, Value: model.YinFireValue},
EarthlyBranch: model.EarthlyBranch{Name: model.Snake, Value: model.SnakeValue},
Year: 1977,
GanZhi: model.GanZhi{
Name: "Earth in the Sand",
ElementName: "Earth",
ElementValue: 5,
},
},
MonthPillar: &model.MonthPillar{
HeavenlyStem: model.HeavenlyStem{Name: model.YinFireName, Value: model.YinFireValue},
EarthlyBranch: model.EarthlyBranch{Name: model.Goat, Value: model.GoatValue},
Month: 7,
GanZhi: model.GanZhi{
Name: "Milky Way Water",
ElementName: "Water",
ElementValue: 1,
},
},
DayPillar: &model.DayPillar{
HeavenlyStem: model.HeavenlyStem{Name: model.YinMetalName, Value: model.YinMetalValue},
EarthlyBranch: model.EarthlyBranch{Name: model.Goat, Value: model.GoatValue},
Day: 12,
GanZhi: model.GanZhi{
Name: "Earth on the Roadside",
ElementName: "Earth",
ElementValue: 5,
},
},
HourPillar: &model.HourPillar{
HeavenlyStem: model.HeavenlyStem{Name: model.YangEarthName, Value: model.YangEarthValue},
EarthlyBranch: model.EarthlyBranch{Name: model.Rat, Value: model.RatValue},
Hour: model.TimeOfDay{Hour: 23, Minute: 30},
GanZhi: model.GanZhi{
Name: "Thunder Fire",
ElementName: "Fire",
ElementValue: 2,
},
},
},
"case 1"},
Expand All @@ -49,21 +69,41 @@ func TestGetFourPillarChart(t *testing.T) {
HeavenlyStem: model.HeavenlyStem{Name: model.YinWoodName, Value: model.YinWoodValue},
EarthlyBranch: model.EarthlyBranch{Name: model.Pig, Value: model.PigValue},
Year: 1995,
GanZhi: model.GanZhi{
Name: "Fire on the Mountain",
ElementName: "Fire",
ElementValue: 2,
},
},
MonthPillar: &model.MonthPillar{
HeavenlyStem: model.HeavenlyStem{Name: model.YangWaterName, Value: model.YangWaterValue},
EarthlyBranch: model.EarthlyBranch{Name: model.Horse, Value: model.HorseValue},
Month: 6,
GanZhi: model.GanZhi{
Name: "Willow Tree Wood",
ElementName: "Wood",
ElementValue: 3,
},
},
DayPillar: &model.DayPillar{
HeavenlyStem: model.HeavenlyStem{Name: model.YangMetalName, Value: model.YangMetalValue},
EarthlyBranch: model.EarthlyBranch{Name: model.Horse, Value: model.HorseValue},
Day: 8,
GanZhi: model.GanZhi{
Name: "Earth on the Roadside",
ElementName: "Earth",
ElementValue: 5,
},
},
HourPillar: &model.HourPillar{
HeavenlyStem: model.HeavenlyStem{Name: model.YinFireName, Value: model.YinFireValue},
EarthlyBranch: model.EarthlyBranch{Name: model.Pig, Value: model.PigValue},
Hour: model.TimeOfDay{Hour: 22, Minute: 05},
GanZhi: model.GanZhi{
Name: "Earth on the Roof",
ElementName: "Earth",
ElementValue: 5,
},
},
},
"case 2"},
Expand Down
96 changes: 96 additions & 0 deletions internal/ultis/get_ganzhi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package ultis

import "strings"

var baziMap = map[string]string{
"Yang Wood Rat": "Gold in the Sea",
"Yin Wood Ox": "Gold in the Sea",
"Yang Fire Tiger": "Fire in the Furnace",
"Yin Fire Rabbit": "Fire in the Furnace",
"Yang Earth Dragon": "Wood in the Great Forest",
"Yin Earth Snake": "Wood in the Great Forest",
"Yang Metal Horse": "Earth on the Roadside",
"Yin Metal Goat": "Earth on the Roadside",
"Yang Water Monkey": "Gold on the Point of the Sword",
"Yin Water Rooster": "Gold on the Point of the Sword",
"Yang Wood Dog": "Fire on the Mountain",
"Yin Wood Pig": "Fire on the Mountain",
"Yang Fire Rat": "Stream Water",
"Yin Fire Ox": "Stream Water",
"Yang Earth Tiger": "City Wall Earth",
"Yin Earth Rabbit": "City Wall Earth",
"Yang Metal Dragon": "Waxing Gold",
"Yin Metal Snake": "Waxing Gold",
"Yang Water Horse": "Willow Tree Wood",
"Yin Water Goat": "Willow Tree Wood",
"Yang Wood Monkey": "Well Spring Water",
"Yin Wood Rooster": "Well Spring Water",
"Yang Fire Dog": "Earth on the Roof",
"Yin Fire Pig": "Earth on the Roof",
"Yang Earth Rat": "Thunder Fire",
"Yin Earth Ox": "Thunder Fire",
"Yang Metal Tiger": "Pine and Cypress Wood",
"Yin Metal Rabbit": "Pine and Cypress Wood",
"Yang Water Dragon": "Flowing River Water",
"Yin Water Snake": "Flowing River Water",
"Yang Wood Horse": "Gold in the Sand",
"Yin Wood Goat": "Gold in the Sand",
"Yang Fire Monkey": "Fire at the Foot of the Mountain",
"Yin Fire Rooster": "Fire at the Foot of the Mountain",
"Yang Earth Dog": "Wood in the Flatland",
"Yin Earth Pig": "Wood in the Flatland",
"Yang Metal Rat": "Earth on the Wall",
"Yin Metal Ox": "Earth on the Wall",
"Yang Water Tiger": "Gold Foil",
"Yin Water Rabbit": "Gold Foil",
"Yang Wood Dragon": "Lamp Fire",
"Yin Wood Snake": "Lamp Fire",
"Yang Fire Horse": "Milky Way Water",
"Yin Fire Goat": "Milky Way Water",
"Yang Earth Monkey": "Posthouse Earth",
"Yin Earth Rooster": "Posthouse Earth",
"Yang Metal Dog": "Ornamental Gold",
"Yin Metal Pig": "Ornamental Gold",
"Yang Water Rat": "Mulberry Wood",
"Yin Water Ox": "Mulberry Wood",
"Yang Wood Tiger": "Big Stream Water",
"Yin Wood Rabbit": "Big Stream Water",
"Yang Fire Dragon": "Earth in the Sand",
"Yin Fire Snake": "Earth in the Sand",
"Yang Earth Horse": "Heavenly Fire",
"Yin Earth Goat": "Heavenly Fire",
"Yang Metal Monkey": "Pomegranate Wood",
"Yin Metal Rooster": "Pomegranate Wood",
"Yang Water Dog": "Ocean Water",
"Yin Water Pig": "Ocean Water",
}

// Function to get the corresponding element
func GetGanzhi(sign string) (string, string, int) {
if element, exists := baziMap[sign]; exists {
mainElement := ""
valueElement := 0
switch {
case strings.Contains(element, "Fire"):
mainElement = "Fire"
valueElement = 2
case strings.Contains(element, "Wood"):
mainElement = "Wood"
valueElement = 3
case strings.Contains(element, "Water"):
mainElement = "Water"
valueElement = 1
case strings.Contains(element, "Earth"):
mainElement = "Earth"
valueElement = 5
case strings.Contains(element, "Gold"):
mainElement = "Metal"
valueElement = 4
default:
mainElement = ""
valueElement = 0
}
return element, mainElement, valueElement
}
return "", "", 0
}
10 changes: 10 additions & 0 deletions model/model_bazi-chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,37 @@ type LuckPillar struct {
type YearPillar struct {
HeavenlyStem HeavenlyStem `json:"heavenly_stem"`
EarthlyBranch EarthlyBranch `json:"earthly_branch"`
GanZhi GanZhi `json:"gan_zhi"`
Year int `json:"year"`
}

type MonthPillar struct {
HeavenlyStem HeavenlyStem `json:"heavenly_stem"`
EarthlyBranch EarthlyBranch `json:"earthly_branch"`
GanZhi GanZhi `json:"gan_zhi"`
Month int `json:"month"`
}

type DayPillar struct {
HeavenlyStem HeavenlyStem `json:"heavenly_stem"`
EarthlyBranch EarthlyBranch `json:"earthly_branch"`
GanZhi GanZhi `json:"gan_zhi"`
Day int `json:"day"`
}

type HourPillar struct {
HeavenlyStem HeavenlyStem `json:"heavenly_stem"`
EarthlyBranch EarthlyBranch `json:"earthly_branch"`
GanZhi GanZhi `json:"gan_zhi"`
Hour TimeOfDay `json:"hour"`
}

type GanZhi struct {
Name string `json:"name"`
ElementName string `json:"element_name"`
ElementValue int `json:"element_value"`
}

type TimeOfDay struct {
Hour int
Minute int
Expand Down

0 comments on commit 8c4577d

Please sign in to comment.