diff --git a/src/client.odin b/src/client.odin index 70a6df6..4b7fdd9 100644 --- a/src/client.odin +++ b/src/client.odin @@ -6,6 +6,7 @@ import "core:io" import "core:mem" import "core:net" import "core:os" +import "core:slice" run_client :: proc(host_addr: net.Endpoint) { serv, list_err := net.dial_tcp(host_addr) @@ -14,7 +15,7 @@ run_client :: proc(host_addr: net.Endpoint) { // player input from stdin scanner: bufio.Scanner - bufio.scanner_init(&scanner, io.to_reader(transmute(io.Stream)os.stdin.stream)) + // bufio.scanner_init(&scanner, io.to_reader(transmute(io.Stream)os.stdin.stream)) LISTEN: for { msg_header: Msg_Type @@ -26,13 +27,14 @@ run_client :: proc(host_addr: net.Endpoint) { action := bufio.scanner_text(&scanner) net.send(serv, transmute([]byte)action) case .State_Update: - view, recv_err := receive_view(serv) + view, recv_err := receive_view(serv, context.temp_allocator) if recv_err != nil do panic(fmt.tprintln("failed to receive view", recv_err)) render_view(view) case .Info: unimplemented() } free_all(context.temp_allocator) + break LISTEN // ONLY 1 LOOP } } @@ -43,13 +45,14 @@ receive_view :: proc( view: Player_View, err: net.Network_Error, ) { + fmt.println("receiving view...") // constant header header_size := offset_of(Player_View, cards) net.recv(serv, (cast([^]byte)&view)[:header_size]) or_return // body of cards - body_data := make([]Card, view.num_cards) - body := raw_data(view.cards[:])[:view.num_cards * size_of(Card)] - net.recv(serv, transmute([]byte)body) or_return + view.cards = make([]Card, view.num_cards) + net.recv(serv, slice.reinterpret([]byte, view.cards)) or_return + fmt.println("successfully received view!") return } @@ -61,7 +64,7 @@ render_view :: proc(view: Player_View) { for p in 0 ..< view.constant.num_players - 1 { fmt.printfln("\tplayer %d's hand:", p) for c in view.cards { - if c._player == p do fmt.println("\t\t", c) + if c._player == p do fmt.println("\t\t", c.value, c.color) } } } diff --git a/src/game.odin b/src/game.odin index 0db963e..b428081 100644 --- a/src/game.odin +++ b/src/game.odin @@ -5,12 +5,12 @@ import "core:math/rand" VALUES :: []u8{1, 1, 1, 2, 2, 3, 3, 4, 4, 5} COLORS :: enum u8 { - RED = 0b000001, - GREEN = 0b000010, - WHITE = 0b000100, - BLUE = 0b001000, - YELLOW = 0b010000, - RAINBOW = 0b100000, + RED, + GREEN, + WHITE, + BLUE, + YELLOW, + RAINBOW, } Card :: bit_field u8 { @@ -77,7 +77,7 @@ init_beliefs :: proc( ) -> []Belief_State { player_beliefs: [dynamic]Belief_State for p in 0 ..< num_players { - bs: Belief_State + bs := make(Belief_State, hand_size) for c in 0 ..< hand_size { bs[c].value = 0b11111 bs[c].color = 0b111111 diff --git a/src/server.odin b/src/server.odin index ba69d4a..fe6f93a 100644 --- a/src/server.odin +++ b/src/server.odin @@ -26,27 +26,27 @@ run_server :: proc(listen_addr: net.Endpoint, num_players: int, num_hints: int, for i in 0 ..< num_players { view := create_view_from_game(g, i, context.temp_allocator) send_err := send_payload(comm[i], {.State_Update, view}) - if send_err != nil do panic("failed to send payload") + if send_err != nil do panic(fmt.tprintln("failed to send payload:", send_err)) } fmt.printfln("player %d's turn!", current_player) - // for p in g.players { - // fmt.println() - // for c in p.hand { - // fmt.println(c.value, c.color) - // } - // } - - action: Action - POKE: for { - poke_err := send_poke(comm[current_player]) - if poke_err != nil do panic(fmt.tprintfln("failed to poke player %d; %s", current_player, poke_err)) - // action, is_valid := receive_action(w.sockets[current_player]) - // if is_valid do break POKE - fmt.println("invalid action received. try again.") + for hand in g.player_hands { + fmt.println() + for card in hand { + fmt.println(card.value, card.color) + } } - perform_action(&g, action) + + // action: Action + // POKE: for { + // poke_err := send_poke(comm[current_player]) + // if poke_err != nil do panic(fmt.tprintfln("failed to poke player %d; %s", current_player, poke_err)) + // // action, is_valid := receive_action(w.sockets[current_player]) + // // if is_valid do break POKE + // fmt.println("invalid action received. try again.") + // } + // perform_action(&g, action) current_player = (current_player + 1) % num_players free_all(context.temp_allocator) break GAME_LOOP @@ -66,7 +66,7 @@ create_comm_world :: proc( comm: Comm_World, err: net.Network_Error, ) { - w := make([dynamic]net.TCP_Socket, num_conn) + w: [dynamic]net.TCP_Socket fmt.printfln("waiting for players (%d)...", num_conn) for len(w) < num_conn { client_sock, source := net.accept_tcp(listener) or_return @@ -93,7 +93,7 @@ Msg_Type :: enum u8 { Info, } -Player_View :: struct #packed { +Player_View :: struct { constant: bit_field u8 { num_players: u8 | 3, hand_size: u8 | 3,