...
This commit is contained in:
@@ -1,43 +1,46 @@
|
||||
module currency
|
||||
|
||||
import freeflowuniverse.herolib.data.encoder
|
||||
//see encoder for binary
|
||||
|
||||
// CurrencyBytes represents serialized Currency data
|
||||
pub struct CurrencyBytes {
|
||||
pub:
|
||||
data []u8
|
||||
}
|
||||
|
||||
// to_bytes converts a Currency to serialized bytes
|
||||
pub fn (c Currency) to_bytes() !CurrencyBytes {
|
||||
mut enc := encoder.new()
|
||||
// import freeflowuniverse.herolib.data.encoder
|
||||
|
||||
// Add unique encoding ID to identify this type of data
|
||||
enc.add_u16(500) // Unique ID for Currency type
|
||||
// // CurrencyBytes represents serialized Currency data
|
||||
// pub struct CurrencyBytes {
|
||||
// pub:
|
||||
// data []u8
|
||||
// }
|
||||
|
||||
// Encode Currency fields
|
||||
enc.add_string(c.name)
|
||||
enc.add_f64(c.usdval)
|
||||
// // to_bytes converts a Currency to serialized bytes
|
||||
// pub fn (c Currency) to_bytes() !CurrencyBytes {
|
||||
// mut enc := encoder.new()
|
||||
|
||||
return CurrencyBytes{
|
||||
data: enc.data
|
||||
}
|
||||
}
|
||||
// // Add unique encoding ID to identify this type of data
|
||||
// enc.add_u16(500) // Unique ID for Currency type
|
||||
|
||||
// from_bytes deserializes bytes to a Currency
|
||||
pub fn from_bytes(bytes CurrencyBytes) !Currency {
|
||||
mut d := encoder.decoder_new(bytes.data)
|
||||
mut currency := Currency{}
|
||||
// // Encode Currency fields
|
||||
// enc.add_string(c.name)
|
||||
// enc.add_f64(c.usdval)
|
||||
|
||||
// Check encoding ID to verify this is the correct type of data
|
||||
encoding_id := d.get_u16()!
|
||||
if encoding_id != 500 {
|
||||
return error('Wrong file type: expected encoding ID 500, got ${encoding_id}, for currency')
|
||||
}
|
||||
// return CurrencyBytes{
|
||||
// data: enc.data
|
||||
// }
|
||||
// }
|
||||
|
||||
// Decode Currency fields
|
||||
currency.name = d.get_string()!
|
||||
currency.usdval = d.get_f64()!
|
||||
// // from_bytes deserializes bytes to a Currency
|
||||
// pub fn from_bytes(bytes CurrencyBytes) !Currency {
|
||||
// mut d := encoder.decoder_new(bytes.data)
|
||||
// mut currency := Currency{}
|
||||
|
||||
return currency
|
||||
}
|
||||
// // Check encoding ID to verify this is the correct type of data
|
||||
// encoding_id := d.get_u16()!
|
||||
// if encoding_id != 500 {
|
||||
// return error('Wrong file type: expected encoding ID 500, got ${encoding_id}, for currency')
|
||||
// }
|
||||
|
||||
// // Decode Currency fields
|
||||
// currency.name = d.get_string()!
|
||||
// currency.usdval = d.get_f64()!
|
||||
|
||||
// return currency
|
||||
// }
|
||||
|
||||
@@ -4,6 +4,7 @@ import encoding.binary as bin
|
||||
import freeflowuniverse.herolib.data.ourtime
|
||||
import time
|
||||
import freeflowuniverse.herolib.data.gid
|
||||
import freeflowuniverse.herolib.data.currency
|
||||
|
||||
pub struct Decoder {
|
||||
pub mut:
|
||||
@@ -148,6 +149,18 @@ pub fn (mut d Decoder) get_ourtime() !ourtime.OurTime {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut d Decoder) get_currency() !currency.Amount {
|
||||
curstring:=d.get_string()!
|
||||
if curstring.len == 0 {
|
||||
return error('currency string is empty')
|
||||
}
|
||||
currencyo := currency.get(curstring)!
|
||||
return currency.Amount{
|
||||
currency: currencyo
|
||||
val: d.get_f64()!
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut d Decoder) get_percentage() !u8 {
|
||||
val := d.get_u8()!
|
||||
if val > 100 {
|
||||
|
||||
@@ -4,6 +4,7 @@ import time
|
||||
import encoding.binary as bin
|
||||
import freeflowuniverse.herolib.data.ourtime
|
||||
import freeflowuniverse.herolib.data.gid
|
||||
import freeflowuniverse.herolib.data.currency
|
||||
|
||||
const kb = 1024
|
||||
|
||||
@@ -102,6 +103,12 @@ pub fn (mut b Encoder) add_ourtime(data ourtime.OurTime) {
|
||||
b.add_u32(u32(data.unixt))
|
||||
}
|
||||
|
||||
pub fn (mut b Encoder) add_currency(data currency.Amount) {
|
||||
b.add_string(data.currency.name)
|
||||
b.add_f64(data.val)
|
||||
}
|
||||
|
||||
|
||||
// adds a float64 value
|
||||
pub fn (mut b Encoder) add_f64(data f64) {
|
||||
// Convert f64 to bits first, then store as u64
|
||||
|
||||
Reference in New Issue
Block a user