package main import ( "errors" "fmt" "log" "strings" "time" door "git.red-green.com/RedGreen/doorgo" ) var ( d door.Door udb *UserDB u *User ) func main() { d.Init("Geode") defer d.Close() udb = &UserDB{} err := udb.Open("users.db3") if err != nil { fmt.Printf("UserDB.Open(filename='users.db3') > %v\r\n", err) return } d.WriteS(door.ColorTextS("BRI YEL ON BLA") + "Geode" + door.ColorTextS("WHI ON BLA") + " by " + door.ColorTextS("BRI RED ON BLA") + "Apollo" + door.ColorTextS("BRI BLA ON BLA") + "@" + door.ColorTextS("BRI GRE ON BLA") + "21:1/236" + door.Reset + door.CRNL) LOGIN: for { d.WriteS(door.GotoS(1, 3) + door.Reset + strings.Repeat(" ", door.Width-1) + door.GotoS(1, 4) + strings.Repeat(" ", door.Width-1) + door.GotoS(1, 5) + strings.Repeat(" ", door.Width-1) + door.GotoS(1, 6) + strings.Repeat(" ", door.Width-1) + door.GotoS(1, 7) + strings.Repeat(" ", door.Width-1) + door.GotoS(1, 8) + strings.Repeat(" ", door.Width-1) + door.GotoS(1, 9) + strings.Repeat(" ", door.Width-1)) d.WriteS(door.GotoS(1, 3) + door.ColorTextS("BRI YEL ON BLA") + "Login: " + door.ColorTextS("BRI WHI ON BLU")) name := d.Input(30) if len(name) == 0 { d.WriteS(door.Reset + door.CRNL + "Goodbye!") return } u = udb.FindUserByName(name) if u == nil { u = &User{Name: name} // New User, So get their password and their class of choice d.WriteS(door.GotoS(1, 4) + door.ColorTextS("BRI YEL ON BLA") + "Password: " + door.ColorTextS("BLU ON BLU")) pwd := d.Input(30) if len(pwd) == 0 { d.WriteS(door.Reset + door.CRNL + "Goodbye!") return } err = u.SetPassword(pwd) if err != nil { d.WriteS(door.Reset + door.CRNL + "Err: " + err.Error() + door.CRNL) _, _, err = d.WaitKey(door.Inactivity) if err != nil { if errors.Is(err, door.ErrDisconnected) { return } } continue LOGIN } d.WriteS(door.Reset + door.CRNL) bb := door.ColorTextS("BRI BLA ON BLA") d.WriteS(bb + "[" + door.ColorTextS("BRI CYA ON BLA") + "K" + bb + "] " + door.ColorTextS("CYA ON BLA") + "Knight" + door.Reset + door.CRNL) d.WriteS(bb + "[" + door.ColorTextS("BRO ON BLA") + "T" + bb + "] " + door.ColorTextS("BRO ON BLA") + "Thief" + door.Reset + door.CRNL) d.WriteS(bb + "[" + door.ColorTextS("BRI GRE ON BLA") + "C" + bb + "] " + door.ColorTextS("GRE ON BLA") + "Cleric" + door.Reset + door.CRNL) d.WriteS(bb + "[" + door.ColorTextS("BRI MAG ON BLA") + "W" + bb + "] " + door.ColorTextS("MAG ON BLA") + "Wizard" + door.Reset + door.CRNL) d.WriteS(bb + "[" + door.ColorTextS("BRI RED ON BLA") + "R" + bb + "] " + door.ColorTextS("RED ON BLA") + "Ranger" + door.Reset + door.CRNL) r, _, err := d.WaitKey(door.Inactivity) if err != nil { if errors.Is(err, door.ErrDisconnected) { return } continue LOGIN } switch r { case 'k', 'K': u.Class = 1 u.Health = 30 u.MaxHealth = 30 u.MaxMagic = 0 u.Magic = 0 u.Gold = 200 case 't', 'T': u.Class = 2 u.Health = 20 u.MaxHealth = 20 u.MaxMagic = 0 u.Magic = 0 u.Gold = 250 // Thieves get bonus gold (+50) case 'c', 'C': u.Class = 3 u.Health = 25 u.MaxHealth = 25 u.MaxMagic = 7 u.Magic = 7 u.Gold = 200 case 'w', 'W': u.Class = 4 u.Health = 20 u.MaxHealth = 20 u.MaxMagic = 12 u.Magic = 12 u.Gold = 200 case 'r', 'R': u.Class = 5 u.Health = 20 u.MaxHealth = 20 u.MaxMagic = 0 u.Magic = 0 u.Gold = 200 default: continue LOGIN } u.Level = 1 err = udb.SaveUser(u) if err != nil { log.Printf("u = %#v", u) log.Printf("UserDB.SaveUser(u) > %v", err) d.WriteS(door.Reset + door.CRNL + "Err: " + err.Error() + door.CRNL) d.WaitKey(door.Inactivity) return } break LOGIN } else { // Existing User, So verify their password d.WriteS(door.GotoS(1, 4) + door.ColorTextS("BRI YEL ON BLA") + "Password: " + door.ColorTextS("BLU ON BLU")) pwd := d.Input(30) if len(pwd) == 0 { d.WriteS(door.Reset + door.CRNL + "Goodbye!") return } if u.VerifyPassword(pwd) != nil { d.WriteS(door.Reset + door.CRNL + door.ColorTextS("BRI RED ON BLA") + "Invalid Username or Password!" + door.Reset + door.CRNL) d.WaitKey(time.Duration(10) * time.Second) u = nil continue LOGIN } else { d.WriteS(door.Reset + door.CRNL) break LOGIN } } } d.WriteS(door.ColorTextS("BRI CYA ON BLA") + "Welcome " + door.ColorTextS("BRI WHI ON BLA") + u.Name + door.ColorTextS("BRI CYA ON BLA") + " the ") switch u.Class { case 1: d.WriteS(door.ColorTextS("CYA ON BLA") + "Knight") case 2: d.WriteS(door.ColorTextS("BRO ON BLA") + "Thief") case 3: d.WriteS(door.ColorTextS("GRE ON BLA") + "Cleric") case 4: d.WriteS(door.ColorTextS("MAG ON BLA") + "Wizard") case 5: d.WriteS(door.ColorTextS("RED ON BLA") + "Ranger") default: d.WriteS(door.ColorTextS("BLI BRI RED ON BLA") + "Unknown") } d.WriteS(door.Reset + door.CRNL) go renderStatusBar() d.WaitKey(door.Inactivity) } func renderStatusBar() { if d.Disconnected || u == nil { return } d.WriteS(door.SavePos + door.GotoS(1, door.Height-1) + door.Reset + strings.Repeat(" ", door.Width-1) + door.RestorePos) d.WriteS(door.SavePos + door.GotoS(1, door.Height) + door.Reset + strings.Repeat(" ", door.Width-1) + door.RestorePos) bb := door.ColorTextS("BRI BLA ON BLA") bar_width := ((door.Width - 3) / 2) - 1 hp := int(float64(bar_width) * (float64(u.Health) / float64(u.MaxHealth))) pos_txt := fmt.Sprintf("%-3d%% (%d, %d) %-3d%%", int((float64(u.Health)/float64(u.MaxHealth))*100.0), u.X, u.Y, int((float64(u.Magic)/float64(u.MaxMagic))*100.0)) d.WriteS(door.SavePos + door.GotoS((door.Width/2)-(len(pos_txt)/2), door.Height-1) + door.ColorTextS("BRI CYA ON BLA") + pos_txt + door.RestorePos) if u.MaxMagic != 0 { mp := int(float64(bar_width) * (float64(u.Magic) / float64(u.MaxMagic))) log.Printf("HP=%d/%d %d%% (%d) MP=%d/%d %d%% (%d) bar_width=%d", u.Health, u.MaxHealth, int((float64(u.Health)/float64(u.MaxHealth))*100.0), hp, u.Magic, u.MaxMagic, int((float64(u.Magic)/float64(u.MaxMagic))*100.0), mp, bar_width) d.WriteS(door.SavePos + door.GotoS(1, door.Height) + bb + "[" + door.ColorTextS("BRI RED ON BLA") + strings.Repeat(door.BARS.Solid, hp) + strings.Repeat(" ", bar_width-hp) + bb + "] [" + door.ColorTextS("BRI MAG ON BLA") + strings.Repeat(door.BARS.Solid, mp) + strings.Repeat(" ", bar_width-mp) + bb + "]" + door.RestorePos) } else { log.Printf("HP=%d/%d %d%% (%d) bar_width=%d", u.Health, u.MaxHealth, int((float64(u.Health)/float64(u.MaxHealth))*100.0), hp, bar_width) d.WriteS(door.SavePos + door.GotoS(1, door.Height) + bb + "[" + door.ColorTextS("BRI RED ON BLA") + strings.Repeat(door.BARS.Solid, hp) + strings.Repeat(" ", bar_width-hp) + bb + "]" + door.RestorePos) } }