-
Notifications
You must be signed in to change notification settings - Fork 45
/
main.go
98 lines (78 loc) · 2.98 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
//go:generate go run backend/code_generation/generate_event_dispatcher.go
import (
"log"
"math/rand"
"net/http"
"time"
)
import (
_ "net/http/pprof"
"os"
"superstellar/backend/ai"
"superstellar/backend/communication"
"superstellar/backend/events"
"superstellar/backend/game"
"superstellar/backend/monitor"
"superstellar/backend/persistence"
"superstellar/backend/simulation"
"superstellar/backend/state"
"superstellar/backend/utils"
)
func main() {
log.SetFlags(log.Lshortfile)
debug := false
if len(os.Args) == 2 && os.Args[1] == "-d" {
debug = true
}
rand.Seed(time.Now().UTC().UnixNano())
eventDispatcher := events.NewEventDispatcher()
physicsTicker := game.NewPhysicsTicker(eventDispatcher)
userNameRegistry := utils.NewUserNameRegistry()
idManager := utils.NewIdManager()
monitor := monitor.NewMonitor(eventDispatcher)
space := state.NewSpace()
updater := simulation.NewUpdater(space, monitor, eventDispatcher, idManager)
eventDispatcher.RegisterUserInputListener(updater)
eventDispatcher.RegisterTimeTickListener(updater)
eventDispatcher.RegisterUserJoinedListener(updater)
eventDispatcher.RegisterUserLeftListener(updater)
eventDispatcher.RegisterObjectDestroyedListener(updater)
eventDispatcher.RegisterTargetAngleListener(updater)
server := communication.NewServer("/superstellar", monitor, eventDispatcher, idManager, userNameRegistry)
eventDispatcher.RegisterUserLeftListener(server)
sender := communication.NewSender(server, space, userNameRegistry)
eventDispatcher.RegisterPhysicsReadyListener(sender)
eventDispatcher.RegisterProjectileFiredListener(sender)
eventDispatcher.RegisterProjectileHitListener(sender)
eventDispatcher.RegisterUserLeftListener(sender)
eventDispatcher.RegisterUserConnectedListener(sender)
eventDispatcher.RegisterUserJoinedListener(sender)
eventDispatcher.RegisterObjectDestroyedListener(sender)
if _, found := os.LookupEnv("DYNAMODB_ENDPOINT"); found {
adapter := persistence.NewDynamoDbWriter()
scoreBoardSerializer := persistence.NewScoreBoardSerializer(userNameRegistry, adapter, idManager, eventDispatcher)
eventDispatcher.RegisterObjectDestroyedListener(scoreBoardSerializer)
scoreBoardReader := persistence.NewScoreBoardReader(adapter)
scoreBoardSender := communication.NewScoreBoardSender(server, scoreBoardReader, userNameRegistry)
eventDispatcher.RegisterUserJoinedListener(scoreBoardSender)
eventDispatcher.RegisterScoreSentListener(scoreBoardSender)
}
botManager := ai.NewBotManager(eventDispatcher, space, idManager, userNameRegistry)
eventDispatcher.RegisterTimeTickListener(botManager)
eventDispatcher.RegisterObjectDestroyedListener(botManager)
botManager.CreateBots(5)
if debug {
fileWriter, err := communication.NewFileWriter(space)
if err != nil {
panic(err)
}
eventDispatcher.RegisterTimeTickListener(fileWriter)
go fileWriter.Run()
}
monitor.Run()
go server.Listen()
go eventDispatcher.RunEventLoop()
go physicsTicker.Run()
log.Fatal(http.ListenAndServe(":8080", nil))
}