#pragmastrict
importSystem.Security.Cryptography;
importSystem.Text;
@scriptExecuteInEditMode()
enumTrackingType{FreeEntry,TrackProgress}
vartrackType : TrackingType = TrackingType.FreeEntry;
enumDeveloperType{debug,live}
vardevelopmentMode : DeveloperType = DeveloperType.live;
privatevarsecretKey : String = “test”;
varsendScoreURL : String = “http://nfx.comuv.com/.../SendHighScores.php?”;
vargetScoresURL : String = “http://nfx.comuv.com/.../GetHighScores.php?”;
varresetScoresURL : String = “http://nfx.comuv.com/.../ResetHighScores.php?”;
varregisterUserURL : String = “http://nfx.comuv.com/.../RegisterUser.php?”;
vargetUserScoreURL : String = “http://nfx.comuv.com/.../GetUserScore.php?”;
varDatabaseToolsURL : String = “http://nfx.comuv.com/.../DatabaseTools.php?”;
//ModulesDone
classModule{
varshowStatus : boolean = true;
varshowScores : boolean = true;
varshowUserScore : boolean = true;
varshowSendScore : boolean = true;
varshowDifficultySwitch : boolean = true;
varshowScopeSwitch : boolean = true;
}
vardisplayOptions : Module;
//ClassThatcontains
classMode{
vardisplayName : String = “Easy”;
varplayerPrefsName : String = “Easy”;
vardatabaseName : String = “Easy”;
}
vardifficultyModes : Mode[ ];
varstartDifficultyMode : int = 0;
privatevarmodeIndex : int = 0;
//ScoreScope
privatevarscoreScopeText : String = “Global”;//AllTime,Daily
privatevarcurrentScope : String = “AllTime”;
classScoreScopeSettings{
vardailyName : String = “Daily”;
varallTimeName : String = “Global”;
}
varscoreScope : ScoreScopeSettings;
//GetHighScoresFields(keepthesamelenght)
privatevarserverHighScores : String[ ] = newString[0];
privatevarscrollView : Vector2;
varscoreType : String = “Points”;
varmaxHighScores : int = 100;
//SendHighScoresFields
privatevarserverHighScoreName : String = “”;
varmaxNameCharacters : int = 20;
//ResetHighScoresFields
varresetNames : int = 100;
varminResetScore : int = 100;
varmaxResetScore : int = 1000;
//BadNames
varblockBadNames : boolean = true; //Blocknamesfrombasnameslist
varbadNamesList : TextAsset; //ListOfbadnames
//PlayerPrefsName
varexistingNamePlayerPrefs : String = “playerName”;
//Rects
varsendScoresOffsetRect : Rect = newRect(450,150,250,30);
vargetScoresOffsetRect : Rect = newRect(450,100,100,25);
varscoreScopeOffsetRect : Rect = newRect(300,100,100,25);
varmessageOffsetBox : Rect = newRect(450,-150,250,25);
varlocalScoreOffsetRect : Rect = newRect(450,-105,250,25);
varserverScoreOffsetRect : Rect = newRect(450,-60,250,40);
varscoresListOffsetRect : Rect = newRect(150,150,380,350);
privatevarbaseRect : Rect = newRect(Screen.width * .5F,Screen.height * .5F,100F,100F);
//status
privatevaryear : int = 2012;
privatevarmonth : int = 9;
privatevarday : int = 1;
privatevardebugScore : int = 0;
privatevardeleteScore : int = 0;
privatevarlocalScore : int = 0;
privatevarserverScore : int = 0;
privatevarserverRank : int = 0;
privatevarstatus : String = “”; //StatusofServerOperations
privatevarrunningHsServer : int = 0; //Arewedoingserversideoperations
privatevarrunningTrack : int = 0; //Areweseekingplayerstatus
//skin
varskin : GUISkin;
functionDatabaseTools(mode : int ,tables : Mode[ ]){
if(runningHsServer == 1){status = “StillRunning”; return;}
runningHsServer = 1; status = “Running”;
//0 = createtables
varhsFm : WWWForm = newWWWForm();
hsFm.AddField(“mode”,mode);
for(varmd : Modeintables){
hsFm.AddField(“table”,md.databaseName);
varhs : WWW = newWWW(DatabaseToolsURL,hsFm);
yieldhs;
if(hs.text.Equals(“Created”)){status = “DatabaseCreated”;}
else{status = “ErrorOccured”;}
Debug.Log(hs.text);
}
//StopRunning
runningHsServer = 0;
SynchTable();
}
functionDatabaseTools(mode : int,table : String){
if(runningHsServer == 1){status = “StillRunning”; return;}
runningHsServer = 1; status = “Running”;
//Modes
//1 = deletebyscore
//2 = deletebydate
varhsFm : WWWForm = newWWWForm();
hsFm.AddField(“mode”,mode);
if(mode == 1){
hsFm.AddField(“table”,table);
hsFm.AddField(“score”,deleteScore);
}elseif(mode == 2){
hsFm.AddField(“table”,table);
hsFm.AddField(“date”,year+“/”+month+“/”+day);
}
varhs : WWW = newWWW(DatabaseToolsURL,hsFm);
yieldhs;
if(hs.text.Equals(“Created”)){status = “DatabaseCreated”;}
if(hs.text.Equals(“Deleted”)){status = table +“EntriesCleaned”;}
else{status = “ErrorOccured”;}
Debug.Log(hs.text);
//StopRunning
runningHsServer = 0;
SynchTable();
}
functionResetHighScores(table : String,mode : String){
if(runningHsServer == 1){status = “StillRunning”; return;}
runningHsServer = 1; status = “Running”;
//
varhsFm : WWWForm = newWWWForm();
hsFm.AddField(“table”,table);
hsFm.AddField(“mode”,mode);
hsFm.AddField(“count”,resetNames);
hsFm.AddField(“min”,minResetScore);
hsFm.AddField(“max”,maxResetScore);
varhs : WWW = newWWW(resetScoresURL,hsFm);
yieldhs;
//Update
status = hs.text;
//Running
runningHsServer = 0;
SynchTable();
}
functionGetUserScore(table : String,name : String){
runningTrack = 1;//Weareseekinguserstats
//
serverRank = 0;
serverScore = 0;
//GetUserScore
varhsFm : WWWForm = newWWWForm();
hsFm.AddField(“name”,name);
hsFm.AddField(“table”,table);
hsFm.AddField(“hash”,GetHash(name));
varhs : WWW = newWWW(getUserScoreURL,hsFm);
yieldhs;
Debug.Log(hs.text);
if(hs.text != “NotFound” && !hs.text.Contains(“Queryfailed”)){
varuserData : String[ ] = hs.text.Split(‘:’[0]);
//ProcessResults
if(userData[1] != null)serverRank = int.Parse(userData[1]);
if(userData[2] != null)serverScore = int.Parse(userData[2]);
}
elseif(hs.text == “NotFound”){
PlayerPrefs.SetInt(“nameRegistered”,0);
}
runningTrack = 0;
}
functionGetHighScores(table : String,scope : String,limit : int){
if(runningHsServer == 1){status = “StillRunning”; return;}
runningHsServer = 1; status = “Running”;
//GetHighScores
serverHighScores = newString[maxHighScores];
for(varst : int = 0;st<serverHighScores.Length;st++){
serverHighScores[st] = “Loading…”;
}
varhsFm : WWWForm = newWWWForm();
hsFm.AddField(“table”,table);
hsFm.AddField(“scope”,scope);
hsFm.AddField(“limit”,limit);
hsFm.AddField(“hash”,GetHash(table));
varhs : WWW = newWWW(getScoresURL,hsFm);
yieldhs;
if(hs.text.Length > 0){
serverHighScores = hs.text.Split(‘%’[0]);
status = “Found”+table+“HighScores”;
Debug.Log(“FoundHighScores: " + scope+” :" +table);
}else{
status = “No”+scope+“Scores”;
Debug.Log(“No”+scope+“Scores”);
}
//StopRunning
runningHsServer = 0;
//GetUserStatsIfTrackingisOn & WeareRegistered
if(trackType == TrackingType.TrackProgress && PlayerPrefs.GetInt(“nameRegistered”) == 1){
StartCoroutine(GetUserScore(difficultyModes[modeIndex].databaseName,serverHighScoreName));
}
}
functionSendHighScores(table : Mode,name : String,score : int,difficultyModesSet : Mode[ ]){
if(runningHsServer == 1){status = “StillRunning”; return;}
runningHsServer = 1; status = “Running”;
//CheckIfWeHaveBeatOurOwnScoreFirst
if(developmentMode == DeveloperType.live){
if(PlayerPrefs.GetInt(table.playerPrefsName) <= PlayerPrefs.GetInt(“sent”+table.playerPrefsName)){
status = table.displayName + “ScorePreviouslySubmitted”;
runningHsServer = 0;
return;
}
}
//Trim
if(name.Length > maxNameCharacters){
runningHsServer = 0;
status = “NameTooLong”; return;
}
//Scan & CheckName
if(blockBadNames && CheckName(name).CompareTo(“offensive”) == 0){
runningHsServer = 0;
status = “ChosenNameIsOffensive”; return;
}
varupdating : int = 0; //0 = nowearemakingafreeentry/1 = updatingentry
varnewRegistration : int = 0; //Wearedoinganewregistration
if(trackType == TrackingType.TrackProgress){
if(PlayerPrefs.GetInt(“nameRegistered”) == 0){ //Wearenotyetregistred
newRegistration = 1;
status = “RegisteringUser”;
varfinalResult : String = “”;
vartables : String = “”;
for(varm : int = 0; m < difficultyModesSet.Length; m++){//Createalistoftablestosend
if(m < difficultyModesSet.Length -1){
tables += difficultyModesSet[m].databaseName + “”;
}else{
tables += difficultyModesSet[m].databaseName;
}
}
varrsFm : WWWForm = newWWWForm();
rsFm.AddField(“name”,name);
rsFm.AddField(“tables”,tables);
rsFm.AddField(“hash”,GetHash(name));
varrs : WWW = newWWW(registerUserURL,rsFm);
yieldrs;
Debug.Log(rs.text+" : “+table.displayName);
finalResult = rs.text;
if(finalResult.Equals(“AlreadyUsed”)){
runningHsServer = 0;
status = “NameAlreadyUsed”; return;
}elseif(finalResult.Equals(“RegistrationComplete”)){//WeRegisteredNowUpdateScore
PlayerPrefs.SetInt(“nameRegistered”,1);
PlayerPrefs.SetString(“registeredName”,name);
}else{
runningHsServer = 0;
status = finalResult; return;
}
}
updating = 1; //Weneedtoupdateentrynow
}
//SENDORUPDATESCORE
status = “Running”; //RunAgain
varhsFm : WWWForm = newWWWForm();
hsFm.AddField(“table”,table.databaseName);
hsFm.AddField(“name”,name);
hsFm.AddField(“score”,score);
hsFm.AddField(“updating”,updating);
hsFm.AddField(“hash”,GetHash(name));
varhs : WWW = newWWW(sendScoreURL,hsFm);
yieldhs;
Debug.Log(hs.text+” : "+table.displayName);
//ProcessResults
if(hs.text.Contains(“Accepted”)){
//UpdateScoreForAntiSpamming
PlayerPrefs.SetInt(“sent”+table.playerPrefsName,PlayerPrefs.GetInt(table.playerPrefsName));
if(newRegistration == 1){
status = "Registered & " + table.displayName +“ScoreSubmitted”;
}else{
status = “New”+ table.displayName +“ScoreSubmitted”;
}
}
//StopRunning
runningHsServer = 0;
yieldnewWaitForSeconds(1); //WaitASecondBeforeSynch
SynchTable();
}
functionSynchTable(){//Update
StartCoroutine(GetHighScores(difficultyModes[modeIndex].databaseName,currentScope,maxHighScores));
}
functionCheckName(usedName : String) : String{ //Makesureimputnameisclean
varnames : String[ ] = badNamesList.text.Split(‘\n’[0]);
for(varn : Stringinnames){
if(usedName.Trim().ToLower().IndexOf(n.Trim().ToLower()) > -1){
return"offensive";
}
}
return"clean";
}
functionGetHash(usedString : String): String{ //CreateaHashtosendtoserver
varmd5 : MD5 = MD5.Create();
varbytes : byte[ ] = Encoding.ASCII.GetBytes(usedString+secretKey);
varhash : byte[ ] = md5.ComputeHash(bytes);
varsb : String = “”;
for(vari : int = 0; i < hash.Length; i++){
sb += hash*.ToString(“x2”);*
}
returnsb;
}
functionStart(){
status = “”;
runningHsServer = 0;
runningTrack = 0;
StopAllCoroutines();
//NameOperations
if(existingNamePlayerPrefs != “”){
serverHighScoreName = PlayerPrefs.GetString(existingNamePlayerPrefs);
}
//GetBase
startDifficultyMode = Mathf.Clamp(startDifficultyMode,0,difficultyModes.Length-1); //Toavoidruntimeerrors
modeIndex = startDifficultyMode;
//SetInitialSettings
scoreScopeText = scoreScope.allTimeName;
currentScope = “AllTime”;
//GetScores
SynchTable();
}
functionOnGUI(){
if(skin)GUI.skin = skin;
//SetBaseRect
baseRect = newRect(Screen.width * .5F,Screen.height * .5F,100F,100F);
//UpdateUserScore
localScore = PlayerPrefs.GetInt(difficultyModes[modeIndex].playerPrefsName);
//StatusBox
if(displayOptions.showStatus){
GUI.Box(newRect(baseRect.x - messageOffsetBox.x,baseRect.y - messageOffsetBox.y,
messageOffsetBox.width,messageOffsetBox.height),status);
}
if(displayOptions.showUserScore){
//LocalScoreBox
GUI.Box(newRect(baseRect.x - localScoreOffsetRect.x,baseRect.y - localScoreOffsetRect.y,localScoreOffsetRect.width,localScoreOffsetRect.height)
,“Local”+difficultyModes[modeIndex].displayName+“Score : “+localScore+””+scoreType);
if(trackType == TrackingType.TrackProgress){
if(PlayerPrefs.GetInt(“nameRegistered”) == 1){
if(runningTrack == 0){
GUI.Box(newRect(baseRect.x - serverScoreOffsetRect.x,baseRect.y - serverScoreOffsetRect.y,serverScoreOffsetRect.width,serverScoreOffsetRect.height)
,“Global”+difficultyModes[modeIndex].displayName+“Score : “+serverScore+””+scoreType +“\n”
+ "GlobalRank: "+serverRank);
}else{
GUI.Box(newRect(baseRect.x - serverScoreOffsetRect.x,baseRect.y - serverScoreOffsetRect.y,
serverScoreOffsetRect.width,serverScoreOffsetRect.height),“SeekingStats”);
}
}else{
GUI.Box(newRect(baseRect.x - serverScoreOffsetRect.x,baseRect.y - serverScoreOffsetRect.y,
serverScoreOffsetRect.width,serverScoreOffsetRect.height),“SendScoreToRegister”);
}
}
}
if(displayOptions.showDifficultySwitch){
//Get & ShowHighScores
if(GUI.Button(newRect(baseRect.x - getScoresOffsetRect.x,baseRect.y - getScoresOffsetRect.y,
getScoresOffsetRect.width,getScoresOffsetRect.height),“”+difficultyModes[modeIndex].displayName)){
if(modeIndex < difficultyModes.Length - 1){
modeIndex++;
}else{modeIndex = 0;}
//Update
StartCoroutine(GetHighScores(difficultyModes[modeIndex].databaseName,currentScope,maxHighScores));
localScore = PlayerPrefs.GetInt(difficultyModes[modeIndex].playerPrefsName);
}
}
if(displayOptions.showScopeSwitch){
//SetScope
if(GUI.Button(newRect(baseRect.x - scoreScopeOffsetRect.x,baseRect.y - scoreScopeOffsetRect.y,
scoreScopeOffsetRect.width,scoreScopeOffsetRect.height),“”+scoreScopeText)){
if(scoreScopeText.Equals(scoreScope.allTimeName)){
scoreScopeText = scoreScope.dailyName;
currentScope = “Daily”;
}else{
scoreScopeText = scoreScope.allTimeName;
currentScope = “AllTime”;
}
StartCoroutine(GetHighScores(difficultyModes[modeIndex].databaseName,currentScope,maxHighScores));
}
}
if(displayOptions.showSendScore){
//SendScores
GUILayout.BeginArea(newRect(baseRect.x - sendScoresOffsetRect.x,baseRect.y - sendScoresOffsetRect.y,
sendScoresOffsetRect.width,sendScoresOffsetRect.height));
GUILayout.BeginHorizontal();
if(trackType == TrackingType.TrackProgress && PlayerPrefs.GetInt(“nameRegistered”) == 1){
serverHighScoreName = PlayerPrefs.GetString(“registeredName”);
GUILayout.Box(serverHighScoreName,GUILayout.Width(150),GUILayout.Height(30));
}else{
serverHighScoreName = GUILayout.TextField(serverHighScoreName,GUILayout.Width(150));
}
if(GUILayout.Button(“Send”)){
StartCoroutine(SendHighScores(difficultyModes[modeIndex],serverHighScoreName,localScore,difficultyModes));
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
//DisplayScores
if(displayOptions.showScores){
GUILayout.BeginArea(newRect(baseRect.x - scoresListOffsetRect.x,baseRect.y - scoresListOffsetRect.y,
scoresListOffsetRect.width,scoresListOffsetRect.height));
scrollView = GUILayout.BeginScrollView(scrollView);
for(varx : int = 1;x<serverHighScores.Length;x++){
if(x > maxHighScores){break;}
if(serverHighScores[×] != null){
varscore : String[ ] = serverHighScores[×].Split(‘:’[0]); //SplittheScoreFromPHPsetUp
if(score.Length > 1){
GUILayout.BeginHorizontal();
GUILayout.Label(x.ToString()+": ");
GUILayout.Space(10);
GUILayout.Label(score[0],GUILayout.Width(200),GUILayout.Height(30));
GUILayout.FlexibleSpace();
GUILayout.Label(score[1]+“”+scoreType,GUILayout.Width(150),GUILayout.Height(20));
GUILayout.EndHorizontal();
}
}else{
GUILayout.Label(serverHighScores[×] +“”+scoreType,GUILayout.Width(100));
}
}
GUILayout.EndScrollView();
GUILayout.EndArea();
}
//EditorTools
if(developmentMode == DeveloperType.debug){
EditorTools();
}
}
functionEditorTools(){
GUILayout.BeginHorizontal();
GUILayout.Label(“”,GUILayout.Width(Screen.width - 230));
GUILayout.FlexibleSpace();
GUILayout.BeginVertical();
GUILayout.Space(100);
GUILayout.Box(“CREATE”,GUILayout.Width(200));
if(GUILayout.Button(“CreateTables”,GUILayout.Width(200))){
StartCoroutine(DatabaseTools(0,difficultyModes));
}
GUILayout.Box(“MANAGE”,GUILayout.Width(200));
if(GUILayout.Button(“ResetAll”+difficultyModes[modeIndex].displayName,GUILayout.Width(200))){
StartCoroutine(ResetHighScores(difficultyModes[modeIndex].databaseName,“Reset”));
}
if(GUILayout.Button(“DeleteAll”+difficultyModes[modeIndex].displayName,GUILayout.Width(200))){
StartCoroutine(ResetHighScores(difficultyModes[modeIndex].databaseName,“Delete”));
}
if(GUILayout.Button("DeleteBelowScore: " +difficultyModes[modeIndex].displayName ,GUILayout.Width(200))){
StartCoroutine(DatabaseTools(1,difficultyModes[modeIndex].databaseName));
}
GUILayout.BeginHorizontal();
GUILayout.Box(“”+deleteScore,GUILayout.Width(50));
deleteScore = Mathf.CeilToInt(GUILayout.HorizontalSlider(deleteScore,1,maxResetScore,GUILayout.Width(150)));
GUILayout.EndHorizontal();
//
if(GUILayout.Button("DeleteBelowDate: "+difficultyModes[modeIndex].displayName,GUILayout.Width(200))){
StartCoroutine(DatabaseTools(2,difficultyModes[modeIndex].databaseName));
}
GUILayout.BeginHorizontal();
GUILayout.Label(“”+year,GUILayout.Width(30));
year = Mathf.CeilToInt(GUILayout.HorizontalSlider(year,2012,2013,GUILayout.Width(30)));
GUILayout.Label(“”+month,GUILayout.Width(20));
month = Mathf.CeilToInt(GUILayout.HorizontalSlider(month,1,12,GUILayout.Width(40)));
GUILayout.Label(“”+day,GUILayout.Width(20));
day = Mathf.CeilToInt(GUILayout.HorizontalSlider(day,1,31,GUILayout.Width(40)));
GUILayout.EndHorizontal();
GUILayout.Box(“TESTDATA”,GUILayout.Width(200));
if(GUILayout.Button(“NullRegistration”,GUILayout.Width(200))){
PlayerPrefs.SetInt(“nameRegistered”,0);
PlayerPrefs.SetString(“registeredName”,“”);
}
//
GUILayout.BeginHorizontal();
GUILayout.Box("Score: "+PlayerPrefs.GetInt(difficultyModes[modeIndex].playerPrefsName),GUILayout.Width(100));
PlayerPrefs.SetInt(difficultyModes[modeIndex].playerPrefsName,Mathf.CeilToInt(GUILayout.HorizontalSlider(PlayerPrefs.GetInt(difficultyModes[modeIndex].playerPrefsName),1,maxResetScore,GUILayout.Width(100))));
GUILayout.EndHorizontal();
//
GUILayout.EndVertical();
GUILayout.EndHorizontal();
}