pull/5/head
Jordan Orelli 10 years ago
parent 45cb593785
commit 7ade356836

@ -1,6 +1,3 @@
package main package main
import ( import ()
)

@ -1,12 +1,14 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"io"
"math/rand"
"net" "net"
"os" "os"
"regexp"
"strings" "strings"
"sync"
"time"
) )
var dataPath = "/projects/exo/expl.speck" var dataPath = "/projects/exo/expl.speck"
@ -30,30 +32,58 @@ func bail(status int, template string, args ...interface{}) {
os.Exit(status) os.Exit(status)
} }
func handleConnection(conn net.Conn) { func handleConnection(conn *Connection) {
namePattern := regexp.MustCompile(`^[[:alpha:]][[:alnum:]-_]{0,19}$`) var mu sync.Mutex
r := bufio.NewReader(conn)
fmt.Fprintf(conn, "what is your name, adventurer?\n") defer conn.Close()
name, err := r.ReadString('\n') conn.Login()
if err == nil {
name = strings.TrimSpace(name)
log_info("player connected: %v", name)
} else {
log_error("player failed to connect: %v", err)
}
if !namePattern.MatchString(name) {
fmt.Fprintf(conn, "that name is illegal.\n")
}
planet, err := randomPlanet() planet, err := randomPlanet()
if err != nil { if err != nil {
log_error("player %s failed to get random planet: %v", name, err) log_error("player %s failed to get random planet: %v", conn.PlayerName(), err)
return return
} }
fmt.Fprintf(conn, "you are on the planet %s\n", planet.name) fmt.Fprintf(conn, "you are on the planet %s\n", planet.name)
for {
line, err := conn.ReadString('\n')
switch err {
case io.EOF:
return
case nil:
break
default:
log_error("failed to read line from player %s: %v", conn.PlayerName(), err)
}
line = strings.TrimSpace(line)
parts := strings.Split(line, " ")
switch parts[0] {
case "scan":
for _, otherPlanet := range planetIndex {
if otherPlanet.name == planet.name {
continue
}
go func(p exoSystem) {
dist := planetDistance(*planet, p)
delay := time.Duration(int64(dist * 100000000))
time.Sleep(delay)
mu.Lock()
fmt.Fprintf(conn, "PONG from planet %s (%v)\n", p.name, delay)
mu.Unlock()
}(otherPlanet)
}
case "broadcast":
case "quit":
return
default:
fmt.Fprintf(conn, "hmm I'm not sure I know that one.\n")
}
}
} }
func main() { func main() {
rand.Seed(time.Now().UnixNano())
setupDb() setupDb()
listener, err := net.Listen("tcp", ":9220") listener, err := net.Listen("tcp", ":9220")
if err != nil { if err != nil {
@ -65,6 +95,6 @@ func main() {
log_error("error accepting connection: %v", err) log_error("error accepting connection: %v", err)
continue continue
} }
go handleConnection(conn) go handleConnection(NewConnection(conn))
} }
} }

@ -0,0 +1,19 @@
package main
import (
"regexp"
)
var namePattern = regexp.MustCompile(`^[[:alpha:]][[:alnum:]-_]{0,19}$`)
func ValidName(name string) bool {
return namePattern.MatchString(name)
}
type Player struct {
name string
}
func (p *Player) Load() {
}

@ -0,0 +1,50 @@
package main
import (
"bufio"
"fmt"
"net"
"strings"
)
type Connection struct {
net.Conn
*bufio.Reader
player Player
}
func NewConnection(conn net.Conn) *Connection {
return &Connection{
Conn: conn,
Reader: bufio.NewReader(conn),
}
}
func (c *Connection) Login() {
for {
fmt.Fprintf(c, "what is your name, adventurer?\n")
name, err := c.ReadString('\n')
if err == nil {
name = strings.TrimSpace(name)
} else {
log_error("player failed to connect: %v", err)
return
}
if !ValidName(name) {
fmt.Fprintf(c, "that name is illegal.\n")
continue
}
log_info("player connected: %v", name)
c.player = Player{name: name}
break
}
}
func (c *Connection) Close() error {
log_info("player disconnecting: %s", c.player.name)
return c.Conn.Close()
}
func (c *Connection) PlayerName() string {
return c.player.name
}
Loading…
Cancel
Save