aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/item_admin_write.go (1111B)


      1 package main
      2 
      3 import (
      4 	"database/sql"
      5 	"fmt"
      6 )
      7 
      8 // An unassigned instance has no zone owner. Its identity and properties become
      9 // visible together in SQLite; assignment will claim it in another transaction.
     10 type unassignedItemWrite struct {
     11 	id     uint32
     12 	record itemRecord
     13 }
     14 
     15 func (write unassignedItemWrite) persist(tx *sql.Tx) error {
     16 	store := itemStore{LastID: write.id, Items: map[uint32]itemRecord{write.id: write.record}}
     17 	if err := store.validate(); err != nil {
     18 		return err
     19 	}
     20 	var savedID uint32
     21 	if err := tx.QueryRow("INSERT INTO items (id, type, template, quality, count, ammo) VALUES (?, ?, ?, ?, ?, ?) RETURNING id",
     22 		write.id, write.record.Type, write.record.Template, write.record.Quality, write.record.Count, write.record.Ammo).Scan(&savedID); err != nil {
     23 		return err
     24 	}
     25 	var lastID uint32
     26 	if err := tx.QueryRow("UPDATE world SET last_item_id=max(last_item_id, ?) WHERE id=1 RETURNING last_item_id", write.id).Scan(&lastID); err != nil {
     27 		return err
     28 	}
     29 	if savedID != write.id || lastID < write.id {
     30 		return fmt.Errorf("item %d allocation was not saved", write.id)
     31 	}
     32 	return nil
     33 }