/// <summary> /// Get the info for the player out of the database and return in /// </summary> /// <param name="tag">The GamerTag of the player</param> /// <returns>PlayerInfo for the gamer</returns> private PlayerInfo GetPlayerInfo(String tag) { BattleBoatsDataSet dataset = new BattleBoatsDataSet(); PlayerInfo info = new PlayerInfo(); String select = "SELECT * FROM PlayerInfo WHERE UserName = \'" + tag + "\'"; SqlCeDataAdapter adapter = new SqlCeDataAdapter(select, conn); adapter.Fill(dataset, "PlayerInfo"); if (dataset.PlayerInfo.Rows.Count == 0)//New user, create table entries for them { BattleBoatsDataSet.PlayerInfoRow playerrow = dataset.PlayerInfo.NewPlayerInfoRow(); playerrow.UserName = tag; playerrow.AmmoUpgrades = 0; playerrow.ArmourUpgrades = 0; playerrow.Money = 0; playerrow.SpeedUpgrades = 0; playerrow.ShipModel = "Basic"; dataset.PlayerInfo.AddPlayerInfoRow(playerrow); SqlCeCommandBuilder builder = new SqlCeCommandBuilder(adapter); builder.QuotePrefix = "["; builder.QuoteSuffix = "]"; adapter.Update(dataset, "PlayerInfo"); } info.PlayerName = dataset.PlayerInfo[0].UserName; info.Ammo_Level = dataset.PlayerInfo[0].AmmoUpgrades; info.Armour_Level = dataset.PlayerInfo[0].ArmourUpgrades; info.Money = dataset.PlayerInfo[0].Money; info.Speed_Level = dataset.PlayerInfo[0].SpeedUpgrades; info.Ship_Model_Name = dataset.PlayerInfo[0].ShipModel; return info; }
/// <summary> /// Called when a gamer joins the game /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void session_GamerJoined(object sender, GamerJoinedEventArgs e) { Console.WriteLine("A new Gamer, " + e.Gamer.Gamertag + " has joined"); if (gameMode == GameMode.Network_Multiplayer) { lob.AddPlayerToLobby(e.Gamer.Gamertag); } foreach (PlayerInfo info in activePlayers) { if (info.PlayerName == e.Gamer.Gamertag) return; } PlayerInfo player = new PlayerInfo(); bool result = player.GetPlayerInfoFromServer(e.Gamer.Gamertag, SERVER_IP, SERVER_PORT_NUM); if (!result) { player = new PlayerInfo(); player.PlayerName = e.Gamer.Gamertag; player.Ammo_Level = 0; player.Armour_Level = 0; player.Money = 10; player.Speed_Level = 0; Console.WriteLine("Creating new profile"); Console.WriteLine(player.ToString()); } player.Player_Ship = AvailableShips[0]; if (gameMode == GameMode.Network_Multiplayer && e.Gamer.Gamertag != SignedInGamer.SignedInGamers[0].Gamertag) player.PlayerLocation = Player_Location.Remote; else player.PlayerLocation = Player_Location.Local; activePlayers.Add(player); if (player.PlayerName == SignedInGamer.SignedInGamers[0].Gamertag) { playerIndex = activePlayers.Count - 1; } }
/// <summary> /// Called when a game is started /// </summary> /// <param name="source"></param> private void HandleStartGame(object source) { if (gameState == GameState.In_Game || gameState == GameState.Main_Menu) return; if (gameMode == GameMode.Network_Multiplayer) { if (session.IsEveryoneReady || !session.IsEveryoneReady && session.SessionState == NetworkSessionState.Lobby) { session.StartGame(); session.Update(); HideMainMenu(); } } else { PlayerInfo info = new PlayerInfo(activePlayers[0].ToString()); info.PlayerName = info.PlayerName + " Guest"; info.Player_Ship = AvailableShips[0]; activePlayers.Add(info); if (session.SessionState == NetworkSessionState.Lobby) { session.StartGame(); session.Update(); HideMainMenu(); } } }
//Networking /// <summary> /// Get the player info from the Server /// </summary> private void GetPlayerInfo() { PlayerInfo playerInfo1; //Information for Player 1 playerInfo1 = new PlayerInfo(); bool result = playerInfo1.GetPlayerInfoFromServer(SignedInGamer.SignedInGamers[0].Gamertag, SERVER_IP, SERVER_PORT_NUM); if (!result) { playerInfo1 = new PlayerInfo(); playerInfo1.PlayerName = SignedInGamer.SignedInGamers[0].Gamertag; playerInfo1.Ammo_Level = 0; playerInfo1.Armour_Level = 0; playerInfo1.Money = 0; playerInfo1.Speed_Level = 0; playerInfo1.Player_Ship = AvailableShips[0]; playerInfo1.PlayerLocation = Player_Location.Local; Console.WriteLine("Creating new profile"); Console.Write(playerInfo1.ToString()); } playerInfo1.Player_Ship = AvailableShips[0]; activePlayers.Add(playerInfo1); }
/// <summary> /// Update the databse for a player /// </summary> /// <param name="newInfo"></param> private void UpdatePlayerInfo(PlayerInfo newInfo) { BattleBoatsDataSet dataset = new BattleBoatsDataSet(); PlayerInfo info = new PlayerInfo(); String select = "SELECT * FROM PlayerInfo WHERE UserName = \'" + newInfo.PlayerName + "\'"; SqlCeDataAdapter adapter = new SqlCeDataAdapter(select, conn); adapter.Fill(dataset, "PlayerInfo"); SqlCeCommandBuilder builder = new SqlCeCommandBuilder(adapter); string update = builder.GetUpdateCommand().CommandText; if (dataset.PlayerInfo.Rows.Count == 1)//New user, create table entries for them { dataset.PlayerInfo.Rows[0]["UserName"] = newInfo.PlayerName; dataset.PlayerInfo.Rows[0]["AmmoUpgrades"] = newInfo.Ammo_Level; dataset.PlayerInfo.Rows[0]["ArmourUpgrades"] = newInfo.Armour_Level; dataset.PlayerInfo.Rows[0]["Money"] = newInfo.Money; dataset.PlayerInfo.Rows[0]["SpeedUpgrades"] = newInfo.Speed_Level; dataset.PlayerInfo.Rows[0]["ShipModel"] = newInfo.Ship_Model_Name; adapter.Update(dataset, "PlayerInfo"); } }
/// <summary> /// Handles communication with the clients /// </summary> /// <param name="client"></param> private void HandleClientComm(object client) { String gamerID = ""; String sentString = ""; ASCIIEncoding encoder = new ASCIIEncoding(); TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); byte[] message = new byte[4096]; int bytesRead; bytesRead = 0; try { //blocks until a client sends a message bytesRead = clientStream.Read(message, 0, 4096); } catch { } //message has successfully been received sentString = encoder.GetString(message, 0, bytesRead); string[] tokens = sentString.Split(":".ToCharArray()); if (tokens.Length > 1)//We're getting an update string, so update { Console.WriteLine("Player Update data for " + tokens[1] + " recieved."); PlayerInfo info = new PlayerInfo(sentString); UpdatePlayerInfo(info); gamerID = info.PlayerName; } else { gamerID = sentString; } //If we have an ID, write the data back if (gamerID != "") { Console.WriteLine("GamerTag = " + gamerID); PlayerInfo info = GetPlayerInfo(gamerID); byte[] infoBytes = encoder.GetBytes(info.ToString()); clientStream.Write(infoBytes, 0, encoder.GetByteCount(info.ToString())); clientStream.Flush(); } tcpClient.Close(); }