-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper.go
86 lines (73 loc) · 1.94 KB
/
paper.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
package object
import (
"../../../backend"
"database/sql"
)
func IgnitePaper(backendHandle *backend.Handle, position backend.Position) error {
var err error
_, err = backendHandle.Exec(`
BEGIN IMMEDIATE;
UPDATE OR ROLLBACK entities_states
SET state_id = 2
WHERE state_id = 1
AND EXISTS (SELECT 1
FROM entities AS e
WHERE e.id = entity_id
AND (e.x, e.y) = (?,?));
COMMIT;
`, position.X, position.Y)
return err
}
func BurnPaper(backendHandle *backend.Handle) (int64, error) {
var (
err error
result sql.Result
extinguished int64
ignited int64
)
result, err = backendHandle.Exec(`
BEGIN IMMEDIATE;
UPDATE OR ROLLBACK entities_states
SET state_id = 3
WHERE state_id = 2
AND abs(random() % 101) < (SELECT io.value
FROM integer_options AS io
WHERE io.id = 5);
`)
if err != nil {
return 0, err
}
extinguished, err = result.RowsAffected()
if err != nil {
return 0, err
}
result, err = backendHandle.Exec(`
UPDATE OR ROLLBACK entities_states
SET state_id = 2
WHERE state_id = 1
AND EXISTS (SELECT 1
FROM entities AS e1,
entities_states AS es,
(VALUES (-1,-1),( 0,-1),( 1,-1),
(-1, 0),( 0, 0),( 1, 0),
(-1, 1),( 0, 1),( 1, 1)) AS d,
entities AS e2
WHERE e1.id = es.entity_id
AND es.state_id = 2
AND e2.id = entities_states.entity_id
AND (e2.x, e2.y) = (e1.x + d.column1, e1.y + d.column2)
LIMIT 1)
AND abs(random() % 101) < (SELECT io.value
FROM integer_options AS io
WHERE io.id = 6);
COMMIT;
`)
if err != nil {
return 0, err
}
ignited, err = result.RowsAffected()
if err != nil {
return 0, err
}
return extinguished + ignited, err
}