Hi...
After reading a bit in the Pyton book I bought I must say that I seem to be able to do some simple scripts...
But I want to create some insim apps... And this still is my problem:
To configure an amount of Servers I want to create an app wihich asks me "Which Admin Commad?" for Security it also should ask for the Admin Pass.
Then it should connect to to all IPs / Ports entered in a List, execute the commad, end exit insim...
But my problem is how to close the insim after the command and how to open another after this?
Or is it possible to connect to more than one Server at once?
This is my try with the use of your template:
#
# Copyright 2008 Alex McBride.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the Lesser General Public License (LGPL) as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import Pyinsim
import sys
# Init globals
insim = Pyinsim.InSim(Pyinsim.INSIM_TCP)
connections = {}
players = {}
Server = ['192.168.2.104', '127.0.0.1']
Port = [29999, 29998]
# Helper functions.
def SendMessage(msg):
"""Send message to LFS."""
if len(msg) > 64:
insim.SendP(Pyinsim.Packet(Pyinsim.ISP_MSX, Msg=msg))
else:
insim.SendP(Pyinsim.Packet(Pyinsim.ISP_MST, Msg=msg))
def SendMessageConn(msg, ucid=0, plid=0):
"""Send message to a specific connection or player."""
insim.SendP(Pyinsim.Packet(Pyinsim.ISP_MTC, Msg=msg, UCID=ucid, PLID=plid))
#def RequestPlayersConns():
# """Request all players and connections to be sent."""
# insim.SendP(Pyinsim.Packet(Pyinsim.ISP_TINY, ReqI=1, SubT=TINY_NCN))
# insim.SendP(Pyinsim.Packet(Pyinsim.ISP_TINY, ReqI=1, SubT=TINY_NPL))
def GetConnection(ucid):
"""Get connection from UCID."""
return connections[ucid]
def GetPlayer(plid):
"""Get player from PLID."""
return players[plid]
def GetPlayerFromUcid(ucid):
"""Get player from UCID."""
for player in players.itervalues():
if player['UCID'] == ucid:
return player
return None
# TODO: Add more helper functions.
# Packet received events
def VersionCheck(ver):
"""Check the version."""
if ver['InSimVer'] != Pyinsim.INSIM_VERSION:
print 'Invalid InSim version detected.'
sys.exit(0)
def ConnectionJoined(ncn):
"""Add connection to connections dictionary."""
connections[ncn['UCID']] = ncn
def ConnectionLeft(cnl):
"""Delete connection from connections dictionary."""
del connections[cnl['UCID']]
def ConnectionRenamed(cpr):
"""Rename player in connections and players lists."""
connection = GetConnection(cpr['UCID'])
connection['PName'] = cpr['PName']
player = GetPlayerFromUcid(cpr['UCID'])
player['PName'] = cpr['PName']
player['Plate'] = cpr['Plate']
def PlayerJoined(npl):
"""Add player to players dictionary."""
players[npl['PLID']] = npl
def PlayerLeft(pll):
"""Delete player from players dictionary."""
del players[pll['PLID']]
def TookOverCar(toc):
"""Change UCID for player."""
player = GetPlayer(toc['PLID'])
player['UCID'] = toc['NewUCID']
# TODO: Add more packet event handlers.
# Bind events.
insim.Bind({Pyinsim.ISP_VER: VersionCheck,
Pyinsim.ISP_NCN: ConnectionJoined,
Pyinsim.ISP_CNL: ConnectionLeft,
Pyinsim.ISP_NPL: PlayerJoined,
Pyinsim.ISP_PLL: PlayerLeft,
Pyinsim.ISP_CPR: ConnectionRenamed,
Pyinsim.ISP_TOC: TookOverCar})
# Connection lost.
def ConnectionLost():
print 'InSim connection lost.'
sys.exit(0)
insim.ConnectionLost(ConnectionLost)
# Ask for Input
Command = str(raw_input('Command?: '))
Adminpass = str(raw_input('Admin Pass?: '))
# Connect to InSim.
try:
insim.Connect(Server[0], Port[0])
except Pyinsim.socket.error, (ex):
print 'Connection to InSim failed: %s' % (ex.args[1])
sys.exit(0)
else:
# Initailise InSim and request players/connections.
insim.SendP(Pyinsim.Packet(Pyinsim.ISP_ISI, Admin=Adminpass,
IName='^3Pyinsim', ReqI=1))
# send message
SendMessage(Command)
# Keep program thread alive.
insim.Run()
finally:
insim.Close