Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submit score and reward distribution #85

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions dojo/src/models.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ pub struct Beast {
pub experience: u32,
pub next_level_experience: u32,
}

#[derive(Drop, Serde, Debug)]
#[dojo::model]
pub struct Score {
#[key]
pub player_id: ContractAddress,
pub tamagotchi_id: ContractAddress,
pub score: u32,
}
44 changes: 43 additions & 1 deletion dojo/src/systems/actions.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use babybeasts::models::Beast;
use starknet::ContractAddress;

#[starknet::interface]
trait IActions<T> {
Expand All @@ -10,13 +11,14 @@ trait IActions<T> {
fn play(ref self: T);
fn clean(ref self: T);
fn revive(ref self: T);
fn submit_score(ref self: T, score: u32);
}

#[dojo::contract]
pub mod actions {
use super::{IActions};
use starknet::{ContractAddress, get_caller_address};
use babybeasts::models::{Beast};
use babybeasts::models::{Beast, Score};

use dojo::model::{ModelStorage, ModelValueStorage};
use dojo::event::EventStorage;
Expand Down Expand Up @@ -221,5 +223,45 @@ pub mod actions {
world.write_model(@beast);
}
}

fn submit_score(ref self: ContractState, score: u32) {
let mut world = self.world(@"babybeasts");
let tamagotchi_id = get_caller_address();

let mut beast: Beast = world.read_model(tamagotchi_id);
assert(beast.player == tamagotchi_id, 'Tamagotchi');
assert(beast.is_alive == true, 'Tamagotchi is alive');

// Guardar el puntaje en el modelo Score
let new_score = Score {
player_id: tamagotchi_id,
tamagotchi_id,
score,
};
world.write_model(@new_score);

// Actualizar estadísticas del Beast basado en el puntaje
if score >= 100 {
beast.happiness += 10;
beast.energy += 5;
}

if score >= 200 {
beast.level += 1;
beast.attack += 2;
beast.defense += 2;
}

// Aquí podrías recuperar historial de puntuaciones si quieres implementar streaks
// let streak_count = world.query_scores(tamagotchi_id, last_n_days);
// if streak_count >= 5 {
// beast.speed += 1;
// }
// if streak_count >= 10 {
// beast.agility += 2;
// }

world.write_model(@beast);
}
}
}