Skip to content

Commit

Permalink
unlizhao完成task4
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhao1 committed Dec 14, 2024
1 parent 1878cec commit f4a9903
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 426 deletions.
34 changes: 34 additions & 0 deletions mover/unlizhao/code/task4/game/Move.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 3
manifest_digest = "EB23EE5AD2BECEC8BDDF7828A2BCBFDE1CE31344B713AA0FC5B0FD1AA2F10F19"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"
dependencies = [
{ id = "Sui", name = "Sui" },
]

[[move.package]]
id = "MoveStdlib"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates\\sui-framework\\packages\\move-stdlib" }

[[move.package]]
id = "Sui"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" }

dependencies = [
{ id = "MoveStdlib", name = "MoveStdlib" },
]

[move.toolchain-version]
compiler-version = "1.39.0"
edition = "2024.beta"
flavor = "sui"

[env]

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0x88c85a9febf512857b73349c4db942805884d8b160032ad0dbd6632213ea9d7b"
latest-published-id = "0x88c85a9febf512857b73349c4db942805884d8b160032ad0dbd6632213ea9d7b"
published-version = "1"
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "task4"
name = "game"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# license = "" # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."] # e.g., ["Joe Smith ([email protected])", "John Snow ([email protected])"]

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/mainnet" }
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }

# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
# Revision can be a branch, a tag, and a commit hash.
Expand All @@ -19,7 +19,7 @@ Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-fram
# Override = { local = "../conflicting/version", override = true }

[addresses]
task4 = "0x0"
game = "0x0"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
Expand All @@ -34,4 +34,3 @@ task4 = "0x0"
# The dev-addresses section allows overwriting named addresses for the `--test`
# and `--dev` modes.
# alice = "0xB0B"

120 changes: 120 additions & 0 deletions mover/unlizhao/code/task4/game/sources/game.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
module game::game {

use std::ascii::{string, String};
use std::option;
use sui::balance::{Balance, zero};
use sui::coin;
use sui::coin::Coin;
use sui::object;
use sui::object::UID;
use sui::random;
use sui::random::{Random};
use sui::transfer::{share_object, public_transfer, transfer};
use sui::tx_context::{TxContext, sender};
use sui::linked_table;
use game::gameFaucet::GAMEFAUCET;

public struct GAME has key {
id: UID,
name: String,
balance: Balance<GAMEFAUCET>,
provider: linked_table::LinkedTable<address, u64>,
}

public struct Admin has key {
id: UID,
}

fun init(ctx: &mut TxContext) {
let game = GAME {
id: object::new(ctx),
name: string(b"looikaizhi"),
balance: zero(),
provider: linked_table::new(ctx),
};

share_object(game);
let admin = Admin { id: object::new(ctx) };
transfer(admin, ctx.sender());
}

public fun game_rule(r: &Random, ctx: &mut TxContext): bool {
let mut gen = random::new_generator(r, ctx);
let value = random::generate_u8_in_range(&mut gen, 1, 4);
if (value == 1) { return true }
else { return false }
}


entry fun play(game: &mut GAME, in_coin: Coin<GAMEFAUCET>, r: &Random, ctx: &mut TxContext) {
let player_in = in_coin.value();
let pool_balance = game.balance.value();

assert!(player_in <= pool_balance / 10);

let win_or_lose: bool = game_rule(r, ctx);
if (win_or_lose) {
// win
let out_balance_pool = game.balance.split(player_in);
let out_coin = coin::from_balance(out_balance_pool, ctx);
public_transfer(out_coin, ctx.sender());
public_transfer(in_coin, ctx.sender());
}else {
// lose,
let in_balance = coin::into_balance(in_coin);
game.balance.join(in_balance);
};

provider_result(game, player_in, !win_or_lose);
}

public entry fun deposit_pool(game: &mut GAME, provider: Coin<GAMEFAUCET>, ctx: &mut TxContext){
let sender = ctx.sender();
let amount = provider.value();

if(!linked_table::contains(&mut game.provider, sender)){
linked_table::push_back(&mut game.provider, sender, 0);
};

// 凭证
let provider_balance = linked_table::borrow_mut(&mut game.provider, sender);
*provider_balance = *provider_balance + amount;

// 存入pool
let in_balance = coin::into_balance(provider);
game.balance.join(in_balance);

}

public entry fun withdraw_pool(game: &mut GAME, amount: u64, ctx: &mut TxContext){
let sender = ctx.sender();

// check
assert!(linked_table::contains(&game.provider, sender));
let provider_balance = linked_table::borrow_mut(&mut game.provider, sender);
assert!(*provider_balance >= amount);

*provider_balance = *provider_balance - amount;

let out_balance_pool = game.balance.split(amount);
let out_coin = coin::from_balance(out_balance_pool, ctx);
public_transfer(out_coin, sender);
}

public fun provider_result(game: &mut GAME, amount: u64, result: bool){
let pool_all_balance = game.balance.value();
assert!(pool_all_balance > 0);

let mut current_key: Option<address> = *linked_table::front(&mut game.provider);
while(option::is_some(&mut current_key)){
let key = option::extract(&mut current_key);
let provider_balance = linked_table::borrow_mut(&mut game.provider, key);

if(result){*provider_balance = *provider_balance + *provider_balance * amount / pool_all_balance}
else{ *provider_balance = *provider_balance - *provider_balance * amount / pool_all_balance};

current_key = *linked_table::next(&mut game.provider, key);
}
}

}
41 changes: 41 additions & 0 deletions mover/unlizhao/code/task4/game/sources/gameFaucet.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module game::gameFaucet {
use sui::coin::{Self, TreasuryCap};
use sui::tx_context::{Self, TxContext};
use sui::transfer;
use sui::object::{Self, UID};
use std::option;

public struct GAMEFAUCET has drop {}

public struct TreasuryCapHolder has key, store {
id: UID,
treasury_cap: TreasuryCap<GAMEFAUCET>,
}

// otw = One-time witness,确保只能在init初始化调用一次,同时结构体只有drop能力
fun init(otw: GAMEFAUCET, ctx: &mut TxContext) {
let (treasury_cap, metadata) = coin::create_currency(
otw,
6,
b"potato89757_faucet",
b"fanshuF",
b"potato is fanshu yeah :3",
option::none(),
ctx
);
transfer::public_freeze_object(metadata);

let treasury_cap_holder = TreasuryCapHolder {
id: object::new(ctx),
treasury_cap,
};
transfer::share_object(treasury_cap_holder);
}

public entry fun mint(treasury: &mut TreasuryCapHolder, amount: u64, ctx: &mut TxContext) {
let sender = tx_context::sender(ctx);
let treasury_cap = &mut treasury.treasury_cap;
let coin = coin::mint(treasury_cap, amount, ctx); // mint代币
transfer::public_transfer(coin, sender); // 转移至指定地址
}
}
170 changes: 0 additions & 170 deletions mover/unlizhao/code/task4/sources/task4.move

This file was deleted.

Loading

0 comments on commit f4a9903

Please sign in to comment.