[plugin] vGuildStoreDC

I fixed it a little bit… I barely added “in_training_area()” check, but I didn’t test since I don’t know which values can be found with phbot API.

I’m dropping it here if you don’t mind.

from phBot import *
import QtBind
import json
import os
import phBotChat
import struct

######################################################################################
#                                                                                    #
#                                  Initializing Vars                                 #
#                                                                                    #
######################################################################################
isConnected = False # avoid possible issues
character_data = None
SERVER_GUILD_STORAGE_DATA_RESPONSE = 0xB250

######################################################################################
#                                                                                    #
#                                  Initializing GUI                                  #
#                                                                                    #
######################################################################################
gui = QtBind.init(__name__,'vGuildStoreDC')

cbx_EnablePlugin = QtBind.createCheckBox(gui, 'cbx_EnablePlugin_clicked','Enable vGuildStoreDC',20,10)
cbx_SendDC = QtBind.createCheckBox(gui, 'cbx_SendDC_clicked','Send DC command to the Blocking Guild Member',20,35)
cbx_SendDC_ListOnly = QtBind.createCheckBox(gui, 'cbx_SendDC_ListOnly_clicked','Send only to Players in List',20,55)
txt_SendingChar = QtBind.createLineEdit(gui, '', 20, 80, 100, 20)
btn_AddSendChar = QtBind.createButton(gui,'btnAddSendChar_clicked',"Add",130,80)
lst_SendingChars = QtBind.createList(gui, 20, 105, 185, 145)
btn_RemoveSendChar = QtBind.createButton(gui,'btnRemSendChar_clicked',"                       Remove                       ",20,255)

cbx_EnableReceiveDC = QtBind.createCheckBox(gui, 'cbx_EnableReceiveDC_clicked','Enable Receive DC command',300,35)
cbx_ReceiveDC_ListOnly = QtBind.createCheckBox(gui, 'cbx_ReceiveDC_ListOnly_clicked','Only Receive DC command from Players in List',300,55)
txt_ReceivingChar = QtBind.createLineEdit(gui, '', 300, 80, 100, 20)
btn_AddReceiveChar = QtBind.createButton(gui,'btnAddReceiveChar_clicked',"Add",410,80)
lst_ReceivingChars = QtBind.createList(gui, 300, 105, 185, 145)
btn_RemoveReceiveChar = QtBind.createButton(gui,'btnRemReceiveChar_clicked',"                       Remove                       ",300,255)

######################################################################################
#                                                                                    #
#                                     Functions                                      #
#                                                                                    #
######################################################################################
# Called when the bot successfully connects to the game server
def connected():
	global isConnected
	isConnected = False
	
# Called when the character enters the game world
def joined_game():
	global isConnected, character_data
	isConnected = True
	character_data = get_character_data()
	config_load()

def get_vGuildStoreDC_path():
	return get_config_dir() + character_data['server'] + "_" + character_data['name'] + "_vGuildStoreDC.json"

def config_load():
	if os.path.exists(get_vGuildStoreDC_path()):
		data = {}
		with open(get_vGuildStoreDC_path(),"r") as f:
			data = json.load(f)
		if "Sending Chars" in data:
			for item in data["Sending Chars"]:
				QtBind.append(gui,lst_SendingChars,item)

		if "Receiving Chars" in data:
			for item in data["Receiving Chars"]:
				QtBind.append(gui,lst_ReceivingChars,item)
		# config radiobutton if is saved
		if data['Plugin enabled'] == True:
			QtBind.setChecked(gui, cbx_EnablePlugin, True)

		if data['Send DC'] == True:
			QtBind.setChecked(gui, cbx_SendDC, True)

		if data['Send DC to List only'] == True:
			QtBind.setChecked(gui, cbx_SendDC_ListOnly, True)

		if data['Enable Receive DC'] == True:
			QtBind.setChecked(gui, cbx_EnableReceiveDC, True)

		if data['Enable Receive DC only from List'] == True:
			QtBind.setChecked(gui, cbx_ReceiveDC_ListOnly, True)

# Save all configs
def config_save():
	if isConnected:
		# Init dict
		data = {}
		# Save current settings
		data['Plugin enabled'] = QtBind.isChecked(gui,cbx_EnablePlugin)

		data['Sending Chars'] = QtBind.getItems(gui,lst_SendingChars)
		data['Receiving Chars'] = QtBind.getItems(gui,lst_ReceivingChars)

		data['Send DC'] = QtBind.isChecked(gui,cbx_SendDC)
		data['Send DC to List only'] = QtBind.isChecked(gui,cbx_SendDC_ListOnly)
		data['Enable Receive DC'] = QtBind.isChecked(gui,cbx_EnableReceiveDC)
		data['Enable Receive DC only from List'] = QtBind.isChecked(gui,cbx_ReceiveDC_ListOnly)

		# Overrides
		with open(get_vGuildStoreDC_path(),"w") as f:
			f.write(json.dumps(data, indent=4, sort_keys=True))
		log("[vGuildStoreDC] Configs has been saved")

def cbx_EnablePlugin_clicked(checked):
	if isConnected:
		log('[vGuildStoreDC] Plugin '+('enabled' if checked else 'disabled'))
		config_save()

def cbx_SendDC_clicked(checked):
	if isConnected:
		log('[vGuildStoreDC] Sending DC Command '+('enabled' if checked else 'disabled'))
		config_save()

def cbx_SendDC_ListOnly_clicked(checked):
	if isConnected:
		log('[vGuildStoreDC] Sending DC Command only to List '+('enabled' if checked else 'disabled'))
		config_save()

def cbx_EnableReceiveDC_clicked(checked):
	if isConnected:
		log('[vGuildStoreDC] Receiving DC Command '+('enabled' if checked else 'disabled'))
		config_save()

def cbx_ReceiveDC_ListOnly_clicked(checked):
	if isConnected:
		log('[vGuildStoreDC] Receiving DC Command only from List '+('enabled' if checked else 'disabled'))
		config_save()

def btnAddSendChar_clicked():
	if isConnected:
		NewSendingchar = QtBind.text(gui, txt_SendingChar)
		CurrentSendingChars = QtBind.getItems(gui, lst_SendingChars)
		if NewSendingchar:
			if not NewSendingchar in CurrentSendingChars:
				QtBind.append(gui, lst_SendingChars, NewSendingchar)
				log("[vGuildStoreDC] Added new Sending Char: ["+NewSendingchar+"]")
				config_save()
			else:
				log("[vGuildStoreDC] This Char is already in List")

def btnRemSendChar_clicked():
	if isConnected:
		SelectedSendingChar = QtBind.text(gui, lst_SendingChars)
		# Check if something is selected
		if SelectedSendingChar:
			QtBind.remove(gui, lst_SendingChars, SelectedSendingChar)
			log("[vGuildStoreDC] Removed Sending Char: ["+SelectedSendingChar+"]")
			config_save()

def btnAddReceiveChar_clicked():
	if isConnected:
		NewReceivingChar = QtBind.text(gui, txt_ReceivingChar)
		CurrentReceivingChars = QtBind.getItems(gui, lst_ReceivingChars)
		if NewReceivingChar:
			if not NewReceivingChar in CurrentReceivingChars:
				QtBind.append(gui, lst_ReceivingChars, NewReceivingChar)
				log("[vGuildStoreDC] Added new Receiving Char: ["+NewReceivingChar+"]")
				config_save()
			else:
				log("[vGuildStoreDC] This Char is already in List")

def btnRemReceiveChar_clicked():
	if isConnected:
		SelectedReceivingChar = QtBind.text(gui, lst_ReceivingChars)
		# Check if something is selected
		if SelectedReceivingChar:
			QtBind.remove(gui, lst_ReceivingChars, SelectedReceivingChar)
			log("[vGuildStoreDC] Removed Receiving Char: ["+SelectedReceivingChar+"]")
			config_save()

def handle_chat(t,player,msg):	
	if QtBind.isChecked(gui, cbx_EnablePlugin):
		if msg == "!DC":
			if QtBind.isChecked(gui, cbx_EnableReceiveDC):
				if QtBind.isChecked(gui, cbx_ReceiveDC_ListOnly):
					ReceivingChars = QtBind.getItems(gui, lst_ReceivingChars)
					if player in ReceivingChars:
						log("[vGuildStoreDC] Disconnect because of receiving DC Command from "+player)
						disconnect()
				else:
					log("[vGuildStoreDC] Disconnect because of receiving DC Command from "+player)
					disconnect()
			else:
				log("[vGuildStoreDC] Receive DC command is disabled ("+player+")")

def handle_joymax(opcode, data):
	if opcode == SERVER_GUILD_STORAGE_DATA_RESPONSE:
		result = data[0]
		if result == 2: # Error
			reasonCode = struct.unpack_from('<H',data,1)[0]
			if reasonCode == 19528: # .. 48 4C ..
				charLength = struct.unpack_from('<H',data,3)[0]
				charName = struct.unpack_from('<' + str(charLength) + 's',data,5)[0].decode('cp1252')
				if QtBind.isChecked(gui, cbx_EnablePlugin):
					if QtBind.isChecked(gui, cbx_SendDC):
						if QtBind.isChecked(gui, cbx_SendDC_ListOnly):
							CurrentSendingChars = QtBind.getItems(gui, lst_SendingChars)
							if charName in CurrentSendingChars:
								log("[vGuildStoreDC] "+charName+" is using guild storage! Trying disconnect him...")
								phBotChat.Private(charName,"!DC")
						else:
							log("[vGuildStoreDC] "+charName+" is using guild storage! Trying disconnect him...")
							phBotChat.Private(charName,"!DC")
	return True

# Check if the character is inside training area
def in_training_area():
	# Calc the distance from point A to B
	def calc_distance(ax,ay,bx,by):
		return ((bx-ax)**2 + (by-ay)**2)**(0.5)

	# Get my current position
	mPos = get_position()
	# Get current training area setup
	tArea = get_training_area()

	# Get radius and position (Not even sure what this dictionary has, phbot API has no info about it)
	tRad = tArea['radius']
	tPos = tArea['position']

	# Compare coordinates but only if they are at the same regional map (Donwhang is not taken on consideration)
	if (mPos['region'] <= 32767 and tPos['region'] <= 32767) or mPos['region'] == tPos['region']:
		# compare world map or in the same dungeon only
		if round(calc_distance(mPos['x'], mPos['y'],tPos['x'],tPos['y']),2) <= tRad:
			return True
	return False

log('[%s] by Chefkoch, Verya & JellyBitz Loaded' % __name__)
5 Likes