• 1 次(票) - 平均星级: 5
  • 1
  • 2
  • 3
  • 4
  • 5
為我小幫手新增的掉寶多功能NPC
#1
Star 
最近我小幫手要幫我新增怪物的掉寶,
為了他的方便,為他新增了一個能新增、刪除和查詢掉寶的NPC
Demo:


腳本在此
代碼:
# ParentID: 1105002
# Character MapleMap ID when accessed: 913050010
# ObjectID: 500003
# Object Position X: -133
# Object Position Y: 34
# Write by 小安

locked_monster = -1

if not chr.isGM():
    sm.sendNext(u"你不是GM")
else:
    while True:
        sele = int(sm.sendNext(u"你想幹嘛?#b\r\n#L1#從地圖中挑選怪物#l\r\n#L2#搜尋怪物#l"))

        if sele == 1:
            mobs = sm.getAllMobIDMap()
            sb = u"請選擇怪物\r\n"
            for i, v in enumerate(mobs):
                sb += u"#L" + str(i) + "##o" + str(v) + "#(ID: " + str(v) + ")\r\n"
                if i > 30:
                    break
            locked_monster = mobs[int(sm.sendNext(sb))]
        else:
            locked_monster = sm.sendNext(sm.getSearchMobID(sm.sendAskText(u"輸入您要找的怪物", "", 1, 30)))
            
        mstat = sm.getMonsterStatsObj(locked_monster)
        if mstat is None:
            sm.sendNext(u"請重新選擇怪物")
            continue
        
        temper = u"您選擇的怪物 #r#o" + str(locked_monster) + "# ID: " + str(locked_monster)
        temper += u"\r\n#k等級: " + str(mstat.getLevel()) + " EXP: " + str(mstat.getExp())
        temper += u"\r\nHP: " + str(mstat.getHp()) + "\r\n\r\n"
        
        sb = temper + u"#b#L1#查詢掉寶(內附刪除功能)#l\r\n#L2#新增掉寶(搜尋道具)#l\r\n#L3#新增掉寶(輸入ID)#l\r\n#L4#回上一頁#l"
        
        while True:
            feature = int(sm.sendNext(sb))
            if feature == 4:
                break
            elif feature == 1:
                drops = sm.getAllMobDrops(locked_monster)
                
                ssb = temper + u"\r\n#k以下為怪物掉寶(點選可刪除)\r\n"
                for index, entry in enumerate(drops):
                    ssb += u"#b#L" + str(index) + u"##t" + str(entry.itemId) + u"#\r\n 機率: " + str(entry.chance) + u" 最小掉落數: " + str(entry.Minimum) + u" 最大掉落數: " + str(entry.Maximum) + u"\r\n"
                    if entry.itemId == 0:
                        continue
                    
                delete = drops[int(sm.sendNext(ssb))]
                asks = u"您確定要為 #r#o" + str(locked_monster) + u"# ID: " + str(locked_monster) + u" 刪除此掉寶嗎?"
                asks += u"\r\n\r\n#b道具: #t" + str(delete.itemId) + u"# - ID: " + str(delete.itemId)
                asks += u"\r\n掉落機率: " + str(delete.chance)
                asks += u"\r\n最小掉落數量: " + str(delete.Minimum)
                asks += u"\r\n最大掉落數量: " + str(delete.Maximum)
                
                if sm.sendAskYesNo(asks):
                    if sm.deleteDrop(locked_monster, delete.getId()):
                        sm.sendNext(u"#b刪除成功")
                    else:
                        sm.sendNext(u"#r刪除失敗")
            else:
                select_item = -1
                if feature == 2:
                    select_item = sm.sendNext(sm.getSearchItemID(sm.sendAskText(u"輸入您要找的道具", "", 1, 30)))
                elif feature == 3:
                    select_item = int(sm.sendAskText(u"輸入您要找的道具ID", "", 1, 30))
                    
                if not sm.itemExists(select_item):
                    sm.sendNext(u"此道具不存在")
                    continue
                chance = int(sm.sendAskText(u"輸入此道具 #i" + str(select_item) + "##t" + str(select_item) + u"# 掉落機率(1~1000000)", "", 1, 7))
                mind = int(sm.sendAskText(u"輸入此道具 #i" + str(select_item) + "##t" + str(select_item) + u"# 最小掉落數量", "", 1, 5))
                maxd = int(sm.sendAskText(u"輸入此道具 #i" + str(select_item) + "##t" + str(select_item) + u"# 最大掉落數量", "", 1, 5))
                
                asks = u"您確定要為 #r#o" + str(locked_monster) + "# ID: " + str(locked_monster) + u" 增加此掉寶嗎?"
                asks += u"\r\n\r\n#b道具: #i" + str(select_item) + "##t" + str(select_item) + u"# - ID: " + str(select_item)
                asks += u"\r\n掉落機率: " + str(chance)
                asks += u"\r\n最小掉落數量: " + str(mind)
                asks += u"\r\n最大掉落數量: " + str(maxd)
                
                if sm.sendAskYesNo(asks):
                    if sm.addDrop(locked_monster, select_item, chance, mind, maxd):
                        sm.sendNext(u"#b新增成功")
                    else:
                        sm.sendNext(u"#r新增失敗")

你還會需要在 MapleMonsterInformationProvider 新增以下函式
代碼:
public boolean addDrop(int monsterId, int itemid, int chance, int min, int max) {
        if(chance > 1000000 || chance <= 0 || min >= 1000 || max >= 1000 || min <= 0  || max <= 0)
            return false;

        //這裡是資料庫增加掉寶的地方 請依照自己端口資料庫使用方式做修改
        MonsterDropEntry entry = new MonsterDropEntry(itemid, chance, min, max, 0, "掉寶");
        DatabaseManager.saveToDB(entry);
        if (drops.containsKey(monsterId)) {
            drops.get(monsterId).add(entry);
        } else {
            ArrayList<MonsterDropEntry> dr = new ArrayList<>();
            dr.add(entry);
            drops.put(monsterId, dr);
        }
        return true;
    }

    public boolean deleteDrop(int monsterId, int entryId) {
        if (drops.containsKey(monsterId)) {
            MonsterDropEntry to_delete = null;
            for (MonsterDropEntry mde : drops.get(monsterId)){
                if(mde.getId() == entryId) {
                    to_delete = mde;
                    break;
                }
            }

            if(to_delete == null) {
                return false;
            }else{
                //這裡是資料庫增加掉寶的地方 請依照自己端口資料庫使用方式做修改
                DatabaseManager.deleteFromDB(to_delete);
                drops.get(monsterId).remove(to_delete);
            }
        } else {
            return false;
        }

        return true;
    }

由於我的端口是用 ORM 所以只要一行程式碼 一般是要寫 SQL Code Insert or Delete ... 之類的
之後有空再教如何使用 ORM 吧 這個有點難度

Angel Angel Angel Angel Angel
[圖: 5996_20160413004324_570d25ac1f165.gif]
  回覆
#2
<p>這個好高級!來研究一下,希望能搞懂,哈哈<br />
謝謝分享Heart</p>
  回覆
#3
白狐端v113不曉得能不能 不知道為甚麼opennpc這個函數不能用 請問大大知道嗎
  回覆
#4
看起來非常豪華
感謝版主無私分享 Big Grin
  回覆


論壇跳轉:


正在瀏覽該主題的用戶: 1 隻魚