This commit is contained in:
tkarrass 2016-02-06 01:48:29 +01:00
parent 65334c1779
commit bba1177326
6 changed files with 48 additions and 23 deletions

View File

@ -13,15 +13,15 @@ import (
) )
var cmdClient = &cobra.Command{ var cmdClient = &cobra.Command{
Use: "client <IP> <NUMBER>", Use: "client <NUMBER>",
Short: "Runs Gosl as a client", Short: "Runs Gosl as a client",
Long: "Runs Gosl as a client", Long: "Runs Gosl as a client",
Run: runClient, //Run: runClient,
} }
//func init() { var (
// cmdClient.Run = runClient ServerHost string
//} )
func register(con net.Conn, id int) error { func register(con net.Conn, id int) error {
w, h := data.TestNC() w, h := data.TestNC()
@ -33,17 +33,24 @@ func register(con net.Conn, id int) error {
func runClient(cmd *cobra.Command, args []string) { func runClient(cmd *cobra.Command, args []string) {
fmt.Println("running client ...") fmt.Println("running client ...")
if len(args) < 2 { if len(args) < 1 {
fmt.Println("Params: <IP> <NUMBER>") fmt.Println("Params: <NUMBER>")
os.Exit(1) os.Exit(1)
} }
server := args[0] id, _ := strconv.Atoi(args[0])
id, _ := strconv.Atoi(args[1])
con, err := net.Dial("tcp", server+":"+strconv.Itoa(SERVERPORT)) con, err := net.Dial("tcp", ServerHost+":"+strconv.Itoa(ServerPort))
if err != nil { if err != nil {
fmt.Println("connect error:", err) fmt.Println("connect error:", err)
} }
register(con, id) register(con, id)
} }
func init() {
CmdGosl.AddCommand(cmdClient)
cmdClient.Run = runClient
cmdClient.Flags().StringVarP(&ServerHost, "host", "H", "127.0.0.1", "Connect to this server")
cmdClient.Flags().IntVarP(&ServerPort, "port", "p", 8090, "Connect to server on this port")
}

View File

@ -6,11 +6,9 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const SERVERPORT int = 8989
type handshake struct{ ID, W, H int } type handshake struct{ ID, W, H int }
var cmdGosl = &cobra.Command{ var CmdGosl = &cobra.Command{
Use: "gosl [command]", Use: "gosl [command]",
Short: "Run a gosl instance", Short: "Run a gosl instance",
Long: `Run a gosl server or client. Long: `Run a gosl server or client.
@ -20,9 +18,7 @@ For further instructions run 'gosl help [command]'
} }
func Execute() { func Execute() {
cmdGosl.AddCommand(cmdServer) if err := CmdGosl.Execute(); err != nil {
cmdGosl.AddCommand(cmdClient)
if err := cmdGosl.Execute(); err != nil {
os.Exit(-1) os.Exit(-1)
} }
} }

View File

@ -2,7 +2,6 @@ package cmd // code.bitsetter.de/fun/gosl/cmd
import ( import (
"encoding/gob" "encoding/gob"
"fmt"
"log" "log"
"net" "net"
"sort" "sort"
@ -29,6 +28,10 @@ type goslClient struct {
h int h int
} }
var (
LevelFile string
ServerPort int
)
var TotalWidth int = 0 var TotalWidth int = 0
var clients map[int]goslClient = make(map[int]goslClient) var clients map[int]goslClient = make(map[int]goslClient)
var clientKeys []int = make([]int, 100) var clientKeys []int = make([]int, 100)
@ -52,7 +55,7 @@ func serveClients() {
for _, k := range clientKeys { for _, k := range clientKeys {
id, client := k, clients[k] id, client := k, clients[k]
if id > 0 { if id > 0 {
fmt.Println("ID:", id, "Client:", client) log.Println("ID:", id, "Client:", client)
} }
} }
time.Sleep(time.Second) time.Sleep(time.Second)
@ -60,8 +63,8 @@ func serveClients() {
} }
func runServer(cmd *cobra.Command, args []string) { func runServer(cmd *cobra.Command, args []string) {
fmt.Println("running server ...") log.Println("running server on port", ServerPort)
listener, err := net.ListenTCP("tcp", &net.TCPAddr{Port: SERVERPORT}) listener, err := net.ListenTCP("tcp", &net.TCPAddr{Port: ServerPort})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
panic("Could not open Listener") panic("Could not open Listener")
@ -79,5 +82,9 @@ func runServer(cmd *cobra.Command, args []string) {
} }
func init() { func init() {
CmdGosl.AddCommand(cmdServer)
cmdServer.Run = runServer cmdServer.Run = runServer
cmdServer.Flags().StringVarP(&LevelFile, "level", "l", "level.lvl", "Use specific levelfile")
cmdServer.Flags().IntVarP(&ServerPort, "port", "p", 8090, "Run server on this port")
} }

14
data/level.go Normal file
View File

@ -0,0 +1,14 @@
package data // code.bitsetter.de/fun/gosl/data
type Layer struct {
data []byte
w, h int
}
type Level struct {
Layers map[int]*Layer
}
func (lvl *Level) AddLayer(z int, l *Layer) {
lvl.Layers[z] = l
}

View File

@ -5,7 +5,7 @@ import (
"log" "log"
"os" "os"
"github.com/rthornton128/goncurses" nc "github.com/rthornton128/goncurses"
) )
type Screen struct { type Screen struct {
@ -14,14 +14,15 @@ type Screen struct {
} }
func TestNC() (int, int) { func TestNC() (int, int) {
win, err := goncurses.Init() win, err := nc.Init()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
os.Exit(1) os.Exit(1)
} }
win.Clear() win.Clear()
h, w := win.MaxYX() h, w := win.MaxYX()
goncurses.End() nc.End()
fmt.Printf("screen: %d x %d\n", w, h) fmt.Printf("screen: %d x %d\n", w, h)
return w, h return w, h
} }

0
memo Normal file
View File