Smart Contract

BattleContract_V2

A.2eed39e014db1d54.BattleContract_V2

Valid From

118,465,419

Deployed

6d ago
Feb 22, 2026, 03:20:36 PM UTC

Dependents

2 imports
1import StakingContract_V2 from 0x2eed39e014db1d54
2
3access(all) contract BattleContract_V2 {  
4    access(all) struct Battle {
5        access(all) let id: UInt64
6        access(all) let owner: Address 
7        access(all) let startDate: UInt64
8        access(all) let endDate: UInt64
9        access(all) let prize: String
10        access(all) let title: String
11        access(all) let image: String
12        access(all) var users: [Address]  
13
14        init(id: UInt64, owner: Address,  startDate: UInt64, endDate: UInt64, prize: String, title: String, image: String) {
15            self.id = id
16            self.owner = owner 
17            self.startDate = startDate
18            self.endDate = endDate
19            self.prize = prize
20            self.title = title
21            self.image = image
22            self.users = []  
23        }  
24    }
25    access(all) struct BattleResponse {
26        access(all) let id: UInt64
27        access(all) let owner: Address 
28        access(all) let startDate: UInt64
29        access(all) let endDate: UInt64
30        access(all) let prize: String
31        access(all) let title: String
32        access(all) let image: String
33        access(all) var users: [Address]  
34        access(all) var usersWithHistory: {Address: [StakingContract_V2.HistoryStats]}
35
36        init(battle: Battle, usersWithHistory: {Address: [StakingContract_V2.HistoryStats]}) {
37            self.id = battle.id
38            self.owner = battle.owner 
39            self.startDate = battle.startDate
40            self.endDate = battle.endDate
41            self.prize = battle.prize
42            self.title = battle.title
43            self.image = battle.image
44            self.users = battle.users
45            self.usersWithHistory = usersWithHistory
46        }  
47    }
48
49    access(self) var battles: {UInt64: Battle} 
50    access(self) var battlesByUser: {Address: [UInt64]} 
51    access(self) var nextBattleId: UInt64
52
53    init() {
54        self.battles = {}
55        self.battlesByUser = {}
56        self.nextBattleId = 1
57    }
58
59    // TODO - Create a stake mechanism for the battle, user needs to stake a certain amount of tokens to create a battle
60    access(all) fun createBattle(start: UInt64, end: UInt64, owner: Address, prize: String, title: String, image: String): UInt64 {
61        let id: UInt64 = self.nextBattleId
62        let startDate: UInt64 = start
63        self.battles[id] = Battle(id: id, owner: owner, startDate: startDate, endDate: end, prize: prize, title: title, image: image) 
64        self.nextBattleId = self.nextBattleId + 1
65        return id
66    }
67
68    access(all) fun joinBattle(battleId: UInt64, user: Address) {
69        pre {
70            self.battles[battleId] != nil: "Battle does not exist"
71        } 
72        
73        if self.battles[battleId]!.users.contains(user) {
74            return
75        }
76
77        self.battles[battleId]!.users.append(user) 
78        if self.battlesByUser[user] == nil {
79            self.battlesByUser[user] = [battleId]
80        } else {
81            self.battlesByUser[user]!.append(battleId)
82        }
83    } 
84
85    access(all) fun getBattle(id: UInt64): BattleContract_V2.BattleResponse? {
86        let battle: BattleContract_V2.Battle? = self.battles[id]
87        if battle == nil {
88            return nil
89        }
90 
91        let users: [Address] = battle!.users
92        let usersWithHistory: {Address: [StakingContract_V2.HistoryStats]} = {}
93        
94        for user in users {
95            let history: [StakingContract_V2.HistoryStats]? = StakingContract_V2.getStats(address: user)
96            if history != nil {
97                usersWithHistory[user] = history!
98            }
99        }
100
101        let battleResponse: BattleContract_V2.BattleResponse = BattleResponse(battle: battle!, usersWithHistory: usersWithHistory)
102        return battleResponse
103    }
104    
105    access(all) fun getBattles(): [Battle] {
106        return self.battles.values  
107    }  
108    
109    access(all) fun getBattlesByUser(user: Address): [UInt64] {
110        return self.battlesByUser[user] ?? []
111    }  
112    
113}
114