-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented #27 (ui will be next committed) ippatsu now works properly riichi is not combined anymore with double riichi fix #33 fix #34 notes: mahjong balance changes not by pure win/lose points, but depending on already having balance - the more you have already, the less you win. This will be properly documented later. mahjong streak changes if you are not tempai. If you win, it's +1, otherwise it's =0. But if you are tempai, it won't be affected. now riichi bet possibility also depends on a player's balance, it's impossible if player has less than 1000 points to required bet saving games for future #36 statistics implementation, profile system (database only) not tested as far.
- Loading branch information
N1ckn1ght
committed
Jun 24, 2022
1 parent
90c1016
commit a34a7fe
Showing
10 changed files
with
320 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package com.example.riichit | ||
|
||
import android.util.Log | ||
import com.example.riichit.Ruleset.newBalance | ||
import com.example.riichit.Utility.operationsLimit | ||
import com.example.riichit.Utility.toInt | ||
|
||
object Operations { | ||
fun addGame( | ||
db: AppDatabase, | ||
profile: Int, | ||
hand: MutableList<Int>, | ||
tsumo: Int, | ||
kanTiles: MutableList<Int>, | ||
discard: MutableList<Int>, | ||
yakuConditional: Map<String, Boolean>? = null | ||
) { | ||
val yakuConditionalArray = arrayOf(0, 0, 0, 0, 0, 0) | ||
yakuConditional?.let { | ||
var i = 0 | ||
for ((_, v) in it) { | ||
yakuConditionalArray[i++] = v.toInt() | ||
} | ||
} | ||
|
||
val handString = hand.toIntArray().contentToString() | ||
val kanTilesString = kanTiles.toIntArray().contentToString() | ||
val discardString = discard.toIntArray().contentToString() | ||
val yakuConditionalString = yakuConditionalArray.contentToString() | ||
|
||
db.gamesDao().addGame( | ||
profile, | ||
handString, | ||
tsumo, | ||
kanTilesString, | ||
discardString, | ||
yakuConditionalString | ||
) | ||
} | ||
|
||
fun updateProfile( | ||
db: AppDatabase, | ||
profile: Int, | ||
mode: Int, | ||
balanceChange: Int, | ||
streakChange: Int | ||
) { | ||
val data = getProfile(db, profile) | ||
|
||
if (data.isEmpty()) { | ||
Log.d("d/operations", "Profile $profile not found in database $db!") | ||
return | ||
} | ||
|
||
var mahjongBalance = data[0].mahjongBalance | ||
var mahjongStreak = data[0].mahjongStreak | ||
var manBalance = data[0].manBalance | ||
var manStreak = data[0].manStreak | ||
when (mode) { | ||
0 -> { | ||
mahjongBalance = newBalance(mahjongBalance, balanceChange) | ||
when (streakChange) { | ||
-1 -> { | ||
mahjongStreak = 0 | ||
} | ||
1 -> { | ||
mahjongStreak += 1 | ||
} | ||
} | ||
} | ||
1 -> { | ||
manBalance = newBalance(manBalance, balanceChange) | ||
when (streakChange) { | ||
-1 -> { | ||
manStreak = 0 | ||
} | ||
1 -> { | ||
manStreak += 1 | ||
} | ||
} | ||
} | ||
} | ||
updateProfile(db, profile, mahjongBalance, mahjongStreak, manBalance, manStreak) | ||
} | ||
|
||
fun createProfile( | ||
db: AppDatabase, | ||
profile: Int | ||
) { | ||
var recursionLevel = 0 | ||
|
||
var data = getProfile(db, profile) | ||
while (data.isEmpty() && recursionLevel++ < operationsLimit) { | ||
db.profilesDao().createProfile() | ||
data = getProfile(db, profile) | ||
} | ||
} | ||
|
||
fun getBalance( | ||
db: AppDatabase, | ||
profile: Int, | ||
mode: Int | ||
): Int { | ||
val data = getProfile(db, profile) | ||
|
||
if (data.isEmpty()) { | ||
Log.d("d/operations", "Profile $profile not found in database $db!") | ||
return 0 | ||
} | ||
|
||
when (mode) { | ||
0 -> { | ||
return data[0].mahjongBalance | ||
} | ||
1 -> { | ||
return data[0].manBalance | ||
} | ||
} | ||
return 0 | ||
} | ||
|
||
private fun getProfile( | ||
db: AppDatabase, | ||
profile: Int | ||
): List<ProfileEntity> { | ||
return db.profilesDao().getProfile(profile) | ||
} | ||
|
||
private fun updateProfile( | ||
db: AppDatabase, | ||
profile: Int, | ||
mahjongBalance: Int, | ||
mahjongStreak: Int, | ||
manBalance: Int, | ||
manStreak: Int | ||
) { | ||
db.profilesDao().updateProfile(profile, mahjongBalance, mahjongStreak, manBalance, manStreak) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.example.riichit | ||
|
||
import android.content.Context | ||
import androidx.lifecycle.LiveData | ||
import androidx.room.* | ||
|
||
@Entity(tableName = "games") | ||
data class GameEntity( | ||
@PrimaryKey(autoGenerate = true) var id: Int, | ||
@ColumnInfo(name = "profile", defaultValue = "0") var profile: Int, | ||
@ColumnInfo(name = "hand", defaultValue = "[]") var hand: String, | ||
@ColumnInfo(name = "tsumo", defaultValue = "136") var tsumo: Int, | ||
@ColumnInfo(name = "kan_tiles", defaultValue = "[]") var kanTiles: String, | ||
@ColumnInfo(name = "discard", defaultValue = "[]") var discard: String, | ||
@ColumnInfo(name = "yaku_conditional", defaultValue = "[0, 0, 0, 0, 0, 0]") var yakuConditional: String, | ||
@ColumnInfo(name = "datetime", defaultValue = "CURRENT_TIMESTAMP") var datetime: String, | ||
) | ||
|
||
@Entity(tableName = "profiles") | ||
data class ProfileEntity( | ||
@PrimaryKey(autoGenerate = true) var id: Int, | ||
@ColumnInfo(name = "mahjong_balance", defaultValue = "0") var mahjongBalance: Int, | ||
@ColumnInfo(name = "man_balance", defaultValue = "0") var manBalance: Int, | ||
@ColumnInfo(name = "mahjong_streak", defaultValue = "0") var mahjongStreak: Int, | ||
@ColumnInfo(name = "man_streak", defaultValue = "0") var manStreak: Int | ||
) | ||
|
||
@Dao | ||
interface GamesDao { | ||
@Query("INSERT INTO games (profile, hand, tsumo, kan_tiles, discard, yaku_conditional) VALUES (:profile, :hand, :tsumo, :kanTiles, :discard, :yakuConditional)") | ||
fun addGame(profile: Int, hand: String, tsumo: Int, kanTiles: String, discard: String, yakuConditional: String) | ||
} | ||
|
||
@Dao | ||
interface ProfilesDao { | ||
@Query("INSERT INTO profiles (mahjong_balance, man_balance) VALUES (:mahjongBalance, :manBalance)") | ||
fun createProfile(mahjongBalance: Int = 25000, manBalance: Int = 25000) | ||
|
||
@Query("SELECT * FROM profiles WHERE id = (:profile)") | ||
fun getProfile(profile: Int): List<ProfileEntity> | ||
|
||
@Query("UPDATE profiles SET mahjong_balance = (:mahjongBalance), mahjong_streak = (:mahjongStreak), man_balance = (:manBalance), man_streak = (:manStreak) WHERE id = (:profile)") | ||
fun updateProfile(profile: Int, mahjongBalance: Int, manBalance: Int, mahjongStreak: Int, manStreak: Int) | ||
} | ||
|
||
@Database(entities = [GameEntity::class, ProfileEntity::class], version = 1) | ||
abstract class AppDatabase : RoomDatabase() { | ||
abstract fun gamesDao(): GamesDao | ||
abstract fun profilesDao(): ProfilesDao | ||
|
||
companion object { | ||
private var INSTANCE: AppDatabase? = null | ||
|
||
internal fun instance(context: Context): AppDatabase { | ||
if (INSTANCE == null) { | ||
INSTANCE = | ||
Room.databaseBuilder(context, AppDatabase::class.java, "quotations.db").build() | ||
} | ||
return INSTANCE!! | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.