/
main.go
63 lines (52 loc) · 1.28 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
package main
import (
"flag"
"fmt"
"github.com/kir-dev/torpedo/engine"
"github.com/kir-dev/torpedo/util"
"log"
"math/rand"
"net/http"
"os"
"time"
)
var (
currentGame *engine.Game = nil
port = flag.String("port", ":8080", "Port to bind to.")
History []*engine.Game = nil
)
func main() {
fmt.Printf("Starting on port %s...\n", *port)
if util.IsDev() {
fmt.Println("Started in DEVELOPMENT mode.")
}
fmt.Println("Press Ctrl-C to exit!")
fmt.Println()
initialize()
// start main loop for the application
go mainLoop()
http.Handle("/public/", http.FileServer(http.Dir(".")))
log.Fatal(http.ListenAndServe(*port, nil))
}
func initialize() {
// seed with current time.
rand.Seed(time.Now().Unix())
// log to stdout instead of stderr
log.SetOutput(os.Stdout)
// load config
var configPath string
flag.StringVar(&configPath, "config", "", "Path of the config file. If left empty, default config will be loaded.")
flag.Parse()
engine.LoadConfig(configPath)
}
// main loop for the program, starts new game when needed
func mainLoop() {
endGame := make(chan int)
for {
currentGame = engine.NewGame(endGame)
currentGame.Start()
<-endGame
History = append(History, currentGame)
util.LogInfo("Game ended with id: %s.", currentGame.Id)
}
}