Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PlayerControl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
public class PlayerControl : MonoBehaviour, IPunObservable
{
private PhotonView photonView;
public GameObject PlayerPrefab;
private GameObject OtherPlayer;
private string PlayerToTransfer;
public Vector3 OldPos;
public Vector3 CurrentPos;
Camera Cam;
Text MoneyText;
Text IdText;
public string[] Id;
private string CurrentId;
[Header("ID игрока")]
public string Id_Name;
[Header("---")]
[Header("Деньги")]
public float Money;
[Header("---")]
[Header("Время в игре")]
public float CurrentSession;
public float OldMoney;
[Header("---")]
private GameObject Admin;
[Header("ID в поле админа")]
public string Id_Admin;
[Header("Изменить баланс")]
public float Money_Admin;
[Header("Статус")]
public string Status_Admin;
[Header("Модель устройства")]
public string DeviceModel;
[Header("Статус бана")]
public string IsBanned;
[Header("Причина и дата бана")]
public string СauseBanned;
//[Header("IMEI устройства")]
//public string IMEI;
public string TransferId;
public string TransferSum;
public string OldTransferSum;
public string SumToAdd;
private bool ImTransfer;
//System.DateTime CurrentTime = System.DateTime.Now;
void Start()
{
photonView = GetComponent<PhotonView>();
if(PlayerPrefab.GetPhotonView().IsMine)
{
LoadGame();
StartCoroutine(AutoSave());
PlayerPrefab.transform.Find("Cam").gameObject.SetActive(true);
PlayerPrefab.transform.Find("Canvas").gameObject.SetActive(true);
Cam = PlayerPrefab.transform.Find("Cam").gameObject.GetComponent<Camera>();
Id_Name = (Id[Random.Range(0,Id.Length)] + Id[Random.Range(0,Id.Length)] + Id[Random.Range(0,Id.Length)] + Id[Random.Range(0,Id.Length)] + Id[Random.Range(0,Id.Length)] + Id[Random.Range(0,Id.Length)] + Id[Random.Range(0,Id.Length)] + Id[Random.Range(0,Id.Length)] + Id[Random.Range(0,Id.Length)]);
CurrentId = ("Твой ID : " + Id_Name);
PlayerPrefab.transform.Find("Canvas").transform.Find("CopyId").gameObject.SetActive(true);
PlayerPrefab.transform.Find("Canvas").transform.Find("TransferButton").gameObject.SetActive(true);
PlayerPrefab.transform.Find("Canvas").transform.Find("Count").gameObject.SetActive(true);
PlayerPrefab.transform.Find("Canvas").transform.Find("ClickMoney").gameObject.SetActive(true);
MoneyText = PlayerPrefab.transform.Find("Canvas").transform.Find("Count").gameObject.GetComponent<Text>();
MoneyText.gameObject.SetActive(true);
IdText = PlayerPrefab.transform.Find("Canvas").transform.Find("Id").gameObject.GetComponent<Text>();
IdText.gameObject.SetActive(true);
IdText.text = CurrentId;
Admin = GameObject.Find("GameManager");
DeviceModel = SystemInfo.deviceModel;
OldPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
//IMEI = SystemInfo.deviceUniqueIdentifier;
}
Admin = GameObject.Find("GameManager");
gameObject.name = Id_Name;
PhotonNetwork.NickName = Id_Name;
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.IsWriting)
{
stream.SendNext(Id_Name);
stream.SendNext(DeviceModel);
//stream.SendNext(IsBanned);
//stream.SendNext(СauseBanned);
//stream.SendNext(IMEI);
stream.SendNext(Money);
stream.SendNext(OldMoney);
stream.SendNext(TransferSum);
stream.SendNext(CurrentSession);
}
else
{
Id_Name = (string) stream.ReceiveNext();
DeviceModel = (string) stream.ReceiveNext();
//IsBanned = (string) stream.ReceiveNext();
//СauseBanned = (string) stream.ReceiveNext();
//IMEI = (string) stream.ReceiveNext();
Money = (float) stream.ReceiveNext();
OldMoney = (float) stream.ReceiveNext();
TransferSum = (string) stream.ReceiveNext();
CurrentSession = (float) stream.ReceiveNext();
}
}
public void SaveGame()
{
if(photonView.IsMine)
{
PlayerPrefs.SetInt("IsReturningUser", 1);
PlayerPrefs.SetFloat("FloatMoney", Money);
PlayerPrefs.SetString("IsBannedString", IsBanned);
PlayerPrefs.SetString("СauseBannedString", СauseBanned);
PlayerPrefs.Save();
Debug.Log("Game data saved!");
SSTools.ShowMessage("Данные сохранены!", SSTools.Position.bottom, SSTools.Time.threeSecond);
}
}
private IEnumerator AutoSave()
{
if(photonView.IsMine)
{
while(true)
{
yield return new WaitForSeconds(200f);
SaveGame();
SSTools.ShowMessage("Сохранение данных...", SSTools.Position.bottom, SSTools.Time.threeSecond);
yield return new WaitForSeconds(2f);
SSTools.ShowMessage("Данные сохранены!", SSTools.Position.bottom, SSTools.Time.threeSecond);
}
}
}
public void LoadGame()
{
if(photonView.IsMine)
{
if (PlayerPrefs.HasKey("IsReturningUser"))
{
Money = PlayerPrefs.GetFloat("FloatMoney");
IsBanned = PlayerPrefs.GetString("IsBannedString");
СauseBanned = PlayerPrefs.GetString("СauseBannedString");
Debug.Log("Game data loaded!");
}
else
{
Debug.LogError("There is no save data!");
SaveGame();
}
}
}
////
public void Update()
{
if(photonView.IsMine)
{
StartCoroutine(Timer());
MoneyText.text = Money.ToString();
TransferSum = PlayerPrefab.transform.Find("Canvas").transform.Find("Fon").transform.Find("TransferSum").transform.Find("Sum").gameObject.GetComponent<Text>().text;
CurrentPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
Admin = GameObject.Find("GameManager");
Id_Admin = Admin.GetComponent<GameManager>().PlayerId;
Money_Admin = Admin.GetComponent<GameManager>().ChangeMoney;
}
[PunRPC]
public void Transfer()
{
if(photonView.IsMine)
{
PlayerPrefab.transform.Find("Canvas").transform.Find("Fon").gameObject.SetActive(true);
OldMoney = Money;
}
}
public void EndTransfer()
{
if(photonView.IsMine)
{
TransferId = PlayerPrefab.transform.Find("Canvas").transform.Find("Fon").transform.Find("TransferId").transform.Find("Id").gameObject.GetComponent<Text>().text;
OtherPlayer = GameObject.Find(TransferId).gameObject;
if(Money >= float.Parse(TransferSum))
{
StartCoroutine(BM());
PlayerPrefab.transform.Find("Canvas").transform.Find("Fon").gameObject.SetActive(false);
OldTransferSum = TransferSum;
Money -= float.Parse(TransferSum);
TransferSum = 0f.ToString();
TransferSum = 0f.ToString();
//SaveGame();
}
else
{
SSTools.ShowMessage("Ваш баланс недостаточен!", SSTools.Position.bottom, SSTools.Time.threeSecond);
}
}
}
private IEnumerator BM()
{
if(photonView.IsMine)
{
ImTransfer = true;
OldPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
yield return new WaitForSeconds(0.1f);
transform.position = new Vector3(OtherPlayer.transform.position.x, OtherPlayer.transform.position.y, OtherPlayer.transform.position.z);
yield return new WaitForSeconds(2f);
transform.position = new Vector3(OldPos.x, OldPos.y, OldPos.z);
TransferSum = 0f.ToString();
if(Money == OldMoney)
{
Money -= float.Parse(TransferSum);
TransferSum = 0f.ToString();
ImTransfer = false;
SaveGame();
}
ImTransfer = false;
}
}
[PunRPC]
public void Click()
{
if(photonView.IsMine)
{
Money += 0.1f;
}
}
public void CopyText(string textToCopy)
{
if(photonView.IsMine)
{
TextEditor editor = new TextEditor
{
text = Id_Name
};
editor.SelectAll();
editor.Copy();
SSTools.ShowMessage("ID скопирован!", SSTools.Position.bottom, SSTools.Time.threeSecond);
}
}
public void Balance()
{
if(Id_Admin == Id_Name)
{
if(photonView.IsMine)
{
Money = Money_Admin;
}
}
}
private IEnumerator Timer()
{
yield return new WaitForSeconds(1f);
CurrentSession += 1f;
}
public void OnTriggerEnter(Collider col) //изменил на прайват(изменить обратно на паблик)
{
Status_Admin = col.gameObject.GetComponent<GameManager>().Status;
//СauseBanned = "Вас забанили! \n \nПричина : \n" + other.gameObject.GetComponent<GameManager>().NewСauseBanned + "\nДата бана : " + CurrentTime;
if(photonView.IsMine)
{
if(Status_Admin == "nulling")
{
Money -= Money;
SaveGame();
}
if(Status_Admin == "add")
{
Money += Money_Admin;
SaveGame();
}
if(Status_Admin == "change")
{
Money = Money_Admin;
SaveGame();
}
if(Status_Admin == "kick")
{
StartCoroutine(Kicked());
}
if(Status_Admin == "ban")
{
PlayerPrefab.transform.Find("Canvas").transform.Find("BannedWindow").gameObject.SetActive(true);
SSTools.ShowMessage("Вас забанили! Причина : " + col.gameObject.GetComponent<GameManager>().NewСauseBanned, SSTools.Position.bottom, SSTools.Time.threeSecond);
IsBanned = "yes";
SaveGame();
}
}
}
public void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag == "Player")
{
SumToAdd = other.gameObject.GetComponent<PlayerControl>().TransferSum;
if(photonView.IsMine)
{
if(CurrentPos == OldPos)
{
if(!ImTransfer)
{
Money += float.Parse(SumToAdd);
SSTools.ShowMessage("Игрок : " + other.gameObject.GetComponent<PlayerControl>().Id_Name + " перевел : " + SumToAdd + "", SSTools.Position.bottom, SSTools.Time.threeSecond);
SumToAdd = 0f.ToString();
TransferSum = 0f.ToString();
StartCoroutine(SlowTransferMoney());
}
}
}
}
}
private IEnumerator Kicked()
{
if(photonView.IsMine)
{
SSTools.ShowMessage("Вас исключили!", SSTools.Position.bottom, SSTools.Time.threeSecond);
yield return new WaitForSeconds(3);
PhotonNetwork.LeaveRoom();
Application.Quit();
}
}
private IEnumerator SlowTransferMoney()
{
if(photonView.IsMine)
{
yield return new WaitForSeconds(3);
SaveGame();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment