Skip to content

Commit 59b6c28

Browse files
author
A. Apesteguia
committed
FINAL GAME SORRY
1 parent de57437 commit 59b6c28

File tree

12 files changed

+122
-22
lines changed

12 files changed

+122
-22
lines changed

app/src/main/java/com/codebinars/a2048game/EditScoreActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ private void saveImage(Bitmap finalBitmap) {
190190
try {
191191
FileOutputStream out = new FileOutputStream(file);
192192
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 40, out);
193-
System.out.println(imageroot);
194193
out.flush();
195194
out.close();
196195
} catch (Exception e) {

app/src/main/java/com/codebinars/a2048game/GameActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import android.app.Activity;
44
import android.os.Bundle;
5-
65
import androidx.annotation.Nullable;
6+
77
public class GameActivity extends Activity {
88
@Override
99
protected void onCreate(@Nullable Bundle savedInstanceState) {

app/src/main/java/com/codebinars/a2048game/database/DBHelper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ private DBHelper(Context applicationContext) {
5757
super(applicationContext, DB_NAME, null, 2);
5858
}
5959

60-
6160
@Override
6261
public void onCreate(SQLiteDatabase db) {
6362
db.execSQL(CREATE_USER_TABLE_STATEMENT);
@@ -172,8 +171,8 @@ public List<ScoreDisplay> getTop10ByUser(String usertop) {
172171
COLUMN_USERNAME+", "+COLUMN_IMAGE+", "+COLUMN_COUNTRY +
173172
" FROM "+USER_TABLE +" USER"+
174173
" INNER JOIN "+SCORE_TABLE+ " SCORE" + " ON "+ "USER."+COLUMN_ID +" = "+COLUMN_USERNAME_ID +
175-
" WHERE " + COLUMN_USERNAME + " = '" + usertop +
176-
"' ORDER BY " + COLUMN_SCORE + " DESC LIMIT 10";
174+
" WHERE " + COLUMN_USERNAME + " LIKE " + "'%" + usertop + "%'" +
175+
" ORDER BY " + COLUMN_SCORE + " DESC LIMIT 10";
177176

178177
Cursor cursor = db.rawQuery(queryAllScores, null);
179178
if (cursor.moveToNext()) {

app/src/main/java/com/codebinars/a2048game/databaseRoom/ScoreDao.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import androidx.room.Dao;
44
import androidx.room.Delete;
55
import androidx.room.Insert;
6+
import androidx.room.OnConflictStrategy;
67
import androidx.room.Query;
78

89
import java.util.List;
@@ -16,20 +17,28 @@ public interface ScoreDao {
1617
@Insert(onConflict = REPLACE)
1718
void addScore(ScoreModel scoreModel);
1819

20+
//Check if user exists. In case it doesn't, create it
21+
@Insert(onConflict = OnConflictStrategy.IGNORE)
22+
Long addUser(UserModel userModel); // 1 = Created, 0 = Ignored
23+
1924
//Delete query
2025
@Delete
2126
void delete(ScoreModel scoreModel);
2227

2328
//Delete by id query
2429
@Query("DELETE FROM scores WHERE score_id = :scoreId")
25-
void deleteById(int scoreId);
30+
void deleteScoreByID(int scoreId);
2631

2732
//Delete all query
2833
@Delete
29-
void deleteAllData(List<ScoreModel> scoreModelList);
34+
void deleteAllData(List<ScoreModel> scoreModelList, List<UserModel> userModelList);
35+
36+
//Update user
37+
@Query("UPDATE users SET user_name = :uName, user_avatar = :uAvatar, user_country = :uCountry WHERE user_id = :uID")
38+
void updateUser(int uID, String uName, String uAvatar, String uCountry);
3039

31-
//Update query
32-
@Query("UPDATE scores SET score_username = :sUsername, score_datetime = :sDateTime, score_duration = :sDuration WHERE score_id = :sID")
40+
//Update score
41+
@Query("UPDATE scores SET score_username_id = :sUsername, score_datetime = :sDateTime, score_duration = :sDuration WHERE score_id = :sID")
3342
void updateScore(int sID, String sUsername, String sDateTime, Float sDuration);
3443

3544
//Select All
@@ -38,14 +47,13 @@ public interface ScoreDao {
3847

3948
//Top 10
4049
@Query("SELECT * FROM scores ORDER BY score_value DESC LIMIT 10")
41-
List<ScoreModel> getTop10();
50+
List<ScoreModel> getTop10Scores();
4251

4352
//Top 10 by User
44-
@Query("SELECT * FROM scores WHERE score_value = :qUser ORDER BY score_value DESC LIMIT 10")
45-
List<ScoreModel> getTop10ByUsername(String qUser);
53+
@Query("SELECT * FROM scores WHERE score_username_id = :qUser ORDER BY score_value DESC")
54+
List<ScoreModel> getTop10ByUser(String qUser);
4655

4756
//Top by Score
4857
@Query("SELECT MAX (score_value) FROM scores")
4958
int getTopScore();
50-
5159
}

app/src/main/java/com/codebinars/a2048game/databaseRoom/ScoreModel.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
import androidx.annotation.NonNull;
44
import androidx.room.ColumnInfo;
55
import androidx.room.Entity;
6+
import androidx.room.ForeignKey;
67
import androidx.room.Ignore;
78
import androidx.room.PrimaryKey;
89

910
import java.util.UUID;
1011

11-
@Entity(tableName = "scores")
12+
import static androidx.room.ForeignKey.CASCADE;
13+
14+
@Entity(
15+
tableName = "scores",
16+
foreignKeys = {
17+
@ForeignKey(entity = UserModel.class,
18+
parentColumns = "user_id",
19+
childColumns = "score_username_id")})
1220
public class ScoreModel {
1321

1422
@PrimaryKey
@@ -19,7 +27,7 @@ public class ScoreModel {
1927
@ColumnInfo(name = "score_value")
2028
protected Integer mScore;
2129

22-
@ColumnInfo(name = "score_username")
30+
@ColumnInfo(name = "score_username_id")
2331
protected String mUsername;
2432

2533
@ColumnInfo(name = "score_datetime")

app/src/main/java/com/codebinars/a2048game/databaseRoom/ScoreRoomDB.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import androidx.room.Room;
77
import androidx.room.RoomDatabase;
88

9-
@Database(entities = {ScoreModel.class}, version = 2, exportSchema = false)
9+
@Database(entities = {ScoreModel.class, UserModel.class}, version = 2, exportSchema = false)
1010
public abstract class ScoreRoomDB extends RoomDatabase {
1111
private static ScoreRoomDB database;
1212
public static String DATABASE_NAME = "scoreDB";
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.codebinars.a2048game.databaseRoom;
2+
3+
4+
import androidx.annotation.NonNull;
5+
import androidx.room.ColumnInfo;
6+
import androidx.room.Entity;
7+
import androidx.room.ForeignKey;
8+
import androidx.room.Ignore;
9+
import androidx.room.PrimaryKey;
10+
import com.codebinars.a2048game.databaseRoom.*;
11+
12+
import java.util.UUID;
13+
14+
@Entity(tableName = "users")
15+
public class UserModel {
16+
17+
@PrimaryKey
18+
@NonNull
19+
@ColumnInfo(name = "user_id")
20+
protected String uId;
21+
22+
@ColumnInfo(name = "user_name")
23+
protected String uName;
24+
25+
@ColumnInfo(name = "user_country")
26+
protected String uCountry;
27+
28+
@ColumnInfo(name = "user_avatar")
29+
protected String uAvatarpath;
30+
31+
@Ignore
32+
public UserModel(@NonNull String uId, String uName, String uCountry, String uAvatarpath) {
33+
this.uId = UUID.randomUUID().toString();
34+
this.uName = uName;
35+
this.uCountry = uCountry;
36+
this.uAvatarpath = uAvatarpath;
37+
}
38+
39+
public UserModel(String uName, String uCountry, String uAvatarpath) {
40+
this.uName = uName;
41+
this.uCountry = uCountry;
42+
this.uAvatarpath = uAvatarpath;
43+
}
44+
45+
@NonNull
46+
public String getuId() {
47+
return uId;
48+
}
49+
50+
public void setuId(@NonNull String uId) {
51+
this.uId = uId;
52+
}
53+
54+
public String getuName() {
55+
return uName;
56+
}
57+
58+
public void setuName(String uName) {
59+
this.uName = uName;
60+
}
61+
62+
public String getuCountry() {
63+
return uCountry;
64+
}
65+
66+
public void setuCountry(String uCountry) {
67+
this.uCountry = uCountry;
68+
}
69+
70+
public String getuAvatarpath() {
71+
return uAvatarpath;
72+
}
73+
74+
public void setuAvatarpath(String uAvatarpath) {
75+
this.uAvatarpath = uAvatarpath;
76+
}
77+
78+
@Override
79+
public String toString() {
80+
return "UserModel{" +
81+
"uId='" + uId + '\'' +
82+
", uName='" + uName + '\'' +
83+
", uCountry='" + uCountry + '\'' +
84+
", uAvatarpath='" + uAvatarpath + '\'' +
85+
'}';
86+
}
87+
}

app/src/main/java/com/codebinars/a2048game/engine/TileManager.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ public void finishedMoving(Tile t) {
347347
* First: Check if we can add more values to Tiles
348348
* Second: Check if there are value matches (Skip if First case is possible)
349349
*/
350-
private void checkEndgame() {
350+
private synchronized void checkEndgame() {
351351
endGame = true;
352352
for (int i = 0; i < 4; i++) {
353353
for (int j = 0; j < 4; j++) {
@@ -429,6 +429,4 @@ public void updateScore(int delta) {
429429
public void reached2048() {
430430
callback.reached2048();
431431
}
432-
433-
434432
}

app/src/main/java/com/codebinars/a2048game/engine/sprites/Score.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public void draw(Canvas canvas) {
8181

8282
@Override
8383
public void update() {
84-
8584
}
8685

8786
public void updateScore(int delta) {

app/src/main/java/com/codebinars/a2048game/scoresView/ImageUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class ImageUtils {
1111
public static Bitmap loadImage(String imageRoot){
12-
Bitmap bitmap = null;
12+
Bitmap bitmap;
1313
try {
1414
File file = new File (imageRoot);
1515
FileInputStream inputStream = new FileInputStream(file);

0 commit comments

Comments
 (0)