Files
hanabi/src/main.odin
T
2026-06-06 00:23:42 +02:00

88 lines
2.0 KiB
Odin

package hanabi
import "core:flags"
import "core:fmt"
import "core:net"
import "core:os"
import "core:strings"
USAGE :: `usage: hanabi <COMMAND> [FLAGS]
COMMAND can be one of the following, with args and FLAGS specified under:
* connect (c)
<ip:port> // server to connect to
* host (h)
<ip:port> // server to host on
<players> // between 2-5
--hints (-H) // number of hints (default=8)
--lives (-l) // number of lives (default=3). try 1 for instant death difficulty!
example:
host $ hanabi host localhost:42069 2 --lives 1 -H 20
player 1 $ hanabi connect localhost:42069
player 2 $ hanabi c localhost:42069`
Host_Args :: struct {
addr: net.Host_Or_Endpoint `args:"pos=0,required" usage:"ip:port to host on"`,
players: int `args:"pos=1,required" usage:"how many players (2-5)"`,
hints: int `usage:"number of hints (default=8)"`,
lives: int `usage:"number of lives (default=3). try 1 for instant death difficulty!"`,
}
Connect_Args :: struct {
addr: net.Host_Or_Endpoint `args:"pos=0,required" usage:"ip:port to connect to"`,
}
resolve_endpoint :: proc(addr: net.Host_Or_Endpoint) -> (endpoint: net.Endpoint) {
switch v in addr {
case net.Endpoint:
endpoint = v
case net.Host:
ep, err := net.resolve_ip4(v.hostname)
if err != nil do panic(fmt.tprintln("failed to resolve endpoint", err))
endpoint = ep
endpoint.port = v.port
}
return
}
main :: proc() {
ok := false
defer if !ok do fmt.println(USAGE)
N := len(os.args) - 2
if N <= 0 do return
// aliases
args: [dynamic]string
for a in os.args {
switch a {
case "-H":
append(&args, "--hints")
case "-l":
append(&args, "--lives")
case:
append(&args, a)
}
}
// COMMAND
switch strings.to_lower(args[1]) {
case "connect", "c":
c: Connect_Args
flags.parse(&c, args[2:], .Unix)
endpoint := resolve_endpoint(c.addr)
run_client(endpoint)
case "host", "h":
h: Host_Args
h.hints = 8
h.lives = 3
flags.parse(&h, args[2:], .Unix)
endpoint := resolve_endpoint(h.addr)
run_server(endpoint, h.players, h.hints, h.lives)
case:
return
}
ok = true
}