-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_approval.py
119 lines (101 loc) · 3.46 KB
/
game_approval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from pyteal import *
PLAYER_DAMAGE = Int(2)
def game():
"""
Initialize monster with specified health
"""
monsterHealth = Btoi(Txn.application_args[0])
handle_creation = Seq(
Assert(monsterHealth >= Int(5)),
App.globalPut(Bytes("Health"), monsterHealth),
App.globalPut(Bytes("MaxDamage"), Int(0)),
App.globalPut(Bytes("Mvp"), Bytes("")),
Return(Int(1)),
)
"""
Initialize player's damage dealt to the monster
"""
handle_optin = Seq(
Assert(App.optedIn(Txn.sender(), Txn.application_id())),
App.localPut(Txn.sender(), Bytes("Damage"), Int(0)),
Return(Int(1)),
)
"""
Attacks the monster
"""
currentMonsterHealth = App.globalGet(Bytes("Health"))
playerCurrentDamage = App.localGet(
Txn.sender(), Bytes("Damage")
) # returns 0 if state is not found
highestDamage = App.globalGet(
Bytes("MaxDamage")
) # highest amount of damage dealt to monster
update_monster_health = If(
PLAYER_DAMAGE > currentMonsterHealth,
App.globalPut(Bytes("Health"), Int(0)), # monster is dead
App.globalPut(
Bytes("Health"), currentMonsterHealth - PLAYER_DAMAGE
), # reduce monster health
)
update_player_total_damage = App.localPut(
Txn.sender(), Bytes("Damage"), playerCurrentDamage + PLAYER_DAMAGE
)
update_mvp = If(
playerCurrentDamage + PLAYER_DAMAGE > highestDamage,
Seq(
App.globalPut(Bytes("Mvp"), Txn.sender()),
App.globalPut(Bytes("MaxDamage"), playerCurrentDamage + PLAYER_DAMAGE),
),
)
attack_monster = Seq(
Assert(currentMonsterHealth > Int(0)),
update_monster_health,
update_mvp,
update_player_total_damage,
Return(Int(1)),
)
"""
Reward player
"""
mvp = App.globalGet(Bytes("Mvp"))
reward_player = Seq(
[
Assert(Txn.sender() == Global.creator_address()),
Assert(currentMonsterHealth <= Int(0)),
Assert(Txn.accounts[1] == mvp),
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.Payment,
TxnField.receiver: Txn.accounts[1],
TxnField.amount: Int(1000000),
TxnField.fee: Int(0),
}
),
InnerTxnBuilder.Submit(),
Return(Int(1)),
]
)
handle_noop = Seq(
Assert(Global.group_size() == Int(1)),
Cond(
[Txn.application_args[0] == Bytes("Attack"), attack_monster],
[Txn.application_args[0] == Bytes("Reward"), reward_player],
),
)
handle_closeout = Return(Int(1))
handle_updateapp = Return(Int(0))
handle_deleteapp = Return(Int(0))
program = Cond(
[Txn.application_id() == Int(0), handle_creation],
[Txn.on_completion() == OnComplete.OptIn, handle_optin],
[Txn.on_completion() == OnComplete.CloseOut, handle_closeout],
[Txn.on_completion() == OnComplete.UpdateApplication, handle_updateapp],
[Txn.on_completion() == OnComplete.DeleteApplication, handle_deleteapp],
[Txn.on_completion() == OnComplete.NoOp, handle_noop],
)
return program
if __name__ == "__main__":
game_sc = compileTeal(game(), mode=Mode.Application, version=8)
with open("./artifacts/game_approval.teal", "w") as f:
f.write(game_sc)