/
map.go
235 lines (171 loc) · 4.39 KB
/
map.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package lua
import (
"fmt"
"github.com/raggaer/castro/app/util"
"github.com/yuin/gopher-lua"
"path/filepath"
"time"
)
// SetMapMetaTable sets a map metatable for the given state
func SetMapMetaTable(luaState *lua.LState) {
// Create and set map metatable
mapMetaTable := luaState.NewTypeMetatable(MapMetaTableName)
luaState.SetGlobal(MapMetaTableName, mapMetaTable)
// Set all map metatable functions
luaState.SetFuncs(mapMetaTable, mapMethods)
}
// EncodeMap encodes the server map
func EncodeMap(L *lua.LState) int {
// Encode map
mapData, err := util.EncodeMap(
filepath.Join(util.Config.Configuration.Datapack, "data", "world", Config.GetGlobal("mapName").String()+".otbm"),
)
if err != nil {
L.RaiseError("Cannot encode map file: %v", err)
return 0
}
// Insert map data into database
if _, err := executeQueryHelper(L, "INSERT INTO castro_map (name, data, created_at, updated_at) VALUES (?, ?, ?, ?)", Config.GetGlobal("mapName").String(), mapData, time.Now(), time.Now()); err != nil {
L.RaiseError("Cannot save encoded map data: %v", err)
}
return 0
}
// HouseList returns the server house list as a lua table
func HouseList(L *lua.LState) int {
// Check if user wants specific town
town := uint32(L.ToInt(2))
// Check if list is on the cache
list, found := util.Cache.Get(
fmt.Sprintf("house_list_%v", town),
)
// If list is on the cache return cache result
if found {
L.Push(list.(*lua.LTable))
return 1
}
// Result table
tbl := &lua.LTable{}
// Loop house list
for _, house := range util.ServerHouseList.List.Houses {
// Check if user wants specific town
if town == 0 {
// Convert house to table
h := StructToTable(house)
// Append to final table
tbl.Append(h)
} else if town == house.TownID {
// Convert house to table
h := StructToTable(house)
// Append to final table
tbl.Append(h)
}
}
// Save list to cache
util.Cache.Add(
fmt.Sprintf("house_list_%v", town),
tbl,
util.Config.Configuration.Cache.Default.Duration,
)
// Push table to stack
L.Push(tbl)
return 1
}
// TownList returns the server town list as a lua table
func TownList(L *lua.LState) int {
// Check if the list is on the cache
list, found := util.Cache.Get("town_list")
if found {
// Push town list
L.Push(list.(*lua.LTable))
return 1
}
// Data holder
result := &lua.LTable{}
// Loop town list
for _, town := range util.OTBMap.Map.Towns {
// Convert town to table
tbl := StructToTable(&town)
// Append to main table
result.Append(tbl)
}
// Save list to cache
util.Cache.Add("town_list", result, util.Config.Configuration.Cache.Default.Duration)
// Push result
L.Push(result)
return 1
}
// GetTownByName grabs a town by the given name
func GetTownByName(L *lua.LState) int {
// Get town name
name := L.Get(2)
// Check for valid ID type
if name.Type() != lua.LTString {
L.ArgError(1, "Invalid name format. Expected string")
return 0
}
// Check if town is on cache
t, found := util.Cache.Get(
fmt.Sprintf("town_%v", name.String()),
)
if found {
// Push town table
L.Push(t.(*lua.LTable))
return 1
}
// Get town
for _, town := range util.OTBMap.Map.Towns {
// If its the town we are looking for
if town.Name == name.String() {
twn := StructToTable(&town)
// Save town to cache
util.Cache.Add(
fmt.Sprintf("town_%v", name.String()),
twn,
util.Config.Configuration.Cache.Default.Duration,
)
// Convert town to lua table and push
L.Push(twn)
return 1
}
}
return 0
}
// GetTownByID grabs a town by the given ID
func GetTownByID(L *lua.LState) int {
// Get town ID
id := L.Get(2)
// Check for valid ID type
if id.Type() != lua.LTNumber {
L.ArgError(1, "Invalid ID format. Expected number")
return 0
}
// Convert town id to uint32
townid := uint32(L.ToInt(2))
// Get town
for _, town := range util.OTBMap.Map.Towns {
// If its the town we are looking for
if town.ID == townid {
// Check if town is on cache
t, found := util.Cache.Get(
fmt.Sprintf("town_%v", town.Name),
)
if found {
// Push town table
L.Push(t.(*lua.LTable))
return 1
}
// Convert town to table
twn := StructToTable(&town)
// Save town to cache
util.Cache.Add(
fmt.Sprintf("town_%v", town.Name),
twn,
util.Config.Configuration.Cache.Default.Duration,
)
// Convert town to lua table and push
L.Push(twn)
return 1
}
}
return 0
}