get rid of all that fmt.Fprintf everywhere

slack
Jordan Orelli 10 years ago
parent c5db5ce93d
commit 5b4fff43e3

@ -21,9 +21,9 @@ var infoCommand = &Command{
name: "info",
help: "gives you some info about your current position",
handler: func(conn *Connection, args ...string) {
fmt.Fprintf(conn, "current planet: %s\n", conn.System().name)
fmt.Fprintf(conn, "bombs: %d\n", conn.bombs)
fmt.Fprintf(conn, "money: %d space duckets\n", conn.money)
conn.Printf("current planet: %s\n", conn.System().name)
conn.Printf("bombs: %d\n", conn.bombs)
conn.Printf("money: %d space duckets\n", conn.money)
},
}
@ -37,14 +37,14 @@ var nearbyCommand = &Command{
log_error("unable to get neighbors: %v", err)
return
}
fmt.Fprintf(conn, "--------------------------------------------------------------------------------\n")
fmt.Fprintf(conn, "%-4s %-20s %s\n", "id", "name", "distance")
fmt.Fprintf(conn, "--------------------------------------------------------------------------------\n")
conn.Printf("--------------------------------------------------------------------------------\n")
conn.Printf("%-4s %-20s %s\n", "id", "name", "distance")
conn.Printf("--------------------------------------------------------------------------------\n")
for _, neighbor := range neighbors {
other := index[neighbor.id]
fmt.Fprintf(conn, "%-4d %-20s %v\n", other.id, other.name, neighbor.distance)
conn.Printf("%-4d %-20s %v\n", other.id, other.name, neighbor.distance)
}
fmt.Fprintf(conn, "--------------------------------------------------------------------------------\n")
conn.Printf("--------------------------------------------------------------------------------\n")
},
}
@ -85,10 +85,10 @@ systems. Star systems that are farther away take longer to communicate with.
for _, cmdName := range args {
cmd, ok := commandRegistry[cmdName]
if !ok {
fmt.Fprintf(conn, "no such command: %v\n", cmdName)
conn.Printf("no such command: %v\n", cmdName)
continue
}
fmt.Fprintf(conn, "%v: %v\n", cmdName, cmd.help)
conn.Printf("%v: %v\n", cmdName, cmd.help)
}
},
}
@ -105,7 +105,7 @@ var commandsCommand = &Command{
fmt.Fprintln(conn, "--------------------------------------------------------------------------------")
for _, name := range names {
cmd := commandRegistry[name]
fmt.Fprintf(conn, "%-16s %s\n", name, cmd.help)
conn.Printf("%-16s %s\n", name, cmd.help)
}
fmt.Fprintln(conn, "--------------------------------------------------------------------------------")
},
@ -116,7 +116,7 @@ var scanCommand = &Command{
help: "super duper scan",
handler: func(conn *Connection, args ...string) {
if !conn.CanScan() {
fmt.Fprintf(conn, "scanners are still recharging. Can scan again in %v\n", conn.NextScan())
conn.Printf("scanners are still recharging. Can scan again in %v\n", conn.NextScan())
return
}
currentGame.Register(NewScan(conn.System()))
@ -149,13 +149,13 @@ var gotoCommand = &Command{
id_n, err := strconv.Atoi(dest_name)
if err != nil {
fmt.Fprintf(conn, `hmm, I don't know a system by the name "%s", try something else`, dest_name)
conn.Printf(`hmm, I don't know a system by the name "%s", try something else`, dest_name)
return
}
to, ok = index[id_n]
if !ok {
fmt.Fprintf(conn, `oh dear, there doesn't seem to be a system with id %d`, id_n)
conn.Printf(`oh dear, there doesn't seem to be a system with id %d`, id_n)
return
}
conn.TravelTo(to)
@ -178,12 +178,12 @@ var colonizeCommand = &Command{
if conn.money > 2000 {
conn.Withdraw(2000)
if system.colonizedBy != nil {
fmt.Fprintf(system.colonizedBy, "your colony on %s has been stolen by %s\n", system.Label(), conn.PlayerName())
system.colonizedBy.Printf("your colony on %s has been stolen by %s\n", system.Label(), conn.PlayerName())
}
system.colonizedBy = conn
fmt.Fprintf(conn, "set up a mining colony on %s\n", conn.System().name)
conn.Printf("set up a mining colony on %s\n", conn.System().name)
} else {
fmt.Fprintf(conn, "not enough money! it costs 2000 duckets to start a mining colony\n")
conn.Printf("not enough money! it costs 2000 duckets to start a mining colony\n")
}
},
}
@ -210,13 +210,13 @@ var bombCommand = &Command{
id_n, err := strconv.Atoi(dest_name)
if err != nil {
fmt.Fprintf(conn, `hmm, I don't know a system by the name "%s", try something else\n`, dest_name)
conn.Printf(`hmm, I don't know a system by the name "%s", try something else\n`, dest_name)
return
}
to, ok = index[id_n]
if !ok {
fmt.Fprintf(conn, `oh dear, there doesn't seem to be a system with id %d\n`, id_n)
conn.Printf(`oh dear, there doesn't seem to be a system with id %d\n`, id_n)
return
}
conn.SendBomb(to)
@ -228,14 +228,14 @@ var mkBombCommand = &Command{
help: "make a bomb. Costs 500 space duckets",
handler: func(conn *Connection, args ...string) {
if conn.money < 500 {
fmt.Fprintf(conn, "not enough money! Bombs cost 500 space duckets to build, you only have %d in the bank.\n", conn.money)
conn.Printf("not enough money! Bombs cost 500 space duckets to build, you only have %d in the bank.\n", conn.money)
return
}
conn.Withdraw(500)
conn.bombs += 1
fmt.Fprintf(conn, "built a bomb!\n")
fmt.Fprintf(conn, "bombs: %d\n", conn.bombs)
fmt.Fprintf(conn, "money: %d space duckets\n", conn.money)
conn.Printf("built a bomb!\n")
conn.Printf("bombs: %d\n", conn.bombs)
conn.Printf("money: %d space duckets\n", conn.money)
},
}
@ -244,7 +244,7 @@ var playersCommand = &Command{
help: "lists the connected players",
handler: func(conn *Connection, args ...string) {
for other, _ := range currentGame.connections {
fmt.Fprintf(conn, "%v\n", other.PlayerName())
conn.Printf("%v\n", other.PlayerName())
}
},
}
@ -265,17 +265,17 @@ func isCommand(name string) bool {
func runCommand(conn *Connection, name string, args ...string) {
cmd, ok := commandRegistry[name]
if !ok {
fmt.Fprintf(conn, "no such command: %s\n", name)
conn.Printf("no such command: %s\n", name)
return
}
if conn.dead {
fmt.Fprintf(conn, "you're dead.\n")
conn.Printf("you're dead.\n")
return
}
if conn.InTransit() && !cmd.mobile {
fmt.Fprintf(conn, "command %s can not be used while in transit\n", name)
conn.Printf("command %s can not be used while in transit\n", name)
return
}

@ -47,7 +47,7 @@ func NewConnection(conn net.Conn) *Connection {
func (c *Connection) Login() {
for {
fmt.Fprintf(c, "what is your name, adventurer?\n")
c.Printf("what is your name, adventurer?\n")
name, err := c.ReadString('\n')
if err == nil {
name = strings.TrimSpace(name)
@ -56,7 +56,7 @@ func (c *Connection) Login() {
return
}
if !ValidName(name) {
fmt.Fprintf(c, "that name is illegal.\n")
c.Printf("that name is illegal.\n")
continue
}
log_info("player connected: %v", name)
@ -67,12 +67,12 @@ func (c *Connection) Login() {
if err := player.Create(); err != nil {
log_error("unable to create player record: %v", err)
}
fmt.Fprintf(c, "you look new around these parts, %s.\n", player.name)
fmt.Fprintf(c, `if you'd like a description of how to play, type the "help" command\n`)
c.Printf("you look new around these parts, %s.\n", player.name)
c.Printf(`if you'd like a description of how to play, type the "help" command\n`)
c.player = player
} else {
c.player = player
fmt.Fprintf(c, "welcome back, %s.\n", player.name)
c.Printf("welcome back, %s.\n", player.name)
}
break
}
@ -100,7 +100,7 @@ func (c *Connection) Tick(frame int64) {
break
}
if sys.money <= 0 {
fmt.Fprintf(c, "system %s is all out of space duckets.\n", sys.Label())
c.Printf("system %s is all out of space duckets.\n", sys.Label())
c.StopMining()
} else {
c.Deposit(1)
@ -115,7 +115,7 @@ func (c *Connection) TravelTo(dest *System) {
dist := c.System().DistanceTo(dest)
c.travelRemaining = int64(dist / (options.lightSpeed * options.playerSpeed))
t := time.Duration(c.travelRemaining) * (time.Second / time.Duration(options.frameRate))
fmt.Fprintf(c, "traveling to: %s. ETA: %v\n", dest.Label(), t)
c.Printf("traveling to: %s. ETA: %v\n", dest.Label(), t)
c.location.Leave(c)
c.location = nil
c.dest = dest
@ -135,7 +135,7 @@ func (c *Connection) SendBomb(target *System) {
c.lastBomb = time.Now()
bomb := NewBomb(c, target)
currentGame.Register(bomb)
fmt.Fprintf(c, "sending bomb to system %v\n", target.Label())
c.Printf("sending bomb to system %v\n", target.Label())
}
func (c *Connection) ReadLines(out chan []string) {
@ -160,8 +160,12 @@ func (c *Connection) ReadLines(out chan []string) {
}
}
func (c *Connection) Printf(template string, args ...interface{}) (int, error) {
return fmt.Fprintf(c, template, args...)
}
func (c *Connection) land() {
fmt.Fprintf(c, "you have arrived at %v\n", c.dest.Label())
c.Printf("you have arrived at %v\n", c.dest.Label())
c.location = c.dest
c.location.Arrive(c)
c.dest = nil
@ -237,16 +241,16 @@ func (c *Connection) MadeKill(victim *Connection) {
func (c *Connection) Mine() {
switch c.state {
case idle:
fmt.Fprintf(c, "now mining %s. %v space duckets remaining.\n", c.System().name, c.System().money)
c.Printf("now mining %s. %v space duckets remaining.\n", c.System().name, c.System().money)
fmt.Fprintln(c, "(press enter to stop mining)")
c.state = mining
default:
fmt.Fprintf(c, "no\n")
c.Printf("no\n")
}
}
func (c *Connection) StopMining() {
fmt.Fprintf(c, "done mining\n")
c.Printf("done mining\n")
c.state = idle
}
@ -270,11 +274,11 @@ func (c *Connection) Win(method string) {
}
func (c *Connection) Die() {
fmt.Fprintf(c, "you were bombed. You will respawn in 1 minutes.\n")
c.Printf("you were bombed. You will respawn in 1 minutes.\n")
c.dead = true
c.System().Leave(c)
time.AfterFunc(30*time.Second, func() {
fmt.Fprintf(c, "respawn in 30 seconds.\n")
c.Printf("respawn in 30 seconds.\n")
})
time.AfterFunc(time.Minute, c.Respawn)
}

@ -95,7 +95,7 @@ func (g *Game) Win(winner *Connection, method string) {
log_info("player %s has won by %s victory", winner.PlayerName(), method)
for conn, _ := range g.connections {
fmt.Fprintf(conn, "player %s has won by %s victory.\n", winner.PlayerName(), method)
conn.Printf("player %s has won by %s victory.\n", winner.PlayerName(), method)
}
}

@ -64,7 +64,7 @@ func handleConnection(conn *Connection) {
case "quit":
return
default:
fmt.Fprintf(conn, "hmm I'm not sure I know that one.\n")
conn.Printf("hmm I'm not sure I know that one.\n")
}
}

@ -47,9 +47,9 @@ func (s *System) Arrive(conn *Connection) {
}
s.players[conn] = true
if s.planets == 1 {
fmt.Fprintf(conn, "you are in the system %s. There is %d planet here.\n", s.Label(), s.planets)
conn.Printf("you are in the system %s. There is %d planet here.\n", s.Label(), s.planets)
} else {
fmt.Fprintf(conn, "you are in the system %s. There are %d planets here.\n", s.Label(), s.planets)
conn.Printf("you are in the system %s. There are %d planets here.\n", s.Label(), s.planets)
}
}
@ -60,7 +60,7 @@ func (s *System) Leave(p *Connection) {
func (s *System) NotifyInhabitants(template string, args ...interface{}) {
s.EachConn(func(conn *Connection) {
fmt.Fprintf(conn, template, args...)
conn.Printf(template, args...)
})
}
@ -150,7 +150,7 @@ func (s *System) Bombed(bomber *Connection) {
bomber.MadeKill(conn)
})
if s.colonizedBy != nil {
fmt.Fprintf(s.colonizedBy, "your mining colony on %s has been destroyed!\n", s.name)
s.colonizedBy.Printf("your mining colony on %s has been destroyed!\n", s.name)
s.colonizedBy = nil
}
@ -170,7 +170,7 @@ func bombNotice(to_id, from_id int) {
to := index[to_id]
from := index[from_id]
to.EachConn(func(conn *Connection) {
fmt.Fprintf(conn, "a bombing has been observed on %s\n", from.name)
conn.Printf("a bombing has been observed on %s\n", from.name)
})
}

Loading…
Cancel
Save