-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCloudBankUtils.gd
201 lines (183 loc) · 7.67 KB
/
CloudBankUtils.gd
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
extends HTTPRequest
var http
var keys
var rawStackForDeposit
var rawStackFromWithdrawal
var rawReceipt
var receiptNumber
var totalCoinsWithdrawn = 0
var onesInBank = 0
var fivesInBank = 0
var twentyFivesInBank = 0
var hundredsInBank = 0
var twohundredfiftiesInBank = 0
signal called_show_coins
func _ready():
pass
#Creates an httpclient and connects to a CloudBank server using the provided Dictionary of keys
func ConnectToBank(p_keys):
keys = p_keys
http = HTTPClient.new()
http.connect_to_host(keys["publickey"], 443, true, false)
while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
http.poll()
print("connecting to bank..")
OS.delay_msec(300)
assert(http.get_status() == HTTPClient.STATUS_CONNECTED)
#func _process(delta):
# # Called every frame. Delta is time since last frame.
# # Update game logic here.
# pass
#Uses HTTP POST to call the CloudBank's show_coins service. Saves the results in this class's member variables
func ShowCoins():
var json = "error"
var formFields = {"pk": keys.privatekey}
var formContent = http.query_string_from_dict(formFields)
var headers = ["Content-Type: application/x-www-form-urlencoded", "Content-Length: " + str(formContent.length())]
var result = http.request(http.METHOD_POST, "/show_coins.aspx", headers, formContent)
while http.get_status() == HTTPClient.STATUS_REQUESTING:
http.poll()
print("requesting show coins..")
OS.delay_msec(300)
if http.has_response():
var responseBytes = PoolByteArray()
while http.get_status() == HTTPClient.STATUS_BODY:
http.poll()
print("processing show coins..")
responseBytes = responseBytes + http.read_response_body_chunk()
json = responseBytes.get_string_from_ascii()
if json.find("error") > -1:
print(json)
else:
var bankTotals = JSON.parse(json)
onesInBank = bankTotals.result["ones"]
fivesInBank = bankTotals.result["fives"]
twentyFivesInBank = bankTotals.result["twentyfives"]
hundredsInBank = bankTotals.result["hundreds"]
twohundredfiftiesInBank = bankTotals.result["twohundredfifties"]
else:
print("httpclient status:" + String(http.get_status()))
emit_signal("called_show_coins")
#Reads a CloudCoin stack file and saves it into member variable rawStackForDeposit ready to be sent to a CloudBank
func LoadStackFromFile(file_name):
var file = File.new()
if(file.file_exists(file_name)):
file.open(file_name, File.READ)
rawStackForDeposit = file.get_as_text()
file.close()
else:
print("file not found at:" + filename)
#Uses HTTP POST to send the CloudCoin stack that is saved in member variable rawStackInDeposit to the CloudBank
#When the response is read the ID for the receipt created by the CloudBank when receiving CloudCoins is saved in member variable receiptNumber
func SendStackToCloudBank():
var CloudBankFeedback
var formFields = {"stack":rawStackForDeposit}
var formContent = http.query_string_from_dict(formFields)
var headers = ["Content-Type: application/x-www-form-urlencoded", "Content-Length: " + str(formContent.length()), "User-Agent: Pirulo/1.0 (Godot)"]
var result = http.request(http.METHOD_POST, "/deposit_one_stack.aspx", headers, formContent)
while http.get_status() == HTTPClient.STATUS_REQUESTING:
http.poll()
OS.delay_msec(300)
if http.has_response():
var responseBytes = PoolByteArray()
while http.get_status() == HTTPClient.STATUS_BODY:
http.poll()
responseBytes = responseBytes + http.read_response_body_chunk()
CloudBankFeedback = responseBytes.get_string_from_ascii()
var j = JSON.parse(CloudBankFeedback)
if typeof(j.result) == TYPE_DICTIONARY:
receiptNumber = j.result["receipt"]
else:
print("CloudServices error: " + CloudBankFeedback)
else:
print("httpclient status:" + String(http.get_status()))
#Uses HTTP GET to read the receipt from the last deposit to the CloudBank done by this class.
#Needs the receiptNumber that is collected by SendStackToCloudBank()
#The entire receipt is saved in member variable rawReceipt
func GetReceipt():
var headers = ["User-Agent: Pirulo/1.0 (Godot)"]
var result = http.request(http.METHOD_GET,"/"+ keys.privatekey + "/Receipts/" + receiptNumber + ".json", headers)
while http.get_status() == HTTPClient.STATUS_REQUESTING:
http.poll()
OS.delay_msec(300)
if http.has_response():
var responseBytes = PoolByteArray()
while http.get_status() == HTTPClient.STATUS_BODY:
http.poll()
responseBytes = responseBytes + http.read_response_body_chunk()
rawReceipt = responseBytes.get_string_from_ascii()
else:
print("httpclient status:" + http.get_status())
#Uses HTTP POST to withdraw a certain amount of CloudCoins from the CloudBank.
#The response will be a CloudCoin stack that will be saved into the member variable rawStackFromWithdrawal
func GetStackFromCloudBank(amountToWithdraw):
var totalCoinsWithdrawn = amountToWithdraw
var formFields = {"pk": keys.privatekey, "amount": totalCoinsWithdrawn}
var formContent = http.query_string_from_dict(formFields)
var headers = ["Content-Type: application/x-www-form-urlencoded", "Content-Length: " + str(formContent.length())]
var result = http.request(http.METHOD_POST, "/withdraw_account.aspx", headers, formContent)
while http.get_status() == HTTPClient.STATUS_REQUESTING:
http.poll()
OS.delay_msec(300)
if http.has_response():
var responseBytes = PoolByteArray()
while http.get_status() == HTTPClient.STATUS_BODY:
http.poll()
responseBytes = responseBytes + http.read_response_body_chunk()
rawStackFromWithdrawal = responseBytes.get_string_from_ascii()
var j = JSON.parse(rawStackFromWithdrawal)
if rawStackFromWithdrawal.find("status") > -1:
print(j.result["status"] + ", " + j.result["message"])
elif typeof(j.result) != TYPE_DICTIONARY :
print("CloudServices error: " + rawStackFromWithdrawal)
else:
print("httpclient status:" + http.get_status())
#A helper function for calculating a CloudCoin's denomination by looking at its serial number(sn)
func GetDenomination(sn):
var nom = 0
if sn < 1:
nom = 0
elif sn < 2097153:
nom = 1
elif sn < 4194305:
nom = 5
elif sn < 6291457:
nom = 25
elif sn < 14680065:
nom = 100
elif sn < 167772117:
nom = 250
else:
nom = 0
return nom
#Reads the receipt held in member variable rawReceipt and returns a Dictionary containing pertinent information
#Said Dictionary's contents: "receipt": a JSONParseResult containing the entire receipt,
#"totalAuthenticCoins": how many total CloudCoins of the last deposit where determined to be authentic,
#"totalAuthenticNotes": how many CloudCoin Notes compose the CloudCoins that were determined to be authentic
func InterpretReceipt():
var json = JSON.parse(rawReceipt)
if typeof(json.result) != TYPE_DICTIONARY:
var error = {"error": rawReceipt}
return error
var totalNotes = json.result["total_authentic"] + json.result["total_fracked"]
var totalCoins = 0
for detail in json.result["receipt"]:
if detail["status"] == "authentic":
totalCoins = totalCoins + GetDenomination(detail["sn"])
var interpretation = {"receipt": json, "totalAuthenticCoins": totalCoins, "totalAuthenticNotes": totalNotes}
return interpretation
#Helper function that returns a possible filename for a CloudCoin that was recently withdrawn.
func GetStackName():
var tag
if receiptNumber == null:
tag = "NewWithdrawal"
else:
tag = String(receiptNumber)
return String(totalCoinsWithdrawn) + ".CloudCoin." + tag + ".stack"
#Saves the CloudCoin stack that is in member variable rawStackFromWithdrawal into a cloudcoin .stack file.
#Param path should be the full filepath and filename of the file being created.
func SaveStackToFile(path):
var file = File.new()
file.open(path + GetStackName(), File.WRITE)
file.store_string(rawStackFromWithdrawal)
file.close()