forked from HnH-Games/hero-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (169 loc) · 3.78 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
A big thanks to DRAGON-LEGEND the biggest inspiration for all off us!
*/
package main
import (
"fmt"
"log"
"math/rand"
"net"
"os"
"sort"
"strconv"
"time"
"hero-emulator/ai"
"hero-emulator/api"
"hero-emulator/config"
"hero-emulator/database"
_ "hero-emulator/factory"
"hero-emulator/logging"
"hero-emulator/nats"
"hero-emulator/redis"
"github.com/robfig/cron"
"github.com/thoas/go-funk"
)
var (
logger = logging.Logger
)
func initDatabase() {
for {
err := database.InitDB()
if err == nil {
log.Printf("Connected to database...")
return
}
log.Printf("Database connection error: %+v, waiting 30 sec...", err)
time.Sleep(time.Duration(30) * time.Second)
}
}
func initRedis() {
for {
err := redis.InitRedis()
if err != nil {
log.Printf("Redis connection error: %+v, waiting 30 sec...", err)
time.Sleep(time.Duration(30) * time.Second)
continue
}
if redisHost := os.Getenv("REDIS_HOST"); redisHost != "" {
log.Printf("Connected to redis...")
go logger.StartLogging()
}
return
}
}
func startServer() {
cfg := config.Default
port := cfg.Server.Port
listen, err := net.Listen("tcp4", ":"+strconv.Itoa(port))
// defer listen.Close() // Uncoment this one if anything goes wrong!
if err != nil {
defer listen.Close() // If anything goes wrong delete this line and uncoment the one on the top.
log.Fatalf("Socket listen port %d failed,%s", port, err)
os.Exit(1)
}
log.Printf("Begin listen on port: %d", port)
StartLogging()
//connections = make(map[string]net.Conn)
//remoteAddrs = make(map[string]int)
// dungeon.ExploreTheWorld()
for {
conn, err := listen.Accept()
if err != nil {
log.Fatalln(err)
continue
}
ws := database.Socket{Conn: conn}
//ws.SetPingDuration(time.Second * 2)
//ws.SetPingHandler(nil)
go ws.Read()
}
}
func cronHandler() {
c := cron.New()
c.AddFunc("0 0 0 * * *", func() {
database.RefreshAIDs()
})
c.Start()
database.RefreshAIDs()
}
func main() {
initRedis()
initDatabase()
cronHandler()
ai.Init()
go database.UnbanUsers()
s := nats.RunServer(nil)
defer s.Shutdown()
c, err := nats.ConnectSelf(nil)
defer c.Close()
if err != nil {
log.Fatalln(err)
}
go api.InitGRPC()
startServer()
}
func resolveOverlappingItems() { //67-306
ids := []string{}
for _, userid := range ids {
fmt.Println("user id:", userid)
bankSlots, _ := database.FindBankSlotsByUserID(userid)
freeSlots := make(map[int16]struct{})
for _, s := range bankSlots {
freeSlots[s.SlotID] = struct{}{}
}
findSlot := func() int16 {
for i := int16(67); i <= 306; i++ {
if _, ok := freeSlots[i]; !ok {
return i
}
}
return -1
}
for i := 0; i < len(bankSlots)-1; i++ {
for j := i; true; j++ {
if len(bankSlots) == j+1 || bankSlots[i].SlotID != bankSlots[j+1].SlotID {
break
}
free := findSlot()
if free == -1 {
continue
}
fmt.Printf("%d => %d\n", bankSlots[j+1].SlotID, free)
freeSlots[free] = struct{}{}
bankSlots[j+1].SlotID = free
bankSlots[j+1].Update()
}
}
}
}
type IntRange struct {
min, max int
}
// get next random value within the interval including min and max
func (ir *IntRange) NextRandom(r *rand.Rand) int {
return r.Intn(ir.max-ir.min+1) + ir.min
}
func StartLogging() {
fi, err := os.OpenFile("Log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) //log file
if err != nil {
log.Fatalf("error opening file: %v", err)
}
log.SetOutput(fi)
}
func createServerMobs(server int) {
aiSet := funk.Filter(funk.Values(database.AIs), func(ai *database.AI) bool {
return ai.Server == 1
}).([]*database.AI)
sort.Slice(aiSet, func(i, j int) bool {
return aiSet[i].ID < aiSet[j].ID
})
for _, ai := range aiSet {
newAI := *ai
newAI.ID = 0
newAI.Server = server
err := newAI.Create()
if err != nil {
log.Print(err)
}
}
}