-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
108 lines (88 loc) · 2.69 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
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"log"
"time"
"github.com/soupstoregames/coda-mud/config"
"github.com/soupstoregames/coda-mud/servers/telnet"
"github.com/soupstoregames/coda-mud/services"
"github.com/soupstoregames/coda-mud/simulation"
"github.com/soupstoregames/coda-mud/simulation/data/state"
"github.com/soupstoregames/coda-mud/simulation/data/static"
"github.com/soupstoregames/go-core/logging"
)
func main() {
var (
conf *config.Config
staticData *static.DataWatcher
stateData *state.FileSystem
sim *simulation.Simulation
usersManager *services.UsersManager
err error
)
logging.Info("Starting")
// load the configuration from environmental variables
if conf, err = config.Load(); err != nil {
logging.Fatal(err.Error())
}
// create the simulation
sim = simulation.NewSimulation()
// create the static data loader
staticData = static.NewDataWatcher(conf.DataPath, sim)
logging.SubscribeToErrorChan(staticData.Errors)
// create a persister to save the simulation state
if stateData, err = state.NewFileSystem(conf); err != nil {
logging.Fatal(err.Error())
}
// create the users service for managing login details
usersManager = services.NewUsersManager()
// load the static data
if err := staticData.InitialLoad(); err != nil {
logging.Fatal(err.Error())
}
// start watching for changes to the static data folder
staticData.Watch()
// load the saved state
loadState(stateData, usersManager, sim)
// set the @spawn room
sim.SetSpawnRoom("@spawn", 1)
// set up save timing for simulation state
startSaveSimulationTicker(usersManager, sim, stateData)
// start the simulation
sim.Start()
// start the telnet server
telnetServer := telnet.NewServer(conf, sim, usersManager)
if err = telnetServer.ListenAndServe(); err != nil {
log.Fatal(err.Error())
}
}
func loadState(loader state.Loader, usersManager *services.UsersManager, sim simulation.StateController) {
var (
users []state.User
characters []state.Character
worlds []state.World
err error
)
if users, characters, worlds, err = loader.Load(); err != nil {
logging.Fatal(err.Error())
}
if usersManager.Load(users); err != nil {
logging.Fatal(err.Error())
}
if sim.Load(characters, worlds); err != nil {
logging.Fatal(err.Error())
}
}
func startSaveSimulationTicker(u *services.UsersManager, s *simulation.Simulation, p state.Persister) {
t := time.NewTicker(time.Minute)
go func() {
for range t.C {
if err := u.Save(p); err != nil {
logging.Warn(fmt.Sprintf("Failed to save users: %s", err.Error()))
}
if err := s.Save(p); err != nil {
logging.Warn(fmt.Sprintf("Failed to save simulation state: %s", err.Error()))
}
}
}()
}