Exemplo n.º 1
0
	public IEnumerator ContactMasterServer(OnNetResult callback)
	{
		// called first to find out if the server is up
		// and to get the game script URLs

		_isBussy = true;

		WWW www = new WWW(GamePHP.MasterUrl);
		yield return www;
		errmsg = "";
		try
		{

#if DO_DEBUG_LOG
			SDUtil.HTMLLog("Contacting Master Server: " + www.url + "<br>");
			if (!string.IsNullOrEmpty(www.error)) SDUtil.HTMLLog(www.error + "<br>");
			if (!string.IsNullOrEmpty(www.text)) SDUtil.HTMLLog(www.text + "<br>");
			SDUtil.HTMLLog("<hr>");
#endif

			if (!string.IsNullOrEmpty(www.error) || string.IsNullOrEmpty(www.text))
			{
				errmsg = string.IsNullOrEmpty(www.error) ? "Network communication error." : www.error;
				if (callback != null) callback(ReturnCode.Failed, errmsg);
			}
			else
			{
				char[] splitter = { '|' };
				string[] v = www.text.Split(splitter);
				if (v.Length < 3)
				{
					errmsg = "Fetching game URLs result was invalid.";
					if (callback != null) callback(ReturnCode.Error, errmsg);
				}
				else
				{
					GamePHP.Instance.MainUrl = v[1];
					GamePHP.Instance.GameUrl = v[2];

					if (string.IsNullOrEmpty(GamePHP.Instance.MainUrl) || string.IsNullOrEmpty(GamePHP.Instance.GameUrl))
					{
						errmsg = "Fetching game URLs result was invalid.";
						if (callback != null) callback(ReturnCode.Error, errmsg);
					}
					else
					{
#if DO_DEBUG_LOG
						SDUtil.HTMLLog("MainUrl = [" + GamePHP.Instance.MainUrl + "]<br>");
						SDUtil.HTMLLog("GameUrl = [" + GamePHP.Instance.GameUrl + "]<br>");
						SDUtil.HTMLLog("<hr>");
#endif
						if (callback != null) callback(ReturnCode.OK, null);
					}
				}
			}
		}
		catch (System.Exception e)
		{
			Debug.LogError(e.Message);
			Debug.LogError(e.StackTrace);
			errmsg = "Network communication error.";
			if (last_request.callback != null) last_request.callback(ReturnCode.Failed, errmsg);
		}
		finally
		{
			_isBussy = false;
		}
	}
Exemplo n.º 2
0
	private void CreateRequest(bool low_priority, string url, OnNetResult callback, Dictionary<string, string> p)
	{
		RequestInfo r = new RequestInfo();
		r.url = url;
		r.callback = callback;
		r.p = p;
		r.retry_on_fail = (low_priority ? 0 : 2); // priority requests should always retry
		if (low_priority) requests_lowpriority.Add(r);
		else requests.Add(r);
	}
Exemplo n.º 3
0
	public void ClearRequestWithCallback(OnNetResult callback, bool from_low_priority_list)
	{
		List<RequestInfo> remove = new List<RequestInfo>();
		if (from_low_priority_list)
		{
			foreach (RequestInfo r in requests_lowpriority)
			{
				if (r.callback == callback) remove.Add(r);
			}
			foreach (RequestInfo r in remove) requests_lowpriority.Remove(r);
			remove.Clear();
		}
		else
		{
			foreach (RequestInfo r in requests)
			{
				if (r.callback == callback) remove.Add(r);
			}
			foreach (RequestInfo r in remove) requests.Remove(r);
			remove.Clear();
		}
	}
Exemplo n.º 4
0
	public void NormalRequest(string url, OnNetResult callback, Dictionary<string, string> p)
	{
		// create low priority request
		CreateRequest(true, url, callback, p);
	}
Exemplo n.º 5
0
	// PriorityRequest and NormalRequest are the two main ways of sending a message to the server.
	// specify the url to contact, a callback to call when server replied and a dict of fields
	// and values to send

	public void PriorityRequest(string url, OnNetResult callback, Dictionary<string, string> p)
	{
		// create important request
		CreateRequest(false, url, callback, p);
	}
Exemplo n.º 6
0
	public IEnumerator Login(OnNetResult callback, string nm, string pw)
	{
		_isBussy = true;
		string session_cookie = null;

		// create params to send
		WWWForm form = new WWWForm();
		form.AddField("nm", nm);
		form.AddField("pw", pw);

		// let www do its thing
		WWW www = new WWW(GamePHP.Instance.MainUrl + "login/", form);
		yield return www;

		try
		{
#if DO_DEBUG_LOG
			SDUtil.HTMLLog("LOGIN: "******"[nm=" + nm + ", pw=" + pw + "]<br>");
			if (!string.IsNullOrEmpty(www.error)) SDUtil.HTMLLog(www.error + "<br>");
			if (!string.IsNullOrEmpty(www.text)) SDUtil.HTMLLog(www.text + "<br>");
			SDUtil.HTMLLog("<hr>");
			foreach (KeyValuePair<string, string> kv in www.responseHeaders)
			{
				SDUtil.HTMLLog(kv.Key + " => " + kv.Value + "<br>");
			}
			SDUtil.HTMLLog("<hr>");
#endif


			// handle the data from www
			if (!string.IsNullOrEmpty(www.error) || string.IsNullOrEmpty(www.text))
			{
				errmsg = string.IsNullOrEmpty(www.error) ? "Network communication error." : www.error; 
				if (callback != null) callback(ReturnCode.Failed, errmsg);
			}
			else
			{
				errmsg = "Network communication error.";

				if (www.text[0] == '1')
				{
					try
					{
						string[] v = www.text.Split(new char[] { '|' });

						session_cookie = v[0].Substring(1);		// session id (in case i could not get it from headers)
						string sessionID = "";
						for (int i = 10; i < session_cookie.Length; ++i)
							sessionID += session_cookie[i];
						RunTimeData.sessionID = sessionID;
						GamePHP.Instance.name = v[1];			// extract the public name of player

						// sample of grabbing various other info after login
						GamePHP.Instance.my_rating = SDUtil.ParseInt(v[2], 1500);	// extract the player's rating
						GamePHP.Instance.my_wins = SDUtil.ParseInt(v[3], 0);
						GamePHP.Instance.my_losses = SDUtil.ParseInt(v[4], 0);
						GamePHP.Instance.my_draws = SDUtil.ParseInt(v[5], 0);
						GamePHP.Instance.my_quits = SDUtil.ParseInt(v[6], 0);
						GamePHP.Instance.opt_email_notify = SDUtil.ParseInt(v[7], 0);
						GamePHP.Instance.owns = v[8];

						GamePHP.Instance.ClearSessionCookie();
						UpdateCookie(www);

						// just incase the cookies could not be found in the header
						if (!GamePHP.Instance.SessionCookieIsSet && !string.IsNullOrEmpty(session_cookie))
						{
							GamePHP.Instance.SetSessionCookie(session_cookie);
						}
					}
					catch
					{
						// this should only possibly happen during development
						Debug.LogError("This should not happen");
						if (callback != null) callback(ReturnCode.Failed, "Network communication error.");
					}
				}

				if (callback != null)
				{
					if (www.text[0] == '1' && GamePHP.Instance.SessionCookieIsSet)
					{
						errmsg = "Done\n";
						//errmsg += "\n***********************\n";
						//foreach (KeyValuePair<string, string> kv in www.responseHeaders) errmsg += kv.Key + " => " + kv.Value + "\n";
						//errmsg += "\n***********************\n";

						callback(ReturnCode.OK, errmsg);
					}
					else if (www.text[0] == '0' || www.text[0] == '!' || !GamePHP.Instance.SessionCookieIsSet)
					{
						errmsg = "Invalid login name or password.";
						if (!GamePHP.Instance.SessionCookieIsSet)
						{
							errmsg = "System error. Could not create session.";
							//errmsg += "\n***********************\n";
							//foreach (KeyValuePair<string, string> kv in www.responseHeaders) errmsg += kv.Key + " => " + kv.Value + "\n";
							//errmsg += "\n***********************\n";
						}
						callback(ReturnCode.Error, errmsg);
					}
					else
					{
						// this should only happen during development since there was an unexpected 
						// value at [0], not 0 or 1 as expected, so probably some php script error
						errmsg = "Network communication error.";
						callback(ReturnCode.Failed, errmsg);
					}
				}
			}

#if DO_DEBUG_LOG
			SDUtil.HTMLLog("Cookie: " + GamePHP.Instance.GetSessionCookie() + "<hr>");
#endif
		}
		catch (System.Exception e)
		{
			Debug.LogError(e.Message);
			Debug.LogError(e.StackTrace);
			errmsg = "Network communication error.";
			if (last_request.callback != null) last_request.callback(ReturnCode.Failed, errmsg);
		}
		finally
		{
			_isBussy = false;
		}
	}
Exemplo n.º 7
0
	public IEnumerator RecoverPW(OnNetResult callback, string email)
	{
		_isBussy = true;

		// create params to send
		WWWForm form = new WWWForm();
		form.AddField("em", email);

		// let www do its thing
		WWW www = new WWW(GamePHP.Instance.MainUrl + "rpw/", form);
		yield return www;

		try
		{
#if DO_DEBUG_LOG
			SDUtil.HTMLLog("Recover PW: " + www.url + "[em=" + email + "]<br>");
			if (!string.IsNullOrEmpty(www.error)) SDUtil.HTMLLog(www.error + "<br>");
			if (!string.IsNullOrEmpty(www.text)) SDUtil.HTMLLog(www.text + "<br>");
			SDUtil.HTMLLog("<hr>");
#endif

			// handle the data from www
			if (!string.IsNullOrEmpty(www.error) || string.IsNullOrEmpty(www.text))
			{
				errmsg = string.IsNullOrEmpty(www.error) ? "Network communication error." : www.error; 
				if (callback != null) callback(ReturnCode.Failed, errmsg);
			}
			else
			{
				errmsg = "Network communication error.";
				if (callback != null)
				{
					if (www.text[0] == '1')
					{
						callback(ReturnCode.OK, null);
					}
					else if (www.text[0] == '0' || www.text[0] == '!')
					{
						errmsg = "Could not complete your request.";
						if (www.text.Length > 1) errmsg = www.text.Substring(1);
						callback(ReturnCode.Error, errmsg);
					}
					else
					{
						// this should only happen during development since there was an unexpected 
						// value at [0], not 0 or 1 as expected, so probably some php script error
						errmsg = "Network communication error.";
						callback(ReturnCode.Failed, errmsg);
					}
				}
			}
		}
		catch (System.Exception e)
		{
			Debug.LogError(e.Message);
			Debug.LogError(e.StackTrace);
			errmsg = "Network communication error.";
			if (last_request.callback != null) last_request.callback(ReturnCode.Failed, errmsg);
		}
		finally
		{
			_isBussy = false;
		}
	}
Exemplo n.º 8
0
	public IEnumerator VersionCheck(OnNetResult callback)
	{
		_isBussy = true;

		WWW www = new WWW(GamePHP.Instance.MainUrl + "ver/");
		yield return www;
		errmsg = "";

		try
		{
#if DO_DEBUG_LOG
			SDUtil.HTMLLog("Version check: " + www.url + "<br>");
			if (!string.IsNullOrEmpty(www.error)) SDUtil.HTMLLog(www.error + "<br>");
			if (!string.IsNullOrEmpty(www.text)) SDUtil.HTMLLog(www.text + "<br>");
			SDUtil.HTMLLog("<hr>");
#endif

			if (!string.IsNullOrEmpty(www.error) || string.IsNullOrEmpty(www.text))
			{
				errmsg = string.IsNullOrEmpty(www.error) ? "Network communication error." : www.error;
				if (callback != null) callback(ReturnCode.Failed, errmsg);
			}
			else
			{
				if (callback != null) callback(ReturnCode.OK, www.text.Substring(1));
			}
		}
		catch (System.Exception e)
		{
			Debug.LogError(e.Message);
			Debug.LogError(e.StackTrace);
			errmsg = "Network communication error.";
			if (last_request.callback != null) last_request.callback(ReturnCode.Failed, errmsg);
		}
		finally
		{

			_isBussy = false;
		}
	}