Skip to content

Commit

Permalink
v0.1.2: Asynchronous master server connections, largefont, traitor mo…
Browse files Browse the repository at this point in the history
…de ends when traitor dies or sub reaches end of level
  • Loading branch information
Regalis committed Aug 13, 2015
1 parent 5771bc7 commit bc4ea09
Show file tree
Hide file tree
Showing 23 changed files with 376 additions and 97 deletions.
7 changes: 7 additions & 0 deletions Subsurface/Content/UI/style.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@
<Sprite texture="Content\UI\uiBackground.png" size="0.0, 0.0" sourcerect ="0.0, 90.0, 0.0, 100.0"/>
</GUITextBox>

<GUITickBox
color="0.5, 0.5, 0.5, 1.0"

outlinecolor="0.5, 0.57, 0.6, 1.0">
<Sprite texture="Content\UI\uiBackground.png" size="0.0, 0.0" sourcerect ="0.0, 90.0, 0.0, 100.0"/>
</GUITickBox>

<GUIMessageBox
padding="40.0, 40.0, 40.0, 40.0"
color="1.0, 1.0, 1.0, 1.0"
Expand Down
4 changes: 2 additions & 2 deletions Subsurface/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.1.0")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyVersion("0.1.2.0")]
[assembly: AssemblyFileVersion("0.1.2.0")]
4 changes: 2 additions & 2 deletions Subsurface/Source/Characters/CharacterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public List<int> PickedItemIDs
{
get { return pickedItems; }
}

public Sprite HeadSprite
{
get
Expand Down Expand Up @@ -156,7 +156,7 @@ private void LoadHeadSprite()
break;
}
}

public GUIFrame CreateInfoFrame(Rectangle rect)
{
GUIFrame frame = new GUIFrame(rect, Color.Transparent);
Expand Down
3 changes: 3 additions & 0 deletions Subsurface/Source/DebugConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ public static void ExecuteCommand(string command, Game1 game)
hull.OxygenPercentage = 100.0f;
}
break;
case "tutorial":
TutorialMode.Start();
break;
case "lobbyscreen":
case "lobby":
Game1.LobbyScreen.Select();
Expand Down
2 changes: 1 addition & 1 deletion Subsurface/Source/GUI/GUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GUI
public static GUIStyle style;

static Texture2D t;
public static SpriteFont Font, SmallFont;
public static SpriteFont Font, SmallFont, LargeFont;

private static GraphicsDevice graphicsDevice;

Expand Down
8 changes: 8 additions & 0 deletions Subsurface/Source/GUI/GUIListBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class GUIListBox : GUIComponent

private bool enabled;

public GUIComponent Selected
{
get
{
return selected;
}
}

public object SelectedData
{
get
Expand Down
3 changes: 2 additions & 1 deletion Subsurface/Source/GUI/GUITextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ public GUITextBlock(Rectangle rect, string text, GUIStyle style, GUIComponent pa
}


public GUITextBlock(Rectangle rect, string text, GUIStyle style, Alignment alignment = Alignment.TopLeft, Alignment textAlignment = Alignment.TopLeft, GUIComponent parent = null, bool wrap = false)
public GUITextBlock(Rectangle rect, string text, GUIStyle style, Alignment alignment = Alignment.TopLeft, Alignment textAlignment = Alignment.TopLeft, GUIComponent parent = null, bool wrap = false, SpriteFont font =null)
: this (rect, text, null, null, alignment, textAlignment, style, parent, wrap)
{
this.Font = font == null ? GUI.Font : font;
}

public GUITextBlock(Rectangle rect, string text, Color? color, Color? textColor, Alignment textAlignment = Alignment.Left, GUIStyle style = null, GUIComponent parent = null, bool wrap = false)
Expand Down
10 changes: 10 additions & 0 deletions Subsurface/Source/GUI/GUITickBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public bool Selected
}
}

public bool Enabled
{
get;
set;
}

public GUITickBox(Rectangle rect, string label, Alignment alignment, GUIComponent parent)
: base(null)
{
Expand All @@ -36,12 +42,16 @@ public GUITickBox(Rectangle rect, string label, Alignment alignment, GUIComponen
box.SelectedColor = Color.DarkGray;

text = new GUITextBlock(new Rectangle(rect.X + 40, rect.Y, 200, 30), label, Color.Transparent, Color.White, Alignment.TopLeft, null, this);

Enabled = true;
}

public override void Update(float deltaTime)
{
base.Update(deltaTime);

if (!Enabled) return;

if (box.Rect.Contains(PlayerInput.GetMouseState.Position))
{
box.State = ComponentState.Hover;
Expand Down
1 change: 1 addition & 0 deletions Subsurface/Source/Game1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ private IEnumerable<object> Load()
{
GUI.Font = ToolBox.TryLoadFont("SpriteFont1", Content);
GUI.SmallFont = ToolBox.TryLoadFont("SmallFont", Content);
GUI.LargeFont = ToolBox.TryLoadFont("LargeFont", Content);

sw = new Stopwatch();

Expand Down
40 changes: 30 additions & 10 deletions Subsurface/Source/GameSession/GameModes/TraitorMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,7 @@ public override void Update(float deltaTime)
base.Update(deltaTime);

if (!isRunning) return;

if (DateTime.Now >= endTime)
{
string endMessage = traitor.character.Info.Name + " was a traitor! ";
endMessage += (traitor.character.Info.Gender == Gender.Male) ? "His" : "Her";
endMessage += " task was to assassinate " + target.character.Info.Name + ". The task was unsuccesful.";
End(endMessage);
return;
}



if (traitor==null || target ==null)
{
Expand Down Expand Up @@ -68,6 +59,35 @@ public override void Update(float deltaTime)
endMessage += " task was to assassinate " + target.character.Info.Name + ". The task was succesful.";
End(endMessage);
}
else if (traitor.character.IsDead)
{
string endMessage = traitor.character.Info.Name + " was a traitor! ";
endMessage += (traitor.character.Info.Gender == Gender.Male) ? "His" : "Her";
endMessage += " task was to assassinate " + target.character.Info.Name + ". ";
endMessage += "The task was unsuccessful - the has submarine reached its destination.";
End(endMessage);
return;
}
else if (Level.Loaded.AtEndPosition)
{
string endMessage = traitor.character.Info.Name + " was a traitor! ";
endMessage += (traitor.character.Info.Gender == Gender.Male) ? "His" : "Her";
endMessage += " task was to assassinate " + target.character.Info.Name + ", but ";
endMessage += (traitor.character.Info.Gender == Gender.Male) ? "he" : "she";
endMessage += " got " + ((traitor.character.Info.Gender == Gender.Male) ? "himself" : "herself");
endMessage += " killed before completing it.";
End(endMessage);
return;
}
else if (DateTime.Now >= endTime)
{
string endMessage = traitor.character.Info.Name + " was a traitor! ";
endMessage += (traitor.character.Info.Gender == Gender.Male) ? "His" : "Her";
endMessage += " task was to assassinate " + target.character.Info.Name + ". The task was unsuccesful.";
End(endMessage);
return;
}

}
}
}
Expand Down
10 changes: 5 additions & 5 deletions Subsurface/Source/Items/CharacterInventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ public override bool TryPutItem(Item item, int i, bool createNetworkEvent)
//PutItem(item, i, false, false);
combined = true;
}
else if (items[i].Combine(item))
{
//PutItem(items[i], i, false, false);
combined = true;
}
//else if (items[i].Combine(item))
//{
// //PutItem(items[i], i, false, false);
// combined = true;
//}

if (!combined) return false;

Expand Down
2 changes: 1 addition & 1 deletion Subsurface/Source/Items/Components/Projectile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private bool OnProjectileCollision(Fixture f1, Fixture f2, Contact contact)
foreach (Item contained in item.ContainedItems)
{
contained.Condition = 0.0f;
if (contained.body!=null)
if (contained.body != null)
{
contained.body.SetTransform(item.SimPosition, contained.body.Rotation);
}
Expand Down
2 changes: 1 addition & 1 deletion Subsurface/Source/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public Item[] ContainedItems
get
{
ItemContainer c = GetComponent<ItemContainer>();
return (c == null) ? null : c.inventory.items;
return (c == null) ? null : Array.FindAll(c.inventory.items, i=>i!=null);
}
}

Expand Down
6 changes: 3 additions & 3 deletions Subsurface/Source/Networking/GameClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ public GameClient(string newName)

}

public void ConnectToServer(string hostIP)
public void ConnectToServer(string hostIP, string password = "")
{

string[] address = hostIP.Split(':');
if (address.Length==1)
{
Expand All @@ -65,7 +64,7 @@ public void ConnectToServer(string hostIP)

if (!int.TryParse(address[1], out Port))
{
DebugConsole.ThrowError("Invalid port: address[1]!");
DebugConsole.ThrowError("Invalid port: "+address[1]+"!");
Port = DefaultPort;
}
}
Expand All @@ -85,6 +84,7 @@ public void ConnectToServer(string hostIP)
client.Start();

outmsg.Write((byte)PacketTypes.Login);
outmsg.Write(password);
outmsg.Write(Game1.Version.ToString());
outmsg.Write(Game1.SelectedPackage.Name);
outmsg.Write(Game1.SelectedPackage.MD5hash.Hash);
Expand Down
71 changes: 60 additions & 11 deletions Subsurface/Source/Networking/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ class GameServer : NetworkMember
private TimeSpan refreshMasterInterval = new TimeSpan(0, 0, 40);
private DateTime refreshMasterTimer;

private bool masterServerResponded;

private bool registeredToMaster;

private string password;

private Client myClient;

public GameServer(string name, int port)
public GameServer(string name, int port, bool isPublic = false, string password="")
{
var endRoundButton = new GUIButton(new Rectangle(Game1.GraphicsWidth - 290, 20, 150, 25), "End round", Alignment.TopLeft, GUI.style, inGameHUD);
endRoundButton.OnClicked = EndButtonHit;

this.name = name;

this.password = password;

config = new NetPeerConfiguration("subsurface");

//config.SimulatedLoss = 0.2f;
Expand Down Expand Up @@ -62,7 +68,11 @@ public GameServer(string name, int port)
DebugConsole.ThrowError("Couldn't start the server", e);
}

RegisterToMasterServer();
if (isPublic)
{
RegisterToMasterServer();
}


updateInterval = new TimeSpan(0, 0, 0, 0, 30);

Expand All @@ -78,6 +88,7 @@ private void RegisterToMasterServer()
request.AddParameter("servername", name);
request.AddParameter("serverport", Port);
request.AddParameter("playercount", PlayerCountToByte(connectedClients.Count, config.MaximumConnections));
request.AddParameter("password", string.IsNullOrWhiteSpace(password) ? 0 : 1);

// execute the request
RestResponse response = (RestResponse)client.Execute(request);
Expand All @@ -98,7 +109,7 @@ private void RegisterToMasterServer()
refreshMasterTimer = DateTime.Now + refreshMasterInterval;
}

private void RefreshMaster()
private IEnumerable<object> RefreshMaster()
{
var client = new RestClient(NetworkMember.MasterServerUrl);

Expand All @@ -112,17 +123,49 @@ private void RefreshMaster()
var sw = new Stopwatch();
sw.Start();

RestResponse response = (RestResponse)client.Execute(request);
masterServerResponded = false;
var restRequestHandle = client.ExecuteAsync(request, response => MasterServerCallBack(response));

sw.Stop();
DateTime timeOut = DateTime.Now + new TimeSpan(0, 0, 10);
while (!masterServerResponded)
{
if (DateTime.Now > timeOut)
{
restRequestHandle.Abort();
DebugConsole.ThrowError("Couldn't connect to master server (request timed out)");
registeredToMaster = false;
}
System.Diagnostics.Debug.WriteLine("took "+sw.ElapsedMilliseconds+" ms");

yield return Status.Running;
}

if (response.StatusCode != System.Net.HttpStatusCode.OK)


yield return Status.Success;




}

private void MasterServerCallBack(IRestResponse response)
{
masterServerResponded = true;

if (response.ErrorException != null)
{
DebugConsole.ThrowError("Error while connecting to master server (" +response.StatusCode+": "+response.StatusDescription+")");
DebugConsole.ThrowError("Error while connecting to master server", response.ErrorException);
registeredToMaster = false;
return;
}


if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
DebugConsole.ThrowError("Error while connecting to master server (" + response.StatusCode + ": " + response.StatusDescription + ")");
registeredToMaster = false;
return;
}
}

public override void Update(float deltaTime)
Expand Down Expand Up @@ -159,7 +202,7 @@ public override void Update(float deltaTime)

if (registeredToMaster && refreshMasterTimer < DateTime.Now)
{
RefreshMaster();
CoroutineManager.StartCoroutine(RefreshMaster());

refreshMasterTimer = DateTime.Now + refreshMasterInterval;
}
Expand Down Expand Up @@ -204,9 +247,10 @@ private void ReadMessage(NetIncomingMessage inc)
Client existingClient = connectedClients.Find(c=> c.Connection == inc.SenderConnection);
if (existingClient==null)
{
string version = "", packageName="", packageHash="", name = "";
string userPassword = "", version = "", packageName="", packageHash="", name = "";
try
{
userPassword = inc.ReadString();
version = inc.ReadString();
packageName = inc.ReadString();
packageHash = inc.ReadString();
Expand All @@ -218,7 +262,12 @@ private void ReadMessage(NetIncomingMessage inc)
break;
}

if (version != Game1.Version.ToString())
if (userPassword != password)
{
inc.SenderConnection.Deny("Wrong password!");
break;
}
else if (version != Game1.Version.ToString())
{
inc.SenderConnection.Deny("Subsurface version " + Game1.Version + " required to connect to the server (Your version: " + version + ")");
break;
Expand Down
Loading

0 comments on commit bc4ea09

Please sign in to comment.