Skip to content

Commit

Permalink
Added Sequence Channel number ordering and support for in game player…
Browse files Browse the repository at this point in the history
… name updates
  • Loading branch information
tomoprime committed May 3, 2012
1 parent 7eefe11 commit 5279cc3
Show file tree
Hide file tree
Showing 28 changed files with 127 additions and 75 deletions.
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -7,7 +7,13 @@ This project was created to help people get a Client / Server Game going using <
Let the games begin,<br>
Tom Acunzo - aka <i>Tomoprime</i>

<h3>Current build v1.1</h3>
<h3>Current Build v1.2</h3>
- Multiplayer
Added support for changing player name while in game. GUIText will get updated on other clients.
Added Sequence Channel numbering grouped by message / packet types see GameServer.cs and NetworkManager.cs for additional notes.
Fixed misc bugs. Clean up some warning messages.

<h3>Build v1.1</h3>

- Multiplayer
Added late joiner support to access a game already in play.
Expand Down Expand Up @@ -50,4 +56,4 @@ Oh! And CryptoHelper.cs was written by a friend, so please don't use or distrib
I hope the code is helpful in someway and that you can learn something from it. :)

Dan "Doddler" McNeill (http://dodsrv.com/crabbattle/)
</pre>
</pre>
82 changes: 49 additions & 33 deletions Server/GameServer.cs

Large diffs are not rendered by default.

Binary file removed Server/bin/Release/CrabBattleServer.exe
Binary file not shown.
Binary file removed Server/bin/Release/Mono.CSharp.dll
Binary file not shown.
Binary file removed Server/bin/Release/Mono.Data.Tds.dll
Binary file not shown.
Binary file removed Server/bin/Release/Mono.Posix.dll
Binary file not shown.
Binary file removed Server/bin/Release/Mono.Security.dll
Binary file not shown.
Binary file removed Server/bin/Release/System.Configuration.dll
Binary file not shown.
Binary file removed Server/bin/Release/System.Core.dll
Binary file not shown.
Binary file removed Server/bin/Release/System.Data.DataSetExtensions.dll
Binary file not shown.
Binary file removed Server/bin/Release/System.Data.dll
Binary file not shown.
Binary file removed Server/bin/Release/System.EnterpriseServices.dll
Binary file not shown.
Binary file removed Server/bin/Release/System.Security.dll
Binary file not shown.
Binary file removed Server/bin/Release/System.Transactions.dll
Binary file not shown.
Binary file removed Server/bin/Release/System.Xml.Linq.dll
Binary file not shown.
Binary file removed Server/bin/Release/System.Xml.dll
Binary file not shown.
Binary file removed Server/bin/Release/System.dll
Binary file not shown.
Binary file removed Server/bin/Release/mscorlib.dll
Binary file not shown.
93 changes: 57 additions & 36 deletions Unity/Assets/Scripts/NetworkManager.cs
Expand Up @@ -66,6 +66,21 @@ public PlayerObject(int id, GameObject obj, string name)
}
}

/*
* Created by TomoPrime
*
* Added update for Sequence Channels
*
* PacketTypes of:
* 0 - Message, LobbyMessage, MessageDebug are set to 0
* 1 - Mostly Server connect types are like: AssignID, AddPlayer, RemovePlayer, StartGame, Disconnect
* 2 - Player related types are like: PlayerHit, PlayerSpecial, PlayerAction
* 3 - Enemy types are like: EnemySync, EnemyHealth, EnemyAction
* 4 - Beat aka heartbeat probably will go away later on replaced by KeepAlive
* 5 - Mostly Game settings like: SettingsChange, PlayerIntro, Ready, UpdateName
* 6 - PlayerCount used to keep track of new joiners and leavers
*/

public sealed class NetworkManager : MonoBehaviour {

private static volatile NetworkManager _instance;
Expand Down Expand Up @@ -159,7 +174,7 @@ void Shutdown ()
{
NetOutgoingMessage outmsg = client.CreateMessage();
outmsg.Write((byte)PacketTypes.Disconnect);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 1);
client.Shutdown(username+": Bye All");
print("Id"+ClientId+" Closing client connection...");
}
Expand Down Expand Up @@ -252,7 +267,7 @@ public void OnGUI()
{
NetOutgoingMessage outmsg = client.CreateMessage();
outmsg.Write((byte)PacketTypes.Disconnect);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 1);
client.Shutdown(username+": Bye All");
print("Closing client connection...");
}
Expand Down Expand Up @@ -327,6 +342,8 @@ void ConnectionWindow(int windowID)
gm.isShowMenu = false;
//gm.gamephase = 2;
ready = true;
if (newname != gm.username)
ChangeName(newname);
}
}
else {
Expand Down Expand Up @@ -358,10 +375,9 @@ void ConnectionWindow(int windowID)

//If they hit enter while typing their name, send the changes to the server.
if (Event.current.keyCode == KeyCode.Return && GUI.GetNameOfFocusedControl() == "Namebox")
if (newname != username && newname != "")
if (newname != gm.username && newname != "")
{
ChangeName(newname);
username = newname;
}
}

Expand All @@ -385,7 +401,7 @@ public void DealEnemyDamage(int damage, bool weakpoint)
outmsg.Write((byte)PacketTypes.HurtTarget);
outmsg.Write((Int16)damage);
outmsg.Write(weakpoint);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, 0);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, 3);
}

public void SendPlayerSpecial(int SpecialType)
Expand All @@ -396,7 +412,7 @@ public void SendPlayerSpecial(int SpecialType)
NetOutgoingMessage outmsg = new NetOutgoingMessage();
outmsg.Write((byte)PacketTypes.PlayerSpecial);
outmsg.Write((Int16)SpecialType);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, 0);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, 2);
}

public void SendPlayerUpdate(int id, float xvel, float yvel, bool firing)
Expand All @@ -414,15 +430,15 @@ public void SendPlayerUpdate(int id, float xvel, float yvel, bool firing)
outmsg.Write(xvel);
outmsg.Write(yvel);
outmsg.Write(firing);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 1);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 2);
}

public void SendKeepAlive()
{
if (gm.isSoloPlay) return;
NetOutgoingMessage outmsg = new NetOutgoingMessage();
outmsg.Write((byte)PacketTypes.KeepAlive);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, 0);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, 4);
}

public void SendLobbyText(string msg)
Expand All @@ -431,7 +447,7 @@ public void SendLobbyText(string msg)
NetOutgoingMessage outmsg = new NetOutgoingMessage();
outmsg.Write((byte)PacketTypes.LobbyMessage);
outmsg.Write(msg);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 1);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 0);
}

public void AddLobbyMessage(string message)
Expand Down Expand Up @@ -485,6 +501,9 @@ public void SendStart()
return;
}

if (newname != gm.username)
ChangeName(newname);

NetOutgoingMessage outmsg = new NetOutgoingMessage();
outmsg.Write((byte)PacketTypes.StartGame);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 1);
Expand All @@ -499,7 +518,7 @@ public void TakeHit()

NetOutgoingMessage outmsg = new NetOutgoingMessage();
outmsg.Write((byte)PacketTypes.PlayerHit);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, 0);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, 2);
}

public void ToggleReady(bool ready)
Expand All @@ -513,7 +532,7 @@ public void ToggleReady(bool ready)
NetOutgoingMessage outmsg = new NetOutgoingMessage();
outmsg.Write((byte)PacketTypes.Ready);
outmsg.Write(ready);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, 0);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 5);
}

gm.isReady = ready;
Expand All @@ -529,7 +548,7 @@ public void TogglePlayIntro(bool intro)
NetOutgoingMessage outmsg = new NetOutgoingMessage();
outmsg.Write((byte)PacketTypes.PlayIntro);
outmsg.Write(gm.isPlayIntro);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, 0);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 5);
}

public void ChangeDifficulty(int newdiff, int newhealth)
Expand All @@ -550,7 +569,7 @@ public void ChangeDifficulty(int newdiff, int newhealth)
outmsg.Write((byte)PacketTypes.SettingsChange);
outmsg.Write((Int16)difficulty);
outmsg.Write((Int16)healthmod);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, 0);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 5);
}

public void ChangeName(string name)
Expand All @@ -561,8 +580,9 @@ public void ChangeName(string name)
NetOutgoingMessage outmsg = new NetOutgoingMessage();
outmsg.Write((byte)PacketTypes.UpdateName);
outmsg.Write(name);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, 0);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 5);
AddConsoleMessage("Name change request sent to the server.");
gm.username = name;
}

// Update is called once per frame
Expand All @@ -588,6 +608,8 @@ void Update ()
// Server sent go ahead to auto start
ToggleReady(true);
gm.gamephase = 1;
if (newname != gm.username)
ChangeName(newname);
print("Received ok to start game intro");
}
break;
Expand Down Expand Up @@ -631,35 +653,21 @@ void Update ()
break;
case (byte)PacketTypes.AssignId:
{
if (ClientId > 0 && username != "")
{
//We were previously connected before. Re-submit our namechange request.
ChangeName(username);
}

ClientId = inc.ReadInt32();

if (username == "")
{
username = "Player " + ClientId;
newname = username;
}

isConnected = true;
//lastBeat = Time.time;

AddConsoleMessage("Server assigned you an id of " + ClientId + ".");
ChangeName(newname);
}
break;
case (byte)PacketTypes.AddPlayer:
{
int playerid = inc.ReadInt16();
print("Got ID "+playerid);
float x = inc.ReadFloat();
float y = inc.ReadFloat();
string name = inc.ReadString();
Vector3 position = new Vector3(x, 15, y);
Debug.Log("Adding player " + playerid + " to scene.");
Debug.Log("Adding player " +name+" ("+playerid+") to scene.");

if (Players.Exists(play => play.Id == playerid)) return;

Expand Down Expand Up @@ -687,14 +695,30 @@ void Update ()
//if (gm.gamephase == 0) break;

PlayerObject player = Players.Find(p => p.Id == playerid);
if (player==null) break;
if (player == null) break;
Debug.Log("Player " + player.Name+" ("+playerid+") had disconnected");
PlayerController pc = player.Obj.GetComponent<PlayerController>();
bool status = Players.Remove(player);
Debug.Log("Removing player " + playerid +" "+status);
Destroy(pc.playername);
Destroy(player.Obj);
}
break;
case (byte)PacketTypes.UpdateName:
{
//A Player changed their name.
string namechange = inc.ReadString();
int playerid = inc.ReadInt16();

//SendLobbyMessage("Server", player.Name + " (Id"+player.Id+") changed their name to '" + newname + "'.");
PlayerObject player = Players.Find(p => p.Id == playerid);
if (player == null) break;
Debug.Log(player.Name + " (Id"+playerid+") changed their name to '" + namechange + "'.");
player.Name = namechange;
var pn = player.Controller.playername;
if (pn == null) break;
pn.guiText.text = player.Name;
}
break;
case (byte)PacketTypes.Beat:
{
Expand All @@ -713,10 +737,7 @@ void Update ()
}
outmsg.Write((float)Enemy.transform.position.x);
outmsg.Write((float)Enemy.transform.position.z);
client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 0);//0
//AddConsoleMessage("Client responded to a server sync message.");
//lastBeat = Time.time;

client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered, 4);
roundtriptime = inc.ReadFloat();
}
break;
Expand All @@ -736,7 +757,7 @@ void Update ()

int specialType = inc.ReadInt16();

Debug.Log("Got special action request for player " + playerid);
Debug.Log("Got special action request for player " + playerid + " ("+player.Name+")");

if(playerid != ClientId)
player.Controller.UseSpecial(specialType);
Expand Down
Binary file modified Unity/Library/ScriptAssemblies/Assembly-CSharp.dll
Binary file not shown.
Binary file modified Unity/Library/ScriptAssemblies/Assembly-CSharp.dll.mdb
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Unity/Library/ScriptAssemblies/Assembly-UnityScript.dll
Binary file not shown.
Binary file modified Unity/Library/ScriptAssemblies/Assembly-UnityScript.dll.mdb
Binary file not shown.
Binary file modified Unity/Library/assetDatabase3
Binary file not shown.
Binary file modified Unity/Library/cache/fb/fb6a21dd44031924da28cc5bfd53e6b7
Binary file not shown.
17 changes: 13 additions & 4 deletions Unity/Unity.userprefs
@@ -1,15 +1,24 @@
<Properties GitUserInfo="UsingGIT">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release" />
<MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/Scripts/NetworkManager.cs">
<Files>
<File FileName="Assets/Scripts/NetworkManager.cs" Line="647" Column="60" />
<File FileName="Assets/Scripts/OpenScene/OpenSceneMgr.cs" Line="28" Column="53" />
<File FileName="Assets/Scripts/OpenScene/GameManager.cs" Line="10" Column="32" />
<File FileName="Assets/Scripts/CameraController.cs" Line="204" Column="6" />
<File FileName="Assets/Scripts/CrabBehavior.cs" Line="106" Column="1" />
<File FileName="Assets/Scripts/CrabManager.cs" Line="931" Column="18" />
<File FileName="Assets/Scripts/Scores.cs" Line="81" Column="80" />
</Files>
<Pads>
<Pad Id="ProjectPad">
<State expanded="True">
<Node name="Assembly-CSharp" expanded="True">
<Node name="Assets" expanded="True">
<Node name="3rdParty" expanded="True" />
<Node name="Scripts" expanded="True">
<Node name="OpenScene" expanded="True">
<Node name="OpenSceneMgr.cs" selected="True" />
</Node>
<Node name="OpenScene" expanded="True" />
<Node name="NetworkManager.cs" selected="True" />
</Node>
</Node>
</Node>
Expand Down

0 comments on commit 5279cc3

Please sign in to comment.