-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (52 loc) · 1.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"math/rand"
"net"
"os"
"time"
"github.com/PondWader/GoPractice/config"
"github.com/PondWader/GoPractice/protocol"
"github.com/PondWader/GoPractice/server"
"github.com/PondWader/GoPractice/utils"
)
var version string = "0.0.1"
func main() {
rand.Seed(time.Now().UnixNano())
printName()
utils.Info("Loading server config...")
cfg := config.LoadConfig()
utils.Info("Config loaded.")
server := server.New(cfg, version)
utils.Info("Server starting...")
listener, err := net.Listen("tcp", ":"+fmt.Sprint(server.Config.Port))
if err != nil {
utils.Error("Error listening on port "+fmt.Sprint(server.Config.Port)+":", err)
return
}
defer listener.Close()
utils.Info("Server listening on port", server.Config.Port)
for {
conn, err := listener.Accept()
if err != nil {
utils.Error("Error accepting connection:", err)
os.Exit(1)
}
utils.Info("Incoming connection from", conn.RemoteAddr())
go handleClient(conn, server)
}
}
func handleClient(conn net.Conn, s *server.Server) {
client := protocol.NewClient(conn, s)
if client.Ended == false {
server.NewPlayer(client, s)
}
}
func printName() {
fmt.Println(utils.Cyan(` ____ ____ _ _
/ ___| ___ | _ \ _ __ __ _ ___| |_(_) ___ ___
| | _ / _ \| |_) | '__/ _` + "`" + ` |/ __| __| |/ __/ _ \
| |_| | (_) | __/| | | (_| | (__| |_| | (_| __/
\____|\___/|_| |_| \__,_|\___|\__|_|\___\___|
`))
}