B-E-A-U-tiful!
/me is desperate to learn Python as soon my Ranking System is finished...
/me is desperate to learn Python as soon my Ranking System is finished...
# Send tiny close to InSim.
tinyClose = Packet(ISP_TINY, SubT=TINY_CLOSE)
insim.SendP(tinyClose)
# Now you can close the socket.
insim.Close()
InSim.SendP(self, Packet(ISP_BTN, ReqI=1, ClickID=cid, UCID=ucid,
BStyle=ISB_DARK | ISB_RIGHT,
W=cw, H=ch, T=cy, L=cx, Text=carbtntext))
InSim.sendP(self, Packet(ISP_BTN, ReqI=1, ClickID=cid, UCID=ucid,
BStyle=ISB_DARK | ISB_RIGHT | [B]ISB_CLICK[/B],
W=cw, H=ch, T=cy, L=cx, Text=carbtntext))
elif ToKph(car["Speed"]) > SPEED_LIMIT:
# Player broke the speed-limit, spec them.
SendMessage("/rcm " + GET_SLOWER)
SendMessage("/rcm_ply " + players[car["PLID"]][0]["PName"])
BlockingTimer(MSG_TIMEOUT)
SendMessage("/rcc_ply " + players[car["PLID"]][0]["PName"])
players[car["Speed"]] = 0
if ToKph(car["Speed"]) > SPEED_LIMIT:
SendMessage("/spec " + players[car["PLID"]][0]["PName"])
SendMessage(SPEEDING_MSG)
def PenalisePlayer(pName):
SendMessage('/p_dt %s' % (pName))
#
# 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
# Store hosts and ports as a dictionary (dict).
hosts = {'192.168.2.104': 29999, '127.0.0.1': 29998}
# Create a list to store each InSim connection.
sockets = []
# Loop over the hosts dictionary.
for host, port in hosts.iteritems():
# Create new InSim object.
insim = Pyinsim.InSim()
# Connect to this server.
insim.Connect(host, port)
# Add connected InSim object to our sockets list.
sockets.append(insim)
if sockets[0].Connected == True:
print 'Socket 0 connected' # First server in sockets list connected.
if sockets[1].Connected == True:
print 'Socket 1 connected' # Second server in sockets list connected.
for socket in sockets:
if socket.Connected == True:
print 'Socket connected!'
# Ask for Input
Command = str(raw_input('Command?: '))
Adminpass = str(raw_input('Admin Pass?: '))
# Loop over the hosts dictionary.
for host, port in hosts.iteritems():
# Create new InSim object.
insim = Pyinsim.InSim()
# Connect to this server.
insim.Connect(host, port)
# Add connected InSim object to our sockets list.
sockets.append(insim)
# Loop over the sockets
for socket in sockets:
if socket.Connected == True:
print 'connected'
socket.SendP(Pyinsim.Packet(Pyinsim.ISP_ISI, Admin=Adminpass,
IName='^3XXX', ReqI=1))
SendMessage(Command)
socket.Run()
Command = str(raw_input('Command?: '))
Adminpass = str(raw_input('Admin Pass?: '))
for host, port in hosts.iteritems():
insim = Pyinsim.InSim()
insim.Connect(host, port)
sockets.append(insim)
for socket in sockets:
if socket.Connected == True:
print 'connected'
socket.SendP(Pyinsim.Packet(Pyinsim.ISP_ISI, Admin=Adminpass,
IName='^3XXX', ReqI=1))
SendMessage(Command)
# TODO: Write rest of program.
# END OF PROGRAM
for socket in sockets:
socket.Run()
hosts = {'192.168.2.104': 29999, '192.168.2.104': 2998, '127.0.0.1':2997, '127.0.0.1': 29996}
for socket in sockets:
if socket.Connected == True:
print 'connected'
socket.SendP(Pyinsim.Packet(Pyinsim.ISP_ISI, Admin=Adminpass,
IName='^3XXX', ReqI=1))
SendMessage('/msg ' + News) #every 10 Minutes
SendMessage('/mag Time to sleep now') #at 00:00h every day
# TODO: Write rest of program.
# END OF PROGRAM
for socket in sockets:
socket.Run()
INTERVAL = 600 # Number of seconds in 10 minutes.
def timer_event():
# Timer event fired, send message.
SendMessage('/msg {0}' % (News))
# Start timer again.
timer = threading.Timer(INTERVAL, timer_event)
timer.start()
# Start timer.
timer = threading.Timer(INTERVAL, timer_event)
timer.start()
if current_time == message_send_time:
SendMessage(msg)