Skip to content

Commit

Permalink
#127 Making player data more... sort of strongly typed, kind of
Browse files Browse the repository at this point in the history
Will hopefully help us as we add more data.
  • Loading branch information
EdgarVerona committed May 2, 2020
1 parent 8c4f89b commit e41877f
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 83 deletions.
4 changes: 4 additions & 0 deletions client/ClientEntry.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func _ready():
#go_to_mobile_vr()
go_to_flat()

# Note that this one time handling of command line arguments is intentionally
# happening after the MainMenu for a given client is initialized: those scenes have
# handlers for join that need to be ready in order for the game to process the join command
# correctly.
handle_commandline_args()


Expand Down
10 changes: 5 additions & 5 deletions client/game/mode/fugitive/FugitiveGame-dev-vr.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func _enter_tree():

# Add our fake players, the normal spawn system will actually spawn these guys
if be_seeker:
GameData.add_player(GameData.create_new_player(1, "real player", FugitiveTeamResolver.PlayerType.Seeker))
GameData.add_player(GameData.create_new_player(10, "dumb donkey 0", FugitiveTeamResolver.PlayerType.Hider))
GameData.add_player_from_raw_data(GameData.create_new_player_raw_data(1, "real player", FugitiveTeamResolver.PlayerType.Seeker))
GameData.add_player_from_raw_data(GameData.create_new_player_raw_data(10, "dumb donkey 0", FugitiveTeamResolver.PlayerType.Hider))
else:
GameData.add_player(GameData.create_new_player(1, "real player", FugitiveTeamResolver.PlayerType.Hider))
GameData.add_player(GameData.create_new_player(10, "dumb donkey 0", FugitiveTeamResolver.PlayerType.Seeker))
GameData.add_player_from_raw_data(GameData.create_new_player_raw_data(1, "real player", FugitiveTeamResolver.PlayerType.Hider))
GameData.add_player_from_raw_data(GameData.create_new_player_raw_data(10, "dumb donkey 0", FugitiveTeamResolver.PlayerType.Seeker))

GameData.add_player(GameData.create_new_player(11, "dumb donkey 1", FugitiveTeamResolver.PlayerType.Hider))
GameData.add_player_from_raw_data(GameData.create_new_player_raw_data(11, "dumb donkey 1", FugitiveTeamResolver.PlayerType.Hider))


func _ready():
Expand Down
12 changes: 6 additions & 6 deletions client/game/mode/fugitive/FugitiveGame-dev.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func _enter_tree():

# Add our fake players, the normal spawn system will actually spawn these guys
if be_seeker:
GameData.add_player(GameData.create_new_player(1, "real player", FugitiveTeamResolver.PlayerType.Seeker))
GameData.add_player(GameData.create_new_player(10, "dumb donkey 0", FugitiveTeamResolver.PlayerType.Hider))
GameData.add_player_from_raw_data(GameData.create_new_player_raw_data(1, "real player", FugitiveTeamResolver.PlayerType.Seeker))
GameData.add_player_from_raw_data(GameData.create_new_player_raw_data(10, "dumb donkey 0", FugitiveTeamResolver.PlayerType.Hider))
else:
GameData.add_player(GameData.create_new_player(1, "real player", FugitiveTeamResolver.PlayerType.Hider))
GameData.add_player(GameData.create_new_player(10, "dumb donkey 0", FugitiveTeamResolver.PlayerType.Seeker))
GameData.add_player_from_raw_data(GameData.create_new_player_raw_data(1, "real player", FugitiveTeamResolver.PlayerType.Hider))
GameData.add_player_from_raw_data(GameData.create_new_player_raw_data(10, "dumb donkey 0", FugitiveTeamResolver.PlayerType.Seeker))

GameData.add_player(GameData.create_new_player(11, "dumb donkey 1", FugitiveTeamResolver.PlayerType.Hider))
GameData.add_player(GameData.create_new_player(12, "dumb donkey 2", FugitiveTeamResolver.PlayerType.Seeker))
GameData.add_player_from_raw_data(GameData.create_new_player_raw_data(11, "dumb donkey 1", FugitiveTeamResolver.PlayerType.Hider))
GameData.add_player_from_raw_data(GameData.create_new_player_raw_data(12, "dumb donkey 2", FugitiveTeamResolver.PlayerType.Seeker))


func _ready():
Expand Down
10 changes: 5 additions & 5 deletions client/game/mode/fugitive/hud/EndGameHud.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ extends Control
func _ready():
hide()


func team_won(winningTeam: int):

var winningTeamName = GameData.currentGame.get_team_name(winningTeam)
$Container/WinnerLabel.text = "%ss won!" % winningTeamName

for playerInfo in GameData.get_players():
var playerInfoData := playerInfo as PlayerData
var node := Label.new()
var playerType := playerInfo[GameData.PLAYER_TYPE] as int
var playerType := playerInfoData.get_type()
var team = GameData.currentGame.get_team_name(playerType)

var playerObj := GameData.currentGame.get_player(playerInfo[GameData.PLAYER_ID]) as FugitivePlayer
var playerObj := GameData.currentGame.get_player(playerInfoData.get_id()) as FugitivePlayer
if playerObj != null:
var frozen: String
if playerObj.frozen:
Expand All @@ -23,9 +23,9 @@ func team_won(winningTeam: int):
frozen = "ESCAPED!"

if playerType == FugitiveTeamResolver.PlayerType.Hider:
node.text = "[%s] %s - %s" % [team, playerInfo[GameData.PLAYER_NAME], frozen]
node.text = "[%s] %s - %s" % [team, playerInfo.get_name(), frozen]
else:
node.text = "[%s] %s" % [team, playerInfo[GameData.PLAYER_NAME]]
node.text = "[%s] %s" % [team, playerInfo.get_name()]

$Container/PlayerList.add_child(node)

Expand Down
8 changes: 4 additions & 4 deletions common/game/mode/fugitive/FugitiveGame.gd
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ func spawn_player(playerId: int, hiderSpawns: Array, seekerSpawns: Array):
var localPlayerId := get_tree().get_network_unique_id()
var localPlayerType: int
if GameData.players.has(localPlayerId):
localPlayerType = GameData.players[localPlayerId][GameData.PLAYER_TYPE]
localPlayerType = GameData.players[localPlayerId].get_type()
else:
localPlayerType = FugitiveTeamResolver.PlayerType.Unset

# Extract the player data
var player = GameData.players[playerId]
var playerName := player[GameData.PLAYER_NAME] as String
var playerType := player[GameData.PLAYER_TYPE] as int
var player := GameData.players[playerId] as PlayerData
var playerName := player.get_name()
var playerType := player.get_type()

# This is the node for the PlayerController
var pcNode: Node
Expand Down
14 changes: 7 additions & 7 deletions common/lobby/Lobby.gd
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func populate_map_list():


func create_player(playerId: int):
var existingPlayer := find_player_node(playerId)
if existingPlayer != null:
var existingPlayerNode := find_player_node(playerId)
if existingPlayerNode != null:
return

print("Creating player in lobby")

var player = GameData.players[playerId]
var player := GameData.players[playerId] as PlayerData
var mode = Maps.get_mode_for_map(GameData.general[GameData.GENERAL_MAP])

var playerListItem = preload("res://common/lobby/PlayerListItem.tscn")
Expand Down Expand Up @@ -76,7 +76,7 @@ func remove_player(playerId: int):


func repopulate_player(playerId: int):
var player = GameData.players[playerId]
var player := GameData.players[playerId] as PlayerData
var mode = Maps.get_mode_for_map(GameData.general[GameData.GENERAL_MAP])

var node := find_player_node(playerId)
Expand Down Expand Up @@ -157,7 +157,7 @@ func can_start() -> bool:

if not players.empty():
for player in players:
var playerteam = player[GameData.PLAYER_TYPE]
var playerteam = player.get_type()
actualTeamSizes[playerteam] += 1

var teamSizesAreValid = true
Expand All @@ -181,8 +181,8 @@ func on_start_game():

# Update if this local client is the host
func update_host():
var host := GameData.get_host()
if host != null and host[GameData.PLAYER_ID] == get_tree().get_network_unique_id():
var host := GameData.get_host() as PlayerData
if host != null and host.get_id() == get_tree().get_network_unique_id():
is_host = true
else:
is_host = false
Expand Down
15 changes: 7 additions & 8 deletions common/lobby/PlayerListItem.gd
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
extends Control

var playerInfo = null
var playerInfo: PlayerData = null

onready var teamButton := $TeamButton as OptionButton
var curTeamResolver = null

func populate(player, is_starting: bool, is_host: bool, game_mode: Dictionary):
func populate(player: PlayerData, is_starting: bool, is_host: bool, game_mode: Dictionary):
playerInfo = player
var playerId = player.get_id()

var playerId = playerInfo[GameData.PLAYER_ID]

$NameLabel.text = playerInfo[GameData.PLAYER_NAME]
$HostLabel.visible = playerInfo[GameData.PLAYER_HOST]
$NameLabel.text = player.get_name()
$HostLabel.visible = player.get_is_host()

curTeamResolver = game_mode[Maps.MODE_TEAM_RESOLVER]

Expand All @@ -21,7 +20,7 @@ func populate(player, is_starting: bool, is_host: bool, game_mode: Dictionary):
var teamname = curTeamResolver.get_team_name(ii)
teamButton.add_item(teamname, ii)

var playerType = playerInfo[GameData.PLAYER_TYPE]
var playerType := player.get_type()
teamButton.selected = playerType

if (is_host or ClientNetwork.is_local_player(playerId)) and not is_starting:
Expand All @@ -31,4 +30,4 @@ func populate(player, is_starting: bool, is_host: bool, game_mode: Dictionary):


func _on_TeamButton_item_selected(id):
ServerNetwork.change_player_type(playerInfo[GameData.PLAYER_ID], id)
ServerNetwork.change_player_type(playerInfo.get_id(), id)
26 changes: 13 additions & 13 deletions networking/ClientNetwork.gd
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,27 @@ func on_disconnected_from_server():
get_tree().disconnect('server_disconnected', self, 'on_disconnected_from_server')


func register_player(recipientId: int, player: Dictionary):
rpc_id(recipientId, "on_register_player", player)

func register_player(recipientId: int, player: PlayerData):
rpc_id(recipientId, "on_register_player", player.player_data_dictionary)

func register_player_from_raw_data(recipientId: int, playerDataDictionary: Dictionary):
rpc_id(recipientId, "on_register_player", playerDataDictionary)

remote func on_register_player(player: Dictionary):
var playerId = player[GameData.PLAYER_ID]
var playerName = player[GameData.PLAYER_NAME]
var playerId = player.id
var playerName = player.name

print("on_register_player: %d - %s" % [playerId, playerName] )
GameData.add_player(player)
GameData.add_player_from_raw_data(player)
emit_signal("create_player", playerId)
print("Total players: %d" % GameData.players.size())

func update_player(playerData: PlayerData):
rpc("on_update_player", playerData.player_data_dictionary)

func update_player(playerInfo):
rpc("on_update_player", playerInfo)


remotesync func on_update_player(playerInfo):
GameData.update_player(playerInfo)
emit_signal("update_player", playerInfo[GameData.PLAYER_ID])
remotesync func on_update_player(playerInfoDictionary: Dictionary):
GameData.update_player_from_raw_data(playerInfoDictionary)
emit_signal("update_player", playerInfoDictionary.id)


func update_game_data():
Expand Down
48 changes: 23 additions & 25 deletions networking/GameData.gd
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
extends Node

const PLAYER_ID = "id"
const PLAYER_NAME = "name"
const PLAYER_TYPE = "type"
const PLAYER_HOST = "host"

var players = {}

var currentGame: GameMode = null
Expand All @@ -23,25 +18,27 @@ func get_players() -> Array:
return players.values()


func get_player(playerId: int):
func get_player(playerId: int) -> PlayerData:
if players.has(playerId):
return players[playerId]
return players[playerId] as PlayerData
else:
return null


func create_new_player(playerId: int, playerName: String, playerType: int) -> Dictionary:
func create_new_player_raw_data(playerId: int, playerName: String, playerType: int) -> Dictionary:
return {
PLAYER_ID: playerId,
PLAYER_NAME: playerName,
PLAYER_TYPE: playerType,
PLAYER_HOST: false
id = playerId,
name = playerName,
type = playerType,
is_host = false
}


func add_player(newPlayer: Dictionary) -> bool:
var playerId = newPlayer[PLAYER_ID]
func add_player_from_raw_data(newPlayerDictionary: Dictionary) -> bool:
var playerId = newPlayerDictionary.id
if not self.players.has(playerId):
var newPlayer := load("res://networking/PlayerData.gd").new() as PlayerData
newPlayer.load(newPlayerDictionary)

self.players[playerId] = newPlayer
return true
else:
Expand All @@ -52,32 +49,33 @@ func reset():
self.players = {}


func get_current_player():
func get_current_player() -> PlayerData:
var id := get_tree().get_network_unique_id()
if players.has(id):
return players[id]
return players[id] as PlayerData
else:
return null


func get_current_player_type() -> int:
var curPlayer = get_current_player()
var curPlayer := get_current_player()
if curPlayer != null:
return curPlayer[PLAYER_TYPE]
return curPlayer.get_type()
else:
return -1


func update_player(player):
var playerId = player[PLAYER_ID]
players[playerId] = player

func update_player_from_raw_data(player_data_dictionary: Dictionary):
var playerId = player_data_dictionary.id
get_player(playerId).load(player_data_dictionary)

func get_host() -> Player:
func get_host() -> PlayerData:
var host = null

for player in GameData.players.values():
if player[GameData.PLAYER_HOST] == true:
var playerData := player as PlayerData

if playerData.get_is_host() == true:
host = player
break

Expand Down
33 changes: 33 additions & 0 deletions networking/PlayerData.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
extends Node

var player_data_dictionary: Dictionary

func _ready():
pass

func load(new_player_data_dictionary: Dictionary):
player_data_dictionary = new_player_data_dictionary

func get_id() -> int:
return player_data_dictionary.id

func set_id(id: int):
player_data_dictionary.id = id

func get_name() -> String:
return player_data_dictionary.name

func set_name(name: String):
player_data_dictionary.name = name

func get_type() -> int:
return player_data_dictionary.type

func set_type(type: int):
player_data_dictionary.type = type

func get_is_host() -> bool:
return player_data_dictionary.is_host

func set_is_host(is_host: bool):
player_data_dictionary.is_host = is_host
Loading

0 comments on commit e41877f

Please sign in to comment.