Browse Source

Refactoring code around.

Steve Thielemann 3 years ago
parent
commit
403255f3a6
5 changed files with 322 additions and 282 deletions
  1. 8 6
      deck.go
  2. 187 0
      menus.go
  3. 109 89
      playcards.go
  4. 0 187
      space-ace.go
  5. 18 0
      utils.go

+ 8 - 6
deck.go

@@ -271,8 +271,10 @@ func CalcCardPos(pos int) Pos {
 // 0-29
 var CardPos []Pos
 
+// What cards block which card?
 var Blocks [][]int
 
+// Pre-calculate the card positions
 func init() {
 	CardPos = make([]Pos, 30)
 	for x := 0; x < 30; x++ {
@@ -329,6 +331,10 @@ func CanPlay(card1 DeckType, card2 DeckType) bool {
 	return false
 }
 
+// Set the size used to represent cards in the deck.
+// 1 deck = 52, 2 decks = 104, 4 decks = 208
+// If we need more then 4 decks, we'll need a bigger
+// type here.
 type DeckType int8
 
 /*
@@ -348,6 +354,8 @@ func ShuffleCards(RNG *rand.Rand, decks int) []DeckType {
 	return result
 }
 
+// Create an array to hold the deck state.
+// Has it played?  Has it been unblocked/face up?
 func MakeCardStates(decks int) []DeckType {
 	var size int = decks * 52
 	var result []DeckType = make([]DeckType, size)
@@ -382,12 +390,6 @@ func RemoveCard(c int, back_color string, off_x int, off_y int, left bool, right
 	result += door.Goto(Pos.X+off_x, Pos.Y+off_y+2) + "     "
 	return result
 }
-func Abs(x int) int {
-	if x < 0 {
-		return -x
-	}
-	return x
-}
 
 func FindNextActiveCard(left bool, state *[]DeckType, current int) int {
 	Pos := &CardPos[current]

+ 187 - 0
menus.go

@@ -0,0 +1,187 @@
+package main
+
+import (
+	"red-green/door"
+	"strings"
+)
+
+func MainMenu() door.Menu {
+	// Make the main menu
+	m := door.Menu{Panel: door.Panel{Width: 25,
+		X:           5,
+		Y:           5,
+		Style:       door.DOUBLE,
+		Title:       "[ Main Menu: ]",
+		TitleOffset: 3,
+		BorderColor: door.ColorText("BRI CYAN ON BLA")}}
+	m.SelectedR = door.MakeMenuRender(door.ColorText("BOLD CYAN"),
+		door.ColorText("BOLD BLUE"),
+		door.ColorText("BOLD CYAN"),
+		door.ColorText("BOLD BLUE"))
+	m.UnselectedR = door.MakeMenuRender(door.ColorText("BOLD YEL ON BLUE"),
+		door.ColorText("BOLD WHI ON BLUE"),
+		door.ColorText("BOLD YEL ON BLUE"),
+		door.ColorText("BOLD CYAN ON BLUE"))
+
+	m.AddSelection("P", "Play Cards")
+	m.AddSelection("S", "View Scores")
+	m.AddSelection("C", "Configure")
+	m.AddSelection("H", "Help")
+	m.AddSelection("A", "About this game")
+	m.AddSelection("Q", "Quit")
+	return m
+}
+
+func ConfigMenu() door.Menu {
+	m := door.Menu{Panel: door.Panel{Width: 31,
+		X:           5,
+		Y:           5,
+		Style:       door.DOUBLE,
+		Title:       "[ Configuration Menu ]",
+		TitleColor:  door.ColorText("BRI CYAN ON BLUE"),
+		BorderColor: door.ColorText("CYAN ON BLUE")}}
+	m.SelectedR = door.MakeMenuRender(door.ColorText("BOLD CYAN"),
+		door.ColorText("BOLD BLUE"),
+		door.ColorText("BOLD CYAN"),
+		door.ColorText("BOLD BLUE"))
+	m.UnselectedR = door.MakeMenuRender(door.ColorText("BOLD YEL ON BLUE"),
+		door.ColorText("BOLD WHI ON BLUE"),
+		door.ColorText("BOLD YEL ON BLUE"),
+		door.ColorText("BOLD CYAN ON BLUE"))
+
+	m.AddSelection("D", "Deck Colors")
+	m.AddSelection("V", "View Settings")
+	m.AddSelection("Q", "Quit")
+	return m
+}
+
+var DeckColors []string
+
+func init() {
+	DeckColors = []string{
+		"All", "Brown", "Green", "Red", "Blue", "Cyan", "Magenta", "White"}
+}
+
+func MakeColorsRender(brackets string, text_color string, colors map[string]string) func(string) string {
+	renderF := func(text string) string {
+		words := find_words(text)
+		var option bool = true
+		var color_word bool = false
+		var result string
+		var last_color string
+		var word_color string
+		var words_id = 0
+		word_pair := words[words_id]
+		normal := colors["ALL"]
+
+		for tpos, c := range text {
+			if option {
+				if c == '[' || c == ']' {
+					if last_color != brackets {
+						result += brackets
+						last_color = brackets
+					}
+					if c == ']' {
+						option = false
+					}
+				} else {
+					if last_color != text_color {
+						result += text_color
+						last_color = text_color
+					}
+				}
+				result += string(c)
+			} else {
+				// Out of the options
+				if color_word {
+					if tpos < word_pair[1] {
+						if last_color != word_color {
+							result += word_color
+							last_color = word_color
+						} else {
+							color_word = false
+							if last_color != normal {
+								result += normal
+								last_color = normal
+							}
+						}
+						result += string(c)
+					}
+				} else {
+					// look for COLOR word
+					if tpos >= word_pair[1] {
+						words_id++
+						if words_id >= len(words) {
+							word_pair = []int{999, 999}
+						} else {
+							word_pair = words[words_id]
+						}
+					}
+
+					if word_pair[0] == tpos {
+						// start of word
+						possible := text[word_pair[0]:word_pair[1]]
+						color_code, has := colors[strings.ToUpper(possible)]
+						// log.Printf("Match %s / found %s %#v\n", possible, color_code, has)
+						if has {
+							word_color = color_code
+						} else {
+							word_color = colors["ALL"]
+						}
+						if last_color != word_color {
+							result += word_color
+							last_color = word_color
+						}
+					}
+
+					result += string(c)
+				}
+			}
+		}
+		return result
+	}
+	return renderF
+}
+
+func DeckColorMenu(current string) door.Menu {
+	m := door.Menu{Panel: door.Panel{Width: 31,
+		X:           5,
+		Y:           5,
+		Style:       door.DOUBLE,
+		Title:       "[ Deck Menu ]",
+		TitleColor:  door.ColorText("BRI CYAN ON BLUE"),
+		BorderColor: door.ColorText("CYAN ON BLUE")}}
+
+	unselectMap := map[string]string{"BLUE": door.ColorText("BRI BLUE ON BLUE"),
+		"BROWN":   door.ColorText("BROWN ON BLUE"),
+		"RED":     door.ColorText("BRI RED ON BLUE"),
+		"CYAN":    door.ColorText("CYAN ON BLUE"),
+		"GREEN":   door.ColorText("BRI GREEN ON BLUE"),
+		"MAGENTA": door.ColorText("BRI MAGENTA ON BLUE"),
+		"WHITE":   door.ColorText("BRI WHITE ON BLUE"),
+		"ALL":     door.ColorText("BRI WHITE ON BLUE")}
+	selectMap := map[string]string{"BLUE": door.ColorText("BRI BLUE ON BLACK"),
+		"BROWN":   door.ColorText("BROWN ON BLACK"),
+		"RED":     door.ColorText("BRI RED ON BLACK"),
+		"CYAN":    door.ColorText("CYAN ON BLACK"),
+		"GREEN":   door.ColorText("BRI GREEN ON BLACK"),
+		"MAGENTA": door.ColorText("BRI MAGENTA ON BLACK"),
+		"WHITE":   door.ColorText("BRI WHITE ON BLACK"),
+		"ALL":     door.ColorText("BRI WHITE ON BLACK")}
+
+	m.SelectedR = MakeColorsRender(door.ColorText("BOLD CYAN"),
+		door.ColorText("BOLD BLUE"),
+		selectMap)
+	m.UnselectedR = MakeColorsRender(door.ColorText("BOLD YEL ON BLUE"),
+		door.ColorText("BOLD WHI ON BLUE"),
+		unselectMap)
+
+	m.Chosen = 0
+	for idx, color := range DeckColors {
+		m.AddSelection(color[:1], color)
+		if color == current {
+			m.Chosen = idx
+		}
+	}
+	return m
+}

+ 109 - 89
playcards.go

@@ -62,12 +62,14 @@ type PlayCards struct {
 	LeftPanel        door.Panel
 	CmdPanel         door.Panel
 	NextQuitPanel    door.Panel
-	Calendar         door.Panel
+	CalendarPanel    door.Panel
 	DeckPanel        Deck
 	Deck             []DeckType
 	State            []DeckType
 	Off_X            int
 	Off_Y            int
+	Calendar_day_t   [31]int64
+	Calendar         door.Screen
 }
 
 // Adjust date to 2:00 AM
@@ -79,6 +81,14 @@ func NormalizeDate(date *time.Time) {
 	*date = date.Add(-offset)
 }
 
+// Find the 1st of the Month (Normalized) 2:00 AM
+func FirstOfMonthDate(date *time.Time) {
+	if date.Day() != 1 {
+		*date = date.AddDate(0, 0, 1-date.Day())
+	}
+	NormalizeDate(date)
+}
+
 func cmdLineRender(bracket string, inner string, outer string) func(string) string {
 	return func(input string) string {
 		var r door.Render = door.Render{Line: input}
@@ -321,100 +331,109 @@ func (pc *PlayCards) Init() {
 
 	// calendar = make_calendar();
 	{
-		W := 41
-		pc.Calendar = door.Panel{Width: W,
-			Style:       door.DOUBLE,
-			BorderColor: door.ColorText("CYAN ON BLACK")}
-
-		calendarRender := func(text string) string {
-			// var result string
-			// var lastColor string
-			result := door.Render{Line: text}
-			digits := door.ColorText("CYAN ON BLACK")
-			digits_play := door.ColorText("BOLD CYAN ON BLACK")
-			spaces := door.ColorText("WHITE ON BLACK")
-			open := door.ColorText("BOLD GREEN ON BLACK")
-			hands := door.ColorText("BOLD YELLOW ON BLACK")
-			full := door.ColorText("RED ON BLACK")
-			nny := door.ColorText("BOLD BLACK ON BLACK")
-
-			for days := 0; days < 7; days++ {
-				var dayText string
-
-				if days == 0 {
-					result.Append(spaces, 1)
-				}
-				dayText = text[1+days*6 : (1+days*6)+3]
-				if dayText[1] == ' ' {
-					result.Append(spaces, 3)
-				} else {
-					// Something is here
-					cday := dayText[2]
-					if cday == 'o' || cday == 'h' {
-						result.Append(digits_play, 2)
-					} else {
-						result.Append(digits, 2)
+		pc.Calendar = door.Screen{}
+		var month time.Time = time.Now()
+		FirstOfMonthDate(&month)
+
+	}
+	/*
+		// This is make_calendar_panel (not make_calendar)
+		{
+			W := 41
+			pc.CalendarPanel = door.Panel{Width: W,
+				Style:       door.DOUBLE,
+				BorderColor: door.ColorText("CYAN ON BLACK")}
+
+			calendarRender := func(text string) string {
+				// var result string
+				// var lastColor string
+				result := door.Render{Line: text}
+				digits := door.ColorText("CYAN ON BLACK")
+				digits_play := door.ColorText("BOLD CYAN ON BLACK")
+				spaces := door.ColorText("WHITE ON BLACK")
+				open := door.ColorText("BOLD GREEN ON BLACK")
+				hands := door.ColorText("BOLD YELLOW ON BLACK")
+				full := door.ColorText("RED ON BLACK")
+				nny := door.ColorText("BOLD BLACK ON BLACK")
+
+				for days := 0; days < 7; days++ {
+					var dayText string
+
+					if days == 0 {
+						result.Append(spaces, 1)
 					}
-					switch cday {
-					case 'o':
-						result.Append(open, 1)
-					case 'h':
-						result.Append(hands, 1)
-					case 'x':
-						result.Append(full, 1)
-					case 'u':
-						result.Append(nny, 1)
+					dayText = text[1+days*6 : (1+days*6)+3]
+					if dayText[1] == ' ' {
+						result.Append(spaces, 3)
+					} else {
+						// Something is here
+						cday := dayText[2]
+						if cday == 'o' || cday == 'h' {
+							result.Append(digits_play, 2)
+						} else {
+							result.Append(digits, 2)
+						}
+						switch cday {
+						case 'o':
+							result.Append(open, 1)
+						case 'h':
+							result.Append(hands, 1)
+						case 'x':
+							result.Append(full, 1)
+						case 'u':
+							result.Append(nny, 1)
+						}
 					}
-				}
 
-				if days == 6 {
-					result.Append(spaces, 1)
-				} else {
-					result.Append(spaces, 3)
+					if days == 6 {
+						result.Append(spaces, 1)
+					} else {
+						result.Append(spaces, 3)
 
+					}
 				}
+				return result.Result
 			}
-			return result.Result
-		}
 
-		for row := 0; row < 6; row++ {
-			calendarUpdate := func() string {
-				var text string
+			for row := 0; row < 6; row++ {
+				calendarUpdate := func() string {
+					var text string
 
-				for d := 0; d < 7; d++ {
-					text += " "
-					v := pc.Day_status[(row*7)+d]
-					if v == 0 {
-						text += "   "
-					} else {
-						text += fmt.Sprintf("%-2d", v)
-						status := pc.Day_status[v-1]
-						switch status {
-						case 0:
-							text += "o"
-						case 1:
-							text += "h"
-						case 2:
-							text += "x"
-						case 3:
-							text += "u"
-						}
-					}
-					if d == 6 {
+					for d := 0; d < 7; d++ {
 						text += " "
-					} else {
-						text += "  "
+						v := pc.Day_status[(row*7)+d]
+						if v == 0 {
+							text += "   "
+						} else {
+							text += fmt.Sprintf("%-2d", v)
+							status := pc.Day_status[v-1]
+							switch status {
+							case 0:
+								text += "o"
+							case 1:
+								text += "h"
+							case 2:
+								text += "x"
+							case 3:
+								text += "u"
+							}
+						}
+						if d == 6 {
+							text += " "
+						} else {
+							text += "  "
+						}
 					}
+					return text
 				}
-				return text
-			}
 
-			pc.Calendar.Lines = append(pc.Calendar.Lines,
-				door.Line{Text: calendarUpdate(),
-					UpdateF: calendarUpdate,
-					RenderF: calendarRender})
+				pc.Calendar.Lines = append(pc.Calendar.Lines,
+					door.Line{Text: calendarUpdate(),
+						UpdateF: calendarUpdate,
+						RenderF: calendarRender})
+			}
 		}
-	}
+	*/
 
 }
 
@@ -425,8 +444,7 @@ func (pc *PlayCards) Play() {
 	NormalizeDate(&pc.Play_day)
 
 	pc.Play_day_t = pc.Play_day.Unix()
-	// played := db.HandsPlayedOnDay(pc.Play_day_t)
-	played := 0
+	played := pc.DB.HandsPlayedOnDay(pc.Play_day_t)
 
 	var r int
 
@@ -677,15 +695,13 @@ Next_Hand:
 				} else {
 					if r == 'D' || r == 'Q' {
 						if pc.Score >= 50 {
-							// pc.DB.SaveScore(now, pc.Time_T, .. pc.Score)
-							_ = pc.Score
+							pc.DB.SaveScore(time.Now().Unix(), pc.Play_day_t, pc.Hand, 0, pc.Score)
 						}
 
 						in_game = false
 					} else {
 						if r == 'N' {
-							// pc.DB.SaveScore(...)
-							_ = pc.Score
+							pc.DB.SaveScore(time.Now().Unix(), pc.Play_day_t, pc.Hand, 0, pc.Score)
 							pc.Hand++
 							goto Next_Hand
 						}
@@ -783,7 +799,7 @@ Next_Hand:
 								pc.Door.Write(pc.ScorePanel.Update())
 
 								// Save Score
-								// pc.DB.SaveScore(now, pc.Play_day_t, pc.Hand, pc.Score)
+								pc.DB.SaveScore(time.Now().Unix(), pc.Play_day_t, pc.Hand, 1, pc.Score)
 								pc.NextQuitPanel.Update()
 								pc.Door.Write(pc.NextQuitPanel.Output())
 
@@ -978,3 +994,7 @@ func (pc *PlayCards) Redraw(Dealing bool) {
 func Bonus() string {
 	return door.ColorText("BOLD YELLOW") + "BONUS"
 }
+
+func (pc *PlayCards) UpdateCalendarDays() {
+
+}

+ 0 - 187
space-ace.go

@@ -6,7 +6,6 @@ import (
 	"os"
 	"path/filepath"
 	"red-green/door"
-	"regexp"
 	"strconv"
 	"strings"
 	"time"
@@ -27,11 +26,6 @@ func init() {
 
 var Config map[string]string
 
-func find_words(text string) [][]int {
-	word, _ := regexp.Compile("([A-Za-z]+)")
-	return word.FindAllStringIndex(text, -1)
-}
-
 func press_a_key(d *door.Door) int {
 	green := door.ColorText("BOLD GREEN")
 	blue := door.ColorText("BOLD BLUE")
@@ -146,56 +140,6 @@ func press_a_key(d *door.Door) int {
 	return r
 }
 
-func MainMenu() door.Menu {
-	// Make the main menu
-	m := door.Menu{Panel: door.Panel{Width: 25,
-		X:           5,
-		Y:           5,
-		Style:       door.DOUBLE,
-		Title:       "[ Main Menu: ]",
-		TitleOffset: 3,
-		BorderColor: door.ColorText("BRI CYAN ON BLA")}}
-	m.SelectedR = door.MakeMenuRender(door.ColorText("BOLD CYAN"),
-		door.ColorText("BOLD BLUE"),
-		door.ColorText("BOLD CYAN"),
-		door.ColorText("BOLD BLUE"))
-	m.UnselectedR = door.MakeMenuRender(door.ColorText("BOLD YEL ON BLUE"),
-		door.ColorText("BOLD WHI ON BLUE"),
-		door.ColorText("BOLD YEL ON BLUE"),
-		door.ColorText("BOLD CYAN ON BLUE"))
-
-	m.AddSelection("P", "Play Cards")
-	m.AddSelection("S", "View Scores")
-	m.AddSelection("C", "Configure")
-	m.AddSelection("H", "Help")
-	m.AddSelection("A", "About this game")
-	m.AddSelection("Q", "Quit")
-	return m
-}
-
-func ConfigMenu() door.Menu {
-	m := door.Menu{Panel: door.Panel{Width: 31,
-		X:           5,
-		Y:           5,
-		Style:       door.DOUBLE,
-		Title:       "[ Configuration Menu ]",
-		TitleColor:  door.ColorText("BRI CYAN ON BLUE"),
-		BorderColor: door.ColorText("CYAN ON BLUE")}}
-	m.SelectedR = door.MakeMenuRender(door.ColorText("BOLD CYAN"),
-		door.ColorText("BOLD BLUE"),
-		door.ColorText("BOLD CYAN"),
-		door.ColorText("BOLD BLUE"))
-	m.UnselectedR = door.MakeMenuRender(door.ColorText("BOLD YEL ON BLUE"),
-		door.ColorText("BOLD WHI ON BLUE"),
-		door.ColorText("BOLD YEL ON BLUE"),
-		door.ColorText("BOLD CYAN ON BLUE"))
-
-	m.AddSelection("D", "Deck Colors")
-	m.AddSelection("V", "View Settings")
-	m.AddSelection("Q", "Quit")
-	return m
-}
-
 func display_information(d *door.Door) {
 	d.Write(door.Clrscr)
 
@@ -238,137 +182,6 @@ func panel_demo(d *door.Door) {
 	d.Write(p.Output() + door.CRNL)
 }
 
-func MakeColorsRender(brackets string, text_color string, colors map[string]string) func(string) string {
-	renderF := func(text string) string {
-		words := find_words(text)
-		var option bool = true
-		var color_word bool = false
-		var result string
-		var last_color string
-		var word_color string
-		var words_id = 0
-		word_pair := words[words_id]
-		normal := colors["ALL"]
-
-		for tpos, c := range text {
-			if option {
-				if c == '[' || c == ']' {
-					if last_color != brackets {
-						result += brackets
-						last_color = brackets
-					}
-					if c == ']' {
-						option = false
-					}
-				} else {
-					if last_color != text_color {
-						result += text_color
-						last_color = text_color
-					}
-				}
-				result += string(c)
-			} else {
-				// Out of the options
-				if color_word {
-					if tpos < word_pair[1] {
-						if last_color != word_color {
-							result += word_color
-							last_color = word_color
-						} else {
-							color_word = false
-							if last_color != normal {
-								result += normal
-								last_color = normal
-							}
-						}
-						result += string(c)
-					}
-				} else {
-					// look for COLOR word
-					if tpos >= word_pair[1] {
-						words_id++
-						if words_id >= len(words) {
-							word_pair = []int{999, 999}
-						} else {
-							word_pair = words[words_id]
-						}
-					}
-
-					if word_pair[0] == tpos {
-						// start of word
-						possible := text[word_pair[0]:word_pair[1]]
-						color_code, has := colors[strings.ToUpper(possible)]
-						// log.Printf("Match %s / found %s %#v\n", possible, color_code, has)
-						if has {
-							word_color = color_code
-						} else {
-							word_color = colors["ALL"]
-						}
-						if last_color != word_color {
-							result += word_color
-							last_color = word_color
-						}
-					}
-
-					result += string(c)
-				}
-			}
-		}
-		return result
-	}
-	return renderF
-}
-
-var DeckColors []string
-
-func init() {
-	DeckColors = []string{
-		"All", "Brown", "Green", "Red", "Blue", "Cyan", "Magenta", "White"}
-}
-
-func DeckColorMenu(current string) door.Menu {
-	m := door.Menu{Panel: door.Panel{Width: 31,
-		X:           5,
-		Y:           5,
-		Style:       door.DOUBLE,
-		Title:       "[ Deck Menu ]",
-		TitleColor:  door.ColorText("BRI CYAN ON BLUE"),
-		BorderColor: door.ColorText("CYAN ON BLUE")}}
-
-	unselectMap := map[string]string{"BLUE": door.ColorText("BRI BLUE ON BLUE"),
-		"BROWN":   door.ColorText("BROWN ON BLUE"),
-		"RED":     door.ColorText("BRI RED ON BLUE"),
-		"CYAN":    door.ColorText("CYAN ON BLUE"),
-		"GREEN":   door.ColorText("BRI GREEN ON BLUE"),
-		"MAGENTA": door.ColorText("BRI MAGENTA ON BLUE"),
-		"WHITE":   door.ColorText("BRI WHITE ON BLUE"),
-		"ALL":     door.ColorText("BRI WHITE ON BLUE")}
-	selectMap := map[string]string{"BLUE": door.ColorText("BRI BLUE ON BLACK"),
-		"BROWN":   door.ColorText("BROWN ON BLACK"),
-		"RED":     door.ColorText("BRI RED ON BLACK"),
-		"CYAN":    door.ColorText("CYAN ON BLACK"),
-		"GREEN":   door.ColorText("BRI GREEN ON BLACK"),
-		"MAGENTA": door.ColorText("BRI MAGENTA ON BLACK"),
-		"WHITE":   door.ColorText("BRI WHITE ON BLACK"),
-		"ALL":     door.ColorText("BRI WHITE ON BLACK")}
-
-	m.SelectedR = MakeColorsRender(door.ColorText("BOLD CYAN"),
-		door.ColorText("BOLD BLUE"),
-		selectMap)
-	m.UnselectedR = MakeColorsRender(door.ColorText("BOLD YEL ON BLUE"),
-		door.ColorText("BOLD WHI ON BLUE"),
-		unselectMap)
-
-	m.Chosen = 0
-	for idx, color := range DeckColors {
-		m.AddSelection(color[:1], color)
-		if color == current {
-			m.Chosen = idx
-		}
-	}
-	return m
-}
-
 /*
 door::renderFunction statusValue(door::ANSIColor status,
                                  door::ANSIColor value) {

+ 18 - 0
utils.go

@@ -0,0 +1,18 @@
+package main
+
+import "regexp"
+
+// Find all the words in a string
+// return [][2]int of index,length
+func find_words(text string) [][]int {
+	word, _ := regexp.Compile("([A-Za-z]+)")
+	return word.FindAllStringIndex(text, -1)
+}
+
+// int version of Abs
+func Abs(x int) int {
+	if x < 0 {
+		return -x
+	}
+	return x
+}