summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/Apollo/iOSBridge/QGameKitiOSBridge.cs
blob: a30b5e18edbf6a423bac5558d9f205fd47c53d3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#if !DISABLE_PLUGIN

#if UNITY_IOS  && !UNITY_EDITOR
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using System;
using System.Threading;
using QGameUtils;

public class QGameKitiOSBridge : MonoBehaviour {

	private static QGameKitiOSBridge singletonInstance = null;
	public QGameKit.UserAccountDelegate accountDelegate;
	public QGameKit.CommentReceiveDelegate commentDelegate;
	public QGameKit.LogDelegate logDelegate;
	public QGameKit.LiveStatusChangedDelegate liveStatusDelegate;
	public QGameKit.ShareDelegate shareDelegate;
	public QGameKit.ErrorCodeListenerDelegate errorCodeDelegate;

	public static QGameKitiOSBridge Setup()
	{ 
		if(singletonInstance != null)
		{
			return singletonInstance;
		}

		GameObject sdkObject = new GameObject("QGameKitiOSBridge");
		DontDestroyOnLoad(sdkObject);
		singletonInstance = sdkObject.AddComponent<QGameKitiOSBridge>();
		
		return singletonInstance;
	}

	private void DidReceivedGetUserAccountRequest()
	{
		QGameKit.UserAccount account = accountDelegate();
		QGameKit.UpdateUserAccount (account);
	}

	private void DidReceivedComments(string data)
	{
		if (commentDelegate == null) {
			return;
		}

		List<QGameKit.LiveComment> comments = new List<QGameKit.LiveComment>();
		List<object> array = Json.Deserialize(data) as List<object>;

		foreach(var item in array) {
			IDictionary itemDict = (IDictionary)item;
			QGameKit.LiveComment comment = new QGameKit.LiveComment();
			comment.type = (QGameKit.CommentType)(Int32.Parse((string)itemDict["type"]));
			comment.nick = (string)itemDict["nick"];
			comment.content = (string)itemDict["content"];
			comment.timestamp = Int64.Parse((string)itemDict["timestamp"]);
			comments.Add(comment);
		}
		this.commentDelegate(comments);
	}

	private void DidReceivedLog(string log)
	{
		if (logDelegate == null) {
			return;
		}
		logDelegate (log);
	}

	private void DidReceivedLiveStatusChanged(string liveStatus)
	{
		if (liveStatusDelegate == null) {
			return;
		}

		QGameKit.LiveStatus status = (QGameKit.LiveStatus)int.Parse(liveStatus);
		liveStatusDelegate (status);
	}

	private void DidReceivedShareContent(string content)
	{
		if (shareDelegate == null) {
			Debug.Log ("Share Delegate Null");
			return;
		}
			
		QGameKit.ShareContent shareContent = new QGameKit.ShareContent();
		var data = Json.Deserialize(content)  as Dictionary<string,object>;
		shareContent.title = data["title"].ToString();
		shareContent.description = data["description"].ToString();
		shareContent.targetUrl = data["targetUrl"].ToString();
		shareContent.imageUrl = data["imageUrl"].ToString();
		shareDelegate(shareContent);
	}

	private void DidReceivedError(string error)
	{
		if (errorCodeDelegate == null) {
			Debug.Log ("Error Delegate Null");
			return;
		}
	
		var data = Json.Deserialize(error)  as Dictionary<string,string>;
		errorCodeDelegate (int.Parse (data["errorCode"]), data["errorMessage"]);
	}
}
#endif

#endif