-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixtures.vsh
109 lines (90 loc) · 2.46 KB
/
fixtures.vsh
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
#!/usr/local/bin/v
// todo: fix src.models. v-analyzer removes `src.` prefix after each "format document"
import db.sqlite
import src.models
import rand
const (
db_name = 'storage.db'
db_file_path = join_path(@VMODROOT, 'data', db_name)
)
fn init_tables(db sqlite.DB) ! {
sql db {
create table models.Team
create table models.Prediction
}!
}
fn init_predictions(db sqlite.DB) ! {
//1) check if there enough teams
//2) check if there any existing predictions. if so, remove
//3) create predictions with random teams
//1)
found_teams := db.q_int("SELECT COUNT(*) FROM `teams`")!
if found_teams < 2
{
panic('Not enought teams...')
}
// 2) just delete everytime.
_ := db.exec_none('DELETE FROM `predictions`')
//3) create
// 3.1 - get list of all odd teams (randomly ordered)
// 3.2 - get list of all even teams (randomly ordered)
// 3.3 - create prediction with a odd and even teams-id
// 3.1 and 3.2
mut even_teams_ids:= db.exec('SELECT id FROM `teams` WHERE (id&1)=0 ORDER BY RANDOM()')!
mut odd_teams_ids:= db.exec('SELECT id FROM `teams` WHERE (id&1)<>0 ORDER BY RANDOM()')!
//
for {
if even_teams_ids.len < 1 || odd_teams_ids.len < 1 {
// exit loop if there's no teams to process
break
}
even_team := even_teams_ids.pop()
home_team_id := even_team.vals[0].int()
odd_team := odd_teams_ids.pop()
away_team_id := odd_team.vals[0].int()
_ := db.exec_param_many("INSERT INTO `predictions` (`home_team_id`, `away_team_id`, `home_goals`, `away_goals`) VALUES(?, ?, ?, ?)", [
home_team_id.str(),
away_team_id.str(),
rand.u8().str(),
rand.u8().str()
])!
}
}
fn init_teams(db sqlite.DB) ! {
found_teams := sql db {
select count from models.Team
}!
if found_teams > 0 {
// sql db { delete from models.Team where id > 0}! // feels kinda hacky. just `delete from models.Team` doesn't work
db.exec_none('DELETE FROM `teams`')
}
// FC team-name
new_teams := [
'Arsenal',
'Chelsea',
'Liverpool',
'Manchester City',
'Manchester United',
'Tottenham Hotspur',
'Real Madrid',
'Barcelona',
'Juventus',
'Inter Milan',
'Beyern Munich',
'Ajax',
'PSG',
'Porto',
]
for _, team_name in new_teams {
team := models.Team{
name: team_name
}
sql db {
insert team into models.Team
}!
}
}
mut db := sqlite.connect(db_file_path) or { panic(err) }
init_tables(db) or { panic(err) }
init_teams(db) or { panic(err) } // works as expected
init_predictions(db) or { panic(err) } // works... but feels kinda wrong