Browse Source

Adds Client to Server, and .gitignore

Also includes a main so it's actually runnable

We also are documenting what will/may be found in the data/ directory (games, etc)
david 7 months ago
parent
commit
35ea38024e
5 changed files with 149 additions and 0 deletions
  1. 17 0
      .data_directory.txt
  2. 4 0
      .gitignore
  3. 86 0
      client.go
  4. 14 0
      main.go
  5. 28 0
      server.go

+ 17 - 0
.data_directory.txt

@@ -0,0 +1,17 @@
+# This file is to show what files will/may be found within the data/ directory
+
+F config.toml (Where server settings will live)
+F motd.txt (A text file to be sent out on login)
+D data/
+D data/logs
+F data/logs/XX-XX-XXXX.log (Where X will be the date in MM-DD-YYYY format)
+D data/games
+D data/games/gameX (Where X is game number)
+F data/games/gameX/config.toml (Where game specific settings will live)
+F data/games/gameX/map.json (Mapping file, Lists ONLY sector and it's warps, no other data)
+F data/games/gameX/ships.json (Ship Models, so you could have a Death Star in one game, and a Battlestar in another, etc)
+D data/games/gameX/data (Where player data, sector data, ports and planets live)
+F data/games/gameX/data/players.db3 (sqlite database: id, name, password, sector, ship_id, corp_id)
+F data/games/gameX/data/corps.json (json file: corp_id, corp_name, corp_leader_id, corp_password, corp_motd)
+F data/games/gameX/data/ships.db3 (sqlite database: id, name, model_id, owner_id, corp_owned, figs, shields, holds, cargo_contents)
+F data/games/gameX/data/sectors.db3 (sqlite database)

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+tradetrek.exe
+tradetrek
+data/
+config.toml

+ 86 - 0
client.go

@@ -0,0 +1,86 @@
+package main
+
+import (
+	"bytes"
+	"errors"
+	"fmt"
+	"net"
+	"os"
+	"time"
+)
+
+type Client struct {
+	server      *Server
+	conn        net.Conn
+	Ip          string
+	Line        string
+	Prompt      string
+	PromptWLine bool
+}
+
+func NewClient(server *Server, conn net.Conn) *Client {
+	ip := conn.RemoteAddr().String()
+	c := &Client{
+		server:      server,
+		conn:        conn,
+		Ip:          ip,
+		Line:        "",
+		Prompt:      "Login: ",
+		PromptWLine: true,
+	}
+	go c.reader()
+	return c
+}
+
+func (c *Client) Write(text string) {
+	c.conn.Write([]byte(text))
+}
+
+var clearLine = bytes.Repeat([]byte("\b \b"), 500)
+
+func (c *Client) InsertWrite(text string) {
+	c.conn.Write(clearLine)
+	c.Write(text)
+	if len(c.Prompt) != 0 {
+		c.Write(c.Prompt)
+		if c.PromptWLine {
+			c.Write(c.Line)
+		}
+	}
+}
+
+func (c *Client) reader() {
+	var (
+		buf  []byte = make([]byte, 1024) // 1kb
+		read int
+		err  error
+	)
+	for {
+		c.conn.SetReadDeadline(time.Now().Add(time.Duration(200) * time.Millisecond))
+		read, err = c.conn.Read(buf)
+		if err != nil {
+			if errors.Is(err, net.ErrClosed) || errors.Is(err, os.ErrClosed) {
+				return
+			} else if errors.Is(err, os.ErrDeadlineExceeded) {
+				if read != 0 {
+					c.process(string(buf[0:read]))
+				}
+				continue
+			} else {
+				fmt.Printf("%s ERR> %v", c.Ip, err)
+				continue
+			}
+		}
+		if read != 0 {
+			c.process(string(buf[0:read]))
+		}
+	}
+}
+
+func (c *Client) process(input string) {
+	// Process the input as far as ENTER/RETURN and keys being pressed
+	// This will require a state machine to track where the client is (Login, Password, Sitting in their ship, Accessing computer, etc)
+
+	// For now we'll just echo it back
+	c.Write(input)
+}

+ 14 - 0
main.go

@@ -0,0 +1,14 @@
+package main
+
+import (
+	"fmt"
+)
+
+func main() {
+	s := &Server{Conns: []*Client{}, Port: 9009}
+	err := s.Run()
+	if err != nil {
+		fmt.Println("Err:", err)
+		return
+	}
+}

+ 28 - 0
server.go

@@ -1 +1,29 @@
 package main
+
+import (
+	"fmt"
+	"net"
+)
+
+type Server struct {
+	Conns []*Client
+	Port  uint32
+}
+
+func (s *Server) Run() error {
+	listener, err := net.Listen("tcp", fmt.Sprintf(":%04d", s.Port))
+	if err != nil {
+		return err
+	}
+	for {
+		conn, err := listener.Accept()
+		if err != nil {
+			return err
+		}
+		c := NewClient(s, conn)
+		s.Conns = append(s.Conns, c)
+		c.Write("Login: ")
+		c.InsertWrite("Trade Trek\r\n")
+		c.InsertWrite("v1.0  Apollo@21:1/236\r\n\r\n")
+	}
+}