Skip to content

Commit 2dd46c2

Browse files
author
Tomasz Szymański
committed
Obsługa opcji gry
1 parent 4782ab6 commit 2dd46c2

15 files changed

+322
-35
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919

2020
# Unity3D Generated File On Crash Reports
2121
sysinfo.txt
22+
BreakoutUnity/Breakout/Assets/data.mdb
23+
BreakoutUnity/Breakout/Assets/data.mdb.meta
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Assets.Scripts.Database.Controllers
7+
{
8+
public static class Facade<T>
9+
{
10+
public static List<T> FindAll()
11+
{
12+
return DBContext.LocalDB.LoadAll<T>().ToList();
13+
}
14+
15+
public static T Find(T item)
16+
{
17+
IEnumerable<T> query = from T t in DBContext.LocalDB
18+
where t.Equals(item)
19+
select t;
20+
21+
T result = query.FirstOrDefault();
22+
23+
return result;
24+
}
25+
26+
internal static bool Save(T item)
27+
{
28+
try
29+
{
30+
DBContext.LocalDB.StoreObject(item);
31+
}
32+
catch (Exception ex)
33+
{
34+
return false;
35+
throw;
36+
}
37+
return true;
38+
}
39+
40+
internal static void Delete(T item)
41+
{
42+
DBContext.LocalDB.Delete(item);
43+
}
44+
45+
public static string ToStringAll()
46+
{
47+
string result = string.Empty;
48+
List<T> data = FindAll();
49+
//Debug.Log(scores.Count);
50+
data.Sort();
51+
52+
for (int i = 0; i < data.Count; ++i)
53+
{
54+
//Debug.LogFormat("{0},{1}", "i=", i);
55+
result += String.Format("{0}.\t{1}\n", i + 1, data[i].ToString());
56+
}
57+
58+
return result;
59+
}
60+
}
61+
}

BreakoutUnity/Breakout/Assets/Scripts/Database/Controllers/Facade.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BreakoutUnity/Breakout/Assets/Scripts/Database/Controllers/Option.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Assets.Scripts.Database.Models
7+
{
8+
public partial class Option : IComparable, IEquatable<Option>
9+
{
10+
public int CompareTo(object obj)
11+
{
12+
Option c = (Option)obj;
13+
return Name.CompareTo(c.Name);
14+
}
15+
16+
public bool Equals(Option other)
17+
{
18+
return Name.Equals(other.Name);
19+
}
20+
21+
public override string ToString()
22+
{
23+
return String.Format("{0}: {1}", Name, Value);
24+
}
25+
26+
public static Option GetDefault(string name)
27+
{
28+
Option result;
29+
30+
switch(name)
31+
{
32+
case OptionName.Controls:
33+
result = OptionDefault.Controls;
34+
break;
35+
case OptionName.Music:
36+
result = OptionDefault.Music;
37+
break;
38+
case OptionName.Sound:
39+
result = OptionDefault.Sound;
40+
break;
41+
case OptionName.UnlockedLevel:
42+
result = OptionDefault.UnlockedLevel;
43+
break;
44+
default:
45+
result = null;
46+
break;
47+
}
48+
49+
return result;
50+
}
51+
}
52+
}

BreakoutUnity/Breakout/Assets/Scripts/Database/Controllers/Option/OptionController.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Assets.Scripts.Database.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace Assets.Scripts.Database.Controllers
8+
{
9+
public class OptionFacade
10+
{
11+
public static List<Option> FindAll()
12+
{
13+
return Facade<Option>.FindAll();
14+
}
15+
16+
public static Option Find(string optionName)
17+
{
18+
Option o = Facade<Option>.Find(Option.GetDefault(optionName));
19+
20+
return o ?? Option.GetDefault(optionName);
21+
}
22+
23+
public static bool Save(Option option)
24+
{
25+
return Facade<Option>.Save(option);
26+
}
27+
28+
public static bool Save(string name, Enum value)
29+
{
30+
//if(value is ControlOption)
31+
return Save(new Option() { Name = name, Value = value });
32+
}
33+
34+
public static void Delete(Option option)
35+
{
36+
Facade<Option>.Delete(option);
37+
}
38+
}
39+
}

BreakoutUnity/Breakout/Assets/Scripts/Database/Controllers/Option/OptionFacade.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BreakoutUnity/Breakout/Assets/Scripts/Database/Controllers/Score/HighscoreFacade.cs

+15-33
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ public static class HighscoreFacade
1010
{
1111
public static List<Highscore> FindAll()
1212
{
13-
return DBContext.LocalDB.LoadAll<Highscore>().ToList();
13+
List<Highscore> list = Facade<Highscore>.FindAll();
14+
list.Sort();
15+
16+
return list;
1417
}
1518

1619
public static List<Highscore> FindBest10()
@@ -40,42 +43,37 @@ orderby h.Score ascending
4043
return query.FirstOrDefault();
4144
}
4245

43-
public static bool Insert(Highscore score)
46+
public static bool Save(Highscore score)
4447
{
4548
List<Highscore> scores = FindBest10();
4649

4750
if (scores.Count < 10
4851
|| scores[9].Score < score.Score)
4952
{
50-
try
51-
{
52-
DBContext.LocalDB.StoreObject(score);
53-
DeleteOverBest10();
54-
}
55-
catch (Exception ex)
56-
{
57-
return false;
58-
throw;
59-
}
53+
bool result = Facade<Highscore>.Save(score);
54+
//Debug.Log(result.ToString());
55+
if (result) DeleteOverBest10();
56+
57+
return result;
6058
}
6159
return true;
6260
}
6361

64-
public static bool Insert(string name, int score)
62+
public static bool Save(string name, int score)
6563
{
66-
return Insert(new Highscore() { Name = name, Score = score });
64+
return Save(new Highscore() { Name = name, Score = score });
6765
}
6866

6967
public static void InsertTestHighScore()
7068
{
7169
Highscore score = new Highscore() { Name = "Test" + ((int)(UnityEngine.Random.value * 100)).ToString(), Score = (int)(UnityEngine.Random.value * 1000) };
72-
//Debug.LogFormat("Test Score: {0}", score.ToString());
73-
HighscoreFacade.Insert(score);
70+
Debug.LogFormat("Test Score: {0}", score.ToString());
71+
HighscoreFacade.Save(score);
7472
}
7573

7674
public static void Delete(Highscore highscore)
7775
{
78-
DBContext.LocalDB.Delete(highscore);
76+
Facade<Highscore>.Delete(highscore);
7977
}
8078

8179
public static void DeleteOverBest10()
@@ -88,21 +86,5 @@ public static void DeleteOverBest10()
8886
Delete(h);
8987
}
9088
}
91-
92-
public static string ToStringAll()
93-
{
94-
string highscore = string.Empty;
95-
List<Highscore> scores = HighscoreFacade.FindAll();
96-
//Debug.Log(scores.Count);
97-
scores.Sort();
98-
99-
for(int i = 0; i < scores.Count; ++i)
100-
{
101-
//Debug.LogFormat("{0},{1}", "i=", i);
102-
highscore += String.Format("{0}.\t{1}\n", i+1, scores[i].ToString());
103-
}
104-
105-
return highscore;
106-
}
10789
}
10890
}

BreakoutUnity/Breakout/Assets/Scripts/Database/Models/Dictionaries.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Assets.Scripts.Database.Models
7+
{
8+
public static class OptionName
9+
{
10+
public const string Controls = "Controls";
11+
public const string Music = "Music";
12+
public const string Sound = "Sound";
13+
public const string UnlockedLevel = "UnlockedLevel";
14+
}
15+
16+
public static class OptionDefault
17+
{
18+
public static Option Controls = new Option() { Name = OptionName.Controls, Value = ControlOption.Arrows };
19+
public static Option Music = new Option() { Name = OptionName.Music, Value = OnOffOption.On };
20+
public static Option Sound = new Option() { Name = OptionName.Sound, Value = OnOffOption.On };
21+
public static Option UnlockedLevel = new Option() { Name = OptionName.Controls, Value = UnlockedLevelOption.Level01 };
22+
}
23+
24+
public enum ControlOption
25+
{
26+
Arrows = 0,
27+
Paddle,
28+
Gyroscope
29+
}
30+
31+
public enum OnOffOption
32+
{
33+
Off = 0,
34+
On = 1
35+
}
36+
37+
public enum UnlockedLevelOption
38+
{
39+
Level01 = 1,
40+
Level02,
41+
Level03,
42+
Level04,
43+
Level05,
44+
Level06,
45+
Level07,
46+
Level08,
47+
Level09,
48+
Level10,
49+
Level11,
50+
Level12,
51+
Level13,
52+
Level14,
53+
Level15,
54+
Level16,
55+
Level17,
56+
Level18,
57+
Level19,
58+
Level20
59+
}
60+
}

BreakoutUnity/Breakout/Assets/Scripts/Database/Models/Dictionaries/OptionDictionary.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Assets.Scripts.Database.Models
7+
{
8+
public partial class Option
9+
{
10+
//public int OID { get; set; }
11+
public string Name { get; set; }
12+
public Enum Value { get; set; }
13+
}
14+
}

0 commit comments

Comments
 (0)