Implemented network system messages in the chat.

This commit is contained in:
unfa 2021-08-23 00:33:54 +02:00
parent df861143a4
commit b0805f1426
2 changed files with 41 additions and 20 deletions

View file

@ -56,6 +56,9 @@ func _unhandled_input(_event) -> void:
var sender_info = main.player_list.get(sender_pid)
chat_history.append_bbcode('\n' + '[b][color=' + sender_info.color.to_html() +']' + str(sender_info.name) + '[/color][/b] : [i]' + message + '[/i]')
@remotesync func chat_notification(message: String) -> void:
chat_history.append_bbcode('\n · ' + '[i]' + message + '[/i]')
func _on_Editor_text_submitted(new_text):
# RPC is currently not implemented in the engine
var sender_id = get_tree().get_network_unique_id()

View file

@ -26,6 +26,7 @@ class PlayerInfo:
var color: Color
var focus: GameFocus
var health: int
var score: int
func _init():#name: String, team: int, color: Color):
var player_name = ""
@ -36,6 +37,7 @@ class PlayerInfo:
self.team = 0
self.focus = 999
self.health = 100
self.score = 0
# else:
# self.name = name
# self.team = team
@ -48,14 +50,16 @@ class PlayerInfo:
'team': str(self.team),
'color': self.color.to_html(),
'focus': self.focus,
'health': self.health
'health': self.health,
'score': self.score,
}
func set(name: String, team: int, color: Color, focus: int, health: int):
func set(name: String, team: int, color: Color, focus: int, health: int, score: int):
self.name = name
self.team = team
self.color = color
self.focus = focus
self.health = health
self.score = score
func deserialize(info):
self.name = info['name']
@ -63,6 +67,7 @@ class PlayerInfo:
self.color = Color.html(info['color'])
self.focus = info['focus']
self.health = info['health']
self.score = info['score']
#func generate():
func _process(delta):
@ -146,30 +151,32 @@ func _input(_event) -> void:
# self.player_list = player_list
@remote func player_list_update(info, pid = get_tree().get_rpc_sender_id()):
var new_info = PlayerInfo.new()
new_info.deserialize(info)
if player_list.players.has(pid):
var old_name = player_list.get(pid).name
if old_name != new_info.name:
chat.chat_notification("Player [b]" + old_name + "[/b] changed name to [b]" + new_info.name + "[/b]")
elif get_tree().network_peer.get_unique_id() == 1:
for i in player_list.players.keys(): #notify existing players the new one has joined
# if i != pid:
chat.rpc(i, &'chat_notification', "Player [b]" + new_info.name + "[/b] joined")
if get_tree().network_peer.get_unique_id() == 1: # if we're server, we should store this
pid = get_tree().get_rpc_sender_id() #disallow clients setting player_info for other players than themselves
var new_info = PlayerInfo.new()
new_info.deserialize(info)
player_list.set(pid, new_info)
rpc(&'player_list_update', info, pid) # broadcast the new entry to clients
else: #we are client, so we're getting data relayed from the server
var new_info = PlayerInfo.new()
new_info.deserialize(info)
player_list.set(pid, new_info) # server relays other PID's data
# if not id:
# id = get_tree().get_rpc_sender_id()
#
# print_debug("player_list_update ", info)
# if info is PlayerInfo:
# player_list.set(id, info)
# else:
# var new_info = PlayerInfo.deserialize(info)
# player_list.set(id, new_info)
# print("Updating player_list wit hnew info: ", new_info)
#
# print("Player list: ", player_list)
# print("Player list item for id ", id, ": ", player_list[id])
func push_local_player_info():
var id = get_tree().network_peer.get_unique_id()
if id != 1:
rpc_id(1, &'player_list_update', player_list.get(id).serialize())
else:
rpc(&'player_list_update', player_list.get(id).serialize(), 1)
func create_player(pid: int, is_local: bool) -> void:
var new_player
@ -212,6 +219,7 @@ func _on_Host_pressed(): # start server and create a local player
get_tree().network_peer = peer
create_player(1, true)
focus = GameFocus.GAME
chat.chat_notification("Started server")
func _on_Connect_pressed():
$NetworkTesting/Host.disabled = true
@ -225,7 +233,7 @@ func _player_connected(pid) -> void:
create_player(pid, false)
if get_tree().network_peer.get_unique_id() == 1: # if we're the server
for i in player_list.players.keys(): # send each player info entry to the newly connected client
for i in player_list.players.keys(): # send the player_list to the new client
rpc_id(pid, &'player_list_update', player_list.get(i).serialize(), i) # send local player info to the server
# if local_player:
@ -237,8 +245,12 @@ func _player_connected(pid) -> void:
func _player_disconnected(pid) -> void:
print("player disconnected, id: ", pid)
if get_tree().network_peer.get_unique_id() == 1: # if we're the server, broadcast that a player left
chat.rpc(&'chat_notification', "Player [b]" + player_list.get(pid).name + "[/b] left")
func _connected_ok() -> void:
print("connected to server")
chat.chat_notification("Connected to server")
var pid = get_tree().get_network_unique_id()
create_player(pid, true)
focus = GameFocus.GAME
@ -246,10 +258,13 @@ func _connected_ok() -> void:
func _connected_fail() -> void:
print("connection to server failed")
chat.chat_notification("Connection failed")
func _server_disconnected() -> void:
print("server disconnected")
role = NetworkRole.NONE
chat.chat_notification("Server disconnected")
func _ready() -> void:
print("Commandline arguments: ", OS.get_cmdline_args())
@ -263,6 +278,9 @@ func _ready() -> void:
func _on_TextEdit_text_submitted(new_text):
player_list.players[get_tree().network_peer.get_unique_id()].name = new_text
push_local_player_info()
#chat_announcement("Player " + old_name + " is now known as " + new_name)
func _on_ColorPickerButton_color_changed(color):
player_list.players[get_tree().network_peer.get_unique_id()].color = color
push_local_player_info()