-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathart-app1.go
113 lines (98 loc) · 3.46 KB
/
art-app1.go
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
/*
A trivial application to illustrate how the blockartlib library can be
used from an application in project 1 for UBC CS 416 2017W2.
Usage:
go run art-app.go
*/
package main
import (
"blockartlib"
"fmt"
"keys"
"os"
"time"
)
func main() {
duration, _ := time.ParseDuration("10s")
time.Sleep(duration) // Sleeps for 30 seconds.
validateNum := 0 // TODO: Change this to a bigger number for submission
shapes := []string{}
blocks := []string{}
minerAddr := os.Args[1]
privateKey, _, err := keys.Generate()
fmt.Printf("%v\n", privateKey.PublicKey)
colour := "blue"
// Open a canvas.
// TODO: use settings
fmt.Printf("ART-APP1: Calling OpenCanvas to Miner with address %s\n", minerAddr)
canvas, _, err := blockartlib.OpenCanvas(minerAddr, *privateKey)
fmt.Println("ART-APP1: Canvas is ", canvas)
if checkError(err) != nil {
fmt.Println("ART-APP1: there was an error opening the canvas", err)
return
}
// Add a line.
fmt.Println("ART-APP1: Calling AddShape to add a red filled square")
shapeHash, blockHash, ink, err := canvas.AddShape(uint8(validateNum), blockartlib.PATH, "M8,4L9,4L9,5L8,5L8,4", "filled", colour)
if checkError(err) != nil {
fmt.Printf("ART-APP1: There was an error with calling AddShape: \n")
fmt.Println(err)
return
}
shapes = append(shapes, shapeHash)
blocks = append(blocks, blockHash)
// Add another line.
fmt.Println("ART-APP1: Calling AddShape to add a transparent triangle. Expect fail")
shapeHash, blockHash, ink2, err := canvas.AddShape(uint8(validateNum), blockartlib.PATH, "M6,1L6,6L1,4L6,1", "filled", colour)
if checkError(err) != nil {
fmt.Println(err)
} else {
shapes = append(shapes, shapeHash)
blocks = append(blocks, blockHash)
}
if ink2 <= ink {
checkError(fmt.Errorf("Err! ink2 not > ink1"))
}
// Delete the first line.
fmt.Println("ART-APP1: Deleting the first line")
ink3, err := canvas.DeleteShape(uint8(validateNum), shapeHash)
if checkError(err) != nil {
fmt.Println(err)
}
// assert ink3 > ink2
if ink3 <= ink2 {
checkError(fmt.Errorf("Err! ink3 not > ink2"))
}
fmt.Println("ART-APP1: Drawing square that intersects with ART-APP's polygon")
shapeHash, blockHash, _, err = canvas.AddShape(uint8(validateNum), blockartlib.PATH, "M4,3 h 1 v 1 h -1 v -1", "filled", colour)
if _, ok := err.(*blockartlib.ShapeOverlapError); ok {
fmt.Printf("Got ShapeOverlapError as expected. Err: %v\n", err)
} else {
shapes = append(shapes, shapeHash)
blocks = append(blocks, blockHash)
}
fmt.Println("ART-APP1: Drawing shape with out of bounds svg string") // M -4,-3 invalid
shapeHash, blockHash, _, err = canvas.AddShape(uint8(validateNum), blockartlib.PATH, "M-4,-3 h 1 v 1 h -1 v -1", "filled", colour)
if _, ok := err.(*blockartlib.InvalidShapeSvgStringError); ok {
fmt.Printf("Got InvalidShapeSvgStringError as expected. Err: %v\n", err)
}
fmt.Println("ART-APP1: Drawing shape with invalid svg string") // Q not supported
shapeHash, blockHash, _, err = canvas.AddShape(uint8(validateNum), blockartlib.PATH, "M-4,-3 Q 1 v 1 h -1 v -1", "filled", colour)
if _, ok := err.(*blockartlib.InvalidShapeSvgStringError); ok {
fmt.Printf("Got InvalidShapeSvgStringError as expected. Err: %v\n", err)
}
fmt.Println("Closing the canvas")
// Close the canvas.
_, err = canvas.CloseCanvas()
if checkError(err) != nil {
return
}
}
// If error is non-nil, print it out and return it.
func checkError(err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
return err
}
return nil
}