Skip to content

Commit 2d0b49b

Browse files
committed
Обновлены примеры
1 parent 3a21089 commit 2d0b49b

File tree

4 files changed

+216
-4
lines changed

4 files changed

+216
-4
lines changed

examples/database/hsqldb.own

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use ["std", "jdbc"]
2+
3+
connection = getConnection("jdbc:hsqldb:file:hsql.db", "", "", "org.hsqldb.jdbcDriver")
4+
statement = connection.createStatement()
5+
statement.executeUpdate("drop table if exists cities")
6+
statement.executeUpdate("CREATE TABLE cities (id IDENTITY, name VARCHAR(32))")
7+
statement.executeUpdate("INSERT INTO cities (name) VALUES('Киев')")
8+
statement.executeUpdate("INSERT INTO cities (name) VALUES('Минск')")
9+
statement.executeUpdate("INSERT INTO cities (name) VALUES('Москва')")
10+
11+
rs = statement.executeQuery("SELECT id, name FROM cities")
12+
while(rs.next()) {
13+
// read the result set
14+
println "name = " + rs.getString("name")
15+
println "id = " + rs.getInt("id")
16+
}
17+
statement.execute("SHUTDOWN")

examples/database/sqlite.own

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use ["std", "jdbc"]
2+
3+
// Example from https://github.com/xerial/sqlite-jdbc
4+
5+
connection = getConnection("jdbc:sqlite:sample.db")
6+
statement = connection.createStatement()
7+
statement.setQueryTimeout(30) // set timeout to 30 sec.
8+
9+
statement.executeUpdate("drop table if exists person")
10+
statement.executeUpdate("create table person (id integer, name string)")
11+
statement.executeUpdate("insert into person values(1, 'leo')")
12+
statement.executeUpdate("insert into person values(2, 'yui')")
13+
14+
rs = statement.executeQuery("select * from person")
15+
while(rs.next()) {
16+
// read the result set
17+
println "name = " + rs.getString("name")
18+
println "id = " + rs.getInt("id")
19+
}

examples/game/pipes_online.own

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
use "std"
2+
use "canvas"
3+
use "socket"
4+
5+
/// --- PIPES CELL ---
6+
CELL_START = 0
7+
HORIZONTAL = 0
8+
VERTICAL = 1
9+
LEFT_TO_DOWN = 2
10+
LEFT_TO_UP = 3
11+
RIGHT_TO_UP = 4
12+
RIGHT_TO_DOWN = 5
13+
CROSS = 6
14+
CELL_LAST = 6
15+
16+
Cells = [
17+
{"index": HORIZONTAL, "next": VERTICAL},
18+
{"index": VERTICAL, "next": HORIZONTAL},
19+
{"index": LEFT_TO_DOWN, "next": LEFT_TO_UP},
20+
{"index": LEFT_TO_UP, "next": RIGHT_TO_UP},
21+
{"index": RIGHT_TO_UP, "next": RIGHT_TO_DOWN},
22+
{"index": RIGHT_TO_DOWN, "next": LEFT_TO_DOWN},
23+
{"index": CROSS, "next": CROSS}
24+
];
25+
26+
27+
def draw(v, cellSize) {
28+
c2 = cellSize / 2
29+
match v {
30+
case HORIZONTAL : fillRect(0, c2 - 2, cellSize, 4)
31+
case VERTICAL : fillRect(c2 - 2, 0, 4, cellSize)
32+
case LEFT_TO_DOWN : {
33+
fillRect(0, c2 - 2, c2, 4)
34+
fillRect(c2 - 2, c2 - 2, 4, c2 + 2)
35+
}
36+
case LEFT_TO_UP : {
37+
fillRect(0, c2 - 2, c2, 4)
38+
fillRect(c2 - 2, 0, 4, c2 + 2)
39+
}
40+
case RIGHT_TO_UP : {
41+
fillRect(c2 - 2, c2 - 2, c2 + 2, 4)
42+
fillRect(c2 - 2, 0, 4, c2 + 2)
43+
}
44+
case RIGHT_TO_DOWN : {
45+
fillRect(c2 - 2, c2 - 2, c2 + 2, 4)
46+
fillRect(c2 - 2, c2 - 2, 4, c2 + 2)
47+
}
48+
case CROSS : {
49+
fillRect(c2 - 2, 0, 4, cellSize)
50+
fillRect(0, c2 - 2, cellSize, 4)
51+
}
52+
}
53+
}
54+
55+
56+
/// --- PIPES BOARD ---
57+
SIZE = 10
58+
59+
// Creating game board
60+
board = newarray(SIZE, SIZE)
61+
boardGhost = newarray(SIZE, SIZE)
62+
63+
def switchCell(x, y) {
64+
board[x][y] = Cells[board[x][y]].next
65+
}
66+
def setGhostCell(x, y) {
67+
boardGhost[x][y] = Cells[boardGhost[x][y]].next
68+
}
69+
70+
71+
/// --- PIPES MAIN ---
72+
translateX = 0 translateY = 0
73+
isGameFinished = false
74+
isWin = false
75+
76+
/* frect with translate ability */
77+
def fillRect(x,y,w,h) {
78+
frect(translateX+x, translateY+y, w, h)
79+
}
80+
81+
WIDTH = 320 HEIGHT = 320
82+
WINDOW_WIDTH = WIDTH * 2
83+
window("Pipes Online", WINDOW_WIDTH, HEIGHT)
84+
cellSize = WIDTH / SIZE
85+
86+
// cursor
87+
curX = 0 curY = 0
88+
curGhostX = 0 curGhostY = 0
89+
90+
// Initialize client
91+
socket = newSocket("http://localhost:6469")
92+
socket.on("gameStart", def(data) {
93+
data = data[0]
94+
for i=0, i<SIZE, i++
95+
for j=0, j<SIZE, j++
96+
boardGhost[i][j] = board[i][j] = data[i][j]
97+
thread(::gameLoop)
98+
})
99+
.on("updateGhostCell", def(data) {
100+
data = data[0]
101+
setGhostCell(data.x, data.y);
102+
})
103+
.on("updateGhostCursor", def(data) {
104+
data = data[0]
105+
curGhostX = data.x
106+
curGhostY = data.y
107+
})
108+
.on("gameFinished", def(data) {
109+
isGameFinished = true
110+
isWin = data[0]
111+
})
112+
socket.connect()
113+
114+
def gameLoop() {
115+
run = 1
116+
while run {
117+
key = keypressed()
118+
if (!isGameFinished) {
119+
if (key == VK_LEFT && curX > 0) {
120+
curX--
121+
socket.emit("updateCursor", {"x": curX, "y": curY})
122+
} else if (key == VK_RIGHT && curX < SIZE - 1) {
123+
curX++
124+
socket.emit("updateCursor", {"x": curX, "y": curY})
125+
} else if (key == VK_UP && curY > 0) {
126+
curY--
127+
socket.emit("updateCursor", {"x": curX, "y": curY})
128+
} else if (key == VK_DOWN && curY < SIZE - 1) {
129+
curY++
130+
socket.emit("updateCursor", {"x": curX, "y": curY})
131+
} else if (key == VK_FIRE) {
132+
switchCell(curX, curY)
133+
socket.emit("switchCell", {"x": curX, "y": curY})
134+
}
135+
else if (key == 48) run = 0
136+
}
137+
138+
// background
139+
color(isGameFinished ? (isWin ? #66FF66 : #FF6666) : #FFFFFF)
140+
frect(0, 0, WIDTH, HEIGHT)
141+
color(isGameFinished ? (!isWin ? #66FF66 : #FF6666) : #DDDDDD)
142+
frect(WIDTH, 0, WIDTH, HEIGHT)
143+
// cursor
144+
color(#4444FF)
145+
frect(curX*cellSize, curY*cellSize, cellSize, cellSize)
146+
color(#4040DD)
147+
frect(WIDTH + curGhostX*cellSize, curGhostY*cellSize, cellSize, cellSize)
148+
for (i=0, i<SIZE, i++) {
149+
color(0)
150+
ic = i*cellSize
151+
// ourrebt board
152+
line(0, ic, cellSize*SIZE, ic)
153+
line(ic, 0, ic, cellSize*SIZE)
154+
// ghost board
155+
line(WIDTH, ic, WIDTH + cellSize*SIZE, ic)
156+
line(WIDTH + ic, 0, WIDTH + ic, cellSize*SIZE)
157+
color(#FF0000)
158+
for j=0, j<SIZE, j++ {
159+
translateX = ic
160+
translateY = j*cellSize
161+
draw(board[i][j], cellSize)
162+
translateX = -ic
163+
translateY = -j*cellSize
164+
// ghost cells
165+
translateX = WIDTH + ic
166+
translateY = j*cellSize
167+
draw(boardGhost[i][j], cellSize)
168+
translateX = - WIDTH - ic
169+
translateY = -j*cellSize
170+
}
171+
}
172+
repaint()
173+
sleep(50)
174+
}
175+
socket.disconnect()
176+
}

examples/network/github_timeline.own

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ thread(::http, "https://api.github.com/events", def(r) {
1616
})
1717

1818
def show_github_events(event) {
19-
println event.created_at.formatTzDate()
20-
println "User: https://github.com/" + event.actor.login
21-
println github_event_type(event)
22-
println "-" * 50
19+
println event.created_at.formatTzDate()
20+
println "User: https://github.com/" + event.actor.login
21+
println github_event_type(event)
22+
println "-" * 50
2323
}
2424

2525
def github_event_type(event) {

0 commit comments

Comments
 (0)