Repository navigation
Expand file tree
/
Copy pathdedicated.lua
More file actions
209 lines (176 loc) · 4.37 KB
/
Copy pathdedicated.lua
File metadata and controls
209 lines (176 loc) · 4.37 KB
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
-- Run this file to run a dedicated server.
-- Make sure the config.txt file is edited to fit the settings you want to have.
--[[print = function(...)
local args = {...}
origPrint( unpack(args) )
local str = ""
for k, v in pairs(args) do
str = str .. v .. "\t"
end
--love.filesystem.append( "log.txt", str .. "\r\n" )
local file = io.open( "printLog.txt", "a")
file:write( str .. "\n" )
file:close()
end]]
DEDICATED = true
STATE = "Menu"
-- Lua file system:
require("lfs")
network = require( "network.network" )
config = require( "config" )
game = require( "game" )
lobby = require( "lobby" )
map = require( "map" )
utility = require( "utility" ) -- helper functions
require( "callbacks" ) -- helper functions
Timer = require("timer")
dedicated = {
currentMapName = nil,
mapCycleID = 0
}
config.load()
--lobby:init()
--game:init()
map:load()
MAIN_SERVER_URL = "http://germanunkol.de/gridcars/serverlist"
GAME_ID = "GridCars"
function dedicated:startServer()
local err
server, err = network:startServer( MAX_PLAYERS, PORT )
if server then
-- set client callbacks:
setServerCallbacks( server )
lobby:show()
updateAdvertisementInfo()
network.advertise:setURL( MAIN_SERVER_URL )
network.advertise:setID( GAME_ID )
network.advertise:start( server, "both" )
else
-- If I can't start a server for some reason, let user know and exit:
print("ERROR: Could not start server:")
print( "\t", err)
os.exit()
end
end
function dedicated:update( dt )
if STATE == "Lobby" then
-- Wait for at least one user:
if server:getNumUsers() > 0 then
-- Check if all clients are ready and if so start game
if not self.postMatchLocked then
if lobby:attemptGameStart() then
game:show()
end
end
end
elseif STATE == "Game" then
-- Let's hope it never gets below 0... :P
if game:getNumUsersPlaying() <= 0 then
game:sendBackToLobby()
end
end
end
-- Show the old map for a set period of time.
function dedicated:postMatchLock()
self.postMatchLocked = true
end
function getDirectoryItems( dir )
local f = {}
for file in lfs.dir( dir ) do
if file ~= "." and file ~= ".." then
table.insert( f, file )
end
end
return f
end
function dedicated:loadMapCycle()
MAP_CYCLE = config.getValue( "MAP_CYCLE" )
MAP_CYCLE_TBL = {}
if MAP_CYCLE then
for file in MAP_CYCLE:gmatch( "([^%s,]+)%s*,?%s*" ) do
table.insert( MAP_CYCLE_TBL, file )
end
end
-- Look for invalid file names ...
local invalidFiles = {}
for k = #MAP_CYCLE_TBL,1,-1 do -- iterate backwards because of the table.remove call
local filename = MAP_CYCLE_TBL[k]
-- Try to open the file, to see if it exists:
local f = io.open( "maps/" .. filename )
if not f then
-- table.insert( invalidFiles, k )
table.remove( MAP_CYCLE_TBL, k )
else
f:close()
end
end
end
function dedicated:chooseMap()
self:loadMapCycle()
local foundMapFromMapCycle = nil
if #MAP_CYCLE_TBL > 0 then
if MAP_CYCLE_TBL[self.mapCycleID+1] then
self.currentMapName = MAP_CYCLE_TBL[self.mapCycleID+1]
self.mapCycleID = self.mapCycleID + 1
else
self.currentMapName = MAP_CYCLE_TBL[1]
self.mapCycleID = 1
end
foundMapFromMapCycle = true
end
if not foundMapFromMapCycle then
print("No map cycle found. Will play all maps in maps/ subfolder:")
local files = getDirectoryItems( "maps/" )
if #files < 1 then
return
end
-- first map?
if self.currentMapName == nil then
self.currentMapName = files[1]
else
for k, f in ipairs(files) do
if f == self.currentMapName then
if files[k+1] then
self.currentMapName = files[k+1]
else
self.currentMapName = files[1]
end
break
end
end
end
else
print("Map cycle:")
for k, file in ipairs( MAP_CYCLE_TBL ) do
if k == self.mapCycleID then
print( "\t>" .. file )
else
print( "\t " .. file )
end
end
end
-- Choose this map, load it and send it to all clients:
lobby:chooseMap( self.currentMapName )
self.postMatchLocked = false
end
-- Call the function to start the server right at startup
dedicated:startServer()
function sleep(sec)
socket.select(nil, nil, sec)
end
-- Continue running for ever:
local time = socket.gettime()
local dt = 0
local t = 0
while true do
network:update( dt )
if STATE == "Game" then
game:update( dt )
elseif STATE == "Lobby" then
lobby:update( dt )
end
dedicated:update( dt )
dt = socket.gettime() - time
time = socket.gettime()
sleep( 0.05 )
end