d1bb149d7d70140455a8e0cc19986896a80e1963
[carveJwlIkooP6JGAAIwe30JlM.git] / player_remote.c
1 #include "player_remote.h"
2
3 static void player_remote_unwatch( struct network_player *player ){
4 addon_cache_unwatch( k_addon_type_player, player->playermodel_view_slot );
5 addon_cache_unwatch( k_addon_type_board, player->board_view_slot );
6 }
7
8 static void player_remote_clear( struct network_player *player ){
9 player_remote_unwatch( player );
10 memset( player, 0, sizeof(*player) );
11 strcpy( player->username, "unknown" );
12 player->subsystem = k_player_subsystem_invalid;
13 }
14
15 static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
16 netmsg_blank *tmp = msg->m_pData;
17
18 if( tmp->inetmsg_id == k_inetmsg_playerjoin ){
19 netmsg_playerjoin *playerjoin = msg->m_pData;
20 if( !packet_minsize( msg, sizeof(*playerjoin) )) return;
21
22 if( playerjoin->index < vg_list_size(netplayers.list) ){
23 struct network_player *player = &netplayers.list[ playerjoin->index ];
24 player_remote_clear( player );
25 player->active = 1;
26
27 /* TODO: interpret the uids */
28 player->board_view_slot = 0;
29 player->playermodel_view_slot = 0;
30
31 vg_strncpy( playerjoin->username, player->username,
32 sizeof(player->username), k_strncpy_always_add_null );
33
34 vg_info( "#%u joined with name: %s\n", player->username );
35 }
36 else {
37 vg_error( "inetmsg_playerjoin: player index out of range\n" );
38 }
39 }
40 else if( tmp->inetmsg_id == k_inetmsg_playerleave ){
41 netmsg_playerleave *playerleave = msg->m_pData;
42 if( !packet_minsize( msg, sizeof(*playerleave) )) return;
43
44 if( playerleave->index < vg_list_size(netplayers.list) ){
45 struct network_player *player = &netplayers.list[ playerleave->index ];
46 player_remote_unwatch( player );
47 player->active = 0;
48 vg_info( "player leave (%d)\n", playerleave->index );
49 }
50 else {
51 vg_error( "inetmsg_playerleave: player index out of range\n" );
52 }
53 }
54 else if( tmp->inetmsg_id == k_inetmsg_playerusername ){
55 netmsg_playerusername *update = msg->m_pData;
56 if( !packet_minsize( msg, sizeof(*update) )) return;
57
58 if( update->index < vg_list_size(netplayers.list) ){
59 struct network_player *player = &netplayers.list[ update->index ];
60 vg_strncpy( update->username, player->username,
61 sizeof(player->username), k_strncpy_always_add_null );
62
63 vg_info( "#%u changed username: %s\n", player->username );
64 }
65 else {
66 vg_error( "inetmsg_playerleave: player index out of range\n" );
67 }
68 }
69 }
70
71 static void remote_player_network_imgui(void){
72 if( !network_client.network_info )
73 return;
74
75 ui_rect panel = { (vg.window_x / 2) - 200, 0, 400, 600 };
76 ui_fill( panel, (ui_colour(k_ui_bg)&0x00ffffff)|0x50000000 );
77
78 char buf[512];
79 const char *netstatus = "PROGRAMMING ERROR";
80
81 struct { enum ESteamNetworkingConnectionState state; const char *str; }
82 states[] = {
83 { k_ESteamNetworkingConnectionState_None, "None" },
84 { k_ESteamNetworkingConnectionState_Connecting, "Connecting" },
85 { k_ESteamNetworkingConnectionState_FindingRoute, "Finding Route" },
86 { k_ESteamNetworkingConnectionState_Connected, "Connected" },
87 { k_ESteamNetworkingConnectionState_ClosedByPeer, "Closed by peer" },
88 { k_ESteamNetworkingConnectionState_ProblemDetectedLocally,
89 "Problem Detected Locally" },
90 { k_ESteamNetworkingConnectionState_FinWait, "Fin Wait" },
91 { k_ESteamNetworkingConnectionState_Linger, "Linger" },
92 { k_ESteamNetworkingConnectionState_Dead, "Dead" }
93 };
94 for( u32 i=0; i<vg_list_size(states); i ++ ){
95 if( states[i].state == network_client.state ){
96 netstatus = states[i].str;
97 break;
98 }
99 }
100 snprintf( buf, 512, "Network: %s", netstatus );
101 ui_info( panel, buf );
102 ui_info( panel, "---------------------" );
103 ui_info( panel, "#-1: localplayer" );
104
105 for( u32 i=0; i<vg_list_size(netplayers.list); i++ ){
106 struct network_player *player = &netplayers.list[i];
107 if( player->active ){
108 const char *sysname = "invalid";
109
110 if( (player->subsystem >= 0) &&
111 (player->subsystem < k_player_subsystem_max) ){
112 sysname = player_subsystems[ player->subsystem ]->name;
113 }
114 snprintf( buf, 512, "#%u: %s [%s]", i, player->username, sysname );
115 ui_info( panel, buf );
116 }
117 }
118 }