-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.gd
177 lines (143 loc) · 4.23 KB
/
Server.gd
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
extends Node
enum {
LOBBY,
GAME
}
sync var game_state = LOBBY
var network = NetworkedMultiplayerENet.new()
var port = ServerData.port
var max_players = ServerData.max_players
var min_players = 1 #2
var loadins = 0
var update_timer = 0
var update_delay = 5
var game_start_timer = 0
var game_start_delay = 5
var game_start = false
func _ready():
start_server()
print("Server launching...")
func _process(delta):
# Console update timer
if update_timer >= update_delay:
update_timer = 0
_update()
else:
update_timer+=delta
# Game startup timer
if game_start_timer > 0:
game_start_timer -= delta
returnStartGame(false, game_start_timer)
else:
if game_start == true:
returnStartGame(true)
game_start = false
game_start_timer = 0
# Server-side checks
if ServerData.players.size() < min_players:
if game_state != LOBBY:
changeGameState(LOBBY)
func _update():
if OS.get_name() != "Windows":
clear_console()
print("Player Count: "+str(ServerData.players.size()))
print(ServerData.players)
func clear_console():
#https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
var escape = PoolByteArray([0x1b]).get_string_from_ascii()
print(escape + "[2J") # Clear the console
print(escape + "[H") # Set the console cursor position to (0, 0)
func start_server():
network.create_server(port, max_players)
get_tree().set_network_peer(network)
print("Server Started!")
network.connect("peer_connected", self, "_peer_connected")
network.connect("peer_disconnected", self, "_peer_disconnected")
# Player disconnect/connect
func _peer_connected(player_id):
print("Player "+str(player_id)+" Connected")
pushGameState()
func _peer_disconnected(player_id):
print("Player "+str(player_id)+" Disconnected")
var players = ServerData.players
if ServerData.players.size() > 0:
if ServerData.players.keys().find(player_id) != -1:
for player in ServerData.players.keys():
if ServerData.players[player].order > ServerData.players[player_id].order:
ServerData.players[player].order -= 1
ServerData.players.erase(player_id)
pushPlayerData()
pushGameState()
rpc("serverUpdate")
# Methods
func constructPlayer(player_id, color = Color(1, 1, 1, 0.25).to_html(), title = "???", mode = "egg"):
ServerData.players[player_id] = {
"color": color,
"title": title,
"mode": mode,
"loading": false,
"order": ServerData.players.keys().size()
}
pushPlayerData()
remote func fetchLobbyJoin(color, title):
var player_id = get_tree().get_rpc_sender_id()
if title == "":
title = str(player_id)
if fetchColorAvailable(color):
constructPlayer(player_id, color, title)
rpc_id(player_id, "returnLobbyJoin", color, title)
else:
rpc_id(player_id, "Error", "color_taken")
remote func fetchColorAvailable(color, requester = null):
var player_id = get_tree().get_rpc_sender_id()
var value = true
for i in ServerData.players.keys():
if ServerData.players[i].color == color:
value = false
break
else:
value = true
if requester != null:
rpc_id(player_id, "returnColorAvailable", requester, color, value)
else:
return value
# Syncing
remote func finishLoading():
var player_id = get_tree().get_rpc_sender_id()
pushPlayerData()
rpc_id(player_id, "serverUpdate")
# Player settings
func pushPlayerData():
rset("players", to_json(ServerData.players))
rpc("serverUpdate")
func pushGameState():
rset("game_state", game_state)
# Game states
func changeGameState(state):
print("Game state changed to: "+str(state))
game_state = state
# Start game
remote func requestStartGame():
var player_id = get_tree().get_rpc_sender_id()
# Check if the request is coming from the host
if ServerData.players[player_id].order == 0:
print("requestStartGame")
# Start or cancel the game
if game_start_timer > 0:
print("Host cancelled the game")
game_start = false
game_start_timer = 0
returnStartGame(false, -1)
else:
# Check if there is the minimum number of players
if ServerData.players.keys().size() >= min_players:
print("Host started the game")
game_start = true
game_start_timer = game_start_delay
func returnStartGame(launch, timer = 0):
if launch:
rpc("returnStartGame", launch, timer)
changeGameState(GAME)
pushGameState()
else:
rpc_unreliable("returnStartGame", launch, timer)