Exemplo n.º 1
0
    public static void Invoke(NetworkUser user, StatsUpdateList updateList)
    {
        string json = JsonUtility.ToJson(updateList);

        // string json = JsonConvert.SerializeObject(updateList, Formatting.Indented);
        _instance.TargetReceiveUpdate(user.connectionToClient, json);
    }
Exemplo n.º 2
0
        public void StatsUpdateTestSerializeJson()
        {
            DateTime now  = DateTime.Now;
            DateTime then = DateTime.Now;

            then.AddMinutes(10);
            TimeSpan span = then - now;

            // serialize stats update
            StatsUpdate stat = new StatsUpdate(1, "Player 1", span, 11, 111);
            string      json = JsonUtility.ToJson(stat);

            // string json = JsonConvert.SerializeObject(stat, Formatting.Indented);

            Assert.IsTrue(json.Length > 0);
            StatsUpdate statNew = JsonUtility.FromJson <StatsUpdate>(json);

            // StatsUpdate statNew = JsonConvert.DeserializeObject<StatsUpdate>(json);
            if (stat != statNew)
            {
                Console.WriteLine("Old stats:\n" + stat.ConvertToDisplayString(false) +
                                  "\n not equal to \nNew stats:\n" + statNew.ConvertToDisplayString(false) +
                                  "\nJson:\n" + json);
                Assert.Fail();
            }

            // Serialize stats update list
            StatsUpdateList statsList = new StatsUpdateList();

            statsList.Add(stat);
            statsList.Add(new StatsUpdate(2, "Player 2", span, 12, 112));
            statsList.Add(new StatsUpdate(3, "Player 3", span, 13, 113));
            statsList.Add(new StatsUpdate(4, "Player 4", span, 14, 114));

            json = JsonUtility.ToJson(statsList);
            // json = JsonConvert.SerializeObject(statsList, Formatting.Indented);

            Assert.IsTrue(json.Length > 0);
            StatsUpdateList statsListNew = JsonUtility.FromJson <StatsUpdateList>(json);

            //StatsUpdateList statsListNew = JsonConvert.DeserializeObject<StatsUpdateList>(json);

            if (!statsList.Equals(statsListNew))
            {
                Console.WriteLine("Old stats:\n" + statsList.BuildUpdateStringOneLine() +
                                  "\n not equal to \nNew stats:\n" + statsListNew.BuildUpdateStringOneLine() +
                                  " Json:" + json);
                Assert.Fail();
            }
        }
Exemplo n.º 3
0
        public void PullSurvivorStatsTest()
        {
            // Do a simple pull from a new list. Expect success.
            StatsPullerTest statsPuller = new StatsPullerTest();

            statsPuller.statsList = new StatsUpdateList();
            DateTime now  = DateTime.Now;
            DateTime then = DateTime.Now;

            then.AddMinutes(10);
            TimeSpan span = then - now;

            statsPuller.statsList.Add(new StatsUpdate(1, "Player 1", span, 11, 111));
            statsPuller.statsList.Add(new StatsUpdate(2, "Player 2", span, 12, 112));
            statsPuller.statsList.Add(new StatsUpdate(3, "Player 3", span, 3, 113));
            statsPuller.statsList.Add(new StatsUpdate(4, "Player 4", span, 14, 114));

            StatsUpdateList newStats = statsPuller.PullSurvivorStats(true);

            if (statsPuller.CompareStats(newStats) == false)
            {
                Console.WriteLine("Stats don't compare. Old:" + statsPuller.statsList.BuildUpdateString() +
                                  " New:" + newStats.BuildUpdateString());
                Assert.Fail();
            }

            // Modify the list, pull, and compare. Expect success.
            statsPuller.statsList.Clear();
            statsPuller.statsList.Add(new StatsUpdate(1, "Player 1", span, 21, 121));
            statsPuller.statsList.Add(new StatsUpdate(2, "Player 2", span, 22, 122));
            statsPuller.statsList.Add(new StatsUpdate(3, "Player 3", span, 23, 123));
            statsPuller.statsList.Add(new StatsUpdate(4, "Player 4", span, 24, 124));

            newStats = statsPuller.PullSurvivorStats(true);
            if (statsPuller.CompareStats(newStats) == false)
            {
                Console.WriteLine("Stats don't compare. Old:" + statsPuller.statsList.BuildUpdateString() +
                                  " New:" + newStats.BuildUpdateString());
                Assert.Fail();
            }

            // Modify the list and compare. Expect failure
            statsPuller.statsList.Add(new StatsUpdate(1, "Player 5", span, 21, 121));
            if (statsPuller.CompareStats(newStats) == true)
            {
                Console.WriteLine("Stats compare and they shouldn't");
                Assert.Fail();
            }
        }
Exemplo n.º 4
0
    public static void ServerSendStatsUpdate(StatsUpdateList updateList)
    {
        // Before we can Invoke our NetworkMessage, we need to make sure our centralized network object is spawned.
        // For doing that, we Instantiate the CentralNetworkObject, we obviously check if we don't already have one that is already instantiated and activated in the current scene.
        // Note : Make sure you instantiate the gameobject, and not spawn it directly, it would get deleted otherwise on scene change, even with DontDestroyOnLoad.

        if (!_centralNetworkObjectSpawned)
        {
            LogHelper.Log(LogTarget.Init, "Creating centralized network object");
            _centralNetworkObjectSpawned = GameObject.Instantiate(CentralNetworkObject);
            NetworkServer.Spawn(_centralNetworkObjectSpawned);
        }

        foreach (NetworkUser user in NetworkUser.readOnlyInstancesList)
        {
            StatsUpdateNetworkComponent.Invoke(user, updateList);
        }
    }
Exemplo n.º 5
0
    private void TargetReceiveUpdate(NetworkConnection target, string updateJson)
    {
        if (NetworkServer.active)
        {
            return;
        }

        StatsUpdateList statsList = JsonUtility.FromJson <StatsUpdateList>(updateJson);

        // StatsUpdateList statsList = JsonConvert.DeserializeObject<StatsUpdateList>(updateJson);
        updateListCache.Clear();

        LogHelper.Log(LogTarget.Networking, "Received StatsUpdateList from server: " + statsList.BuildUpdateStringOneLine());
        LogHelper.Log(LogTarget.Networking, "Update json from server: " + updateJson);
        foreach (StatsUpdate update in statsList.statsUpdateList)
        {
            updateListCache.Add(update);
        }
    }