|
15 | 15 | (define-constant DEFAULT-PRICE u100) |
16 | 16 |
|
17 | 17 | ;; data maps and vars |
18 | | -(define-map lobbies {id: uint} {owner: principal, description: (string-ascii 99), balance: uint, price: uint, factor: uint, commission: uint, mapy: (string-ascii 30), length: (string-ascii 10), traffic: (string-ascii 10), curves: (string-ascii 10), hours: uint, active: bool}) |
| 18 | +(define-map lobbies uint {owner: principal, description: (string-ascii 99), balance: uint, price: uint, factor: uint, commission: uint, mapy: (string-ascii 30), length: (string-ascii 10), traffic: (string-ascii 10), curves: (string-ascii 10), hours: uint, active: bool}) |
19 | 19 | (define-map scoreboard {lobby-id: uint, address: principal} {score: uint, rank: uint, sum-rank-factor: uint, rank-factor: uint, rewards: uint, rac: uint, nft: (string-ascii 99)}) |
20 | 20 | (define-data-var lobby-count uint u0) |
21 | 21 | (define-data-var contract-owner principal tx-sender) |
|
29 | 29 | ) |
30 | 30 |
|
31 | 31 | (define-private (add-balance (id uint) (participant principal) (amount uint)) |
32 | | - (begin |
33 | | - (unwrap-panic (stx-transfer? amount participant (as-contract tx-sender))) |
34 | | - (match |
35 | | - (map-get? lobbies {id: id}) |
36 | | - lobby |
37 | | - (map-set lobbies {id: id} (merge lobby {balance: (+ (default-to u0 (get balance (map-get? lobbies {id: id}))) amount)})) |
38 | | - false |
39 | | - ) |
| 32 | + (let ((lobby (unwrap! (map-get? lobbies id) (ok false)))) |
| 33 | + (try! (stx-transfer? amount participant (as-contract tx-sender))) |
| 34 | + (map-set lobbies id (merge lobby {balance: (+ (get balance lobby) amount)})) |
| 35 | + (ok true) |
40 | 36 | ) |
41 | 37 | ) |
42 | 38 |
|
|
52 | 48 | (lobby-id (increment-lobby-count)) |
53 | 49 | ) |
54 | 50 | ;; (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED) |
55 | | - (map-set lobbies {id: lobby-id} |
| 51 | + (map-set lobbies lobby-id |
56 | 52 | { |
57 | 53 | owner: tx-sender, description: description, balance: u0, price: price, factor: factor, commission: commission, |
58 | 54 | mapy: mapy, length: length, traffic: traffic, curves: curves, hours: hours, active: true |
|
64 | 60 | ) |
65 | 61 |
|
66 | 62 | (define-read-only (get-lobby (id uint)) |
67 | | - (ok (unwrap-panic (map-get? lobbies {id: id}))) |
| 63 | + (map-get? lobbies id) |
68 | 64 | ) |
69 | 65 |
|
70 | 66 |
|
71 | 67 | ;; anyone can call to join lobbies |
72 | 68 | (define-public (join (id uint)) |
73 | 69 | (let ( |
74 | | - (entry-price (default-to DEFAULT-PRICE (get price (map-get? lobbies {id: id})))) |
| 70 | + (lobby (unwrap! (map-get? lobbies id) ERR-NOT-FOUND)) |
| 71 | + (entry-price (get price lobby)) |
75 | 72 | (joined (map-insert scoreboard {lobby-id: id, address: tx-sender} {score: u0, rank: u0, sum-rank-factor: u0, rank-factor: u0, rewards: u0, rac: u0, nft: ""})) |
76 | 73 | ) |
77 | | - (unwrap-panic (map-get? lobbies {id: id})) |
78 | | - (asserts! (default-to false (get active (map-get? lobbies {id: id}))) ERR-NOT-ACTIVE) |
| 74 | + (asserts! (get active lobby) ERR-NOT-ACTIVE) |
79 | 75 | (asserts! joined ERR-ALREADY-JOINED) |
80 | | - (add-balance id tx-sender entry-price) |
| 76 | + (try! (add-balance id tx-sender entry-price)) |
81 | 77 | (print {action: "join", lobby-id: id, address: tx-sender }) |
82 | 78 | (ok OK-SUCCESS) |
83 | 79 | ) |
84 | 80 | ) |
85 | 81 |
|
86 | 82 | (define-public (disable-lobby (id uint)) |
87 | | - (begin |
| 83 | + (let ((lobby (unwrap! (map-get? lobbies id) ERR-NOT-FOUND))) |
88 | 84 | (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED) |
89 | | - (match |
90 | | - (map-get? lobbies {id: id}) |
91 | | - lobby |
92 | | - (map-set lobbies {id: id} (merge lobby {active: false})) |
93 | | - false |
94 | | - ) |
| 85 | + (map-set lobbies id (merge lobby {active: false})) |
95 | 86 | (ok true) |
96 | 87 | ) |
97 | 88 | ) |
98 | 89 |
|
99 | 90 | (define-read-only (get-score (lobby-id uint) (address principal)) |
100 | | - (ok (unwrap-panic (map-get? scoreboard {lobby-id: lobby-id, address: address}))) |
| 91 | + (map-get? scoreboard {lobby-id: lobby-id, address: address}) |
101 | 92 | ) |
102 | 93 |
|
103 | 94 | ;; PUBLISH-MANY |
|
108 | 99 | ) |
109 | 100 | ) |
110 | 101 | (define-private (publish-result (run-result { lobby-id: uint, address: principal, score: uint, rank: uint, sum-rank-factor: uint, rank-factor: uint, rewards: uint, rac: uint, nft: (string-ascii 99)})) |
111 | | - (publish-only (get lobby-id run-result) (get address run-result) (get score run-result) (get rank run-result) (get sum-rank-factor run-result) (get rank-factor run-result) (get rewards run-result) (get rac run-result) (get nft run-result)) |
112 | | -) |
113 | | -(define-private (publish-only (lobby-id uint) (address principal) (score uint) (rank uint) (sum-rank-factor uint) (rank-factor uint) (rewards uint) (rac uint) (nft (string-ascii 99))) |
114 | | - (let |
115 | | - ( |
116 | | - (publishOk (try! (publish lobby-id address score rank sum-rank-factor rank-factor rewards rac nft))) |
117 | | - ) |
118 | | - (ok publishOk) |
119 | | - ) |
| 102 | + (publish (get lobby-id run-result) (get address run-result) (get score run-result) (get rank run-result) (get sum-rank-factor run-result) (get rank-factor run-result) (get rewards run-result) (get rac run-result) (get nft run-result)) |
120 | 103 | ) |
121 | 104 | (define-private (publish (lobby-id uint) (address principal) (score uint) (rank uint) (sum-rank-factor uint) (rank-factor uint) (rewards uint) (rac uint) (nft (string-ascii 99))) |
122 | 105 | (begin |
123 | 106 | (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED) |
124 | | - (unwrap-panic (map-get? scoreboard {lobby-id: lobby-id, address: address})) |
125 | | - (asserts! (default-to false (get active (map-get? lobbies {id: lobby-id}))) ERR-NOT-ACTIVE) |
| 107 | + (asserts! (is-some (map-get? scoreboard {lobby-id: lobby-id, address: address})) ERR-NOT-FOUND) |
| 108 | + (asserts! (default-to false (get active (map-get? lobbies lobby-id))) ERR-NOT-ACTIVE) |
126 | 109 | (map-set scoreboard {lobby-id: lobby-id, address: address} {score: score, rank: rank, sum-rank-factor: sum-rank-factor, rank-factor: rank-factor, rewards: rewards, rac: rac, nft: nft}) |
127 | 110 | (print {action: "publish", lobby-id: lobby-id, address: address, score: score, rank: rank, sum-rank-factor: sum-rank-factor, rank-factor: rank-factor, rewards: rewards, rac: rac, nft: nft}) |
128 | 111 | (ok true) |
|
137 | 120 | ) |
138 | 121 | ) |
139 | 122 | (define-private (finish-result (run-result { lobby-id: uint, address: principal, score: uint, rank: uint, sum-rank-factor: uint, rank-factor: uint, rewards: uint, rac: uint, nft: (string-ascii 99)})) |
140 | | - (finish-only (get lobby-id run-result) (get address run-result) (get score run-result) (get rank run-result) (get sum-rank-factor run-result) (get rank-factor run-result) (get rewards run-result) (get rac run-result) (get nft run-result)) |
141 | | -) |
142 | | -(define-private (finish-only (lobby-id uint) (address principal) (score uint) (rank uint) (sum-rank-factor uint) (rank-factor uint) (rewards uint) (rac uint) (nft (string-ascii 99))) |
143 | | - (let |
144 | | - ( |
145 | | - (finishOk (try! (finish lobby-id address score rank sum-rank-factor rank-factor rewards rac nft))) |
146 | | - ) |
147 | | - (ok finishOk) |
148 | | - ) |
| 123 | + (finish (get lobby-id run-result) (get address run-result) (get score run-result) (get rank run-result) (get sum-rank-factor run-result) (get rank-factor run-result) (get rewards run-result) (get rac run-result) (get nft run-result)) |
149 | 124 | ) |
150 | 125 | ;; distribute rewards for all runs in a lobby |
151 | 126 | (define-private (finish (lobby-id uint) (address principal) (score uint) (rank uint) (sum-rank-factor uint) (rank-factor uint) (rewards uint) (rac uint) (nft (string-ascii 99))) |
152 | 127 | (begin |
153 | | - (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED) |
154 | | - (unwrap-panic (map-get? scoreboard {lobby-id: lobby-id, address: address})) |
155 | | - (asserts! (default-to false (get active (map-get? lobbies {id: lobby-id}))) ERR-NOT-ACTIVE) |
156 | | - (map-set scoreboard {lobby-id: lobby-id, address: address} {score: score, rank: rank, sum-rank-factor: sum-rank-factor, rank-factor: rank-factor, rewards: rewards, rac: rac, nft: nft}) |
| 128 | + (try! (publish lobby-id address score rank sum-rank-factor rank-factor rewards rac nft)) |
157 | 129 | (try! (as-contract (stx-transfer? rac tx-sender address))) |
158 | 130 | (print {action: "finish", lobby-id: lobby-id, address: address, score: score, rank: rank, sum-rank-factor: sum-rank-factor, rank-factor: rank-factor, rewards: rewards, rac: rac, nft: nft}) |
159 | 131 | (ok true) |
|
0 commit comments