Skip to content

Commit

Permalink
Add origin field to SnapshotConflict object
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Ibáñez Sánchez <jacobibanez@jacobibanez.com>
  • Loading branch information
Iakobs committed Oct 8, 2024
1 parent 39cda79 commit ec12747
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.8.2
### Add origin field to SnapshotConflict object
As suggested by @RProduction in [this issue](https://github.com/Iakobs/godot-play-game-services/issues/35), I added an `origin` field to the `SnapshotConflict` object coming in the `SnapshotClient.conflict_emitted` signal. This field value is either `SAVE` or `LOAD`, indicating what method originally triggered the Snapshot conflict.

## v1.8.1
### Update version in `plugin.cfg` file
The plugin version was not updated in `v1.8.0`, causing confusion to users of the plugin. This patch fixes it.
Expand Down
3 changes: 3 additions & 0 deletions plugin/export_scripts_template/autoloads/snapshots_client.gd
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,22 @@ class Snapshot:

## A class representing a conflict when saving or loading data.
class SnapshotConflict:
var origin: String ## The original caller of the method, either "SAVE" or "LOAD"
var conflict_id: String ## The conflict id.
var conflicting_snapshot: Snapshot ## The modified version of the Snapshot in the case of a conflict. This may not be the same as the version that you tried to save.
var server_snapshot: Snapshot ## The most-up-to-date version of the Snapshot known by Google Play games services to be accurate for the player’s device.

## Constructor that creates a SnapshotConflict from a [Dictionary] containing the properties.
func _init(dictionary: Dictionary) -> void:
if dictionary.has("origin"): origin = dictionary.origin
if dictionary.has("conflictId"): conflict_id = dictionary.conflictId
if dictionary.has("conflictingSnapshot"): conflicting_snapshot = Snapshot.new(dictionary.conflictingSnapshot)
if dictionary.has("serverSnapshot"): server_snapshot = Snapshot.new(dictionary.serverSnapshot)

func _to_string() -> String:
var result := PackedStringArray()

result.append("origin: %s" % origin)
result.append("conflict_id: %s" % conflict_id)
result.append("conflicting_snapshot: {%s}" % conflicting_snapshot)
result.append("server_snapshot: {%s}" % server_snapshot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ object SnapshotSignals {
/**
* This signal is emitted when saving or loading a game, whenever a conflict occurs.
*
* @return A [Dictionary] representing a [com.google.android.gms.games.SnapshotsClient.SnapshotConflict](https://developers.google.com/android/reference/com/google/android/gms/games/SnapshotsClient.SnapshotConflict).
* @return A [Dictionary] representing a [com.google.android.gms.games.SnapshotsClient.SnapshotConflict](https://developers.google.com/android/reference/com/google/android/gms/games/SnapshotsClient.SnapshotConflict), with an additional field `origin` which indicates whether the `SAVE` or `LOAD` method triggered this conflict.
*/
val conflictEmitted = SignalInfo("conflictEmitted", String::class.java)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import com.jacobibanez.plugin.android.godotplaygameservices.utils.toStringAndSav
import org.godotengine.godot.Dictionary
import org.godotengine.godot.Godot

fun fromConflict(godot: Godot, conflict: SnapshotConflict) = Dictionary().apply {
fun fromConflict(godot: Godot, origin: String, conflict: SnapshotConflict) = Dictionary().apply {
put("origin", origin)
put("conflictId", conflict.conflictId)
put("conflictingSnapshot", fromSnapshot(godot, conflict.conflictingSnapshot))
put("serverSnapshot", fromSnapshot(godot, conflict.snapshot))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import org.godotengine.godot.Dictionary
import org.godotengine.godot.Godot
import org.godotengine.godot.plugin.GodotPlugin.emitSignal

private enum class Origin {
SAVE, LOAD
}

class SnapshotsProxy(
private val godot: Godot,
private val snapshotsClient: SnapshotsClient = PlayGames.getSnapshotsClient(godot.getActivity()!!)
Expand Down Expand Up @@ -69,7 +73,7 @@ class SnapshotsProxy(
snapshotsClient.open(fileName, true, RESOLUTION_POLICY_HIGHEST_PROGRESS)
.addOnSuccessListener { dataOrConflict ->
if (dataOrConflict.isConflict) {
handleConflict(dataOrConflict.conflict)
handleConflict(Origin.SAVE.name, dataOrConflict.conflict)
return@addOnSuccessListener
}
dataOrConflict.data?.let { snapshot ->
Expand Down Expand Up @@ -100,7 +104,7 @@ class SnapshotsProxy(
if (task.isSuccessful) {
val dataOrConflict = task.result
if (dataOrConflict.isConflict) {
handleConflict(dataOrConflict.conflict)
handleConflict(Origin.LOAD.name, dataOrConflict.conflict)
return@addOnCompleteListener
}
dataOrConflict.data?.let { snapshot ->
Expand Down Expand Up @@ -186,7 +190,7 @@ class SnapshotsProxy(
}
}

private fun handleConflict(conflict: SnapshotConflict?) {
private fun handleConflict(origin: String, conflict: SnapshotConflict?) {
conflict?.let {
val snapshot = it.snapshot
val fileName = snapshot.metadata.uniqueName
Expand All @@ -199,7 +203,7 @@ class SnapshotsProxy(
godot,
GODOT_PLUGIN_NAME,
conflictEmitted,
Gson().toJson(fromConflict(godot, it))
Gson().toJson(fromConflict(godot, origin, it))
)
}
}
Expand Down

0 comments on commit ec12747

Please sign in to comment.