about summary refs log tree commit diff
path: root/src/db.go
blob: 42dc2d4acd111d1ab5c9383f88e18a471ad8a561 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main

import (
	"database/sql"
	"log"
)

const create string = `
CREATE TABLE IF NOT EXISTS users (
	id INTEGER NOT NULL PRIMARY KEY,
	created_at DATETIME NOT NULL,
	name TEXT,
	passwordHash TEXT
);
CREATE TABLE IF NOT EXISTS bots (
	id INTEGER NOT NULL PRIMARY KEY,
	created_at DATETIME NOT NULL,
	name TEXT,
	source TEXT
);
CREATE TABLE IF NOT EXISTS battles (
	id INTEGER NOT NULL PRIMARY KEY,
	created_at DATETIME NOT NULL,
	name TEXT,
	public BOOLEAN,
	raw_output TEXT
	max_rounds INTEGER
);
CREATE TABLE IF NOT EXISTS archs (
	id INTEGER NOT NULL PRIMARY KEY,
	name TEXT,
    UNIQUE(name)
);
INSERT OR IGNORE INTO archs (name) VALUES
	("null"), ("6502"), ("6502.cs"), ("8051"), ("alpha"), ("amd29k"),
	("any.as"), ("any.vasm"), ("arm.nz"), ("arm"), ("avr"), ("bf"), ("bpf.mr"),
	("bpf"), ("chip8"), ("cr16"), ("cris"), ("dalvik"), ("dis"), ("ebc"),
	("evm"), ("fslsp"), ("gb"), ("h8300"), ("i4004"), ("i8080"), ("java"),
	("jdh8"), ("kvx"), ("lh5801"), ("lm32"), ("m680x"), ("m68k"), ("mcore"),
	("mcs96"), ("mips"), ("msp430"), ("nios2"), ("or1k"), ("pic"), ("ppc"),
	("propeller"), ("pyc"), ("riscv"), ("riscv.cs"), ("rsp"), ("s390"),
	("sh"), ("sh.cs"), ("snes"), ("sparc"), ("tms320"), ("tricore"),
	("tricore.cs"), ("v850"), ("vax"), ("wasm"), ("ws"), ("x86"), ("x86.nz"),
	("xap"), ("xcore"), ("arm.gnu"), ("lanai"), ("loongarch"), ("m68k.gnu"),
	("mips.gnu"), ("nds32"), ("pdp11"), ("ppc.gnu"), ("s390.gnu"),
	("sparc.gnu"), ("xtensa"), ("z80")
;

/*
	("x86-64"), ("Alpha"), ("ARM"), ("AVR"), ("BPF"), ("MIPS"), ("PowerPC"),
	("SPARC"), ("RISC-V"), ("SH"), ("m68k"), ("S390"), ("XCore"), ("CR16"),
	("HPPA"), ("ARC"), ("Blackfin"), ("Z80"), ("H8/300"), ("V810"), ("PDP11"),
	("m680x"), ("V850"), ("CRIS"), ("XAP (CSR)"), ("PIC"), ("LM32"), ("8051"),
	("6502"), ("i4004"), ("i8080"), ("Propeller"), ("EVM"), ("OR1K Tricore"),
	("CHIP-8"), ("LH5801"), ("T8200"), ("GameBoy"), ("SNES"), ("SPC700"),
	("MSP430"), ("Xtensa"), ("xcore"), ("NIOS II"), ("Java"), ("Dalvik"),
	("Pickle"), ("WebAssembly"), ("MSIL"), ("EBC"), ("TMS320"), ("c54x"), ("c55x"),
	("c55+"), ("c64x"), ("Hexagon"), ("Brainfuck"), ("Malbolge"),
	("whitespace"), ("DCPU16"), ("LANAI"), ("lm32"), ("MCORE"), ("mcs96"),
	("RSP"), ("SuperH-4"), ("VAX"), ("KVX"), ("Am29000"), ("LOONGARCH"),
	("JDH8"), ("s390x"), ("STM8.")
*/

CREATE TABLE IF NOT EXISTS bits (
	id INTEGER NOT NULL PRIMARY KEY,
	name TEXT,
    UNIQUE(name)
);
INSERT OR IGNORE INTO bits (name) VALUES
	("8"), ("16"), ("32"), ("64")
;

CREATE TABLE IF NOT EXISTS user_bot_rel (
	user_id INTEGER,
	bot_id INTEGER,
	PRIMARY KEY(user_id, bot_id)
);
CREATE TABLE IF NOT EXISTS arch_bot_rel (
	arch_id INTEGER,
	bot_id INTEGER,
	PRIMARY KEY(arch_id, bot_id)
);
CREATE TABLE IF NOT EXISTS bit_bot_rel (
	bit_id INTEGER,
	bot_id INTEGER,
	PRIMARY KEY(bit_id, bot_id)
);

CREATE TABLE IF NOT EXISTS user_battle_rel (
	user_id INTEGER,
	battle_id INTEGER,
	PRIMARY KEY(user_id, battle_id)
);
CREATE TABLE IF NOT EXISTS owner_battle_rel (
	user_id INTEGER,
	battle_id INTEGER,
	PRIMARY KEY(user_id, battle_id)
);
CREATE TABLE IF NOT EXISTS bot_battle_rel (
	bot_id INTEGER,
	battle_id INTEGER,
	PRIMARY KEY(bot_id, battle_id)
);
CREATE TABLE IF NOT EXISTS arch_battle_rel (
	arch_id INTEGER,
	battle_id INTEGER,
	PRIMARY KEY(arch_id, battle_id)
);
CREATE TABLE IF NOT EXISTS bit_battle_rel (
	bit_id INTEGER,
	battle_id INTEGER,
	PRIMARY KEY(bit_id, battle_id)
);
`

type State struct {
	db       *sql.DB      // the database storing the "business data"
	sessions *SqliteStore // the database storing sessions
}

func NewState() (*State, error) {
	db, err := sql.Open("sqlite3", databasePath)
	if err != nil {
		log.Println("Error opening the db: ", err)
		return nil, err
	}
	if _, err := db.Exec(create); err != nil {
		log.Println("Error creating the tables: ", err)
		return nil, err
	}
	return &State{
		db: db,
	}, nil
}