-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.gd
56 lines (49 loc) · 1.64 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
extends Node
var PORT: int = 5000
const MAX_PLAYERS: int = 200
var _ok
var game_count: int = 0
func new_game_id() -> int:
game_count += 1
return game_count
func _ready():
var env_port = OS.get_environment("PORT")
if env_port != "":
PORT = int(env_port)
var server = NetworkedMultiplayerENet.new()
server.create_server(PORT, MAX_PLAYERS)
get_tree().set_network_peer(server)
_ok = get_tree().connect("network_peer_connected", self, "on_client_connected" )
_ok = get_tree().connect("network_peer_disconnected", self, "on_client_disconnected")
const Player = preload("res://Player.tscn")
func on_client_connected(id: int) -> void:
print("Player ", str(id), " connected")
var player = Player.instance()
player.set_name(str(id))
player.id = id
$Players.add_child(player)
func on_client_disconnected(id: int) -> void:
var player = get_node("Players/" + str(id))
if player.is_gaming():
# clean game
var room: int = player.room
get_node("/root/Rooms/" + str(room)).leave(room)
pass
player.queue_free()
const Room = preload("res://Room.tscn")
func new_game_room(create_player: int) -> int:
var id: int = new_game_id()
var player_node = get_node("/room/Players/" + str(create_player))
var room := Room.instance()
room.set_name(str(id))
room.room_name = player_node.player_name
room.join(create_player)
player_node.join(id)
$Rooms.add_child(room)
return id
func list_rooms() -> Array:
var rooms = $Rooms.get_children()
var re = []
for room in rooms:
re.push_back([int(room.get_name()), room.room_name])
return []