Browse Source

Added comments. Added TimeLeft.

Steve Thielemann 3 năm trước cách đây
mục cha
commit
93b3c3bdc1
4 tập tin đã thay đổi với 49 bổ sung14 xóa
  1. 12 1
      door/color.go
  2. 5 0
      door/convert.go
  3. 21 13
      door/door.go
  4. 11 0
      testdoor/testdoor.go

+ 12 - 1
door/color.go

@@ -5,6 +5,10 @@ import (
 	"strings"
 )
 
+// Convert an array of int to \x1b[1;2;3m for ANSI Color
+// output.
+// https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
+// https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
 func Color(arg ...int) string {
 	var result string = "\x1b["
 	for i := range arg {
@@ -15,6 +19,14 @@ func Color(arg ...int) string {
 	return result
 }
 
+// BUG(bugz) INVERSE does not work.
+
+// Convert a string like "BRIGHT WHITE ON BLUE" into the expected
+// ANSI Color codes.  You can use just the first 3 letters.
+// Supports BRIGHT, BOLD, BLINK and ON (to set the background color).
+// BLACK, RED, GREEN, BROWN / YELLOW, BLUE, MAGENTA, CYAN, WHITE
+// https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
+// https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
 func ColorText(color string) string {
 	// split on spaces, uppercase, match first 3 letter
 	var result []int
@@ -104,6 +116,5 @@ func ColorText(color string) string {
 		}
 
 	}
-	// fmt.Println("ColorText:", result)
 	return Color(result...)
 }

+ 5 - 0
door/convert.go

@@ -1,5 +1,10 @@
 package door
 
+// I used iconv in door++, but it was horrible.  It doesn't translate
+// control codes to Unicode.
+
+// This converts CP437 to Unicode for rendering in Unicode terminals
+// like the linux telnet client.
 func CP437_to_Unicode(cp437 string) string {
 	var result string
 

+ 21 - 13
door/door.go

@@ -30,8 +30,6 @@ g00r00                       Line 7 : User's handle/alias
 */
 
 var Reset string = Color(0)
-var READFD int
-var WRITEFD int
 
 type DropfileConfig struct {
 	comm_type      int
@@ -51,8 +49,15 @@ type Door struct {
 	config  DropfileConfig
 	READFD  int
 	WRITEFD int
+	TimeOut time.Time // Fixed point in time, when time expires
 }
 
+// Return the amount of time left as time.Duration
+func (d *Door) TimeLeft() time.Duration {
+	return d.TimeOut.Sub(time.Now())
+}
+
+// Read the BBS door file.  We only support door32.sys.
 func (d *Door) ReadDropfile(filename string) {
 	file, err := os.Open(filename)
 	if err != nil {
@@ -71,7 +76,6 @@ func (d *Door) ReadDropfile(filename string) {
 	for scanner.Scan() {
 		line := scanner.Text()
 		lines = append(lines, line)
-		// fmt.Printf("[%s]\n", line)
 	}
 
 	d.config.comm_type, err = strconv.Atoi(lines[0])
@@ -87,11 +91,10 @@ func (d *Door) ReadDropfile(filename string) {
 	d.config.node_number, err = strconv.Atoi(lines[10])
 
 	d.READFD = d.config.comm_handle
-	//if d.READFD == 0 {
-	//	d.WRITEFD = 1
-	//} else {
 	d.WRITEFD = d.config.comm_handle
-	//}
+
+	// Calculate the time when time expires.
+	d.TimeOut = time.Now().Add(time.Duration(d.config.time_left) * time.Minute)
 }
 
 func (d *Door) HasKey() bool {
@@ -109,12 +112,14 @@ func (d *Door) HasKey() bool {
 	return true
 }
 
-var Unicode bool
-var CP437 bool
-var Full_CP437 bool
-var Height int
-var Width int
+var Unicode bool    // Unicode support detected
+var CP437 bool      // CP437 support detected
+var Full_CP437 bool // Full CP437 support detected (handles control codes properly)
+var Height int      // Screen height detected
+var Width int       // Screen width detected
 
+// Detect client terminal capabilities, Unicode, CP437, Full_CP437,
+// screen Height and Width.
 func (d *Door) detect() {
 	// if d.config.comm_handle == 0 {
 	// d.Write("\377\375\042\377\373\001") // fix telnet client
@@ -188,6 +193,8 @@ func (d *Door) detect() {
 	fmt.Printf("Unicode %v Screen: %d X %d\n", Unicode, Width, Height)
 }
 
+// Initialize door framework.  Parse commandline, read dropfile,
+// detect terminal capabilities.
 func (d *Door) Init() {
 	var dropfile string
 
@@ -201,10 +208,11 @@ func (d *Door) Init() {
 
 	d.ReadDropfile(dropfile)
 
-	fmt.Printf("BBS %s, User %s / Handle %s / File %d\n", d.config.BBSID, d.config.real_name, d.config.handle, d.READFD)
+	fmt.Printf("BBS %s, User %s / Handle %s / File %d\n", d.config.BBSID, d.config.real_name, d.config.handle, d.config.comm_handle)
 	d.detect()
 }
 
+// Write string to client.
 func (d *Door) Write(output string) {
 	buffer := []byte(output)
 	n, err := syscall.Write(d.WRITEFD, buffer)

+ 11 - 0
testdoor/testdoor.go

@@ -27,7 +27,18 @@ func main() {
 	d.Write(b.Row(fmt.Sprintf("%-20s", "Meow?")) + door.CRNL)
 	d.Write(b.Bottom() + door.CRNL)
 
+	left := d.TimeLeft()
+
+	message = fmt.Sprintf("You have %0.2f minutes / %0.2f seconds remaining..."+door.CRNL, left.Minutes(), left.Seconds())
+	d.Write(message)
+
 	d.Write("Returning you to the BBS..." + door.CRNL)
 	d.SleepKey(3)
+
+	left = d.TimeLeft()
+
+	message = fmt.Sprintf("You had %0.2f minutes / %0.2f seconds remaining!"+door.CRNL, left.Minutes(), left.Seconds())
+	d.Write(message)
+
 	fmt.Println("Ending testdoor.go")
 }