From 8cdd7bdaa6bdbb8e83603db7fe654eabcbb6bced Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Tue, 4 Jun 2019 03:53:29 +0000 Subject: [PATCH] cleaning up enter travel text --- commands.go | 22 ++++++++++++++++++++++ travel.go | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/commands.go b/commands.go index b9607a6..10d58d5 100644 --- a/commands.go +++ b/commands.go @@ -3,6 +3,7 @@ package main import ( "fmt" "strings" + "text/template" ) var commandRegistry map[string]*Command @@ -102,6 +103,27 @@ are farther away take longer to communicate with. }, } +type status struct { + State string + Balance int + Bombs int + Kills int + Location string + Description string +} + +var statusTemplate = template.Must(template.New("status").Parse(` +******************************************************************************** +Current State: {{.State}} +Balance: {{.Balance}} +Bombs: {{.Bombs}} +Kills: {{.Kills}} +Location: {{.Location}} + +{{.Description}} +******************************************************************************** +`)) + var statusCommand = Command{ name: "status", help: "display your current status", diff --git a/travel.go b/travel.go index 4dfb6cc..3862158 100644 --- a/travel.go +++ b/travel.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "text/template" "time" ) @@ -33,20 +34,35 @@ func NewTravel(c *Connection, start, dest *System) ConnectionState { help: "displays estimated time of arrival", arity: 0, handler: func(c *Connection, args ...string) { - c.Printf("Remaining: %v\n", t.remaining()) - c.Printf("Current time: %v\n", time.Now()) - c.Printf("ETA: %v\n", t.eta()) + c.Printf("%v\n", t.remaining()) }, }, } return t } +var enterTravelTemplate = template.Must(template.New("enter-travel").Parse(` +Departing: {{.Departing}} +Destination: {{.Destination}} +Total Trip Time: {{.Duration}} +Current Time: {{.Time}} +ETA: {{.ETA}} +`)) + func (t *TravelState) Enter(c *Connection) { - c.Printf("Leaving %v, bound for %v.\n", t.start, t.dest) - c.Printf("Trip duration: %v\n", t.tripTime()) - c.Printf("Current time: %v\n", time.Now()) - c.Printf("ETA: %v\n", t.eta()) + enterTravelTemplate.Execute(c, struct { + Departing *System + Destination *System + Duration time.Duration + Time time.Time + ETA time.Time + }{ + t.start, + t.dest, + t.tripTime(), + time.Now(), + t.eta(), + }) t.start.Leave(c) } @@ -68,7 +84,15 @@ func (t *TravelState) String() string { } func (t *TravelState) PrintStatus(c *Connection) { - panic("not done") + desc := fmt.Sprintf("Traveling from %v to %v", t.start, t.dest) + statusTemplate.Execute(c, status{ + State: "In Transit", + Balance: c.money, + Bombs: c.bombs, + Kills: c.kills, + Location: fmt.Sprintf("%s -> %s", t.start, t.dest), + Description: desc, + }) } func (t *TravelState) progress(c *Connection, args ...string) {