You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

507 lines
16 KiB

using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using Shop;
using Equip;
public class User
{
//------------------------------------------------
private bool m_needSave = false;
private bool m_loadComplete = false;
private Dictionary<string, string> m_flags = new Dictionary<string, string>();
private int m_coins = 50;
private int m_energy = 20;
private int m_crystals = 5;
private int m_level = 1;
private SkinType m_currentSkinType = SkinType.RACCOON;
//------------------------------------------------
public User()
{
}
//------------------------------------------------
public void SaveUnlockedItems()
{
string weapons = EquipSettings.GetUnlockedItemsSerializedStr(EquipType.Weapon);
SetFlagByName("unlocked_weapons", weapons);
string armor = EquipSettings.GetUnlockedItemsSerializedStr(EquipType.Armor);
SetFlagByName("unlocked_armor", armor);
string implant = EquipSettings.GetUnlockedItemsSerializedStr(EquipType.Implant);
SetFlagByName("unlocked_implant", implant);
}
//------------------------------------------------
public void SaveEquipedItems()
{
string equipedItemsStr = EquipSettings.GetEquipedItemsSerializedStr();
SetFlagByName("equiped_items", equipedItemsStr);
}
//------------------------------------------------
public SkinType CurrentSkin
{
get { return m_currentSkinType; }
set {
m_currentSkinType = value;
SetFlagByName("current_selected_skin", m_currentSkinType.ToString());
}
}
//------------------------------------------------
public int Coins
{
get { return m_coins; }
}
//------------------------------------------------
public int Energy
{
get { return m_energy; }
}
//------------------------------------------------
public int Crystals
{
get { return m_crystals; }
}
//------------------------------------------------
public int Level
{
get { return m_level; }
}
//------------------------------------------------
public bool IsHaveEnergy()
{
if (m_energy >= GlobalsVar.gStartLvlEnergyCount)
{
m_energy -= GlobalsVar.gStartLvlEnergyCount;
MainMenu mainMenu = GlobalsVar.gBoard as MainMenu;
if (mainMenu)
mainMenu.ChangeEnergyCountLabel();
return true;
}
return false;
}
//------------------------------------------------
public void IncEnergy(int delta = 0)
{
if (delta > 0)
m_energy += delta;
else
m_energy++;
Save();
}
//------------------------------------------------
public void IncCurrency(ShopType type, int delta = 0)
{
switch (type)
{
case ShopType.Coins:
m_coins += delta;
break;
case ShopType.Gems:
m_crystals += delta;
break;
}
Save();
}
//------------------------------------------------
public void IncLevel()
{
m_level++;
Save();
}
//------------------------------------------------
public void ClearFlag(string flagName)
{
if (m_flags.ContainsKey(flagName))
m_flags.Remove(flagName);
Save();
}
//------------------------------------------------
public void SetMainAbActive(string abName)
{
string flagName = abName + "_active";
SetFlagByName(flagName, "True");
Save();
}
//------------------------------------------------
public bool GetMainAbActive(string abName)
{
string flagName = abName + "_active";
if (m_flags.ContainsKey(flagName))
{
if (m_flags[flagName] == "True")
return true;
else
return false;
}
else
return false;
}
//------------------------------------------------
public void SetMainAbLvlUp(string abName, int lvlUp)
{
string flagName = abName + "_lvlup";
SetFlagByName(flagName, lvlUp.ToString());
}
//------------------------------------------------
public int GetMainAbLvlUp(string abName)
{
string flagName = abName + "_lvlup";
return GetIntFlagByName(flagName);
}
//------------------------------------------------
public void SetFlagByName(string flagName, string flagValue)
{
m_flags[flagName] = flagValue;
Save();
}
//------------------------------------------------
public string GetFlagByName(string flagName)
{
if (m_flags.ContainsKey(flagName))
return m_flags[flagName];
return "";
}
//------------------------------------------------
public int GetIntFlagByName(string flagName)
{
if (m_flags.ContainsKey(flagName))
return Convert.ToInt32(m_flags[flagName]);
return 0;
}
//------------------------------------------------
public void SetUserMaxLevel(string levelName)
{
string val = levelName.Substring(5, levelName.Length - 5);
string[] arr = val.Split('_');
if (arr.Length != 2)
{
CommonFunctions.myassert(false);
return;
}
int stageId = 0;
CommonFunctions.myassert(int.TryParse(arr[0], out stageId));
int roomId = 0;
CommonFunctions.myassert(int.TryParse(arr[1], out roomId));
int userMaxStageId = GetIntFlagByName("max_stage_id");
if (stageId > userMaxStageId)
{
SetFlagByName("max_stage_id", stageId.ToString());
SetFlagByName("max_room_id", roomId.ToString());
DevToDevRealization.SetUserLevel(CommonFunctions.GetLinearLvlId(stageId, roomId));
}
else
{
int userMaxRoomId = GetIntFlagByName("max_room_id");
if (roomId > userMaxRoomId)
{
SetFlagByName("max_room_id", roomId.ToString());
DevToDevRealization.SetUserLevel(CommonFunctions.GetLinearLvlId(stageId, roomId));
}
}
}
//------------------------------------------------
public void Save(bool delay = true)
{
if (delay)
{
m_needSave = true;
return;
}
if (!m_loadComplete)
return;
m_needSave = false;
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-GB");
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode pRoot = doc.CreateElement("root");
doc.AppendChild(pRoot);
XmlAttribute coinsAttr = doc.CreateAttribute("coins");
coinsAttr.Value = m_coins.ToString();
pRoot.Attributes.Append(coinsAttr);
XmlAttribute energyAttr = doc.CreateAttribute("energy");
energyAttr.Value = m_energy.ToString();
pRoot.Attributes.Append(energyAttr);
XmlAttribute crystalsAttr = doc.CreateAttribute("crystals");
crystalsAttr.Value = m_crystals.ToString();
pRoot.Attributes.Append(crystalsAttr);
XmlAttribute levelAttr = doc.CreateAttribute("level");
levelAttr.Value = m_level.ToString();
pRoot.Attributes.Append(levelAttr);
XmlAttribute randCountAttr = doc.CreateAttribute("rand_count");
randCountAttr.Value = GlobalsVar.gCurrentAbRandomCount.ToString();
pRoot.Attributes.Append(randCountAttr);
XmlAttribute musicVolumeAttr = doc.CreateAttribute("need_music");
musicVolumeAttr.Value = GlobalsVar.gNeedPlayMusic.ToString();
pRoot.Attributes.Append(musicVolumeAttr);
XmlAttribute soundsVolumeAttr = doc.CreateAttribute("need_sounds");
soundsVolumeAttr.Value = GlobalsVar.gNeedPlaySounds.ToString();
pRoot.Attributes.Append(soundsVolumeAttr);
XmlAttribute timeStamp = doc.CreateAttribute("time_stamp");
timeStamp.Value = (UnbiasedTime.Instance.Now().Ticks / TimeSpan.TicksPerMillisecond).ToString(culture);
pRoot.Attributes.Append(timeStamp);
XmlAttribute heartsTime = doc.CreateAttribute("energy_time");
heartsTime.Value = GlobalsVar.gCurrentTimeToRegenerateEnergy.ToString(culture);
pRoot.Attributes.Append(heartsTime);
XmlAttribute freeCoinsTime = doc.CreateAttribute("free_coins_time");
freeCoinsTime.Value = GlobalsVar.gCurrentTimeToFreeStoreCoins.ToString(culture);
pRoot.Attributes.Append(freeCoinsTime);
// Save user flags---------------------------
XmlNode flags = doc.CreateElement("flags");
pRoot.AppendChild(flags);
foreach (var flag in m_flags)
{
XmlNode flagElement = doc.CreateElement("flag");
flags.AppendChild(flagElement);
XmlAttribute flagNameAttr = doc.CreateAttribute("name");
flagElement.Attributes.Append(flagNameAttr);
flagNameAttr.Value = flag.Key;
XmlAttribute flagValueAttr = doc.CreateAttribute("value");
flagElement.Attributes.Append(flagValueAttr);
flagValueAttr.Value = flag.Value;
}
//------------------------------------------
SaveXmlToFile("profile.xml", doc);
}
//------------------------------------------------
void SaveXmlToFile(string fileToSaveName, XmlDocument doc)
{
string fileName = Path.Combine(Application.persistentDataPath, fileToSaveName);
File.Delete(fileName);
StreamWriter streamWriter = new StreamWriter(File.OpenWrite(fileName));
doc.Save(streamWriter);
streamWriter.Flush();
streamWriter.Dispose();
}
//------------------------------------------------
bool IsFileValidXml(string fileName)
{
try
{
XmlDocument file = new XmlDocument();
#if NETFX_CORE
StreamReader streamReader = new StreamReader (File.OpenRead (fileName));
file.Load( streamReader );
streamReader.Dispose ();
#else
file.Load(fileName);
#endif
return true;
}
catch (XmlException exception)
{
Debug.Log("Save file broken!!! " + exception.Message);
CommonFunctions.myassert(false);
return false;
}
}
//------------------------------------------------
public void Load()
{
m_loadComplete = false;
bool needTryLoadFromBackup = false;
string fileName = Path.Combine(Application.persistentDataPath, "profile.xml");
Debug.Log("SAVE FILE = " + fileName);
if (File.Exists(fileName) && IsFileValidXml(fileName))
LoadFromFile(fileName, "");
else
needTryLoadFromBackup = true;
if (needTryLoadFromBackup)
{
string fileNameBackup = Path.Combine(Application.persistentDataPath, "profile_backup.xml");
if (File.Exists(fileNameBackup) && IsFileValidXml(fileNameBackup))
{
LoadFromFile(fileNameBackup, "");
needTryLoadFromBackup = false;
}
}
if (needTryLoadFromBackup)
{
string fileNameBackup = Path.Combine(Application.persistentDataPath, "profile_backup_2.xml");
if (File.Exists(fileNameBackup) && IsFileValidXml(fileNameBackup))
LoadFromFile(fileNameBackup, "");
}
m_loadComplete = true;
}
//------------------------------------------------
void LoadFromFile(string fileName, string fileData)
{
XmlDocument file = new XmlDocument();
if (fileName.Length > 0)
file.Load(fileName);
else if (fileData.Length > 0)
file.LoadXml(fileData);
XmlNode pRoot = file.DocumentElement;
if (pRoot == null)
{
CommonFunctions.myassert(false, "Root element not found or xml doesn't load!");
return;
}
NumberStyles style = NumberStyles.Number;
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-GB");
foreach (XmlAttribute attr in pRoot.Attributes)
{
if (attr.Name == "coins")
m_coins = Convert.ToInt32(attr.Value);
if (attr.Name == "crystals")
m_crystals = Convert.ToInt32(attr.Value);
if (attr.Name == "level")
m_level = Convert.ToInt32(attr.Value);
if (attr.Name == "rand_count")
GlobalsVar.gCurrentAbRandomCount = Convert.ToInt32(attr.Value);
else if (attr.Name == "energy")
{
m_energy = Convert.ToInt32(attr.Value);
if (m_energy < 0)
m_energy = 0;
}
else if (attr.Name == "need_music")
CommonFunctions.myassert(bool.TryParse(attr.Value, out GlobalsVar.gNeedPlayMusic));
else if (attr.Name == "need_sounds")
CommonFunctions.myassert(bool.TryParse(attr.Value, out GlobalsVar.gNeedPlaySounds));
else if (attr.Name == "time_stamp")
{
long lastSaveTime = Convert.ToInt64(attr.Value);
long timeNow = (UnbiasedTime.Instance.Now()).Ticks / TimeSpan.TicksPerMillisecond;
GlobalsVar.gDeltaTimeFromLastSave = (timeNow - lastSaveTime) / 1000f;
}
else if (attr.Name == "energy_time")
{
string value = attr.Value;
CommonFunctions.myassert(float.TryParse(value, style, culture, out GlobalsVar.gCurrentTimeToRegenerateEnergy));
if (GlobalsVar.gCurrentTimeToRegenerateEnergy < 0f)
GlobalsVar.gCurrentTimeToRegenerateEnergy = 0f;
}
else if (attr.Name == "free_coins_time")
{
string value = attr.Value;
CommonFunctions.myassert(float.TryParse(value, style, culture, out GlobalsVar.gCurrentTimeToFreeStoreCoins));
if (GlobalsVar.gCurrentTimeToFreeStoreCoins < 0f)
GlobalsVar.gCurrentTimeToFreeStoreCoins = 0f;
}
}
// Load user flags------------------------------------------------
XmlNode flagsElement = pRoot.SelectSingleNode("flags");
foreach (XmlNode flag in flagsElement.ChildNodes)
{
string name = "";
string value = "";
foreach (XmlAttribute attr in flag.Attributes)
{
if (attr.Name == "name")
name = attr.Value;
if (attr.Name == "value")
value = attr.Value;
}
m_flags[name] = value;
}
//----------------------------------------------------------------
InitCurrentSelectedSkin();
CommonFunctions.UpdateTimeToFreeCoins(GlobalsVar.gDeltaTimeFromLastSave);
}
//------------------------------------------------
private void InitCurrentSelectedSkin()
{
string flagVal = GetFlagByName("current_selected_skin");
if (flagVal == "RACCOON")
m_currentSkinType = SkinType.RACCOON;
else if (flagVal == "FOX")
m_currentSkinType = SkinType.FOX;
else if (flagVal == "SKUNK")
m_currentSkinType = SkinType.SKUNK;
else
{
m_currentSkinType = SkinType.RACCOON;
SetFlagByName("current_selected_skin", m_currentSkinType.ToString());
}
}
//------------------------------------------------
public void Update()
{
if (m_needSave)
Save(false);
}
}