Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 3cb70579

Přidáno uživatelem Zuzana Káčereková před více než 3 roky(ů)

Added communication model and XML serialization

Zobrazit rozdíly:

Client/Client/Assets/NetworkingTest.cs
1
using System.Collections;
2
using System.Collections.Generic;
3
using UnityEngine;
4

  
5
public class NetworkingTest : MonoBehaviour
6
{
7
    // Start is called before the first frame update
8
    void Start()
9
    {
10
        Response response = Response.Randomize();
11
        var xml = XmlCommunication.Serialize(response);
12
        Debug.Log(xml);
13

  
14
        Request request = Request.Randomize();
15
        xml = XmlCommunication.Serialize(request);
16
        Debug.Log(xml);
17
    }
18

  
19
}
Client/Client/Assets/NetworkingTest.cs.meta
1
fileFormatVersion: 2
2
guid: 660804ddf1eaa76418a412f568b07b8c
3
MonoImporter:
4
  externalObjects: {}
5
  serializedVersion: 2
6
  defaultReferences: []
7
  executionOrder: 0
8
  icon: {instanceID: 0}
9
  userData: 
10
  assetBundleName: 
11
  assetBundleVariant: 
Client/Client/Assets/Scenes/WebGL.unity
9971 9971
  m_Script: {fileID: 11500000, guid: ac0fb3ff0286f974397f0b6b2c3c10ac, type: 3}
9972 9972
  m_Name: 
9973 9973
  m_EditorClassIdentifier: 
9974
  buildings: []
9975 9974
--- !u!114 &1446501681
9976 9975
MonoBehaviour:
9977 9976
  m_ObjectHideFlags: 0
......
10309 10308
  serializedVersion: 6
10310 10309
  m_Component:
10311 10310
  - component: {fileID: 1478102839}
10311
  - component: {fileID: 1478102840}
10312 10312
  m_Layer: 0
10313 10313
  m_Name: NetworkManager
10314 10314
  m_TagString: Untagged
10315 10315
  m_Icon: {fileID: 0}
10316 10316
  m_NavMeshLayer: 0
10317 10317
  m_StaticEditorFlags: 0
10318
  m_IsActive: 0
10318
  m_IsActive: 1
10319 10319
--- !u!4 &1478102839
10320 10320
Transform:
10321 10321
  m_ObjectHideFlags: 0
......
10330 10330
  m_Father: {fileID: 0}
10331 10331
  m_RootOrder: 1
10332 10332
  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
10333
--- !u!114 &1478102840
10334
MonoBehaviour:
10335
  m_ObjectHideFlags: 0
10336
  m_CorrespondingSourceObject: {fileID: 0}
10337
  m_PrefabInstance: {fileID: 0}
10338
  m_PrefabAsset: {fileID: 0}
10339
  m_GameObject: {fileID: 1478102838}
10340
  m_Enabled: 1
10341
  m_EditorHideFlags: 0
10342
  m_Script: {fileID: 11500000, guid: 660804ddf1eaa76418a412f568b07b8c, type: 3}
10343
  m_Name: 
10344
  m_EditorClassIdentifier: 
10333 10345
--- !u!1 &1483322681
10334 10346
GameObject:
10335 10347
  m_ObjectHideFlags: 0
Client/Client/Assets/Scripts/Model/Buildings.cs
4 4

  
5 5
public class Buildings : MonoBehaviour
6 6
{
7
    public string[] buildings;
7
    public static string[] buildings = new string[] { 
8
        "FST+FEK", "FDU", "FAV", "FEL", "REK", "MENZA", "LIB", "CIV", "UNI14", 
9
        "DOM", "HUS", "CHOD", "JUNG", "KLAT", "KOLL", "RIEG", "SADY", "SED+VEL", 
10
        "TES", "TYL", "KARMA", "KBORY", "KLOCH", "KKLAT" };
11

  
8 12

  
9 13
    // Start is called before the first frame update
10 14
    void Start()
11 15
    {
12
        TextAsset file = Resources.Load<TextAsset>("buildings");
13
        string[] split = file.ToString().Split('\n');
14
        List<string> buildingsTMP = new List<string>();
15

  
16
        foreach (string s in split)
17
        {
18
            string trimmed = s.Trim();
19
            if (trimmed.Length > 0)
20
                buildingsTMP.Add(trimmed);
21
        }
22

  
23
        buildings = buildingsTMP.ToArray();
16
        // TextAsset file = Resources.Load<TextAsset>("buildings");
17
        // string[] split = file.ToString().Split('\n');
18
        // List<string> buildingsTMP = new List<string>();
19
        // 
20
        // foreach (string s in split)
21
        // {
22
        //     string trimmed = s.Trim();
23
        //     if (trimmed.Length > 0)
24
        //         buildingsTMP.Add(trimmed);
25
        // }
26
        // 
27
        // buildings = buildingsTMP.ToArray();
24 28
    }
25 29
}
Client/Client/Assets/Scripts/Model/Date.cs
1 1
using System.Collections;
2 2
using System.Collections.Generic;
3 3
using UnityEngine;
4
using System.Xml.Serialization;
5
using System;
4 6

  
5
public class Date
7
public class Date : IComparable<Date>
6 8
{
9
    [XmlIgnore]
10
    public static int MIN_HOUR = 7;
11
    [XmlIgnore]
12
    public static int MAX_HOUR = 18;
13

  
14
    [XmlElement]
7 15
    public int day;
16
    
17
    [XmlElement]
8 18
    public int month;
19
    
20
    [XmlElement]
9 21
    public int year;
22

  
23
    [XmlElement]
10 24
    public int hour;
25

  
26
    public static Date Randomize()
27
    {
28
        Date d = new Date();
29

  
30
        d.day = DateTime.Now.Day;
31
        d.month = DateTime.Now.Month;
32
        d.year = DateTime.Now.Year;
33
        d.hour = UnityEngine.Random.Range(7, 18);
34

  
35
        return d;
36
    }
37

  
38
    public Date Clone()
39
    {
40
        Date clone = new Date();
41
        clone.day = day;
42
        clone.month = month;
43
        clone.year = year;
44
        clone.hour = hour;
45
        return clone;
46
    }
47

  
48
    public int CompareTo(Date other)
49
    {
50
        if (year < other.year) return -1;
51
        if (year > other.year) return 1;
52

  
53
        if (month < other.month) return -1;
54
        if (month > other.month) return 1;
55

  
56
        if (day < other.day) return -1;
57
        if (day > other.day) return 1;
58

  
59
        if (hour < other.hour) return -1;
60
        if (hour > other.hour) return 1;
61

  
62
        return 0;
63
    }
11 64
}
Client/Client/Assets/Scripts/Model/DateTimeManager.cs
36 36
        currentDate.hour = DateTime.Now.Hour;
37 37
        day = DateTime.Now.DayOfWeek;
38 38

  
39
        if (startDate == null) startDate = currentDate;
40
        if (endDate == null) endDate = startDate;
41

  
39 42
        foreach (var observer in observers)
40 43
            observer.Notify(startDate, endDate);
41 44
    }
Client/Client/Assets/Scripts/Model/Prediction.cs
1 1
using System.Collections;
2 2
using System.Collections.Generic;
3 3
using UnityEngine;
4
using System.Xml.Serialization;
4 5

  
5 6
public class Prediction
6 7
{
8
    [XmlElement]
7 9
    public Date dateTime;
10

  
11
    [XmlArray("rushLevels")]
12
    [XmlArrayItem("rushLevel")]
8 13
    public double[] predictions;
9 14
}
Client/Client/Assets/Scripts/Model/Request.cs
1 1
using System.Collections;
2 2
using System.Collections.Generic;
3 3
using UnityEngine;
4
using System.Xml.Serialization;
5
using System;
4 6

  
7
[XmlRoot("Request")]
5 8
public class Request
6 9
{
10
    [XmlElement]
7 11
    public bool useWeather;
8 12

  
13

  
14
    [XmlElement]
9 15
    public double temperature;
16
    
17
    [XmlElement]
10 18
    public double wind;
19
    
20
    [XmlElement]
11 21
    public double rain;
22

  
23
    [XmlElement]
12 24
    public WeatherCondition weather;
13 25

  
26

  
27
    [XmlElement]
14 28
    public bool useEndDate;
15 29

  
30

  
31
    [XmlElement]
16 32
    public Date start;
33

  
34
    [XmlElement]
17 35
    public Date end;
36

  
37

  
38
    public static Request Randomize()
39
    {
40
        Request r = new Request();
41

  
42
        r.useWeather = (UnityEngine.Random.Range(0f, 1f) < 0.5f) ? true : false;
43

  
44
        if (r.useWeather)
45
        {
46
            r.temperature = UnityEngine.Random.Range(0f, 40f);
47
            r.wind = UnityEngine.Random.Range(0f, 100f);
48
            r.rain = UnityEngine.Random.Range(0f, 100f);
49
            r.weather = (WeatherCondition)UnityEngine.Random.Range(0, 3);
50
        }
51

  
52
        r.start = Date.Randomize();
53

  
54
        r.useEndDate = (UnityEngine.Random.Range(0f, 1f) < 0.5f) ? true : false;
55

  
56
        if (r.useEndDate)
57
        {
58
            r.end = Date.Randomize();
59
            if (r.start.CompareTo(r.end) < 0)
60
            {
61
                Date tmp = r.start;
62
                r.start = r.end;
63
                r.end = tmp;
64
            }
65
            else if (r.start.CompareTo(r.end) == 0) 
66
            { 
67
                r.useEndDate = false;
68
            }
69
        }
70

  
71
        return r;
72
    }
73

  
18 74
}
Client/Client/Assets/Scripts/Model/Response.cs
1 1
using System.Collections;
2 2
using System.Collections.Generic;
3 3
using UnityEngine;
4
using System.Xml.Serialization;
5
using System.IO;
4 6

  
7
[XmlRoot("Response")]
5 8
public class Response
6 9
{
10
    [XmlElement]
7 11
    public int hoursPerSegment;
12

  
13
    [XmlArray("predictions")]
14
    [XmlArrayItem("prediction")]
8 15
    public Prediction[] predicitons;
16

  
17
    public static Response Randomize()
18
    {
19
        Response r = new Response();
20

  
21
        r.hoursPerSegment = 3;
22

  
23
        Date start = Date.Randomize();
24
        Date end = start.Clone();
25

  
26
        end.day += Random.Range(0, 2);
27
        end.hour += Random.Range(0, 18);
28

  
29
        // If end gets generated before start, swap them
30
        if (start.day == end.day && start.hour > end.hour)
31
        {
32
            Date tmp = start;
33
            start = end;
34
            end = tmp;
35
        }
36

  
37
        List<Prediction> predictionList = new List<Prediction>();
38
        Date curr = start;
39

  
40
        while (curr.CompareTo(end) <= 0)
41
        {
42
            Prediction p = new Prediction();
43

  
44
            p.dateTime = curr;
45
            p.predictions = new double[Buildings.buildings.Length];
46

  
47
            for (int i = 0; i < p.predictions.Length; i++)
48
                p.predictions[i] = Random.Range(-1, 100);
49

  
50
            predictionList.Add(p);
51

  
52
            Date next = curr.Clone();
53
            next.hour += r.hoursPerSegment;
54

  
55
            if (next.hour > 18)
56
            {
57
                next.day++;
58
                next.hour = 7;
59
            }
60

  
61
            curr = next;
62
        }
63

  
64
        r.predicitons = predictionList.ToArray();
65

  
66
        return r;
67
    }
68

  
9 69
}
Client/Client/Assets/Scripts/Model/XmlCommunication.cs
1
using System.Collections;
2
using System.Collections.Generic;
3
using UnityEngine;
4
using System.Xml.Serialization;
5
using System.IO;
6

  
7
public class XmlCommunication
8
{
9
    public static string Serialize<T>(T toSerialize)
10
    {
11
        var xml = new XmlSerializer(toSerialize.GetType());
12

  
13
        using (var stream = new StringWriter())
14
        {
15
            xml.Serialize(stream, toSerialize);
16
            return stream.ToString();
17
        }
18
    }
19

  
20
    public static T Deserialize<T>(T toDeserialize, string xmlString)
21
    {
22
        using (var stream = new StringReader(xmlString))
23
        {
24
            var xml = new XmlSerializer(toDeserialize.GetType());
25
            T deserialized = (T)xml.Deserialize(stream);
26
            return deserialized;
27
        }
28
    }
29
}
Client/Client/Assets/Scripts/Model/XmlCommunication.cs.meta
1
fileFormatVersion: 2
2
guid: 88ce9ea0438c02546acb5bc75651adb1
3
MonoImporter:
4
  externalObjects: {}
5
  serializedVersion: 2
6
  defaultReferences: []
7
  executionOrder: 0
8
  icon: {instanceID: 0}
9
  userData: 
10
  assetBundleName: 
11
  assetBundleVariant: 

Také k dispozici: Unified diff