1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Shader client code to manage the game context
24 # Context of the game, either local or remote
25 var context
: GameContext is lazy
do
29 var port
= default_listening_port
31 if args
.not_empty
then
32 # Use first argument as the server address
34 if args
.length
> 1 then port
= args
[1].to_i
36 print
"Looking for a server..."
38 var servers
= discover_local_servers
39 if servers
.not_empty
then
40 address
= servers
.first
.address
41 port
= servers
.first
.port
45 if address
== null then
46 print
"Launching a local server"
49 return new LocalServerContext
51 print
"Connecting to {address}:{port}"
53 # Args are: tinks server_address {port}
54 if args
.length
> 1 then port
= args
[1].to_i
56 # Setup connection config
57 var server_config
= new RemoteServerConfig(address
, port
)
58 var server
= new RemoteServer(server_config
)
60 # Connect then complete handshake
61 assert server
.connect
else print_error
"Connection to server failed with {server.socket.last_error or else "none"}"
62 assert server
.handshake
else print_error
"Handshake with server failed"
64 # Download and setup remote game
65 var context
= new RemoteGameContext(server
)
72 # `Tank` of the local player, if any
73 fun local_tank
: nullable Tank
75 # FIXME use a ? to one line this
76 var local_player
= context
.local_player
77 if local_player
== null then return null
78 return local_player
.tank