Skip to content

Commit

Permalink
Merge branch 'release/v1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Apr 26, 2017
2 parents 2a661e2 + 366e8c1 commit e6974fb
Show file tree
Hide file tree
Showing 42 changed files with 439 additions and 186 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.jslib
9 changes: 9 additions & 0 deletions Assets/Plugins/WebSocket.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ SocketCreate: function(url)
var array = new Uint8Array(e.data);
socket.messages.push(array);
}
else if(typeof e.data === "string") {
var reader = new FileReader();
reader.addEventListener("loadend", function() {
var array = new Uint8Array(reader.result);
socket.messages.push(array);
});
var blob = new Blob([e.data]);
reader.readAsArrayBuffer(blob);
}
};

socket.socket.onclose = function (e) {
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace AssemblyCSharp
{
public static class Constants
{
public const string kServerHostname = "52.59.156.131";
public const int kServerPort = 41234;
public const string kServerHostname = "sardines-ngj2017.herokuapp.com";
public const int kServerPort = 443;
public const int kClientPort = 41235;
public const int kGameStateUpdateTickMs = 100;
public const int kGameStateUpdateTickMs = 250;
public const int kSecondsBetweenFirstHideAndRoundEnd = 10;
public const int kSecondsBetweenRoundEndAndNextRoundStart = 5;
public const int kNumHidingPlaces = 10;
Expand Down
67 changes: 27 additions & 40 deletions Assets/Scripts/DummyPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public void EndTitleScreen() {
}

void Update () {
QueryUDPConnections ();
QueryWebSocketConnections ();
ReadAndHandleReceivedMessages ();
}

IEnumerator BackgroundSendGameStateToServerTask() {
Expand All @@ -102,6 +101,8 @@ private void HandleServerToClientHelloMessage(ServerToClientHelloMessage message
if (hidingPlace) {
hidingPlace.SetHidingPlaceIndex (message.hidingPlace);
}

status = Status.waitingForPlayers;
}

private void HandleServerToClientStartMessage(ServerToClientStartMessage message) {
Expand Down Expand Up @@ -129,31 +130,28 @@ private void HandleServerToClientStartMessage(ServerToClientStartMessage message
}
}

private void QueryWebSocketConnections() {
string serverMessage;
private void ReadAndHandleReceivedMessages() {
Message serverMessage;

if (status == Status.titleScreen) {
// don't process websockets
return;
}

if (serverCommunication.TryGetServerWebSocketMessage (out serverMessage)) {
Debug.Log ("Server sent WS : " + serverMessage);

if (serverMessage.Contains ("ServerToClientHelloMessage")) {
Debug.Log ("Setting ID and position");
ServerToClientHelloMessage message = JsonUtility.FromJson<ServerToClientHelloMessage> (serverMessage);
HandleServerToClientHelloMessage (message);
status = Status.waitingForPlayers;
connectingScreen.enabled = false;
waitingForPlayersScreen.enabled = true;
winScreen.enabled = false;
loseScreen.enabled = false;
if (serverCommunication.TryGetReceivedMessage (out serverMessage)) {
var serverToClientHelloMessage = serverMessage as ServerToClientHelloMessage;
if (serverToClientHelloMessage != null) {
HandleServerToClientHelloMessage (serverToClientHelloMessage);
}

var serverToClientStartMessage = serverMessage as ServerToClientStartMessage;
if (serverToClientStartMessage != null) {
HandleServerToClientStartMessage (serverToClientStartMessage);
}

else if (serverMessage.Contains("ServerToClientStartMessage")) {
ServerToClientStartMessage message = JsonUtility.FromJson<ServerToClientStartMessage> (serverMessage);
HandleServerToClientStartMessage (message);
var serverGameStateMessage = serverMessage as ServerGameStateMessage;
if (serverGameStateMessage != null) {
HandleServerGameStateMessage (serverGameStateMessage);
}
}
}
Expand All @@ -165,33 +163,22 @@ private void SendGameStateMessage()
return;
}

string text = JsonUtility.ToJson(
new ClientGameStateMessage(
id,
transform.position,
transform.forward,
GetComponent<Rigidbody>().velocity,
frozen,
gameId
)
var message = new ClientGameStateMessage(
id,
transform.position,
transform.forward,
GetComponent<Rigidbody>().velocity,
frozen,
gameId
);
serverCommunication.SendClientUdpMessage (text);
serverCommunication.SendClientMessage (message);
}

private void QueryUDPConnections()
{
ServerGameStateMessage message = serverCommunication.CheckForOtherClientGameStates ();
//Debug.Log (message);
if (message == null) {
private void HandleServerGameStateMessage(ServerGameStateMessage message) {
if (message.gameId != gameId) {
return;
}

if (message.gameId == gameId) {
ProcessOtherPlayers (message);
}
}

private void ProcessOtherPlayers(ServerGameStateMessage message) {
// Debug.Log ("Processing other players, message: " + message);
foreach (ClientGameStateMessage client in message.clients)
{
Expand Down
4 changes: 1 addition & 3 deletions Assets/Scripts/Messages/ClientGameStateMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
namespace AssemblyCSharp
{
[Serializable]
public class ClientGameStateMessage
public class ClientGameStateMessage : Message
{
public string type;
public int id;
public Vector3Serialisable playerPosition;
public Vector3Serialisable playerDirection;
Expand All @@ -25,7 +24,6 @@ public ClientGameStateMessage (int id, Vector3 position, Vector3 forwardDirectio
{
this.id = id;
this.gameId = gameId;
type = this.GetType().Name;
playerPosition = new Vector3Serialisable (position);
playerDirection = new Vector3Serialisable (forwardDirection);
playerVelocity = new Vector3Serialisable (velocity);
Expand Down
8 changes: 1 addition & 7 deletions Assets/Scripts/Messages/ClientHelloMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
namespace AssemblyCSharp
{
[Serializable]
public class ClientHelloMessage
public class ClientHelloMessage : Message
{
public string type;

public ClientHelloMessage ()
{
type = this.GetType().Name;
}
}
}

33 changes: 33 additions & 0 deletions Assets/Scripts/Messages/Message.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

using AssemblyCSharp;

namespace AssemblyCSharp
{
[Serializable]
public abstract class Message
{
public string type;

public Message() {
type = this.GetType().Name;
}

public string Serialize() {
return JsonUtility.ToJson (this);
}

public override string ToString() {
return Serialize ();
}
}
}

12 changes: 12 additions & 0 deletions Assets/Scripts/Messages/Message.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions Assets/Scripts/Messages/MessageParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

using AssemblyCSharp;

namespace AssemblyCSharp
{
public class MessageParser
{
private class TypeMessage : Message {

}

public Message Parse(string str) {
var typeMessage = JsonUtility.FromJson<TypeMessage> (str);
switch (typeMessage.type) {
case "ClientGameStateMessage":
return JsonUtility.FromJson<ClientGameStateMessage> (str);
case "ClientHelloMessage":
return JsonUtility.FromJson<ClientHelloMessage> (str);
case "ServerGameStateMessage":
return JsonUtility.FromJson<ServerGameStateMessage> (str);
case "ServerToClientHelloMessage":
return JsonUtility.FromJson<ServerToClientHelloMessage> (str);
case "ServerToClientStartMessage":
return JsonUtility.FromJson<ServerToClientStartMessage> (str);
default:
Debug.LogError ("Unknown message type!");
return null;
}
}
}
}

12 changes: 12 additions & 0 deletions Assets/Scripts/Messages/MessageParser.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Assets/Scripts/Messages/ServerGameStateMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
namespace AssemblyCSharp
{
[Serializable]
public class ServerGameStateMessage
public class ServerGameStateMessage: Message
{
public List<ClientGameStateMessage> clients;
public string type;
public int gameId;
}
}
Expand Down
3 changes: 1 addition & 2 deletions Assets/Scripts/Messages/ServerToClientHelloMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace AssemblyCSharp
{
[Serializable]
public class ServerToClientHelloMessage
public class ServerToClientHelloMessage : Message
{
public string type;
public int id;
public int gameId;
public Vector3Serialisable playerPosition;
Expand Down
4 changes: 1 addition & 3 deletions Assets/Scripts/Messages/ServerToClientStartMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
namespace AssemblyCSharp
{
[Serializable]
public class ServerToClientStartMessage
public class ServerToClientStartMessage : Message
{
public int gameId;
public string type;
public int hidingPlace;
public Vector3Serialisable playerPosition;

public ServerToClientStartMessage (int gameId, int hidingPlace, Vector3Serialisable playerPosition)
{
this.gameId = gameId;
type = this.GetType().Name;
this.hidingPlace = hidingPlace;
this.playerPosition = playerPosition;
}
Expand Down
Loading

0 comments on commit e6974fb

Please sign in to comment.