Project

General

Profile

« Previous | Next » 

Revision 1f6d1786

Added by Leszek Koltunski almost 2 years ago

Lots of progress with the main screen

View differences:

distorted-sokoban/src/main/java/org/distorted/sokoban/SokobanDatabase.java
14 14
public class SokobanDatabase 
15 15
{ 
16 16
	private static final String TAG_DB = "SokobanDatabase";
17
	private static final String PREFS_NAME = "sokobanPrefsFile";	
18
	
17
	private static final String PREFS_NAME = "sokobanPrefsFile";
19 18
	public static final int BEGIN_LEVELS = 1000;
20 19
	public static final int INVALID = 1000000;
21 20

  
22
	private static int numrunnings;
23
	private static int numplayings;
24
	private static int hash;
25
	private static String username;
26
	private static String veriname;
27
	private static int uniqueid;
28
	private static int scrollpos;
29
	private static boolean finishedBootup = false;
30
	private static String iso = null;
31
	 
32
	private static SokobanLevels mLevels;
33
	private static SokobanDatabase mThis =null;
21
  private static SokobanDatabase mThis =null;
22

  
23
	private int mNumRunnings;
24
	private int mNumPlayings;
25
	private String mUsername;
26
	private String mVeriname;
27
	private int mUniqueid;
28
	private int mScrollpos;
29
	private final boolean mFinishedBootup;
30
	private String mISO = null;
34 31
	private final WeakReference<Context> mContext;
35
	
36 32

  
37 33
	private static final String[] bl = new String[]
38 34
	  {
......
633 629
	{
634 630
	    mContext = new WeakReference<>(co);
635 631
	    
636
	    mLevels = SokobanLevels.getInstance();
637
	    numrunnings = 0;
638
	    numplayings = 0;
639
	    uniqueid    = 0;
640
	    username    ="";
641
	    veriname    ="";
642
	    scrollpos   = 0;
632
	    SokobanLevels levels = SokobanLevels.getInstance();
633
	    mNumRunnings = 0;
634
	    mNumPlayings = 0;
635
	    mUniqueid    = 0;
636
	    mUsername    ="";
637
	    mVeriname    ="";
638
	    mScrollpos   = 0;
643 639
	    
644 640
	    for(int i=0; i<BUILTIN_LEVELS; i++)
645 641
	      {
646 642
	      int xlen = Integer.parseInt(bl[2*i]);
647 643
	      SokobanLevel sl = new SokobanLevel( null, null, INVALID, INVALID, null, false,xlen, bl[2*i+1]);
648
	      mLevels.addLevel(sl,i);
644
	      levels.addLevel(sl,i);
649 645
	      }
650 646

  
651
	    hash = computeHash();
652
	    
653 647
	    try
654 648
		  {
655 649
		  SharedPreferences settings = co.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
......
660 654
		  Log.e( TAG_DB, "Failed to retrieve preferences: "+ex.toString());
661 655
		  }
662 656
	    
663
	    if( veriname==null ) veriname="";
664
	    if( username==null ) username="";  
665
	    if( uniqueid==0 ) uniqueid = getDeviceID();
666

  
667
	    mLevels.setScroll(scrollpos);
668
	    mLevels.updateMyRecords(username);
657
	    if( mVeriname==null ) mVeriname="";
658
	    if( mUsername==null ) mUsername="";
659
	    if( mUniqueid==0 ) mUniqueid = getDeviceID();
669 660

  
670
	    if( numrunnings==0 ) SokobanCanvas.setState(SokobanCanvas.STATE_HELP);
661
	    levels.setScroll(mScrollpos);
662
	    levels.updateMyRecords(mUsername);
671 663

  
672
	    if( hash != computeHash() )  // someone has been messing with RMS !
673
	      {
674
	    Log.e(TAG_DB, "messing in DB");	
675
	    	
676
	      SokobanLevel sl;
677
	      veriname = "";
678

  
679
	      for(int i=0; ; i++)
680
	        {
681
	        sl = SokobanLevels.getLevel(i);
682
	        if( sl==null ) break;
683
	        sl.resetPrivate();
684
	        }
685
	      }
686

  
687
	    finishedBootup = true;
664
	    mFinishedBootup = true;
688 665
	    SokobanCanvas.setRepaint();
689 666
	}
690 667

  
......
705 682
            
706 683
            	try
707 684
            	{
708
            		num = Integer.valueOf(key).intValue();
685
            		num = Integer.parseInt(key);
709 686
            		sett= String.valueOf(val);
710 687
            		
711 688
            		switch(num)
712 689
            		{
713
            		case 1 : username    = sett; break;
714
            		case 2 : veriname    = sett; break;
715
            		case 3 : hash        = Integer.valueOf(sett).intValue(); break;
716
            		case 4 : numrunnings = Integer.valueOf(sett).intValue(); break;
717
            		case 5 : numplayings = Integer.valueOf(sett).intValue(); break;
718
            		case 6 : uniqueid    = Integer.valueOf(sett).intValue(); break;
719
            		case 7 : scrollpos   = Integer.valueOf(sett).intValue(); break;
720
            		default: recoverLevel(num,sett); break;
690
            		case 1 : mUsername    = sett; break;
691
            		case 2 : mVeriname    = sett; break;
692
            		case 3 : mNumRunnings = Integer.parseInt(sett); break;
693
            		case 4 : mNumPlayings = Integer.parseInt(sett); break;
694
            		case 5 : mUniqueid    = Integer.parseInt(sett); break;
695
            		case 6 : mScrollpos   = Integer.parseInt(sett); break;
721 696
            		}
722 697
            	}
723 698
            	catch( Exception ex )
724 699
            	{
725
            		Log.e(TAG_DB, "error retrieving preference "+String.valueOf(val)+" :"+ex.toString());
700
            		Log.e(TAG_DB, "error retrieving preference "+val+" :"+ex.toString());
726 701
            	}                     	     
727 702
            }   
728 703
        }
729 704
	}
730 705

  
731
///////////////////////////////////////////////////////////////////
732
	  
733
	private void recoverLevel(int id, String value)
734
	{
735
		int begin=0,end=0,lvlNum;
736
		String tmp;
737

  
738
		//Log.d(TAG_DB, "recovering level "+id+" :"+value);
739
		
740
		end = value.indexOf(" ", begin+1);
741

  
742
		if( end<0 )
743
		{
744
			Log.e(TAG_DB, "error recovering level! value: "+value);
745
			return;
746
		}
747

  
748
		tmp = value.substring(begin,end);
749
		lvlNum = Integer.valueOf(tmp).intValue();
750
		
751
		recoverLevelData(id,lvlNum,end,value);
752
	}
753

  
754
///////////////////////////////////////////////////////////////////
755

  
756
	private void recoverLevelData(int id, int num, int begin, String value)
757
	{
758
		int end;
759
		String tmp;
760
		SokobanLevel sl;
761
		int intrinsic   = INVALID;
762
		int mytime      = INVALID;
763
		int mymoves     = INVALID;
764
		int worldtime   = INVALID;
765
		int worldmoves  = INVALID;
766
		String worldname   = "";
767
		String worldcountry= "";
768
		boolean submitted  = false;
769

  
770
		// recover intrinsic num
771
		end = value.indexOf(" ", begin+1);
772

  
773
		if( end<0 )
774
		{
775
			Log.e(TAG_DB, "1 error recovering builtin level, value: "+value);
776
			return;
777
		}
778

  
779
		try
780
		{
781
			tmp = value.substring( begin+1,end);
782
			intrinsic = Integer.valueOf(tmp).intValue();
783
		}
784
		catch(Exception ex1 ) {Log.e(TAG_DB, "1: "+ex1.toString()); }
785

  
786
		begin = end;
787

  
788
		// recover mytime
789
		end = value.indexOf(" ", begin+1);
790

  
791
		if( end<0 )
792
		{
793
			Log.e(TAG_DB, "1 error recovering builtin level, value: "+value);
794
			return;
795
		}
796

  
797
		try
798
		{
799
			tmp = value.substring( begin+1,end);
800
			mytime = Integer.valueOf(tmp).intValue();
801
		}
802
		catch(Exception ex1 ) {Log.e(TAG_DB, "1: "+ex1.toString()); }
803

  
804
		begin = end;
805

  
806
		// recover mymoves
807
		end = value.indexOf(" ", begin+1);
808

  
809
		if( end<0 )
810
		{
811
			Log.e(TAG_DB, "2 error recovering builtin level, value: "+value);
812
			return;
813
		}
814

  
815
		try
816
		{
817
			tmp = value.substring( begin+1,end);
818
			mymoves = Integer.valueOf(tmp).intValue();
819
		}
820
		catch(Exception ex1 ) {Log.e(TAG_DB, "2: "+ex1.toString()); }
821

  
822
		begin = end;
823

  
824
		// recover worldtime
825
		end = value.indexOf(" ", begin+1);
826

  
827
		if( end<0 )
828
		{
829
			Log.e(TAG_DB, "3 error recovering builtin level, value: "+value);
830
			return;
831
		}
832

  
833
		try
834
		{
835
			tmp = value.substring( begin+1,end);
836
			worldtime = Integer.valueOf(tmp).intValue();
837
		}
838
		catch(Exception ex1 ) {Log.e(TAG_DB, "3: "+ex1.toString()); }
839

  
840
		begin = end;
841

  
842
		// recover worldmoves
843
		end = value.indexOf(" ", begin+1);
844

  
845
		if( end<0 )
846
		{
847
			Log.e(TAG_DB, "4 error recovering builtin level, value: "+value);
848
			return;
849
		}
850

  
851
		try
852
		{
853
			tmp = value.substring( begin+1,end);
854
			worldmoves = Integer.valueOf(tmp).intValue();
855
		}
856
		catch(Exception ex1 ) {Log.e(TAG_DB, "4: "+ex1.toString()); }
857

  
858
		begin = end;
859

  
860
		// 	recover worldname
861
		end = value.indexOf(" ", begin+1);
862

  
863
		if( end<0 )
864
		{
865
			Log.e(TAG_DB, "5 error recovering builtin level, value: "+value);
866
			return;
867
		}
868

  
869
		try
870
		{
871
			worldname = value.substring( begin+1,end);
872
		}
873
		catch(Exception ex1 ) {Log.e(TAG_DB, "5: "+ex1.toString()); }
874

  
875
		begin = end;
876

  
877
		// recover worldcountry
878
		end = value.indexOf(" ", begin+1);
879

  
880
		if( end<0 )
881
		{
882
			Log.e(TAG_DB, "6 error recovering builtin level, value: "+value);
883
			return;
884
		}
885

  
886
		try
887
		{
888
			worldcountry = value.substring( begin+1,end);
889
		}
890
		catch(Exception ex1 ) {Log.e(TAG_DB,"6: "+ex1.toString()); }
891

  
892
		begin = end;
893

  
894
		// recover submitted
895
		end = value.indexOf(" ", begin+1);
896

  
897
		if( end<0 )
898
		{
899
			Log.e(TAG_DB, "7 error recovering builtin level, value: "+value);
900
			return;
901
		}
902

  
903
		try
904
		{
905
			tmp = value.substring( begin+1,end);
906
			submitted = (Integer.valueOf(tmp).intValue()==1 ? true:false);
907
		}
908
		catch(Exception ex1 ) {Log.e(TAG_DB, "7: "+ex1.toString()); }
909

  
910
		begin = end;
911

  
912
		if( num<BUILTIN_LEVELS )
913
		{
914
			sl = SokobanLevels.getLevel(num);
915

  
916
			if( sl==null ) { Log.e(TAG_DB, "Error recovering builtin level "+num); return; }
917

  
918
		//	Log.d(TAG_DB, "setting my info: "+mymoves+" "+mytime+" "+username+" "+getCountry() );
919
			
920
			sl.setMyInfo(mymoves,mytime, username, getCountry() );
921
			sl.addRecordInfo(worldmoves,worldtime,0,worldname,worldcountry);
922
			sl.setSubmitted(submitted);
923
			sl.setRecordId(id);
924
		}
925
		else
926
		{
927
			String author="", coun="", position="";
928
			int cols=0;
929

  
930
			// recover author
931
			end = value.indexOf(" ", begin+1);
932

  
933
			if( end<0 )
934
			{
935
				Log.e(TAG_DB, "8 error recovering builtin level, value: "+value);
936
				return;
937
			}
938

  
939
			try
940
			{
941
				author = value.substring( begin+1,end);
942
			}
943
			catch(Exception ex1 ) {Log.e(TAG_DB, "8: "+ex1.toString()); }
944

  
945
			begin = end;
946

  
947
			// recover country
948
			end = value.indexOf(" ", begin+1);
949

  
950
			if( end<0 )
951
			{
952
				Log.e(TAG_DB, "9 error recovering builtin level, value: "+value);
953
				return;
954
			}
955

  
956
			try
957
			{
958
				coun = value.substring( begin+1,end);
959
			}
960
			catch(Exception ex1 ) {Log.e(TAG_DB, "9: "+ex1.toString()); }
961

  
962
			begin = end;
963

  
964
			// recover cols
965
			end = value.indexOf(" ", begin+1);
966

  
967
			if( end<0 )
968
			{
969
				Log.e(TAG_DB, "10 error recovering builtin level, value: "+value);
970
				return;
971
			}
972

  
973
			try
974
			{
975
				tmp = value.substring( begin+1,end);
976
				cols = Integer.valueOf(tmp).intValue();
977
			}
978
			catch(Exception ex1 ) {Log.e(TAG_DB, "10: "+ex1.toString()); }
979

  
980
			begin = end;
981

  
982
			// recover position
983
			end = value.indexOf(" ", begin+1);
984

  
985
			if( end<0 )
986
			{
987
				Log.e(TAG_DB, "11 error recovering builtin level, value: "+value);
988
				return;
989
			}
990

  
991
			try
992
			{
993
				position = value.substring( begin+1,end);
994
			}
995
			catch(Exception ex1 ) {Log.e(TAG_DB, "11: "+ex1.toString()); }
996

  
997
			begin = end;
998

  
999
	//		Log.d(TAG_DB, "loading created level: "+author+" "+coun+" "+mymoves+" "+mytime);
1000
			
1001
			SokobanRecordInfo sri = new SokobanRecordInfo(worldmoves,worldtime,worldname,worldcountry);
1002
			sl = new SokobanLevel(author,coun,mymoves,mytime,sri,submitted,cols,position);
1003
			mLevels.insertLevel(sl,num,intrinsic,id);
1004
		}	
1005
}
1006

  
1007 706
///////////////////////////////////////////////////////////////////
1008 707
	
1009 708
	private int getDeviceID()
......
1029 728
	
1030 729
	public boolean saveValues()
1031 730
	    {
1032
		//Log.e(TAG_DB, "saveValues: "+finishedBootup);
1033
		
1034
	    if( !finishedBootup ) return false;
731
	    if( !mFinishedBootup ) return false;
1035 732

  
1036 733
	    boolean ret = false;
1037 734
	   
1038
	    numrunnings++;
1039
	    hash = computeHash();
1040
	    scrollpos = SokobanLevels.getScroll();
735
	    mNumRunnings++;
736
	    mScrollpos = SokobanLevels.getScroll();
1041 737
	    
1042 738
	    try
1043 739
	      {
1044
	        Context co = mContext.get();
1045
          SharedPreferences settings = co.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
1046
          SharedPreferences.Editor editor = settings.edit();
740
	      Context co = mContext.get();
741
        SharedPreferences settings = co.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
742
        SharedPreferences.Editor editor = settings.edit();
1047 743
	      
1048
          editor.putString("1", username);
1049
          editor.putString("2", veriname);
1050
          editor.putInt("3", hash);
1051
          editor.putInt("4", numrunnings);
1052
          editor.putInt("5", numplayings);
1053
          editor.putInt("6", uniqueid);
1054
          editor.putInt("7", scrollpos);
1055
        
1056
         // Log.d(TAG_DB, "saving: username="+username+" veriname="+veriname+" hash="+hash+" numrunnings="+numrunnings+" numplayings="+numplayings+" uniqueid="+uniqueid+" scrollpos="+scrollpos);
1057
  		
1058
	      if( saveLevels(editor)==false ) return false;
744
        editor.putString("1", mUsername);
745
        editor.putString("2", mVeriname);
746
        editor.putInt("3"   , mNumRunnings);
747
        editor.putInt("4"   , mNumPlayings);
748
        editor.putInt("5"   , mUniqueid);
749
        editor.putInt("6"   , mScrollpos);
1059 750

  
1060 751
	      editor.commit();
1061 752
	      ret = true;
......
1067 758

  
1068 759
	    return ret;
1069 760
	    }
1070
 
1071
///////////////////////////////////////////////////////////////////
1072

  
1073
	private static boolean saveLevels(SharedPreferences.Editor editor)
1074
	{
1075
		String tmp;
1076
		int sb;
1077
		SokobanLevel level;
1078
		SokobanRecordInfo wr;
1079

  
1080
		for(int i=0; ; i++)
1081
		{
1082
			level = SokobanLevels.getLevel(i);
1083
			if( level==null ) break;
1084 761

  
1085
			if( level.isDirty() )
1086
			{
1087
				wr = level.getRecordInfo(0);
1088
				sb = level.isSubmitted()==true?1:0;
1089

  
1090
				if( wr!=null )
1091
					tmp = i+" "+level.getIntrinsicNum()+" "+level.getMyTime()+" "+
1092
						  level.getMyMoves()+" "+wr.getTime()+" "+wr.getMove()+" "+
1093
						  wr.getName()+" "+wr.getCountry()+" "+sb+" ";
1094
				else
1095
					tmp = i+" "+level.getIntrinsicNum()+" "+level.getMyTime()+" "+
1096
						  level.getMyMoves()+" "+INVALID+" "+INVALID+" unk unk "+sb+" ";
1097

  
1098
				if( i>=BUILTIN_LEVELS )
1099
				{
1100
					tmp+=level.getAuthor()+" "+level.getCountry()+" "+level.getCols()+" "+level.encodeLevel()+" ";
1101
				}
1102

  
1103
				editor.putString(String.valueOf(level.getRecordId()), tmp);
1104
				
1105
				//Log.d(TAG_DB, "saving level "+i+" record id:"+level.getRecordId()+": "+tmp);
1106
				
1107
			}
1108
		}
1109

  
1110
		return true;
1111
	}
1112
	
1113 762
///////////////////////////////////////////////////////////////////
1114 763

  
1115 764
	public static void init(Activity act)
......
1119 768

  
1120 769
///////////////////////////////////////////////////////////////////
1121 770

  
1122
	private static int computeHash()
1123
	{
1124
		 
1125
		final int MODULO = 227;
1126
		int ret = 0;
1127
		SokobanLevel sl;
1128

  
1129
		for(int i=0; ; i++)
1130
		   {
1131
		   sl = SokobanLevels.getLevel(i);
1132
		   if( sl==null ) break;
1133

  
1134
		   ret +=   sl.getMyMoves();
1135
		   ret += 2*sl.getMyTime();
1136
		   }
1137

  
1138
		int length = veriname==null ? 0 : veriname.length();
1139

  
1140
		for(int i=0;i<length;i++)
1141
		   ret += i*veriname.charAt(i);
1142

  
1143
		return (ret%=MODULO);
1144
	}
1145
	
1146
///////////////////////////////////////////////////////////////////
1147

  
1148
	public static int getNumRunnings()
771
	public int getNumRunnings()
1149 772
	{
1150
		return numrunnings;
773
		return mNumRunnings;
1151 774
	}
1152 775

  
1153 776
///////////////////////////////////////////////////////////////////
1154 777

  
1155
	public static int getId()
778
	public int getId()
1156 779
	{
1157
	    return uniqueid;
780
	    return mUniqueid;
1158 781
	}
1159 782

  
1160 783
///////////////////////////////////////////////////////////////////
1161 784
	
1162
	public static int getNumPlayings()
785
	public int getNumPlayings()
1163 786
	{
1164
		return numplayings;
787
		return mNumPlayings;
1165 788
	}
1166 789

  
1167 790
///////////////////////////////////////////////////////////////////
1168 791

  
1169
	public static void incNumPlayings()
792
	public void incNumPlayings()
1170 793
	{
1171
		numplayings++;
794
		mNumPlayings++;
1172 795
	}
1173 796

  
1174 797
///////////////////////////////////////////////////////////////////
1175 798
	
1176
	public static void setName(String newname)
799
	public void setName(String newname)
1177 800
	{
1178
		username = newname;
801
		mUsername = newname;
1179 802
	}
1180 803

  
1181 804
///////////////////////////////////////////////////////////////////
1182 805
	
1183
	public static String getName()
806
	public String getName()
1184 807
	{
1185
		return username;
808
		return mUsername;
1186 809
	}
1187 810

  
1188 811
///////////////////////////////////////////////////////////////////
1189 812

  
1190
	public static void setVeri(String newveri)
813
	public void setVeri(String newveri)
1191 814
	{
1192
		veriname = newveri;
815
		mVeriname = newveri;
1193 816
	}
1194 817

  
1195 818
///////////////////////////////////////////////////////////////////
1196 819

  
1197
	public static String getVeri()
820
	public String getVeri()
1198 821
	{
1199
		return veriname;
822
		return mVeriname;
1200 823
	}
1201 824

  
1202 825
///////////////////////////////////////////////////////////////////
......
1208 831

  
1209 832
///////////////////////////////////////////////////////////////////
1210 833

  
1211
	public static String getCountry()
834
  public int getNumLevels()
835
		{
836
		return BUILTIN_LEVELS;
837
		}
838

  
839
///////////////////////////////////////////////////////////////////
840

  
841
	public String getCountry()
1212 842
	{
1213
		if( iso==null ) iso = SokobanCanvas.getIso();
843
		if( mISO==null ) mISO = SokobanCanvas.getIso();
1214 844
	
1215 845
		char digit1, digit2;
1216 846
		
1217 847
		try
1218 848
		  {
1219
		  digit1 = iso.charAt(0);
1220
		  digit2 = iso.charAt(1);
849
		  digit1 = mISO.charAt(0);
850
		  digit2 = mISO.charAt(1);
1221 851
		  }
1222 852
		catch( Exception ex )
1223 853
		  {

Also available in: Unified diff